Check allowedStartRules exist. (#532)

Fixes #524
master
felix 7 years ago committed by Futago-za Ryuu
parent 958e15879d
commit ee00a1ae6a

@ -5,7 +5,7 @@ let asts = require("../asts");
let visitor = require("../visitor");
// Checks that all referenced rules exist.
function reportUndefinedRules(ast) {
function reportUndefinedRules(ast, options) {
let check = visitor.build({
rule_ref(node) {
if (!asts.findRule(ast, node.name)) {
@ -18,6 +18,15 @@ function reportUndefinedRules(ast) {
});
check(ast);
if (options.allowedStartRules) {
options.allowedStartRules.forEach(rule => {
if (!asts.findRule(ast, rule)) {
throw new GrammarError(
"Start rule \"" + rule + "\" is not defined.");
}
});
}
}
module.exports = reportUndefinedRules;

@ -30,6 +30,12 @@ describe("PEG.js API", function() {
"c = 'x'"
].join("\n");
it("throws an error on missing rule", function() {
expect(() => peg.generate(grammar, {
allowedStartRules: ["missing"]
})).to.throw();
});
// The |allowedStartRules| option is implemented separately for each
// optimization mode, so we need to test it in both.

@ -57,13 +57,15 @@ module.exports = function(chai, utils) {
);
});
Assertion.addMethod("reportError", function(grammar, props) {
Assertion.addMethod("reportError", function(grammar, props, options) {
options = options !== undefined ? options : {};
let ast = parser.parse(grammar);
let passed, result;
try {
utils.flag(this, "object")(ast);
utils.flag(this, "object")(ast, options);
passed = true;
} catch (e) {
result = e;

@ -18,4 +18,12 @@ describe("compiler pass |reportUndefinedRules|", function() {
}
});
});
it("checks allowedStartRules", function() {
expect(pass).to.reportError("start = 'a'", {
message: "Start rule \"missing\" is not defined."
}, {
allowedStartRules: ["missing"]
});
});
});

Loading…
Cancel
Save