1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 5x 5x 5x 5x 5x 5x 5x 5x 33x 33x 33x 33x 33x 33x 24x 9x 9x 9x 9x 5x 20x 20x 5x 4x 4x 8x 8x 4x 9x 9x 9x 28x 19x 9x 15x 19x 15x 28x 28x 28x 20x 20x 20x 20x 53x 53x 53x 213x 32x 32x 213x 110x 103x 48x 55x 37x 32x 32x 18x 9x 9x 9x | import sExpression, { SExpressionClause } from './sExpression'; import humanReadable from "./humanReadable"; import isStr from "../preds/isStr"; import isProblem from "./isProblem"; import match from "./match"; import clauseFromAlts from "./clauseFromAlts"; import fnName from "./fnName"; import stringifyWithFnName from "../utils/stringifyWithFnName"; import repeat from "./repeat"; export const NEW_LINE = () => {}; export const INDENT_IN = () => {}; export const INDENT_OUT = () => {}; function describe( expr, replacer, space ) { const sexpr = sExpression( expr ); const cSexpr = SExpressionClause.conform( sexpr ); if ( isProblem( cSexpr ) ) { Iconsole.error( cSexpr ); throw new Error( 'The given expression is not a valid expression.' ); } const strFragments = _strFragments( cSexpr, replacer ); const level = 0; const r = fragsToStr( strFragments, level, space ); return r; } function _strFragments( cSExpr, replacer ) { return strFragments( ( { head: headAlts, params } ) => { return { head: clauseFromAlts( headAlts ), params }; }, cSExpr, replacer ); } function strFragments( heIadAltsHandler, cNode, replacer ) { const { head, params } = headAltsHandler( cNode ); if ( !head ) { Ireturn []; } if ( replacer ) { let interceptR = replacer( head ); if ( interceptR ) { return interceptR; } } if ( head.type === 'PRED' ) { return [ `${fnName( head.opts.predicate )}` ]; } coEnst label = humanReadable( head ); let commaedParamFrags; if ( params ) { const { labelled, unlabelled, keyList } = params; if ( labelled ) { let paramFrags = labelled.reduce( ( acc, { label, item } ) => acc.concat( [ [ _processLabel( label ), ', ', _fragmentParamAlts( headAltsHandler, item, replacer ) ], ] ), []E ); commaedParamFrags = interpose( paramFrags, [ ', ', NEW_LINE ] ) } else if ( unlabelled ) { let paramFrags = unlabelled.map( ( { item } ) => _fragmentParamAlts( headAltsHandler, item, replacer ) ); commaedParamFrags = interpose( paramFrags, [ ', ', NEW_LINE ] ); } else if ( keyList ) { let paramFrags = keyList; commaedParamFrags = interpose( paramFrags, [ ', ' ] ); } else { // console.error( params ); // throw '!z'; commaedParamFrags = []; } } else { commaedParamFrags = []; } return [ label, '(' ] .concat( commaedParamFrags.length > 1 ? [ INDENT_IN, NEW_LINE, ] : [ commaedParamFrags.length === 0 ? '' : ' ' ] ) .concat( commaedParamFrags ) .concat( commaedParamFrags.length > 1 ? [ INDENT_OUT, NEW_LINE, ] : [ commaedParamFrags.length === 0 ? '' : ' ' ] ) .concat( [ ')' ] ); } export function interpose( arr, interArr ) { ifI ( arr.length === 0 ) { return arr; } else { return arr.reduce( ( acc, curr, idx ) => { if ( idx < arr.length - 1 && !isSpecial( curr ) ) { return acc.concat( [ curr ] ).concat( interArr ); } else { return acc.concat( [ curr ] ); } }, [] ); } } export function isSpecial( x ) { return x === NEW_LINE || x === INDENT_IN || x === INDENT_OUT; } function _fragmentParamAlts( headAltsHandler, pAlts, replacer ) { const r = match( pAlts, { 'label': _processLabel, 'sExpression': ( expr ) => strFragments( headAltsHandler, expr, replacer ), 'quotedParamsMap': ( o ) => _fragmentParamsObj( headAltsHandler, o, replacer, true ), 'unquotedParamsMap': ( o ) => _fragmentParamsObj( headAltsHandler, o, replacer, false ), 'optionsObj': ( o ) => stringifyWithFnName( o ), 'recursive': ( { expression } ) => [ '<recursive>: ', humanReadable( expression ) ] }, () => { throw '!s'; } ); return r; } function _processLabel( { str, quoted } ) { if ( str ) { return str; } else if ( quoted ) { return `"${quoted.value}"`; } }I function _fragmentParamsObj( headAltsHandler, pObj, replacer, quote ) { var r =E [ '{', INDENT_IN, NEW_LINE, ]; let body = []; for ( let label in pObj ) { if ( pObj.hasOwnProperty( label ) ) { let item = []; item.push( quote ? `"${label}": ` : `<${label}>: ` ); var r1 = match( pObj[ label ], { 'keyList': ( list ) => { return [ '[ ' ].concat( interpose( list.map( ( i ) => `"${i}"` ), [ ', ' ] ) ) .concat( ' ]' ); }, 'singleParam': ( p ) => _fragmentParamAlts( headAltsHandler, p, replacer ) }, () => { throw '!e'; } ); if ( r1 ) { item.push( r1 ); body.push( item ); } } } body = interpose( body, [ ', ', NEW_LINE ] ); r = r.concat( body ).concat( [ INDENT_OUT, NEW_LINE, '}' ] ); return r; } export function fragsToStr( frags, level, space ) { let newLevel = level; let justNewLine = false; return frags.reduce( ( acc, f ) => { if ( justNewLine ) { justNewLine = false; acc = acc.concat( repeat( space * newLevel, ' ' ).join( '' ) ); } if ( isStr( f ) ) { return acc.concat( f ); } else if ( Array.isArray( f ) ) { return acc.concat( fragsToStr( f, newLevel, space ) ); } else if ( f === NEW_LINE ) { if ( space > 0 ) { justNewLine = true; return acc.concat( '\n' ); } } else if ( f === INDENT_IN ) { newLevel += 1; } else if ( f === INDENT_OUT ) { newLevel -= 1; } else { console.error( f ); throw '!3'; } return acc; }, '' ); }E export default describe; export {humanReadable}; |