pegjs/lib/compiler/passes/report-duplicate-labels.js
David Majda 6fa8ad63f9 Replace some functions with arrow functions
Because arrow functions work rather differently than normal functions (a
bad design mistake if you ask me), I decided to be conservative with the
conversion.

I converted:

  * event handlers
  * callbacks
  * arguments to Array.prototype.map & co.
  * small standalone lambda functions

I didn't convert:

  * functions assigned to object literal properties (the new shorthand
    syntax would be better here)
  * functions passed to "describe", "it", etc. in specs (because Jasmine
    relies on dynamic "this")

See #442.
2016-09-12 16:07:43 +02:00

54 lines
1.5 KiB
JavaScript

"use strict";
let GrammarError = require("../../grammar-error"),
objects = require("../../utils/objects"),
visitor = require("../visitor");
/* Checks that each label is defined only once within each scope. */
function reportDuplicateLabels(ast) {
function checkExpressionWithClonedEnv(node, env) {
check(node.expression, objects.clone(env));
}
let check = visitor.build({
rule: function(node) {
check(node.expression, { });
},
choice: function(node, env) {
node.alternatives.forEach(alternative => {
check(alternative, objects.clone(env));
});
},
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;