Mirror: The magical sticky regex-based parser generator 🧙

Clean up assign index / tag logic in Generator

Changed files
+31 -47
src
babel
+22 -24
src/babel/__snapshots__/plugin.test.js.snap
···
_node_expression2 = (0, _reghex._pattern)(2);
const node = function _node(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = (0, _reghex.tag)([], 'node');
+
node = [];
if (match = (0, _reghex._exec)(state, _node_expression)) {
node.push(match);
···
return;
}
-
return node;
+
return (0, _reghex.tag)(node, 'node');
};"
`;
···
var _inner_expression = _pattern(/inner/);
const inner = function _inner(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = tag([], 'inner');
+
node = [];
if (match = _exec(state, _inner_expression)) {
node.push(match);
···
return;
}
-
return node;
+
return tag(node, 'inner');
};
const node = function _node(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = tag([], 'node');
+
node = [];
if (match = inner(state)) {
node.push(match);
···
return;
}
-
return node;
+
return tag(node, 'node');
};"
`;
···
_node_expression3 = _pattern(3);
const node = function _node(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = _tag([], 'node');
+
node = [];
if (match = _exec(state, _node_expression)) {
node.push(match);
···
}
}
-
return node;
+
return _tag(node, 'node');
};"
`;
···
_node_expression5 = _pattern(5);
const node = function _node(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = _tag([], 'node');
+
node = [];
block_0: {
var index_0 = state.index;
···
}
}
-
return node;
+
return _tag(node, 'node');
}
loop_0: for (var iter_0 = 0; true; iter_0++) {
···
}
}
-
return node;
+
return _tag(node, 'node');
};"
`;
···
var _inner_transform = x => x;
const first = function _inner(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = _tag([], 'inner');
-
-
return _inner_transform(node);
+
node = [];
+
return _inner_transform(_tag(node, 'inner'));
};
const transform = x => x;
const second = function _node(state) {
+
var last_index = state.index;
var match,
-
last_index = state.index,
-
node = _tag([], 'node');
-
-
return transform(node);
+
node = [];
+
return transform(_tag(node, 'node'));
};"
`;
+9 -23
src/babel/generator.js
···
const loopId = t.identifier(`loop_${this.depth}`);
const iterId = t.identifier(`iter_${this.depth}`);
const indexId = t.identifier(`index_${this.depth}`);
-
const lastIndex = t.memberExpression(ids.state, t.identifier('index'));
+
const assignIndex = new AssignIndexNode(indexId);
let statements;
if (quantifier && !quantifier.singular && quantifier.required) {
···
t.booleanLiteral(true),
t.updateExpression('++', iterId),
t.blockStatement([
-
t.variableDeclaration('var', [
-
t.variableDeclarator(indexId, lastIndex),
-
]),
+
assignIndex.statement(),
...this.childNode.statements(),
])
)
···
t.whileStatement(
t.booleanLiteral(true),
t.blockStatement([
-
t.variableDeclaration('var', [
-
t.variableDeclarator(indexId, lastIndex),
-
]),
+
assignIndex.statement(),
...this.childNode.statements(),
])
)
),
];
} else if (quantifier && !quantifier.required) {
-
statements = [
-
t.variableDeclaration('var', [
-
t.variableDeclarator(indexId, lastIndex),
-
]),
-
...this.childNode.statements(),
-
];
+
statements = [assignIndex.statement(), ...this.childNode.statements()];
} else {
statements = this.childNode.statements();
}
···
export class RootNode {
constructor(ast, nameNode, transformNode) {
const indexId = t.identifier('last_index');
+
const node = t.callExpression(ids.tag, [ids.node, nameNode]);
this.returnStatement = t.returnStatement(
-
transformNode ? t.callExpression(transformNode, [ids.node]) : ids.node
+
transformNode ? t.callExpression(transformNode, [node]) : node
);
-
this.nameNode = nameNode;
+
this.assignIndex = new AssignIndexNode(indexId);
this.node = new AlternationNode(ast, 0, {
returnStatement: this.returnStatement,
restoreIndex: new RestoreIndexNode(indexId, true),
···
}
statements() {
-
const indexId = t.identifier('last_index');
-
const lastIndex = t.memberExpression(ids.state, t.identifier('index'));
-
return [
+
this.assignIndex.statement(),
t.variableDeclaration('var', [
t.variableDeclarator(ids.match),
-
t.variableDeclarator(indexId, lastIndex),
-
t.variableDeclarator(
-
ids.node,
-
t.callExpression(ids.tag, [t.arrayExpression(), this.nameNode])
-
),
+
t.variableDeclarator(ids.node, t.arrayExpression()),
]),
...this.node.statements(),
this.returnStatement,