2012-11-10 09:47:22 +01:00
|
|
|
var utils = require("../../utils");
|
|
|
|
|
2012-04-19 14:23:21 +02:00
|
|
|
/* Checks that all referenced rules exist. */
|
2012-11-10 09:47:22 +01:00
|
|
|
module.exports = function(ast) {
|
2012-04-19 14:23:21 +02:00
|
|
|
function nop() {}
|
|
|
|
|
|
|
|
function checkExpression(node) { check(node.expression); }
|
|
|
|
|
|
|
|
function checkSubnodes(propertyName) {
|
2012-11-10 09:47:22 +01:00
|
|
|
return function(node) { utils.each(node[propertyName], check); };
|
2012-04-19 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 09:47:22 +01:00
|
|
|
var check = utils.buildNodeVisitor({
|
2012-04-19 14:23:21 +02:00
|
|
|
grammar: checkSubnodes("rules"),
|
|
|
|
rule: checkExpression,
|
2012-06-24 16:55:30 +02:00
|
|
|
named: checkExpression,
|
2012-04-19 14:23:21 +02:00
|
|
|
choice: checkSubnodes("alternatives"),
|
2012-06-26 20:28:06 +02:00
|
|
|
action: checkExpression,
|
2012-04-19 14:23:21 +02:00
|
|
|
sequence: checkSubnodes("elements"),
|
|
|
|
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) {
|
2012-11-10 09:47:22 +01:00
|
|
|
if (!utils.findRuleByName(ast, node.name)) {
|
2012-04-19 14:23:21 +02:00
|
|
|
throw new PEG.GrammarError(
|
|
|
|
"Referenced rule \"" + node.name + "\" does not exist."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
literal: nop,
|
2012-06-25 21:46:47 +02:00
|
|
|
"class": nop,
|
|
|
|
any: nop
|
2012-04-19 14:23:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
};
|