Mirror: The magical sticky regex-based parser generator 🧙

Update tests and codegen

Changed files
+52 -1
src
+2 -1
src/codegen.js
···
`;
const astExpression = (ast, depth, opts) => {
+
const capture = !!opts.capture && !ast.capture;
const restoreLength =
(opts.length && opts.abort && js`${_node}.length = ln${opts.length};`) ||
'';
const expression = `${ast.expression.id}(${_state})`;
return js`
if ((${_match} = ${ast.expression.id}(${_state})) != null) {
-
${opts.capture ? js`${_node}.push(${_match})` : ''}
+
${capture ? js`${_node}.push(${_match})` : ''}
} else {
${opts.onAbort}
${restoreIndex(opts.index)}
+50
src/core.test.js
···
);
});
+
describe('non-capturing shorthand', () => {
+
const node = match('node')`${/1/} :${/2/}+`;
+
it.each`
+
input | result | lastIndex
+
${'12'} | ${['1']} | ${2}
+
${'122'} | ${['1']} | ${3}
+
${'13'} | ${undefined} | ${0}
+
${'1'} | ${undefined} | ${0}
+
${'_'} | ${undefined} | ${0}
+
`(
+
'should return $result when $input is passed',
+
({ input, result, lastIndex }) => {
+
expectToParse(node, input, result, lastIndex);
+
}
+
);
+
});
+
describe('non-capturing group with plus matcher, then required matcher', () => {
const node = match('node')`(?: ${/1/}+) ${/2/}`;
it.each`
···
);
});
+
describe('positive lookahead shorthand', () => {
+
const node = match('node')`=${/1/} ${/\d/}`;
+
it.each`
+
input | result | lastIndex
+
${'1'} | ${['1']} | ${1}
+
${'13'} | ${['1']} | ${1}
+
${'2'} | ${undefined} | ${0}
+
${'_'} | ${undefined} | ${0}
+
`(
+
'should return $result when $input is passed',
+
({ input, result, lastIndex }) => {
+
expectToParse(node, input, result, lastIndex);
+
}
+
);
+
});
+
describe('positive lookahead group with plus matcher', () => {
const node = match('node')`(?= ${/1/}+) ${/\d/}`;
it.each`
···
describe('negative lookahead group', () => {
const node = match('node')`(?! ${/1/}) ${/\d/}`;
+
it.each`
+
input | result | lastIndex
+
${'2'} | ${['2']} | ${1}
+
${'23'} | ${['2']} | ${1}
+
${'1'} | ${undefined} | ${0}
+
${'1'} | ${undefined} | ${0}
+
${'_'} | ${undefined} | ${0}
+
`(
+
'should return $result when $input is passed',
+
({ input, result, lastIndex }) => {
+
expectToParse(node, input, result, lastIndex);
+
}
+
);
+
});
+
+
describe('negative lookahead shorthand', () => {
+
const node = match('node')`!${/1/} ${/\d/}`;
it.each`
input | result | lastIndex
${'2'} | ${['2']} | ${1}