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

38 lines
843 B
JavaScript

"use strict";
const GrammarError = require( "../../grammar-error" );
const visitor = require( "../../ast" ).visitor;
const __hasOwnProperty = Object.prototype.hasOwnProperty;
// Checks that each rule is defined only once.
function reportDuplicateRules( ast ) {
const rules = {};
const check = visitor.build( {
rule( node ) {
const name = node.name;
if ( __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;