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
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
const replaceRuleRefs = session.buildVisitor( {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
rule_ref( node, proxy, real ) {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
if ( node.name === proxy ) node.name = real;
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
}
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
} );
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
const allowedStartRules = options.allowedStartRules;
|
|
|
|
const rules = [];
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
for ( const rule of ast.rules ) {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
if ( isProxyRule( rule ) ) {
|
|
|
|
|
|
|
|
replaceRuleRefs( ast, rule.name, rule.expression.name );
|
2018-03-29 04:55:50 +02:00
|
|
|
if ( allowedStartRules.indexOf( rule.name ) < 0 ) continue;
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
rules.push( rule );
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
}
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-03-29 04:55:50 +02:00
|
|
|
ast.rules = rules;
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2014-05-04 14:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = removeProxyRules;
|