Mirror: The magical sticky regex-based parser generator 🧙
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Clean up assign index / tag logic in Generator

+31 -47
+22 -24
src/babel/__snapshots__/plugin.test.js.snap
··· 9 9 _node_expression2 = (0, _reghex._pattern)(2); 10 10 11 11 const node = function _node(state) { 12 + var last_index = state.index; 12 13 var match, 13 - last_index = state.index, 14 - node = (0, _reghex.tag)([], 'node'); 14 + node = []; 15 15 16 16 if (match = (0, _reghex._exec)(state, _node_expression)) { 17 17 node.push(match); ··· 27 27 return; 28 28 } 29 29 30 - return node; 30 + return (0, _reghex.tag)(node, 'node'); 31 31 };" 32 32 `; 33 33 ··· 37 37 var _inner_expression = _pattern(/inner/); 38 38 39 39 const inner = function _inner(state) { 40 + var last_index = state.index; 40 41 var match, 41 - last_index = state.index, 42 - node = tag([], 'inner'); 42 + node = []; 43 43 44 44 if (match = _exec(state, _inner_expression)) { 45 45 node.push(match); ··· 48 48 return; 49 49 } 50 50 51 - return node; 51 + return tag(node, 'inner'); 52 52 }; 53 53 54 54 const node = function _node(state) { 55 + var last_index = state.index; 55 56 var match, 56 - last_index = state.index, 57 - node = tag([], 'node'); 57 + node = []; 58 58 59 59 if (match = inner(state)) { 60 60 node.push(match); ··· 63 63 return; 64 64 } 65 65 66 - return node; 66 + return tag(node, 'node'); 67 67 };" 68 68 `; 69 69 ··· 75 75 _node_expression3 = _pattern(3); 76 76 77 77 const node = function _node(state) { 78 + var last_index = state.index; 78 79 var match, 79 - last_index = state.index, 80 - node = _tag([], 'node'); 80 + node = []; 81 81 82 82 if (match = _exec(state, _node_expression)) { 83 83 node.push(match); ··· 119 119 } 120 120 } 121 121 122 - return node; 122 + return _tag(node, 'node'); 123 123 };" 124 124 `; 125 125 ··· 133 133 _node_expression5 = _pattern(5); 134 134 135 135 const node = function _node(state) { 136 + var last_index = state.index; 136 137 var match, 137 - last_index = state.index, 138 - node = _tag([], 'node'); 138 + node = []; 139 139 140 140 block_0: { 141 141 var index_0 = state.index; ··· 156 156 } 157 157 } 158 158 159 - return node; 159 + return _tag(node, 'node'); 160 160 } 161 161 162 162 loop_0: for (var iter_0 = 0; true; iter_0++) { ··· 204 204 } 205 205 } 206 206 207 - return node; 207 + return _tag(node, 'node'); 208 208 };" 209 209 `; 210 210 ··· 214 214 var _inner_transform = x => x; 215 215 216 216 const first = function _inner(state) { 217 + var last_index = state.index; 217 218 var match, 218 - last_index = state.index, 219 - node = _tag([], 'inner'); 220 - 221 - return _inner_transform(node); 219 + node = []; 220 + return _inner_transform(_tag(node, 'inner')); 222 221 }; 223 222 224 223 const transform = x => x; 225 224 226 225 const second = function _node(state) { 226 + var last_index = state.index; 227 227 var match, 228 - last_index = state.index, 229 - node = _tag([], 'node'); 230 - 231 - return transform(node); 228 + node = []; 229 + return transform(_tag(node, 'node')); 232 230 };" 233 231 `;
+9 -23
src/babel/generator.js
··· 231 231 const loopId = t.identifier(`loop_${this.depth}`); 232 232 const iterId = t.identifier(`iter_${this.depth}`); 233 233 const indexId = t.identifier(`index_${this.depth}`); 234 - const lastIndex = t.memberExpression(ids.state, t.identifier('index')); 234 + const assignIndex = new AssignIndexNode(indexId); 235 235 236 236 let statements; 237 237 if (quantifier && !quantifier.singular && quantifier.required) { ··· 245 245 t.booleanLiteral(true), 246 246 t.updateExpression('++', iterId), 247 247 t.blockStatement([ 248 - t.variableDeclaration('var', [ 249 - t.variableDeclarator(indexId, lastIndex), 250 - ]), 248 + assignIndex.statement(), 251 249 ...this.childNode.statements(), 252 250 ]) 253 251 ) ··· 260 258 t.whileStatement( 261 259 t.booleanLiteral(true), 262 260 t.blockStatement([ 263 - t.variableDeclaration('var', [ 264 - t.variableDeclarator(indexId, lastIndex), 265 - ]), 261 + assignIndex.statement(), 266 262 ...this.childNode.statements(), 267 263 ]) 268 264 ) 269 265 ), 270 266 ]; 271 267 } else if (quantifier && !quantifier.required) { 272 - statements = [ 273 - t.variableDeclaration('var', [ 274 - t.variableDeclarator(indexId, lastIndex), 275 - ]), 276 - ...this.childNode.statements(), 277 - ]; 268 + statements = [assignIndex.statement(), ...this.childNode.statements()]; 278 269 } else { 279 270 statements = this.childNode.statements(); 280 271 } ··· 382 373 export class RootNode { 383 374 constructor(ast, nameNode, transformNode) { 384 375 const indexId = t.identifier('last_index'); 376 + const node = t.callExpression(ids.tag, [ids.node, nameNode]); 385 377 386 378 this.returnStatement = t.returnStatement( 387 - transformNode ? t.callExpression(transformNode, [ids.node]) : ids.node 379 + transformNode ? t.callExpression(transformNode, [node]) : node 388 380 ); 389 381 390 - this.nameNode = nameNode; 382 + this.assignIndex = new AssignIndexNode(indexId); 391 383 this.node = new AlternationNode(ast, 0, { 392 384 returnStatement: this.returnStatement, 393 385 restoreIndex: new RestoreIndexNode(indexId, true), ··· 399 391 } 400 392 401 393 statements() { 402 - const indexId = t.identifier('last_index'); 403 - const lastIndex = t.memberExpression(ids.state, t.identifier('index')); 404 - 405 394 return [ 395 + this.assignIndex.statement(), 406 396 t.variableDeclaration('var', [ 407 397 t.variableDeclarator(ids.match), 408 - t.variableDeclarator(indexId, lastIndex), 409 - t.variableDeclarator( 410 - ids.node, 411 - t.callExpression(ids.tag, [t.arrayExpression(), this.nameNode]) 412 - ), 398 + t.variableDeclarator(ids.node, t.arrayExpression()), 413 399 ]), 414 400 ...this.node.statements(), 415 401 this.returnStatement,