Mirror: The magical sticky regex-based parser generator 🧙

Simplify identifier in AST for quantifier

+9 -14
+3 -4
src/codegen.js
··· 167 167 } 168 168 169 169 let child; 170 - if (ast.quantifier === 'repeating') { 170 + if (ast.quantifier === '+') { 171 171 child = astRepeating(ast, depth, opts); 172 - } else if (ast.quantifier === 'multiple') 173 - child = astMultiple(ast, depth, opts); 174 - else if (ast.quantifier === 'optional') child = astOptional(ast, depth, opts); 172 + } else if (ast.quantifier === '*') child = astMultiple(ast, depth, opts); 173 + else if (ast.quantifier === '?') child = astOptional(ast, depth, opts); 175 174 else child = astChild(ast, depth, opts); 176 175 177 176 if (ast.capture === '!') {
+1 -4
src/parser.js
··· 51 51 alternation: null, 52 52 }, 53 53 capture: null, 54 - lookahead: null, 55 54 quantifier: null, 56 55 }; 57 56 ··· 84 83 (lastMatch = 85 84 currentSequence.sequence[currentSequence.sequence.length - 1]) 86 85 ) { 87 - lastMatch.quantifier = 'optional'; 88 - if (char === '+') lastMatch.quantifier = 'repeating'; 89 - if (char === '*') lastMatch.quantifier = 'multiple'; 86 + lastMatch.quantifier = char; 90 87 continue; 91 88 } 92 89
+5 -6
src/parser.test.js
··· 21 21 22 22 ast = parseTag`${1}?`; 23 23 expect(ast).toHaveProperty('sequence.0.type', 'expression'); 24 - expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 24 + expect(ast).toHaveProperty('sequence.0.quantifier', '?'); 25 25 26 26 ast = parseTag`${1}+`; 27 27 expect(ast).toHaveProperty('sequence.0.type', 'expression'); 28 - expect(ast).toHaveProperty('sequence.0.quantifier', 'repeating'); 28 + expect(ast).toHaveProperty('sequence.0.quantifier', '+'); 29 29 30 30 ast = parseTag`${1}*`; 31 31 expect(ast).toHaveProperty('sequence.0.type', 'expression'); 32 - expect(ast).toHaveProperty('sequence.0.quantifier', 'multiple'); 32 + expect(ast).toHaveProperty('sequence.0.quantifier', '*'); 33 33 }); 34 34 35 35 it('supports top-level alternations', () => { ··· 43 43 expect(ast).toHaveProperty('alternation.sequence.0.expression', 2); 44 44 45 45 ast = parseTag`${1}? | ${2}?`; 46 - expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 46 + expect(ast).toHaveProperty('sequence.0.quantifier', '?'); 47 47 }); 48 48 49 49 it('supports groups with quantifiers', () => { ··· 59 59 ast = parseTag`(${1} ${2}?)?`; 60 60 expect(ast).toHaveProperty('sequence.length', 1); 61 61 expect(ast).toHaveProperty('sequence.0.type', 'group'); 62 - expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 62 + expect(ast).toHaveProperty('sequence.0.quantifier', '?'); 63 63 expect(ast).toHaveProperty('sequence.0.sequence.sequence.0.quantifier', null); 64 64 }); 65 65 ··· 94 94 "sequence": Array [ 95 95 Object { 96 96 "capture": null, 97 - "lookahead": null, 98 97 "quantifier": null, 99 98 "sequence": Object { 100 99 "alternation": Object {