Mirror: The magical sticky regex-based parser generator 🧙

Remove interpolations exception from codegen

Changed files
+16 -23
src
+9 -12
src/codegen.js
···
const _node = 'node';
const _match = 'x';
-
let _interpolations = 0;
-
function js(/* arguments */) {
let body = arguments[0][0];
for (let i = 1; i < arguments.length; i++)
···
return next;
};
-
const assignIndex = (depth) =>
-
js`var y${depth} = ${_state}.y` +
-
(_interpolations ? js`, x${depth} = ${_state}.x;` : ';');
+
const assignIndex = (depth) => js`
+
var y${depth} = ${_state}.y,
+
x${depth} = ${_state}.x;
+
`;
-
const restoreIndex = (depth) =>
-
js`${_state}.y = y${depth}` +
-
(_interpolations ? js`, ${_state}.x = x${depth};` : ';');
+
const restoreIndex = (depth) => js`
+
${_state}.y = y${depth};
+
${_state}.x = x${depth};
+
`;
const astExpression = (ast, depth, opts) => {
const restoreLength =
···
`;
};
-
const astRoot = (ast, name, transform, interpolations) => {
-
_interpolations = interpolations;
-
+
const astRoot = (ast, name, transform) => {
return js`
(function (${_state}) {
${assignIndex(1)}
···
var ${_match};
${astSequence(ast, 2, {
-
interpolations,
index: 1,
length: 0,
onAbort: '',
+7 -11
src/core.js
···
} else {
match = pattern.exec(input)[0] || match;
}
+
+
state.y = pattern.lastIndex;
}
-
state.y = pattern.lastIndex;
return match;
};
···
};
export const match = (name, transform) => (quasis, ...expressions) => {
-
let interpolations = 0;
-
const ast = parseDSL(
quasis,
-
expressions.map((expression, i) => {
-
if (expression === interpolation) interpolations++;
-
return {
-
fn: typeof expression === 'function' && expression.length,
-
id: `_${i}`,
-
};
-
})
+
expressions.map((expression, i) => ({
+
fn: typeof expression === 'function' && expression.length,
+
id: `_${i}`,
+
}))
);
const makeMatcher = new Function(
execId + ',_n,_t,' + expressions.map((_expression, i) => `_${i}`).join(','),
-
'return ' + astRoot(ast, '_n', transform ? '_t' : null, interpolations)
+
'return ' + astRoot(ast, '_n', transform ? '_t' : null)
);
return makeMatcher(_exec, name, transform, ...expressions.map(_pattern));