You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pegjs/lib/compiler/passes/report-duplicate-rules.js

37 lines
793 B
JavaScript

"use strict";
const GrammarError = require( "../../grammar-error" );
const visitor = require( "../visitor" );
// Checks that each rule is defined only once.
function reportDuplicateRules( ast ) {
const rules = {};
const check = visitor.build( {
rule( node ) {
const name = node.name;
if ( Object.prototype.hasOwnProperty.call( rules, name ) ) {
const start = rules[ name ].start;
throw new GrammarError(
`Rule "${ name }" is already defined at line ${ start.line }, column ${ start.column }.`,
node.location
);
}
rules[ node.name ] = node.location;
}
} );
check( ast );
}
module.exports = reportDuplicateRules;