2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-14 10:01:47 +02:00
|
|
|
let visitor = require("../visitor");
|
2012-11-10 09:47:22 +01:00
|
|
|
|
2012-04-19 14:23:21 +02:00
|
|
|
/*
|
|
|
|
* Removes proxy rules -- that is, rules that only delegate to other rule.
|
|
|
|
*/
|
2014-05-04 14:11:44 +02:00
|
|
|
function removeProxyRules(ast, options) {
|
2012-04-19 14:23:21 +02:00
|
|
|
function isProxyRule(node) {
|
|
|
|
return node.type === "rule" && node.expression.type === "rule_ref";
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceRuleRefs(ast, from, to) {
|
2016-09-08 16:04:36 +02:00
|
|
|
let replace = visitor.build({
|
2014-06-04 07:23:34 +02:00
|
|
|
rule_ref: function(node) {
|
|
|
|
if (node.name === from) {
|
|
|
|
node.name = to;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:23:21 +02:00
|
|
|
});
|
|
|
|
|
2014-06-04 07:23:34 +02:00
|
|
|
replace(ast);
|
2012-04-19 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let indices = [];
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2016-09-09 13:27:24 +02:00
|
|
|
ast.rules.forEach((rule, i) => {
|
2012-04-19 14:23:21 +02:00
|
|
|
if (isProxyRule(rule)) {
|
|
|
|
replaceRuleRefs(ast, rule.name, rule.expression.name);
|
2016-09-14 10:01:47 +02:00
|
|
|
if (options.allowedStartRules.indexOf(rule.name) === -1) {
|
2013-01-06 10:16:17 +01:00
|
|
|
indices.push(i);
|
2012-04-19 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
indices.reverse();
|
|
|
|
|
2016-09-09 13:27:24 +02:00
|
|
|
indices.forEach(i => { ast.rules.splice(i, 1); });
|
2014-05-04 14:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = removeProxyRules;
|