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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 15x 4657x 3828x 3768x 3768x 3768x 2599x 2599x 2599x 6367x 1437x 1437x 1437x 1437x 37361x 37361x 2962x 2962x 2962x 2962x 7596x 2962x 2962x 1443x 1443x 1443x 1443x 1414x 29x 29x 1443x 1443x 2937x 2937x 2937x 2937x 2599x 2599x 25x 2574x 2599x 338x 2937x 2937x 4632x 3768x 4632x 2882x 4632x 3768x 3768x 3768x 4632x 2937x 2599x 2937x 2937x 2599x 2599x 2599x 2937x 4657x 4657x 4657x 1437x 1437x 1437x 272x 1165x 942x 223x 158x 65x 40x 25x 25x 1437x 15x 3768x 1414x 2354x 2354x 1636x 718x 717x 1x 1x 2599x 2599x 70x 2529x 535x 1994x 23x 1971x 1946x 25x 25x 7596x 3768x 3768x 3768x 3768x 1601x 2167x 2167x 3768x 3768x 1415x 2353x 3768x 3768x 3828x 3828x 3828x 3828x 2686x 1142x 1142x 1142x | /*eslint func-names: 0*/ function Value( val ) { this.value = val; } function Piece( frag ) { this.fragment = frag; } function GroupedPiece( frag ) { this.fragment = frag; } function NamedPiece( name, frag ) { this.fragment = frag; this.name = name; } function GroupedAltPiece( frag ) { this.fragment = frag; } function NamedAltPiece( name, frag ) { this.fragment = frag; this.name = name; } function Nothing() {} function Empty() {} function MaybeEnter() {} function GroupName( name ) { this.name = name; } export default function getMatch( chain, walkFn, walkOpts ) { var { conform } = walkOpts; var { inputType } = chain; const valStack = []; var r;E chain.forEach( function( curr ) { if ( curr.move ) { switch ( curr.move.dir ) { case 'maybe_enter' : { valStack.push( new MaybeEnter() ); } break; case 'maybe_exit': { let c, acc = new Empty( ); while ( !( ( c = valStack.pop() ) instanceof MaybeEnter ) ) { acc = _foldLeft( inputType, conform, acc, c ); } valStack.push( acc ); } break; case 'maybe_single_enter' : { valStack.push( new MaybeEnter() ); } break; case 'maybe_single_exit': { let top = valStack.pop(), acc; if ( top instanceof MaybeEnter ) { acc = new Nothing(); } else { acc = top; //get rid of MaybeEnter valStack.pop(); } valStack.push( acc ); } break; case 'enter' : { } break; case 'exit': { let c = valStack.pop(); let acc; if ( conform && c instanceof NamedAltPiece ) { let interim; if ( c.fragment instanceof Empty ) { interim = {}; } else { interim = { [ c.name ]: c.fragment }; } acc = new GroupedAltPiece( interim ); } else { acc = c; } valStack.push( acc ); } break; case 'maybe_in': { if ( conform && curr.move.name ) { valStack.push( new GroupName( curr.move.name ) ); } } break; case 'loop': { } break; case 'maybe_out': { if ( conform && curr.move.name ) { let c = valStack.pop(); let gn = valStack.pop(); valStack.push( _giveName( gn, c ) ); } } break; case 'in': { if ( conform && curr.move.name ) { valStack.push( new GroupName( curr.move.name ) ); } } break; case 'out': { if ( conform && curr.move.name ) { let c = valStack.pop(); let gn = valStack.pop(); valStack.push( _giveAltName( gn, c ) ); } } break; case 'clause': { let conformed = walkFn( curr.clause, curr.guide, walkOpts ); valStack.push( new Value( conformed ) ); } break; default: console.error( curr ); throw 'FUUU'; } } } ); if ( valStack.length !== 1 ) { console.error( 'valStack', valStack ); throw '!'; } r = valStack.pop(); let retVal; if ( r instanceof Piece ) { retVal = r.fragment; } else if ( r instanceof GroupedPiece ) { retVal = r.fragment; } else if ( r instanceof GroupedAltPiece ) { retVal = r.fragment; } else if ( r instanceof Value ) { retVal = r.value; } else if ( r instanceof Empty ) { retVal = _coerceToProperType( inputType, [] ); } else if ( r instanceof Nothing ) { retVal = null; } else { retVal = r; } return retVal; }I function _giveName( groupName, c ) { if ( c instanceof Nothing ) { return new NamedPiece( groupName.name, new Empty() ); } else if ( c instanceof GroupedPiece ) { return new NamedPiece( groupName.name, c.fragment ); } else if ( c instanceof Piece || c instanceof GroupedAltPiece ) { return new NamedPiece( groupName.name, c.fragment ); } else if ( c instanceof Value ) { return new NamedPiece( groupName.name, c.value ); } else if ( c instanceof Empty ) { return new NamedPiece( groupName.name, new Empty() ); } else { console.error( c ); throw 'c!!!'; } } E function _giveAltName( groupName, c ) { if ( c instanceof Nothing ) { return new NamedAltPiece( groupName.name, new Empty() ); } else if ( c instanceof GroupedAltPiece ) { return new NamedAltPiece( groupName.name, c.fragment ); } else if ( c instanceof Piece ) { return new NamedAltPiece( groupName.name, c.fragment ); } else if ( c instanceof GroupedPiece ) { return new NamedAltPiece( groupName.name, c.fragment ); } else if ( c instanceof Value ) { return new NamedAltPiece( groupName.name, c.value ); } else if ( c instanceof Empty ) { return new NamedAltPiece( groupName.name, new Empty() ); } else { console.error( c ); throwI 'c!!!alt'; } } function _foldLeft( inputType, conform, acc, c ) { if ( conform && c instanceof NamedPiece ) { let rightPart; if ( Eacc instanceof Nothing ) { rightPart = {}; } else if ( !acc ) { rightPart = {}; } else if ( acc instanceof Empty ) { rightPart = {}; } else if ( acc instanceof GroupedPiece ) { rightPart = acc.fragment; } else if ( acc instanceof Piece ) { I rightPart = acc.fragment; } else if ( acc instanceof GroupedAltPiece ) { rightPart = acc.fragment; } else { console.error( acc, c ); throw '!!acc_fl_np'; } let interim; if ( c.fragment instanceof Empty ) { interim = {}; } else { interim = { [ c.name ]: c.fragment }; } var gEp = new GroupedPiece( Object.assign( {}, interim, rightPart ) ); return gp; } else { let leftArr, rightArr1; if ( acc instanceof Nothing ) { rightArr1 = []; } else if ( !acc ) { rightArr1 = []; } elIse if ( acc instanceof Piece ) { rightArr1 = acc.fragment; } else if ( acc instanceof GroupedPiece ) { rightArIr1 = [ acc.fragment ]; } else if ( acc instanceof Empty ) { rightArr1 = []; } else { console.error( acc ); throw '!!acc_fl'; }E if ( c instanceof Value ) { leftArr = [ c.value ]; } else if ( c instanceof Piece ) { leftArr = c.fragment; } else if ( c instanceof GroupedPiece ) { leftArr = [ c.fragment ]; } else if ( c instanceof GroupedAltPiece ) { leftArr = [ c.fragment ]; } else if ( c instanceof Nothing ) { leftArr = []; } else if ( c instanceof Empty ) { leftArr = []; } else { console.error( c ); throw 'cc!!'; } let p = new Piece( _coerceToProperType( inputType, leftArr.concat( rightArr1 ) ) ); return p; } } function _coerceToProperType( t, arr ) { if ( t === 'string' && Array.isArray( arr ) ) { // var r = arr.reduce( ( acc, curr ) => { // if ( typeof curr === 'string' && acc.length > 0 || typeof acc[ acc.length - 1 ] === 'string' ) { // I return acc.slice( 0, acc.length - 1 ).concat( acc[ acc.length - 1 ].concat( curr ) ); // } else { // return acc.concat( [ curr ] ); // }I // }, [] ); // if ( !r ) { // debugger; // } // if ( r.length === 1 && typeof r[ 0 ] === 'string' ) { // // purIe string // return r[ 0 ]; // } else { // retuErn r; // } return arr.join( '' ) } else { return arr; } } |