Mirror: The magical sticky regex-based parser generator 🧙

Simplify codegen variable names

Changed files
+96 -99
src
babel
__snapshots__
+81 -81
src/babel/__snapshots__/plugin.test.js.snap
···
_node_expression2 = (0, _reghex._pattern)(2);
const node = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = (0, _reghex._exec)(state, _node_expression)) {
-
node.push(match);
+
if (x = (0, _reghex._exec)(state, _node_expression)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
-
if (match = (0, _reghex._exec)(state, _node_expression2)) {
-
node.push(match);
+
if (x = (0, _reghex._exec)(state, _node_expression2)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
···
var _inner_expression = _pattern(/inner/);
const inner = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = _exec(state, _inner_expression)) {
-
node.push(match);
+
if (x = _exec(state, _inner_expression)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
···
};
const node = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = inner(state)) {
-
node.push(match);
+
if (x = inner(state)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
···
_node_expression3 = _pattern(3);
const node = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = _exec(state, _node_expression)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
-
var length_2 = node.length;
+
var ln2 = node.length;
alternation_3: {
block_3: {
-
var index_3 = state.index;
+
var idx3 = state.index;
-
if (match = _exec(state, _node_expression2)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression2)) {
+
node.push(x);
} else {
-
state.index = index_3;
-
node.length = length_2;
+
state.index = idx3;
+
node.length = ln2;
break block_3;
}
break alternation_3;
}
-
loop_3: for (var count_3 = 0; true; count_3++) {
-
var index_3 = state.index;
+
loop_3: for (var j3 = 0; 1; j3++) {
+
var idx3 = state.index;
if (!_exec(state, _node_expression3)) {
-
if (count_3) {
-
state.index = index_3;
+
if (j3) {
+
state.index = idx3;
break loop_3;
} else {}
-
state.index = index_1;
-
node.length = length_2;
+
state.index = idx1;
+
node.length = ln2;
return;
}
}
···
"import { match, tag, _exec, _pattern } from 'reghex';
const inner = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = node(state)) {
-
node.push(match);
+
if (x = node(state)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
···
};
const node = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
-
if (match = inner(state)) {
-
node.push(match);
+
if (x = inner(state)) {
+
node.push(x);
} else {
-
state.index = index_1;
+
state.index = idx1;
return;
}
···
_node_expression5 = _pattern(5);
const node = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
alternation_2: {
block_2: {
-
var index_2 = state.index;
+
var idx2 = state.index;
-
loop_2: for (var count_2 = 0; true; count_2++) {
-
var index_2 = state.index;
+
loop_2: for (var j2 = 0; 1; j2++) {
+
var idx2 = state.index;
-
if (match = _exec(state, _node_expression)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression)) {
+
node.push(x);
} else {
-
if (count_2) {
-
state.index = index_2;
+
if (j2) {
+
state.index = idx2;
break loop_2;
} else {}
-
state.index = index_2;
+
state.index = idx2;
break block_2;
}
}
···
break alternation_2;
}
-
loop_2: for (var count_2 = 0; true; count_2++) {
-
var index_2 = state.index;
+
loop_2: for (var j2 = 0; 1; j2++) {
+
var idx2 = state.index;
-
if (match = _exec(state, _node_expression2)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression2)) {
+
node.push(x);
} else {
-
if (count_2) {
-
state.index = index_2;
+
if (j2) {
+
state.index = idx2;
break loop_2;
} else {}
-
state.index = index_1;
+
state.index = idx1;
return;
}
}
-
loop_2: while (true) {
-
var index_2 = state.index;
-
var length_2 = node.length;
+
loop_2: for (;;) {
+
var idx2 = state.index;
+
var ln2 = node.length;
-
if (match = _exec(state, _node_expression3)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression3)) {
+
node.push(x);
} else {
-
state.index = index_2;
-
node.length = length_2;
+
state.index = idx2;
+
node.length = ln2;
break loop_2;
}
-
var index_4 = state.index;
+
var idx4 = state.index;
-
if (match = _exec(state, _node_expression4)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression4)) {
+
node.push(x);
} else {
-
state.index = index_4;
+
state.index = idx4;
}
-
if (match = _exec(state, _node_expression5)) {
-
node.push(match);
+
if (x = _exec(state, _node_expression5)) {
+
node.push(x);
} else {
-
state.index = index_2;
-
node.length = length_2;
+
state.index = idx2;
+
node.length = ln2;
break loop_2;
}
}
···
var _inner_transform = x => x;
const first = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
node.tag = 'inner';
return _inner_transform(node);
};
···
const transform = x => x;
const second = function (state) {
-
var index_1 = state.index;
+
var idx1 = state.index;
var node = [];
-
var match;
+
var x;
node.tag = 'node';
return transform(node);
};"
+15 -18
src/codegen.js
···
const _state = 'state';
-
const _match = 'match';
const _node = 'node';
+
const _match = 'x';
function js(/* arguments */) {
let body = arguments[0][0];
···
};
const assignIndex = (depth) =>
-
depth ? js`var index_${depth} = ${_state}.index;` : '';
+
depth ? js`var idx${depth} = ${_state}.index;` : '';
const restoreIndex = (depth) =>
-
depth ? js`${_state}.index = index_${depth};` : '';
+
depth ? js`${_state}.index = idx${depth};` : '';
const abortOnCondition = (condition, hooks) => js`
if (${condition}) {
${restoreIndex(opts.index)}
-
${opts.abort || ''}
+
${opts.abort}
} else {
-
${opts.onAbort || ''}
+
${opts.onAbort}
}
`;
const astExpression = (ast, depth, opts) => {
const restoreLength =
-
opts.length &&
-
opts.abort &&
-
js`
-
${_node}.length = length_${opts.length};
-
`;
+
(opts.length && opts.abort && js`${_node}.length = ln${opts.length};`) ||
+
'';
const abort = js`
-
${opts.onAbort || ''}
+
${opts.onAbort}
${restoreIndex(opts.index)}
-
${restoreLength || ''}
-
${opts.abort || ''}
+
${restoreLength}
+
${opts.abort}
`;
if (!opts.capture) {
···
if (!opts.length && capture) {
opts.length = depth;
return js`
-
${js`var length_${depth} = ${_node}.length;`}
+
${js`var ln${depth} = ${_node}.length;`}
${astSequence(ast.sequence, depth + 1, opts)}
`;
}
···
const astQuantifier = (ast, depth, opts) => {
const { index, abort, onAbort } = opts;
-
const invert = `invert_${depth}`;
+
const invert = `inv_${depth}`;
const loop = `loop_${depth}`;
-
const count = `count_${depth}`;
+
const count = `j${depth}`;
opts = copy(opts);
if (ast.capture === '!') {
···
`;
child = js`
-
${loop}: for (var ${count} = 0; true; ${count}++) {
+
${loop}: for (var ${count} = 0; 1; ${count}++) {
${assignIndex(depth)}
${astChild(ast, depth, opts)}
}
···
opts.onAbort = '';
child = js`
-
${loop}: while (true) {
+
${loop}: for (;;) {
${assignIndex(depth)}
${astChild(ast, depth, opts)}
}