You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
12 years ago
|
var utils = require("../../utils");
|
||
|
|
||
12 years ago
|
/* Checks that no left recursion is present. */
|
||
12 years ago
|
module.exports = function(ast) {
|
||
12 years ago
|
function nop() {}
|
||
|
|
||
|
function checkExpression(node, appliedRules) {
|
||
|
check(node.expression, appliedRules);
|
||
|
}
|
||
|
|
||
|
function checkSubnodes(propertyName) {
|
||
|
return function(node, appliedRules) {
|
||
12 years ago
|
utils.each(node[propertyName], function(subnode) {
|
||
12 years ago
|
check(subnode, appliedRules);
|
||
|
});
|
||
|
};
|
||
|
}
|
||
|
|
||
12 years ago
|
var check = utils.buildNodeVisitor({
|
||
12 years ago
|
grammar: checkSubnodes("rules"),
|
||
|
|
||
|
rule:
|
||
|
function(node, appliedRules) {
|
||
|
check(node.expression, appliedRules.concat(node.name));
|
||
|
},
|
||
|
|
||
12 years ago
|
named: checkExpression,
|
||
12 years ago
|
choice: checkSubnodes("alternatives"),
|
||
12 years ago
|
action: checkExpression,
|
||
12 years ago
|
|
||
|
sequence:
|
||
|
function(node, appliedRules) {
|
||
|
if (node.elements.length > 0) {
|
||
|
check(node.elements[0], appliedRules);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
labeled: checkExpression,
|
||
|
simple_and: checkExpression,
|
||
|
simple_not: checkExpression,
|
||
|
semantic_and: nop,
|
||
|
semantic_not: nop,
|
||
|
optional: checkExpression,
|
||
|
zero_or_more: checkExpression,
|
||
|
one_or_more: checkExpression,
|
||
|
|
||
|
rule_ref:
|
||
|
function(node, appliedRules) {
|
||
12 years ago
|
if (utils.contains(appliedRules, node.name)) {
|
||
12 years ago
|
throw new PEG.GrammarError(
|
||
|
"Left recursion detected for rule \"" + node.name + "\"."
|
||
|
);
|
||
|
}
|
||
12 years ago
|
check(utils.findRuleByName(ast, node.name), appliedRules);
|
||
12 years ago
|
},
|
||
|
|
||
|
literal: nop,
|
||
12 years ago
|
"class": nop,
|
||
|
any: nop
|
||
12 years ago
|
});
|
||
|
|
||
|
check(ast, []);
|
||
|
};
|