I have a grammar in ANTLR4 around which I am writing an application. A snippet of the pertinent grammar is shown below:
grammar SomeGrammar;
// ... a bunch of other parse rules
operand
: id | literal ;
id
: ID ;
literal
: LITERAL ;
// A bunch of other lexer rules
LITERAL : NUMBER | BOOLEAN | STRING;
NUMBER : INTEGER | FLOAT ;
INTEGER : [0-9]+ ;
FLOAT : INTEGER '.' INTEGER | '.' INTEGER ;
BOOLEAN : 'TRUE' | 'FALSE' ;
ID : [A-Za-z]+[A-Za-z0-9_]* ;
STRING : '"' .*? '"' ;
I generate the antlr4
JavaScript Lexer and Parser like so:
$ antlr4 -o . -Dlanguage=JavaScript -listener -visitor
and then I overload the exitLiteral ()
prototype to check if an operand is a literal. The issue is that if I pass
a
it (force) parses it to a literal, and throws an error (e.g. below shown with grun
):
$ grun YARL literal -gui -tree
a
line 1:0 mismatched input 'a' expecting LITERAL
(literal a)
The same error when I use the JavaScript Parser which I overloaded like so:
SomeGrammarLiteralPrinter.prototype.exitLiteral = function (ctx) {
debug ("Literal is " + ctx.getText ()); // Literal is a
};
I would like to catch the error so that I can decide that it is an ID
, and not a LITERAL
. How do I do that?
Any help is appreciated.