2016-06-22 15:02:59 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-17 15:08:47 +02:00
|
|
|
let GrammarError = require("../../grammar-error");
|
2016-09-22 09:25:31 +02:00
|
|
|
let visitor = require("../visitor");
|
2016-06-22 15:02:59 +02:00
|
|
|
|
2016-09-17 16:28:28 +02:00
|
|
|
// Checks that each rule is defined only once.
|
2016-06-22 15:02:59 +02:00
|
|
|
function reportDuplicateRules(ast) {
|
2016-09-08 16:04:36 +02:00
|
|
|
let rules = {};
|
2016-06-22 15:02:59 +02:00
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let check = visitor.build({
|
2016-10-07 17:03:10 +02:00
|
|
|
rule(node) {
|
2016-10-07 20:44:26 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(rules, node.name)) {
|
2016-06-22 15:02:59 +02:00
|
|
|
throw new GrammarError(
|
2016-06-27 12:27:41 +02:00
|
|
|
"Rule \"" + node.name + "\" is already defined "
|
|
|
|
+ "at line " + rules[node.name].start.line + ", "
|
|
|
|
+ "column " + rules[node.name].start.column + ".",
|
2016-06-22 15:02:59 +02:00
|
|
|
node.location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-27 12:27:41 +02:00
|
|
|
rules[node.name] = node.location;
|
2016-06-22 15:02:59 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reportDuplicateRules;
|