I'm writing a language parser in Antlr4. I'm already quite well versed with it, but I don't want to fall into a pitfall (again), so here it is:
expression
| gate=expression QUESTION
(ifTrue=expression)? COLON
(ifFalse=expression)? # TernaryExpression
| Identifier # IdentifierExpression
| literal # LiteralExpression
| expression logicalComparator expression # LogicalComparisonExpression
| expression logicalOperator expression # LogicalOperationExpression
;
and the input:
user.field == 'STRING' ? user.field + user.otherField : user.somethingElse
The tree I obtain is:
(expression
(expression
(expression user) . (expression field)
)
(logicalComparator = =)
(expression
(expression (literal 'STRING'))
? (expression
(expression
(expression user) . (expression field)
)
(binaryOperator +)
(expression
(expression user) . (expression otherField)
)
)
: (expression
(expression user) . (expression somethingElse)
)
)
)
(An expression of logical comparison, where left-hand-side is user.field, comparator is == and right-hand-side is the ternary operator.)
The actual result should be a ternary operator, where gate expression is a logical comparison.
How can I fix it? I was certain, that the fact I placed TernaryExpression above LogicalComparisonExpression will suffice, but apparently it doesn't.