In my grammar I validate boolean expressions that look something like this:
((foo == true) && (bar != false) || (qux == norf))
I obtain the string from ANTLR4's context object by calling getText():
def enterBody(self, ctx):
expression = ctx.condition.getText() # condition here being shorthand for a grammar rule (`condition=expr`)
However, the string is returned whole (i.e. no spaces between each individual token) and I have no way of knowing what each token is:
((foo==true)&&(bar!=false)||(qux==norf))
Ideally, I would like it stored in a list in the following format:
['(', '(', 'foo', '==', 'true', ')', '&&', '(', 'bar', '!=', 'false', ')', '||', '(', 'qux', '==', 'norf', ')', ')']
The ANTLR4 Python documentation is rather sparse and I'm not sure if there's a method that accomplishes this.