Mirror: The magical sticky regex-based parser generator 🧙

Remove special string optimisation

This made little difference compard to the RegExp#test
optimisation.

Changed files
+3 -17
src
+3 -1
src/babel/__snapshots__/plugin.test.js.snap
···
exports[`works with local recursion 1`] = `
"import { match as m, tag, _exec, _pattern } from 'reghex';
const inner = function (state) {
var index_1 = state.index;
var node = [];
var match;
-
if (match = _exec(state, \\"inner\\")) {
node.push(match);
} else {
state.index = index_1;
···
exports[`works with local recursion 1`] = `
"import { match as m, tag, _exec, _pattern } from 'reghex';
+
var _inner_expression = _pattern(/inner/);
+
const inner = function (state) {
var index_1 = state.index;
var node = [];
var match;
+
if (match = _exec(state, _inner_expression)) {
node.push(match);
} else {
state.index = index_1;
-9
src/babel/transform.js
···
const matchPath = binding.path.get('init');
if (this.isMatch(matchPath)) return expression;
}
-
} else if (
-
t.isRegExpLiteral(expression) &&
-
!regexPatternsRe.test(expression.pattern)
-
) {
-
// NOTE: This is an optimisation path, where the pattern regex is inlined
-
// and has determined to be "simple" enough to be turned into a string
-
return t.stringLiteral(
-
expression.pattern.replace(/\\./g, (x) => x[1])
-
);
}
const id = path.scope.generateUidIdentifier(
···
const matchPath = binding.path.get('init');
if (this.isMatch(matchPath)) return expression;
}
}
const id = path.scope.generateUidIdentifier(
-7
src/core.js
···
if (typeof pattern === 'function') {
while (typeof pattern === 'function') pattern = pattern(state);
return pattern;
-
} else if (typeof pattern === 'string') {
-
const end = state.index + pattern.length;
-
const sub = state.input.slice(state.index, end);
-
if (sub === pattern) {
-
state.index = end;
-
match = sub;
-
}
} else if (isStickySupported) {
pattern.lastIndex = state.index;
if (pattern.test(state.input)) {
···
if (typeof pattern === 'function') {
while (typeof pattern === 'function') pattern = pattern(state);
return pattern;
} else if (isStickySupported) {
pattern.lastIndex = state.index;
if (pattern.test(state.input)) {