pegjs/lib/compiler/passes/report-left-recursion.js
David Majda 4ec9e6ba10 Remove useless test in the |reportLeftRecursion| pass
Empty sequences are not allowed anymore so we don't have to test for
them.
2014-06-07 10:50:55 +02:00

31 lines
854 B
JavaScript

var arrays = require("../../utils/arrays"),
GrammarError = require("../../grammar-error"),
asts = require("../asts"),
visitor = require("../visitor");
/* Checks that no left recursion is present. */
function reportLeftRecursion(ast) {
var check = visitor.build({
rule: function(node, appliedRules) {
check(node.expression, appliedRules.concat(node.name));
},
sequence: function(node, appliedRules) {
check(node.elements[0], appliedRules);
},
rule_ref: function(node, appliedRules) {
if (arrays.contains(appliedRules, node.name)) {
throw new GrammarError(
"Left recursion detected for rule \"" + node.name + "\"."
);
}
check(asts.findRule(ast, node.name), appliedRules);
}
});
check(ast, []);
}
module.exports = reportLeftRecursion;