Browse Source
Based on a pull request by Futago-za Ryuu (@futagoza): https://github.com/pegjs/pegjs/pull/329 Resolves #318.redux
5 changed files with 55 additions and 6 deletions
@ -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