Reputation: 85
I have an Antlr4 question. Given the grammar excerpt below. What is the correct approach to testing for the existence of the optional actualParameters
subtree within a visitor?
I have tried the getChildCount method of the procedureCallStatement context. I've also tested for a null actualParameters parameter on the context.
I do not want to visit the actualParameters subtree if it does not exist. Doing so causes an exception.
Thank you!
Kelvin Johnson
program : statement (';' statement)* ';'?;
statement : CALLPREFIX('(' actualParameters? ')')? #procedureCallStatement;
actualParameters : expressionStatement (';' expressionStatement)* ;
expressionStatement : '(' expressionStatement ')' #parensExpression
| IDENT'[' expressionStatement ']' #subscript
...
Upvotes: 3
Views: 1011
Reputation: 99869
The automatically-generated context method ProcedureCallStatementContext.actualParameters()
will return the ActualParametersContext
if one was parsed, otherwise it will return null
.
You might make use of it in a visitor like this:
public T VisitProcedureCallStatement(ProcedureCallStatementContext ctx) {
if (ctx.actualParameters() != null) {
// do something here
}
...
}
Upvotes: 4
Reputation: 5962
Either call ctx.getActualParameters() or label it
statement : CALLPREFIX('(' args=actualParameters? ')')? #procedureCallStatement;
and then use ctx.getArgs()
Upvotes: 1