2016-06-27 13:42:28 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let GrammarError = require("../../grammar-error"),
|
2016-06-27 13:42:28 +02:00
|
|
|
visitor = require("../visitor");
|
|
|
|
|
|
|
|
/* Checks that each label is defined only once within each scope. */
|
|
|
|
function reportDuplicateLabels(ast) {
|
2016-09-14 13:48:12 +02:00
|
|
|
function cloneEnv(env) {
|
|
|
|
let clone = {};
|
|
|
|
|
|
|
|
Object.keys(env).forEach(name => {
|
|
|
|
clone[name] = env[name];
|
|
|
|
});
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2016-06-27 13:42:28 +02:00
|
|
|
function checkExpressionWithClonedEnv(node, env) {
|
2016-09-14 13:48:12 +02:00
|
|
|
check(node.expression, cloneEnv(env));
|
2016-06-27 13:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let check = visitor.build({
|
2016-06-27 13:42:28 +02:00
|
|
|
rule: function(node) {
|
|
|
|
check(node.expression, { });
|
|
|
|
},
|
|
|
|
|
|
|
|
choice: function(node, env) {
|
2016-09-09 13:27:24 +02:00
|
|
|
node.alternatives.forEach(alternative => {
|
2016-09-14 13:48:12 +02:00
|
|
|
check(alternative, cloneEnv(env));
|
2016-06-27 13:42:28 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
action: checkExpressionWithClonedEnv,
|
|
|
|
|
|
|
|
labeled: function(node, env) {
|
|
|
|
if (env.hasOwnProperty(node.label)) {
|
|
|
|
throw new GrammarError(
|
|
|
|
"Label \"" + node.label + "\" is already defined "
|
|
|
|
+ "at line " + env[node.label].start.line + ", "
|
|
|
|
+ "column " + env[node.label].start.column + ".",
|
|
|
|
node.location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
check(node.expression, env);
|
|
|
|
|
|
|
|
env[node.label] = node.location;
|
|
|
|
},
|
|
|
|
|
|
|
|
text: checkExpressionWithClonedEnv,
|
|
|
|
simple_and: checkExpressionWithClonedEnv,
|
|
|
|
simple_not: checkExpressionWithClonedEnv,
|
|
|
|
optional: checkExpressionWithClonedEnv,
|
|
|
|
zero_or_more: checkExpressionWithClonedEnv,
|
|
|
|
one_or_more: checkExpressionWithClonedEnv,
|
|
|
|
group: checkExpressionWithClonedEnv
|
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reportDuplicateLabels;
|