| 1 |
"use strict"; |
| 2 |
|
| 3 |
var GrammarError = require("../../grammar-error"), |
| 4 |
arrays = require("../../utils/arrays"), |
| 5 |
objects = require("../../utils/objects"), |
| 6 |
visitor = require("../visitor"); |
| 7 |
|
| 8 |
|
| 9 |
function reportDuplicateLabels(ast) { |
| 10 |
function checkExpressionWithClonedEnv(node, env) { |
| 11 |
check(node.expression, objects.clone(env)); |
| 12 |
} |
| 13 |
|
| 14 |
var check = visitor.build({ |
| 15 |
rule: function(node) { |
| 16 |
check(node.expression, { }); |
| 17 |
}, |
| 18 |
|
| 19 |
choice: function(node, env) { |
| 20 |
arrays.each(node.alternatives, function(alternative) { |
| 21 |
check(alternative, objects.clone(env)); |
| 22 |
}); |
| 23 |
}, |
| 24 |
|
| 25 |
action: checkExpressionWithClonedEnv, |
| 26 |
|
| 27 |
labeled: function(node, env) { |
| 28 |
if (env.hasOwnProperty(node.label)) { |
| 29 |
throw new GrammarError( |
| 30 |
"Label \"" + node.label + "\" is already defined " |
| 31 |
+ "at line " + env[node.label].start.line + ", " |
| 32 |
+ "column " + env[node.label].start.column + ".", |
| 33 |
node.location |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
check(node.expression, env); |
| 38 |
|
| 39 |
env[node.label] = node.location; |
| 40 |
}, |
| 41 |
|
| 42 |
text: checkExpressionWithClonedEnv, |
| 43 |
simple_and: checkExpressionWithClonedEnv, |
| 44 |
simple_not: checkExpressionWithClonedEnv, |
| 45 |
optional: checkExpressionWithClonedEnv, |
| 46 |
zero_or_more: checkExpressionWithClonedEnv, |
| 47 |
one_or_more: checkExpressionWithClonedEnv, |
| 48 |
group: checkExpressionWithClonedEnv |
| 49 |
}); |
| 50 |
|
| 51 |
check(ast); |
| 52 |
} |
| 53 |
|
| 54 |
module.exports = reportDuplicateLabels; |