2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2014-05-08 11:48:56 +02:00
|
|
|
var arrays = require("../../utils/arrays"),
|
|
|
|
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) {
|
2014-05-08 14:51:17 +02:00
|
|
|
var 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
|
|
|
}
|
|
|
|
|
2013-01-06 10:32:52 +01:00
|
|
|
var indices = [];
|
2012-04-19 14:23:21 +02:00
|
|
|
|
2014-05-08 11:48:56 +02:00
|
|
|
arrays.each(ast.rules, function(rule, i) {
|
2012-04-19 14:23:21 +02:00
|
|
|
if (isProxyRule(rule)) {
|
|
|
|
replaceRuleRefs(ast, rule.name, rule.expression.name);
|
2014-05-08 11:48:56 +02:00
|
|
|
if (!arrays.contains(options.allowedStartRules, rule.name)) {
|
2013-01-06 10:16:17 +01:00
|
|
|
indices.push(i);
|
2012-04-19 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
indices.reverse();
|
|
|
|
|
2014-05-09 13:39:56 +02:00
|
|
|
arrays.each(indices, function(i) { ast.rules.splice(i, 1); });
|
2014-05-04 14:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = removeProxyRules;
|