2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-17 16:28:28 +02:00
|
|
|
// Removes proxy rules -- that is, rules that only delegate to other rule.
|
2018-01-30 03:38:49 +01:00
|
|
|
function removeProxyRules( ast, session, options ) {
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2017-10-25 20:19:42 +02:00
|
|
|
function isProxyRule( node ) {
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2017-10-25 20:19:42 +02:00
|
|
|
return node.type === "rule" && node.expression.type === "rule_ref";
|
2012-04-19 14:23:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:19:42 +02:00
|
|
|
function replaceRuleRefs( ast, from, to ) {
|
|
|
|
|
2018-01-30 03:38:49 +01:00
|
|
|
const replace = session.buildVisitor( {
|
2017-10-25 20:19:42 +02:00
|
|
|
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 );
|
|
|
|
|
|
|
|
} );
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2014-05-04 14:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = removeProxyRules;
|