pegjs/lib/compiler/passes/report-duplicate-rules.js

36 lines
747 B
JavaScript
Raw Normal View History

"use strict";
const __hasOwnProperty = Object.prototype.hasOwnProperty;
2016-09-17 16:28:28 +02:00
// Checks that each rule is defined only once.
2018-01-30 03:38:49 +01:00
function reportDuplicateRules( ast, session ) {
const rules = {};
2018-01-30 03:38:49 +01:00
const check = session.buildVisitor( {
rule( node ) {
const name = node.name;
if ( __hasOwnProperty.call( rules, name ) ) {
const start = rules[ name ].start;
2018-02-02 05:32:39 +01:00
session.error(
`Rule "${ name }" is already defined at line ${ start.line }, column ${ start.column }.`,
node.location
);
}
rules[ node.name ] = node.location;
}
} );
check( ast );
}
module.exports = reportDuplicateRules;