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

Configure Feed

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

Simplify AST for non-capturing groups

+18 -23
+10 -10
src/codegen.js
··· 14 14 length: next.length != null ? next.length : prev.length, 15 15 onAbort: next.onAbort != null ? next.onAbort : prev.onAbort, 16 16 abort: next.abort != null ? next.abort : prev.abort, 17 - capturing: next.capturing != null ? next.capturing : prev.capturing, 17 + capture: next.capture != null ? next.capture : prev.capture, 18 18 }); 19 19 20 20 const assignIndex = (depth) => ··· 47 47 ${opts.abort || ''} 48 48 `; 49 49 50 - if (!opts.capturing) { 50 + if (!opts.capture) { 51 51 return js` 52 52 if (!(${ast.expression})) { 53 53 ${abort} ··· 68 68 if (ast.sequence.length === 1) 69 69 return astExpression(ast.sequence[0], depth, opts); 70 70 71 - const capturing = !!opts.capturing && !!ast.capturing; 71 + const capture = !!opts.capture && !ast.capture; 72 72 73 73 let group = ''; 74 - if (!opts.length && capturing) { 74 + if (!opts.length && capture) { 75 75 return js` 76 76 ${js`var length_${depth} = ${_node}.length;`} 77 77 ${astSequence( ··· 79 79 depth + 1, 80 80 newOpts(opts, { 81 81 length: depth, 82 - capturing, 82 + capture, 83 83 }) 84 84 )} 85 85 `; ··· 89 89 ast.sequence, 90 90 depth + 1, 91 91 newOpts(opts, { 92 - capturing, 92 + capture, 93 93 }) 94 94 ); 95 95 }; ··· 159 159 const { index, abort } = opts; 160 160 const label = `invert_${depth}`; 161 161 162 - if (ast.lookahead === 'negative') { 162 + if (ast.capture === '!') { 163 163 opts = newOpts(opts, { 164 164 index: depth, 165 165 abort: js`break ${label};`, ··· 174 174 else if (ast.quantifier === 'optional') child = astOptional(ast, depth, opts); 175 175 else child = astChild(ast, depth, opts); 176 176 177 - if (ast.lookahead === 'negative') { 177 + if (ast.capture === '!') { 178 178 return js` 179 179 ${label}: { 180 180 ${assignIndex(depth)} ··· 183 183 ${abort} 184 184 } 185 185 `; 186 - } else if (ast.lookahead) { 186 + } else if (ast.capture === '=') { 187 187 return js` 188 188 ${assignIndex(depth)} 189 189 ${child} ··· 247 247 length: 0, 248 248 onAbort: '', 249 249 abort: js`return;`, 250 - capturing: true, 250 + capture: true, 251 251 })} 252 252 253 253 ${_node}.tag = ${name};
+4 -6
src/parser.js
··· 50 50 sequence: [], 51 51 alternation: null, 52 52 }, 53 - capturing: true, 53 + capture: null, 54 54 lookahead: null, 55 55 quantifier: null, 56 56 }; ··· 70 70 } 71 71 72 72 if (nextChar === ':') { 73 - currentGroup.capturing = false; 73 + currentGroup.capture = nextChar; 74 74 continue; 75 75 } else if (nextChar === '=') { 76 - currentGroup.capturing = false; 77 - currentGroup.lookahead = 'positive'; 76 + currentGroup.capture = nextChar; 78 77 continue; 79 78 } else if (nextChar === '!') { 80 - currentGroup.capturing = false; 81 - currentGroup.lookahead = 'negative'; 79 + currentGroup.capture = nextChar; 82 80 continue; 83 81 } 84 82 } else if (
+4 -7
src/parser.test.js
··· 67 67 const ast = parseTag`(?: ${1})`; 68 68 expect(ast).toHaveProperty('sequence.length', 1); 69 69 expect(ast).toHaveProperty('sequence.0.type', 'group'); 70 - expect(ast).toHaveProperty('sequence.0.capturing', false); 71 - expect(ast).toHaveProperty('sequence.0.lookahead', null); 70 + expect(ast).toHaveProperty('sequence.0.capture', ':'); 72 71 expect(ast).toHaveProperty('sequence.0.sequence.sequence.length', 1); 73 72 }); 74 73 ··· 76 75 const ast = parseTag`(?= ${1})`; 77 76 expect(ast).toHaveProperty('sequence.length', 1); 78 77 expect(ast).toHaveProperty('sequence.0.type', 'group'); 79 - expect(ast).toHaveProperty('sequence.0.capturing', false); 80 - expect(ast).toHaveProperty('sequence.0.lookahead', 'positive'); 78 + expect(ast).toHaveProperty('sequence.0.capture', '='); 81 79 expect(ast).toHaveProperty('sequence.0.sequence.sequence.length', 1); 82 80 }); 83 81 ··· 85 83 const ast = parseTag`(?! ${1})`; 86 84 expect(ast).toHaveProperty('sequence.length', 1); 87 85 expect(ast).toHaveProperty('sequence.0.type', 'group'); 88 - expect(ast).toHaveProperty('sequence.0.capturing', false); 89 - expect(ast).toHaveProperty('sequence.0.lookahead', 'negative'); 86 + expect(ast).toHaveProperty('sequence.0.capture', '!'); 90 87 expect(ast).toHaveProperty('sequence.0.sequence.sequence.length', 1); 91 88 }); 92 89 ··· 96 93 "alternation": null, 97 94 "sequence": Array [ 98 95 Object { 99 - "capturing": true, 96 + "capture": null, 100 97 "lookahead": null, 101 98 "quantifier": null, 102 99 "sequence": Object {