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"); let visitor = require("../visitor");
// Checks that all referenced rules exist. // Checks that all referenced rules exist.
function reportUndefinedRules(ast) { function reportUndefinedRules(ast, options) {
let check = visitor.build({ let check = visitor.build({
rule_ref(node) { rule_ref(node) {
if (!asts.findRule(ast, node.name)) { if (!asts.findRule(ast, node.name)) {
@ -18,6 +18,15 @@ function reportUndefinedRules(ast) {
}); });
check(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; module.exports = reportUndefinedRules;

@ -30,6 +30,12 @@ describe("PEG.js API", function() {
"c = 'x'" "c = 'x'"
].join("\n"); ].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 // The |allowedStartRules| option is implemented separately for each
// optimization mode, so we need to test it in both. // 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 ast = parser.parse(grammar);
let passed, result; let passed, result;
try { try {
utils.flag(this, "object")(ast); utils.flag(this, "object")(ast, options);
passed = true; passed = true;
} catch (e) { } catch (e) {
result = 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