Mirror: The magical sticky regex-based parser generator 🧙

Merge pull request #12 from kitten/fix/zero-length-matches

breaking: Only consider undefined/null failed matches

Changed files
+8 -7
src
+4 -4
src/babel/__snapshots__/plugin.test.js.snap
···
var node = [];
var x;
-
if (x = __private.exec(state, _re_expression)) {
+
if ((x = __private.exec(state, _re_expression)) != null) {
node.push(x);
} else {
state.y = y1;
···
return;
}
-
if (x = __private.exec(state, str)) {
+
if ((x = __private.exec(state, str)) != null) {
node.push(x);
} else {
state.y = y1;
···
var node = [];
var x;
-
if (x = __private.exec(state, _re_expression)) {
+
if ((x = __private.exec(state, _re_expression)) != null) {
node.push(x);
} else {
state.y = y1;
···
return;
}
-
if (x = __private.exec(state, \\"2\\")) {
+
if ((x = __private.exec(state, \\"2\\")) != null) {
node.push(x);
} else {
state.y = y1;
+1 -1
src/codegen.js
···
: `${_private}.exec(${_state}, ${ast.expression.id})`;
return js`
-
if (${_match} = ${expression}) {
+
if ((${_match} = ${expression}) != null) {
${opts.capture ? js`${_node}.push(${_match})` : ''}
} else {
${opts.onAbort}
+2 -1
src/core.js
···
if (pattern.test(input))
match = input.slice(state.y, pattern.lastIndex);
} else {
-
match = pattern.exec(input)[0] || match;
+
const x = pattern.exec(input);
+
match = x[1] == null ? x[0] : match;
}
state.y = pattern.lastIndex;
+1 -1
src/core.test.js
···
describe('interpolation parsing', () => {
const node = match('node')`
${/1/}
-
${interpolation((x) => x > 1 && x)}
+
${interpolation((x) => (x > 1 ? x : null))}
${/3/}
`;