tangled
alpha
login
or
join now
kitten.sh
/
reghex
0
fork
atom
Mirror: The magical sticky regex-based parser generator 🧙
0
fork
atom
overview
issues
pulls
pipelines
Simplify identifier in AST for quantifier
kitten.sh
5 years ago
cb877faf
109214c5
+9
-14
3 changed files
expand all
collapse all
unified
split
src
codegen.js
parser.js
parser.test.js
+3
-4
src/codegen.js
···
167
167
}
168
168
169
169
let child;
170
170
-
if (ast.quantifier === 'repeating') {
170
170
+
if (ast.quantifier === '+') {
171
171
child = astRepeating(ast, depth, opts);
172
172
-
} else if (ast.quantifier === 'multiple')
173
173
-
child = astMultiple(ast, depth, opts);
174
174
-
else if (ast.quantifier === 'optional') child = astOptional(ast, depth, opts);
172
172
+
} else if (ast.quantifier === '*') child = astMultiple(ast, depth, opts);
173
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
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
87
-
lastMatch.quantifier = 'optional';
88
88
-
if (char === '+') lastMatch.quantifier = 'repeating';
89
89
-
if (char === '*') lastMatch.quantifier = 'multiple';
86
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
24
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
24
24
+
expect(ast).toHaveProperty('sequence.0.quantifier', '?');
25
25
26
26
ast = parseTag`${1}+`;
27
27
expect(ast).toHaveProperty('sequence.0.type', 'expression');
28
28
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'repeating');
28
28
+
expect(ast).toHaveProperty('sequence.0.quantifier', '+');
29
29
30
30
ast = parseTag`${1}*`;
31
31
expect(ast).toHaveProperty('sequence.0.type', 'expression');
32
32
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'multiple');
32
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
46
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
46
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
62
-
expect(ast).toHaveProperty('sequence.0.quantifier', 'optional');
62
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
97
-
"lookahead": null,
98
97
"quantifier": null,
99
98
"sequence": Object {
100
99
"alternation": Object {