Mirror: The magical sticky regex-based parser generator 🧙

Fix index restoration for negative lookaheads

Changed files
+33 -9
src
babel
+17
src/babel/__tests__/suite.js
···
);
});
+
describe('longer negative lookahead group', () => {
+
const node = match('node')`${/1/} (?! ${/2/} ${/3/}) ${/\d/} ${/\d/}`;
+
it.each`
+
input | result | lastIndex
+
${'145'} | ${['1', '4', '5']} | ${3}
+
${'124'} | ${['1', '2', '4']} | ${3}
+
${'123'} | ${undefined} | ${0}
+
${'2'} | ${undefined} | ${0}
+
${'_'} | ${undefined} | ${0}
+
`(
+
'should return $result when $input is passed',
+
({ input, result, lastIndex }) => {
+
expectToParse(node, input, result, lastIndex);
+
}
+
);
+
});
+
describe('negative lookahead group with plus matcher', () => {
const node = match('node')`(?! ${/1/}+) ${/\d/}`;
it.each`
+16 -9
src/babel/generator.js
···
}
if (ast.type === 'group' && ast.lookahead === 'negative') {
+
childOpts.abort = new AbortNode(invertId);
+
childOpts.restoreIndex = this.restoreIndex;
+
this.restoreIndex = opts.restoreIndex;
this.blockId = invertId;
this.abort = opts.abort;
-
childOpts.abort = new AbortNode(invertId);
}
if (quantifier && !quantifier.singular && quantifier.required) {
···
statements = this.childNode.statements();
}
-
if (this.restoreIndex && this.assignIndex) {
-
statements.unshift(this.assignIndex.statement());
-
statements.push(this.restoreIndex.statement());
-
}
-
-
if (this.blockId) {
+
if (this.blockId && this.assignIndex && this.restoreIndex) {
statements = [
t.labeledStatement(
this.blockId,
-
t.blockStatement([...statements, this.abort.statement()])
+
t.blockStatement(
+
[
+
this.assignIndex.statement(),
+
...statements,
+
this.restoreIndex.statement(),
+
this.abort.statement(),
+
].filter(Boolean)
+
)
),
-
];
+
].filter(Boolean);
+
} else if (this.assignIndex && this.restoreIndex) {
+
statements.unshift(this.assignIndex.statement());
+
statements.push(this.restoreIndex.statement());
}
return statements;