I am writing a simple "language" in ANTLR4/JavaScript which can associate numbers to variables and print them. This works fine but after extending the print statement to take one or many variables I don't figure out how to get the count of them. (I am using a visitor, not listener, but am interested for both.)
Grammar:
print : 'print' ID (',' ID)* ';' ;
How do I find out how many ID tokens there are?
Currently I hacked something together as follows:
visitPrint( ctx ) {
let i = 0;
let c = undefined;
while( (c = ctx.ID(i)) ) {
let val = ctx.ID(i++).getText();
print( this.variables[val] );
}
}
Shouldn't there be a better way to do this, like some count() method?
Thanks for your response!