Mirror: The magical sticky regex-based parser generator 🧙

Remove interpolations exception from codegen

Changed files
+16 -23
src
+9 -12
src/codegen.js
··· 3 3 const _node = 'node'; 4 4 const _match = 'x'; 5 5 6 - let _interpolations = 0; 7 - 8 6 function js(/* arguments */) { 9 7 let body = arguments[0][0]; 10 8 for (let i = 1; i < arguments.length; i++) ··· 18 16 return next; 19 17 }; 20 18 21 - const assignIndex = (depth) => 22 - js`var y${depth} = ${_state}.y` + 23 - (_interpolations ? js`, x${depth} = ${_state}.x;` : ';'); 19 + const assignIndex = (depth) => js` 20 + var y${depth} = ${_state}.y, 21 + x${depth} = ${_state}.x; 22 + `; 24 23 25 - const restoreIndex = (depth) => 26 - js`${_state}.y = y${depth}` + 27 - (_interpolations ? js`, ${_state}.x = x${depth};` : ';'); 24 + const restoreIndex = (depth) => js` 25 + ${_state}.y = y${depth}; 26 + ${_state}.x = x${depth}; 27 + `; 28 28 29 29 const astExpression = (ast, depth, opts) => { 30 30 const restoreLength = ··· 181 181 `; 182 182 }; 183 183 184 - const astRoot = (ast, name, transform, interpolations) => { 185 - _interpolations = interpolations; 186 - 184 + const astRoot = (ast, name, transform) => { 187 185 return js` 188 186 (function (${_state}) { 189 187 ${assignIndex(1)} ··· 191 189 var ${_match}; 192 190 193 191 ${astSequence(ast, 2, { 194 - interpolations, 195 192 index: 1, 196 193 length: 0, 197 194 onAbort: '',
+7 -11
src/core.js
··· 26 26 } else { 27 27 match = pattern.exec(input)[0] || match; 28 28 } 29 + 30 + state.y = pattern.lastIndex; 29 31 } 30 32 31 - state.y = pattern.lastIndex; 32 33 return match; 33 34 }; 34 35 ··· 51 52 }; 52 53 53 54 export const match = (name, transform) => (quasis, ...expressions) => { 54 - let interpolations = 0; 55 - 56 55 const ast = parseDSL( 57 56 quasis, 58 - expressions.map((expression, i) => { 59 - if (expression === interpolation) interpolations++; 60 - return { 61 - fn: typeof expression === 'function' && expression.length, 62 - id: `_${i}`, 63 - }; 64 - }) 57 + expressions.map((expression, i) => ({ 58 + fn: typeof expression === 'function' && expression.length, 59 + id: `_${i}`, 60 + })) 65 61 ); 66 62 67 63 const makeMatcher = new Function( 68 64 execId + ',_n,_t,' + expressions.map((_expression, i) => `_${i}`).join(','), 69 - 'return ' + astRoot(ast, '_n', transform ? '_t' : null, interpolations) 65 + 'return ' + astRoot(ast, '_n', transform ? '_t' : null) 70 66 ); 71 67 72 68 return makeMatcher(_exec, name, transform, ...expressions.map(_pattern));