I'm currrently building a date parser using antlr. The inputs it takes are
year monthName numDayOfMonth era
numDayOfMonth monthName year era
These are all under the rule stringDate
, so my grammar looks like this
stringDate: year monthName numDayOfMonth
| numDayOfMonth monthName year;
numYear: NUMBER ;
strMonth: MONTH ;
numDayOfMonth: NUMBER ;
NUMBER: [0-9]+ ;
MONTH: 'jan' | 'feb' | 'mar' | 'apr' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec' ;
In my listeners, I check to make sure that numDayOfMonth
is within the range [1, 31]
to make sure that that the number is a valid date. I do the same for the months (first I transform them into their corresponding month).
The problem is, if it input the date 2013 June 13
, The date gets parsed correctly. However, when I input 13 June 2013
, it gets parsed incorrectly because the parser gets confused and thinks 2013 is a day, not a year, and therefore the check fails during exitNumDayOfMonth
. I've been scratching my head about how to handle this. I essentially want the evaluator to skip the rule of i encounter a num > 31
, but I'm not entirely sure of how to skip a rule. I have tried return
ing, and throwing errors, but nothing seems to work.
Is there a way to make the evaluator skip this rule and go on to the alternative instead?