Mirror: The magical sticky regex-based parser generator 🧙

Simplify identifier in AST for quantifier

Changed files
+9 -14
src
+3 -4
src/codegen.js
···
}
let child;
-
if (ast.quantifier === 'repeating') {
+
if (ast.quantifier === '+') {
child = astRepeating(ast, depth, opts);
-
} else if (ast.quantifier === 'multiple')
-
child = astMultiple(ast, depth, opts);
-
else if (ast.quantifier === 'optional') child = astOptional(ast, depth, opts);
+
} else if (ast.quantifier === '*') child = astMultiple(ast, depth, opts);
+
else if (ast.quantifier === '?') child = astOptional(ast, depth, opts);
else child = astChild(ast, depth, opts);
if (ast.capture === '!') {
+1 -4
src/parser.js
···
alternation: null,
},
capture: null,
-
lookahead: null,
quantifier: null,
};
···
(lastMatch =
currentSequence.sequence[currentSequence.sequence.length - 1])
) {
-
lastMatch.quantifier = 'optional';
-
if (char === '+') lastMatch.quantifier = 'repeating';
-
if (char === '*') lastMatch.quantifier = 'multiple';
+
lastMatch.quantifier = char;
continue;
}
+5 -6
src/parser.test.js
···
ast = parseTag`${1}?`;
expect(ast).toHaveProperty('sequence.0.type', 'expression');
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
+
expect(ast).toHaveProperty('sequence.0.quantifier', '?');
ast = parseTag`${1}+`;
expect(ast).toHaveProperty('sequence.0.type', 'expression');
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'repeating');
+
expect(ast).toHaveProperty('sequence.0.quantifier', '+');
ast = parseTag`${1}*`;
expect(ast).toHaveProperty('sequence.0.type', 'expression');
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'multiple');
+
expect(ast).toHaveProperty('sequence.0.quantifier', '*');
});
it('supports top-level alternations', () => {
···
expect(ast).toHaveProperty('alternation.sequence.0.expression', 2);
ast = parseTag`${1}? | ${2}?`;
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
+
expect(ast).toHaveProperty('sequence.0.quantifier', '?');
});
it('supports groups with quantifiers', () => {
···
ast = parseTag`(${1} ${2}?)?`;
expect(ast).toHaveProperty('sequence.length', 1);
expect(ast).toHaveProperty('sequence.0.type', 'group');
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
+
expect(ast).toHaveProperty('sequence.0.quantifier', '?');
expect(ast).toHaveProperty('sequence.0.sequence.sequence.0.quantifier', null);
});
···
"sequence": Array [
Object {
"capture": null,
-
"lookahead": null,
"quantifier": null,
"sequence": Object {
"alternation": Object {