Mirror: The magical sticky regex-based parser generator 🧙

Remove object-spread from codegen.js

-1
package.json
···
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0",
"@babel/core": "7.9.6",
"@babel/plugin-transform-modules-commonjs": "^7.9.6",
-
"@babel/plugin-transform-object-assign": "^7.8.3",
"@rollup/plugin-buble": "^0.21.3",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
···
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0",
"@babel/core": "7.9.6",
"@babel/plugin-transform-modules-commonjs": "^7.9.6",
"@rollup/plugin-buble": "^0.21.3",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
+2 -5
rollup.config.js
···
unicodeRegExp: false,
dangerousForOf: true,
templateString: false,
},
-
objectAssign: 'Object.assign',
exclude: 'node_modules/**',
}),
babel({
···
extensions: ['ts', 'tsx', 'js'],
exclude: 'node_modules/**',
presets: [],
-
plugins: [
-
'@babel/plugin-transform-object-assign',
-
'babel-plugin-closure-elimination',
-
],
}),
];
···
unicodeRegExp: false,
dangerousForOf: true,
templateString: false,
+
spreadRest: false,
},
exclude: 'node_modules/**',
}),
babel({
···
extensions: ['ts', 'tsx', 'js'],
exclude: 'node_modules/**',
presets: [],
+
plugins: ['babel-plugin-closure-elimination'],
}),
];
+53 -32
src/codegen.js
···
return body.trim();
}
const assignIndex = (depth) =>
depth ? js`var index_${depth} = ${_state}.index;` : '';
···
if (!opts.length && capturing) {
return js`
${js`var length_${depth} = ${_node}.length;`}
-
${astSequence(ast.sequence, depth + 1, {
-
...opts,
-
length: depth,
-
capturing,
-
})}
`;
}
-
return astSequence(ast.sequence, depth + 1, {
-
...opts,
-
capturing,
-
});
};
const astChild = (ast, depth, opts) =>
···
return js`
${label}: for (var ${count} = 0; true; ${count}++) {
${assignIndex(depth)}
-
${astChild(ast, depth, {
-
...opts,
-
onAbort: js`
if (${count}) {
${restoreIndex(depth)}
break ${label};
···
${opts.onAbort || ''}
}
`,
-
})}
}
`;
};
···
return js`
${label}: while (true) {
${assignIndex(depth)}
-
${astChild(ast, depth, {
-
...opts,
-
length: 0,
-
index: depth,
-
abort: js`break ${label};`,
-
onAbort: '',
-
})}
}
`;
};
const astOptional = (ast, depth, opts) => js`
${assignIndex(depth)}
-
${astChild(ast, depth, {
-
...opts,
-
index: depth,
-
abort: '',
-
onAbort: '',
-
})}
`;
const astQuantifier = (ast, depth, opts) => {
···
const label = `invert_${depth}`;
if (ast.lookahead === 'negative') {
-
opts = {
-
...opts,
index: depth,
abort: js`break ${label};`,
-
};
}
let child;
···
let childOpts = opts;
if (ast.alternation) {
-
childOpts = {
-
...childOpts,
index: depth,
abort: js`break ${block};`,
onAbort: '',
-
};
}
let sequence = '';
···
return body.trim();
}
+
const newOpts = (prev, next) => ({
+
index: next.index != null ? next.index : prev.index,
+
length: next.length != null ? next.length : prev.length,
+
onAbort: onAbort.length != null ? onAbort.length : onAbort.length,
+
abort: abort.length != null ? abort.length : abort.length,
+
capturing: capturing.length != null ? capturing.length : capturing.length,
+
});
+
const assignIndex = (depth) =>
depth ? js`var index_${depth} = ${_state}.index;` : '';
···
if (!opts.length && capturing) {
return js`
${js`var length_${depth} = ${_node}.length;`}
+
${astSequence(
+
ast.sequence,
+
depth + 1,
+
newOpts(opts, {
+
length: depth,
+
capturing,
+
})
+
)}
`;
}
+
return astSequence(
+
ast.sequence,
+
depth + 1,
+
newOpts(opts, {
+
capturing,
+
})
+
);
};
const astChild = (ast, depth, opts) =>
···
return js`
${label}: for (var ${count} = 0; true; ${count}++) {
${assignIndex(depth)}
+
${astChild(
+
ast,
+
depth,
+
newOpts(opts, {
+
onAbort: js`
if (${count}) {
${restoreIndex(depth)}
break ${label};
···
${opts.onAbort || ''}
}
`,
+
})
+
)}
}
`;
};
···
return js`
${label}: while (true) {
${assignIndex(depth)}
+
${astChild(
+
ast,
+
depth,
+
newOpts(opts, {
+
length: 0,
+
index: depth,
+
abort: js`break ${label};`,
+
onAbort: '',
+
})
+
)}
}
`;
};
const astOptional = (ast, depth, opts) => js`
${assignIndex(depth)}
+
${astChild(
+
ast,
+
depth,
+
newOpts(opts, {
+
index: depth,
+
abort: '',
+
onAbort: '',
+
})
+
)}
`;
const astQuantifier = (ast, depth, opts) => {
···
const label = `invert_${depth}`;
if (ast.lookahead === 'negative') {
+
opts = newOpts(opts, {
index: depth,
abort: js`break ${label};`,
+
});
}
let child;
···
let childOpts = opts;
if (ast.alternation) {
+
childOpts = newOpts(opts, {
index: depth,
abort: js`break ${block};`,
onAbort: '',
+
});
}
let sequence = '';
-7
yarn.lock
···
"@babel/helper-simple-access" "^7.8.3"
babel-plugin-dynamic-import-node "^2.3.3"
-
"@babel/plugin-transform-object-assign@^7.8.3":
-
version "7.8.3"
-
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-assign/-/plugin-transform-object-assign-7.8.3.tgz#dc3b8dd50ef03837868a37b7df791f64f288538e"
-
integrity sha512-i3LuN8tPDqUCRFu3dkzF2r1Nx0jp4scxtm7JxtIqI9he9Vk20YD+/zshdzR9JLsoBMlJlNR82a62vQExNEVx/Q==
-
dependencies:
-
"@babel/helper-plugin-utils" "^7.8.3"
-
"@babel/template@^7.3.3", "@babel/template@^7.8.3", "@babel/template@^7.8.6":
version "7.8.6"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.8.6.tgz#86b22af15f828dfb086474f964dcc3e39c43ce2b"
···
"@babel/helper-simple-access" "^7.8.3"
babel-plugin-dynamic-import-node "^2.3.3"
"@babel/template@^7.3.3", "@babel/template@^7.8.3", "@babel/template@^7.8.6":
version "7.8.6"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.8.6.tgz#86b22af15f828dfb086474f964dcc3e39c43ce2b"