2014-05-08 11:48:56 +02:00
|
|
|
var arrays = require("../../utils/arrays"),
|
|
|
|
GrammarError = require("../../grammar-error"),
|
|
|
|
asts = require("../asts"),
|
|
|
|
visitor = require("../visitor");
|
2012-11-10 09:47:22 +01:00
|
|
|
|
2012-04-19 14:23:21 +02:00
|
|
|
/* Checks that no left recursion is present. */
|
2014-05-04 14:11:44 +02:00
|
|
|
function reportLeftRecursion(ast) {
|
2014-05-08 14:51:17 +02:00
|
|
|
var check = visitor.build({
|
2014-06-04 07:23:34 +02:00
|
|
|
rule: function(node, appliedRules) {
|
|
|
|
check(node.expression, appliedRules.concat(node.name));
|
|
|
|
},
|
|
|
|
|
|
|
|
sequence: function(node, appliedRules) {
|
2014-06-07 08:49:04 +02:00
|
|
|
check(node.elements[0], appliedRules);
|
2014-06-04 07:23:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2012-04-19 14:23:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
check(ast, []);
|
2014-05-04 14:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reportLeftRecursion;
|