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.
pegjs/lib/compiler/passes/report-missing-rules.js

51 lines
1.4 KiB
JavaScript

var arrays = require("../../utils/arrays"),
GrammarError = require("../../grammar-error"),
asts = require("../asts"),
visitor = require("../visitor");
/* Checks that all referenced rules exist. */
function reportMissingRules(ast) {
function nop() {}
function checkExpression(node) { check(node.expression); }
function checkSubnodes(propertyName) {
return function(node) { arrays.each(node[propertyName], check); };
}
var check = visitor.buildNodeVisitor({
grammar: checkSubnodes("rules"),
rule: checkExpression,
named: checkExpression,
choice: checkSubnodes("alternatives"),
action: checkExpression,
sequence: checkSubnodes("elements"),
labeled: checkExpression,
text: 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) {
if (!asts.findRuleByName(ast, node.name)) {
throw new GrammarError(
"Referenced rule \"" + node.name + "\" does not exist."
);
}
},
literal: nop,
"class": nop,
any: nop
});
check(ast);
}
module.exports = reportMissingRules;