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

38 lines
736 B
JavaScript
Raw Normal View History

"use strict";
const GrammarError = require( "../../grammar-error" );
2016-09-17 16:28:28 +02:00
// Checks that all referenced rules exist.
2018-01-30 03:38:49 +01:00
function reportUndefinedRules( ast, session, options ) {
2018-01-30 03:38:49 +01:00
const check = session.buildVisitor( {
rule_ref( node ) {
2018-01-26 08:49:34 +01:00
if ( ! ast.findRule( node.name ) ) {
throw new GrammarError(
`Rule "${ node.name }" is not defined.`,
node.location
);
}
}
} );
check( ast );
options.allowedStartRules.forEach( rule => {
2018-01-26 08:49:34 +01:00
if ( ! ast.findRule( rule ) ) {
throw new GrammarError( `Start rule "${ rule }" is not defined.` );
}
} );
}
module.exports = reportUndefinedRules;