| 1 |
"use strict"; |
| 2 |
|
| 3 |
var arrays = require("../../utils/arrays"), |
| 4 |
visitor = require("../visitor"); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
function removeProxyRules(ast, options) { |
| 10 |
function isProxyRule(node) { |
| 11 |
return node.type === "rule" && node.expression.type === "rule_ref"; |
| 12 |
} |
| 13 |
|
| 14 |
function replaceRuleRefs(ast, from, to) { |
| 15 |
var replace = visitor.build({ |
| 16 |
rule_ref: function(node) { |
| 17 |
if (node.name === from) { |
| 18 |
node.name = to; |
| 19 |
} |
| 20 |
} |
| 21 |
}); |
| 22 |
|
| 23 |
replace(ast); |
| 24 |
} |
| 25 |
|
| 26 |
var indices = []; |
| 27 |
|
| 28 |
arrays.each(ast.rules, function(rule, i) { |
| 29 |
if (isProxyRule(rule)) { |
| 30 |
replaceRuleRefs(ast, rule.name, rule.expression.name); |
| 31 |
if (!arrays.contains(options.allowedStartRules, rule.name)) { |
| 32 |
indices.push(i); |
| 33 |
} |
| 34 |
} |
| 35 |
}); |
| 36 |
|
| 37 |
indices.reverse(); |
| 38 |
|
| 39 |
arrays.each(indices, function(i) { ast.rules.splice(i, 1); }); |
| 40 |
} |
| 41 |
|
| 42 |
module.exports = removeProxyRules; |