Mirror: The magical sticky regex-based parser generator 🧙

Golf some bytes off of parser

Changed files
+17 -26
src
+17 -26
src/parser.js
··· 1 + const syntaxError = (char) => { 2 + throw new SyntaxError('Unexpected token "' + char + '"'); 3 + }; 4 + 1 5 export const parse = (quasis, expressions) => { 2 6 let quasiIndex = 0; 3 7 let stackIndex = 0; ··· 9 13 let lastMatch; 10 14 let currentSequence = rootSequence; 11 15 12 - while (stackIndex < quasis.length + expressions.length) { 16 + for ( 17 + let quasiIndex = 0, stackIndex = 0; 18 + stackIndex < quasis.length + expressions.length; 19 + stackIndex++ 20 + ) { 13 21 if (stackIndex % 2 !== 0) { 14 22 currentSequence.push({ 15 23 expression: expressions[stackIndex++ >> 1], ··· 17 25 } 18 26 19 27 const quasi = quasis[stackIndex >> 1]; 20 - while (quasiIndex < quasi.length) { 28 + for (quasiIndex = 0; quasiIndex < quasi.length; ) { 21 29 const char = quasi[quasiIndex++]; 22 - 23 30 if (char === ' ' || char === '\t' || char === '\r' || char === '\n') { 24 - continue; 25 31 } else if (char === '|' && currentSequence.length) { 26 32 currentSequence = currentSequence.alternation = []; 27 - continue; 28 33 } else if (char === ')' && currentSequence.length) { 29 34 currentGroup = null; 30 35 currentSequence = sequenceStack.pop(); 31 - if (currentSequence) continue; 36 + if (!currentSequence) syntaxError(char); 32 37 } else if (char === '(') { 33 - currentGroup = { 34 - sequence: [], 35 - }; 36 - 37 38 sequenceStack.push(currentSequence); 38 - currentSequence.push(currentGroup); 39 + currentSequence.push((currentGroup = { sequence: [] })); 39 40 currentSequence = currentGroup.sequence; 40 - continue; 41 41 } else if (char === '?' && !currentSequence.length && currentGroup) { 42 42 const nextChar = quasi[quasiIndex++]; 43 - if (nextChar === ':') { 43 + if (nextChar === ':' || nextChar === '=' || nextChar === '!') { 44 44 currentGroup.capture = nextChar; 45 - continue; 46 - } else if (nextChar === '=') { 47 - currentGroup.capture = nextChar; 48 - continue; 49 - } else if (nextChar === '!') { 50 - currentGroup.capture = nextChar; 51 - continue; 45 + } else { 46 + syntaxError(char); 52 47 } 53 48 } else if ( 54 49 (char === '?' || char === '+' || char === '*') && 55 50 (lastMatch = currentSequence[currentSequence.length - 1]) 56 51 ) { 57 52 lastMatch.quantifier = char; 58 - continue; 53 + } else { 54 + syntaxError(char); 59 55 } 60 - 61 - throw new SyntaxError('Unexpected token "' + char + '"'); 62 56 } 63 - 64 - stackIndex++; 65 - quasiIndex = 0; 66 57 } 67 58 68 59 return rootSequence;