e6d018a88d
This is related to my last commit. I've updated all the JavaScript files to satisfy 'eslint-config-futagozaryuu', my eslint configuration. I'm sure I've probally missed something, but I've run all NPM scripts and Gulp tasks, fixed any bugs that cropped up, and updated some stuff (mainly related to generated messages), so as far as I can, tell this conversion is over (I know I've probally jixed it just by saying this ;P).
84 lines
1.8 KiB
JavaScript
84 lines
1.8 KiB
JavaScript
"use strict";
|
|
|
|
const GrammarError = require( "../../grammar-error" );
|
|
const visitor = require( "../visitor" );
|
|
|
|
// Checks that each label is defined only once within each scope.
|
|
function reportDuplicateLabels( ast ) {
|
|
|
|
let check;
|
|
|
|
function cloneEnv( env ) {
|
|
|
|
const clone = {};
|
|
|
|
Object.keys( env ).forEach( name => {
|
|
|
|
clone[ name ] = env[ name ];
|
|
|
|
} );
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
function checkExpressionWithClonedEnv( node, env ) {
|
|
|
|
check( node.expression, cloneEnv( env ) );
|
|
|
|
}
|
|
|
|
check = visitor.build( {
|
|
rule( node ) {
|
|
|
|
check( node.expression, {} );
|
|
|
|
},
|
|
|
|
choice( node, env ) {
|
|
|
|
node.alternatives.forEach( alternative => {
|
|
|
|
check( alternative, cloneEnv( env ) );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
action: checkExpressionWithClonedEnv,
|
|
|
|
labeled( node, env ) {
|
|
|
|
const label = node.label;
|
|
|
|
if ( Object.prototype.hasOwnProperty.call( env, label ) ) {
|
|
|
|
const start = env[ label ].start;
|
|
|
|
throw new GrammarError(
|
|
`Label "${ label }" is already defined at line ${ start.line }, column ${ start.column }.`,
|
|
node.location
|
|
);
|
|
|
|
}
|
|
|
|
check( node.expression, env );
|
|
env[ label ] = node.location;
|
|
|
|
},
|
|
|
|
text: checkExpressionWithClonedEnv,
|
|
simple_and: checkExpressionWithClonedEnv,
|
|
simple_not: checkExpressionWithClonedEnv,
|
|
optional: checkExpressionWithClonedEnv,
|
|
zero_or_more: checkExpressionWithClonedEnv,
|
|
one_or_more: checkExpressionWithClonedEnv,
|
|
group: checkExpressionWithClonedEnv
|
|
} );
|
|
|
|
check( ast );
|
|
|
|
}
|
|
|
|
module.exports = reportDuplicateLabels;
|