e6d018a88d
This is related to my last commit. I've updated all the JavaScript files to satisfy 'eslint-config-futagozaryuu', my eslint configuration. I'm sure I've probally missed something, but I've run all NPM scripts and Gulp tasks, fixed any bugs that cropped up, and updated some stuff (mainly related to generated messages), so as far as I can, tell this conversion is over (I know I've probally jixed it just by saying this ;P).
60 lines
1 KiB
JavaScript
60 lines
1 KiB
JavaScript
"use strict";
|
|
|
|
const visitor = require( "../visitor" );
|
|
|
|
// Removes proxy rules -- that is, rules that only delegate to other rule.
|
|
function removeProxyRules( ast, options ) {
|
|
|
|
function isProxyRule( node ) {
|
|
|
|
return node.type === "rule" && node.expression.type === "rule_ref";
|
|
|
|
}
|
|
|
|
function replaceRuleRefs( ast, from, to ) {
|
|
|
|
const replace = visitor.build( {
|
|
rule_ref( node ) {
|
|
|
|
if ( node.name === from ) {
|
|
|
|
node.name = to;
|
|
|
|
}
|
|
|
|
}
|
|
} );
|
|
|
|
replace( ast );
|
|
|
|
}
|
|
|
|
const indices = [];
|
|
|
|
ast.rules.forEach( ( rule, i ) => {
|
|
|
|
if ( isProxyRule( rule ) ) {
|
|
|
|
replaceRuleRefs( ast, rule.name, rule.expression.name );
|
|
if ( options.allowedStartRules.indexOf( rule.name ) === -1 ) {
|
|
|
|
indices.push( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
indices.reverse();
|
|
|
|
indices.forEach( i => {
|
|
|
|
ast.rules.splice( i, 1 );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
module.exports = removeProxyRules;
|