Report duplicate rule definitions as errors
Based on a pull request by Futago-za Ryuu (@futagoza): https://github.com/pegjs/pegjs/pull/329 Resolves #318.redux
parent
149c829897
commit
eb5875bc6a
@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var GrammarError = require("../../grammar-error"),
|
||||
visitor = require("../visitor");
|
||||
|
||||
/* Checks that each rule is defined only once. */
|
||||
function reportDuplicateRules(ast) {
|
||||
var rules = {};
|
||||
|
||||
var check = visitor.build({
|
||||
rule: function(node) {
|
||||
if (rules.hasOwnProperty(node.name)) {
|
||||
throw new GrammarError(
|
||||
"Rule \"" + node.name + "\" is already defined.",
|
||||
node.location
|
||||
);
|
||||
}
|
||||
|
||||
rules[node.name] = true;
|
||||
}
|
||||
});
|
||||
|
||||
check(ast);
|
||||
}
|
||||
|
||||
module.exports = reportDuplicateRules;
|
@ -0,0 +1,20 @@
|
||||
/* global peg */
|
||||
|
||||
"use strict";
|
||||
|
||||
describe("compiler pass |reportDuplicateRules|", function() {
|
||||
var pass = peg.compiler.passes.check.reportDuplicateRules;
|
||||
|
||||
it("reports duplicate rules", function() {
|
||||
expect(pass).toReportError([
|
||||
'start = "a"',
|
||||
'start = "b"'
|
||||
].join('\n'), {
|
||||
message: 'Rule "start" is already defined.',
|
||||
location: {
|
||||
start: { offset: 12, line: 2, column: 1 },
|
||||
end: { offset: 23, line: 2, column: 12 }
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue