Mirror: The magical sticky regex-based parser generator 🧙

Merge pull request #14 from kitten/fix/eof-matches

Fix empty matches being impossible at end of input

authored by kitten.sh and committed by

GitHub 84b30d24 050ed1f2

+11 -7
+6 -7
src/core.js
··· 10 10 11 11 const execString = (pattern) => { 12 12 return (state) => { 13 - const input = state.quasis[state.x]; 14 - if (input && state.y < input.length) { 13 + if (state.x < state.quasis.length) { 14 + const input = state.quasis[state.x]; 15 15 for (let i = 0, l = pattern.length; i < l; i++) 16 16 if (input.charCodeAt(state.y + i) !== pattern.charCodeAt(i)) 17 17 return null; ··· 26 26 ? new RegExp(pattern.source, 'y') 27 27 : new RegExp(pattern.source + '|()', 'g'); 28 28 return (state) => { 29 - const input = state.quasis[state.x]; 30 - if (input && state.y < input.length) { 29 + if (state.x < state.quasis.length) { 30 + const input = state.quasis[state.x]; 31 31 pattern.lastIndex = state.y; 32 - 33 32 let match; 34 33 if (isStickySupported) { 35 34 if (pattern.test(input)) ··· 59 58 let match; 60 59 61 60 if ( 62 - state.y >= state.quasis[state.x].length && 63 - state.x < state.expressions.length 61 + state.x < state.expressions.length && 62 + state.y >= state.quasis[state.x].length 64 63 ) { 65 64 state.y = 0; 66 65 match = state.expressions[state.x++];
+5
src/core.test.js
··· 24 24 `('should return $result when $input is passed', ({ input, result }) => { 25 25 expectToParse(node, input, result); 26 26 }); 27 + 28 + it('matches empty regex patterns', () => { 29 + const node = match('node')`${/[ ]*/}`; 30 + expectToParse(node, '', ['']); 31 + }); 27 32 }); 28 33 29 34 describe('optional matcher', () => {