I have the following ANTLR grammar
relation
: IDENTIFIER EQUAL relative_date
;
relative_date
: K_NOW (PLUS | MINUS) NUMERIC_LITERAL TIME_UNIT
;
IDENTIFIER
: //'"' (~'"' | '""')* '"'
'`' (~'`' | '``')* '`'
| '[' ~']'* ']'
| [a-zA-Z_] [a-zA-Z_.0-9]*
;
TIME_UNIT
: ('h'|'m'|'s'|'d'|'w'|'M'|'y'|'q')
;
PLUS : '+';
MINUS : '-';
EQUAL: '=';
K_NOW : N O W;
NUMERIC_LITERAL
: [0-9]+ ;
If I put TIME_UNIT
before IDENTIFIER
parser
something = now - 5d
worksd = now - 5d
DOES NOT work and fails at firstd
and saysIDENTIFIER
required
If I put TIME_UNIT
after IDENTIFIER
parser
something = now - 5d
fails at the secondd
and says TIME_UNIT requiredd = now - 5d
fails at the secondd
and says TIME_UNIT required
Can someone help me how can I change the grammar to work in both cases? Like when it is a relative date use TIME_UNIT
lexer otherwise IDENTIFIER
lexer