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 quantifier type in parser AST

+14 -49
+5 -6
src/codegen.js
··· 2 2 const _match = 'match'; 3 3 const _node = 'node'; 4 4 5 - export function js(/* arguments */) { 5 + function js(/* arguments */) { 6 6 let body = arguments[0][0]; 7 7 for (let i = 1; i < arguments.length; i++) 8 - body = body + arguments[i] + (arguments[0][i] || ''); 8 + body = body + arguments[i] + arguments[0][i]; 9 9 return body.trim(); 10 10 } 11 11 ··· 144 144 } 145 145 146 146 let child; 147 - if (ast.quantifier && !ast.quantifier.singular && ast.quantifier.required) { 147 + if (ast.quantifier === 'repeating') { 148 148 child = astRepeating(ast, depth, opts); 149 - } else if (ast.quantifier && !ast.quantifier.singular) 149 + } else if (ast.quantifier === 'multiple') 150 150 child = astMultiple(ast, depth, opts); 151 - else if (ast.quantifier && !ast.quantifier.required) 152 - child = astOptional(ast, depth, opts); 151 + else if (ast.quantifier === 'optional') child = astOptional(ast, depth, opts); 153 152 else child = astChild(ast, depth, opts); 154 153 155 154 if (ast.lookahead === 'negative') {
+1 -2
src/core.js
··· 1 - import { js, astRoot } from './codegen'; 1 + import { astRoot } from './codegen'; 2 2 import { parse as parseDSL } from './parser'; 3 3 4 4 const isStickySupported = typeof /./g.sticky === 'boolean'; 5 5 6 6 export const _pattern = (input) => { 7 7 if (typeof input === 'function') return input; 8 - 9 8 const source = typeof input !== 'string' ? input.source : input; 10 9 return isStickySupported 11 10 ? new RegExp(source, 'y')
+3 -10
src/parser.js
··· 86 86 (lastMatch = 87 87 currentSequence.sequence[currentSequence.sequence.length - 1]) 88 88 ) { 89 - if (lastMatch.type === 'group' && lastMatch.lookahead) { 90 - throw new SyntaxError('Unexpected quantifier on lookahead group'); 91 - } 92 - 93 - lastMatch.quantifier = { 94 - type: 'quantifier', 95 - required: char === '+', 96 - singular: char === '?', 97 - }; 98 - 89 + lastMatch.quantifier = 'optional'; 90 + if (char === '+') lastMatch.quantifier = 'repeating'; 91 + if (char === '*') lastMatch.quantifier = 'multiple'; 99 92 continue; 100 93 } 101 94
+5 -31
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', { 25 - type: 'quantifier', 26 - required: false, 27 - singular: true, 28 - }); 24 + expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 29 25 30 26 ast = parseTag`${1}+`; 31 27 expect(ast).toHaveProperty('sequence.0.type', 'expression'); 32 - expect(ast).toHaveProperty('sequence.0.quantifier', { 33 - type: 'quantifier', 34 - required: true, 35 - singular: false, 36 - }); 28 + expect(ast).toHaveProperty('sequence.0.quantifier', 'repeating'); 37 29 38 30 ast = parseTag`${1}*`; 39 31 expect(ast).toHaveProperty('sequence.0.type', 'expression'); 40 - expect(ast).toHaveProperty('sequence.0.quantifier', { 41 - type: 'quantifier', 42 - required: false, 43 - singular: false, 44 - }); 32 + expect(ast).toHaveProperty('sequence.0.quantifier', 'multiple'); 45 33 }); 46 34 47 35 it('supports top-level alternations', () => { ··· 55 43 expect(ast).toHaveProperty('alternation.sequence.0.expression', 2); 56 44 57 45 ast = parseTag`${1}? | ${2}?`; 58 - expect(ast).toHaveProperty('sequence.0.quantifier.type', 'quantifier'); 59 - expect(ast).toHaveProperty( 60 - 'alternation.sequence.0.quantifier.type', 61 - 'quantifier' 62 - ); 46 + expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 63 47 }); 64 48 65 49 it('supports groups with quantifiers', () => { ··· 75 59 ast = parseTag`(${1} ${2}?)?`; 76 60 expect(ast).toHaveProperty('sequence.length', 1); 77 61 expect(ast).toHaveProperty('sequence.0.type', 'group'); 78 - expect(ast).toHaveProperty('sequence.0.quantifier.type', 'quantifier'); 62 + expect(ast).toHaveProperty('sequence.0.quantifier', 'optional'); 79 63 expect(ast).toHaveProperty('sequence.0.sequence.sequence.0.quantifier', null); 80 - expect(ast).toHaveProperty( 81 - 'sequence.0.sequence.sequence.1.quantifier.type', 82 - 'quantifier' 83 - ); 84 64 }); 85 65 86 66 it('supports non-capturing groups', () => { ··· 108 88 expect(ast).toHaveProperty('sequence.0.capturing', false); 109 89 expect(ast).toHaveProperty('sequence.0.lookahead', 'negative'); 110 90 expect(ast).toHaveProperty('sequence.0.sequence.sequence.length', 1); 111 - }); 112 - 113 - it('throws when a quantifier is combined with a lookahead', () => { 114 - expect(() => parseTag`(?! ${1})+`).toThrow(); 115 - expect(() => parseTag`(?! ${1})?`).toThrow(); 116 - expect(() => parseTag`(?! ${1})*`).toThrow(); 117 91 }); 118 92 119 93 it('supports groups with alternates', () => {