1 // antlr4 -Dlanguage=Python3 ./dateparse_utils.g4
2 // Hi, self. In ANTLR grammars, there are two separate types of symbols: those
3 // for the lexer and those for the parser. The former begin with a CAPITAL
4 // whereas the latter begin with lowercase. The order of the lexer symbols
5 // is the order that the lexer will recognize them in. There's a good tutorial
8 // https://tomassetti.me/antlr-mega-tutorial/
10 // There are also a zillion premade grammars at:
12 // https://github.com/antlr/grammars-v4
14 grammar dateparse_utils;
19 | SPACE* dateExpr SPACE* dtdiv? SPACE* timeExpr
20 | SPACE* timeExpr SPACE* tddiv? SPACE+ dateExpr
25 | baseAndOffsetDateExpr
30 | baseAndOffsetTimeExpr
34 : twentyFourHourTimeExpr
39 twentyFourHourTimeExpr
40 : hour ((SPACE|tdiv)+ minute ((SPACE|tdiv)+ second ((SPACE|tdiv)+ micros)? )? )? SPACE* tzExpr?
44 : hour ((SPACE|tdiv)+ minute ((SPACE|tdiv)+ second ((SPACE|tdiv)+ micros)? )? )? SPACE* ampm SPACE* tzExpr?
47 ampm: ('a'|'am'|'p'|'pm'|'AM'|'PM'|'A'|'P');
50 : monthDayMaybeYearExpr
51 | dayMonthMaybeYearExpr
53 | specialDateMaybeYearExpr
54 | nthWeekdayInMonthMaybeYearExpr
55 | firstLastWeekdayInMonthMaybeYearExpr
56 | deltaDateExprRelativeToTodayImplied
61 : monthExpr (SPACE|ddiv)+ dayOfMonth ((SPACE|ddiv)+ year)?
65 : dayOfMonth (SPACE|ddiv)+ monthName ((SPACE|ddiv)+ year)?
69 : year (SPACE|ddiv)+ monthExpr (SPACE|ddiv)+ dayOfMonth
72 nthWeekdayInMonthMaybeYearExpr
73 : nth SPACE+ dayName SPACE+ ('in'|'of') SPACE+ monthName ((ddiv|SPACE)+ year)?
76 firstLastWeekdayInMonthMaybeYearExpr
77 : firstOrLast SPACE+ dayName (SPACE+ ('in'|'of'))? SPACE+ monthName ((ddiv|SPACE)+ year)?
80 specialDateMaybeYearExpr
81 : (thisNextLast SPACE+)? specialDate ((SPACE|ddiv)+ year)?
84 thisNextLast: (THIS|NEXT|LAST) ;
87 : baseDate SPACE+ deltaPlusMinusExpr
88 | deltaPlusMinusExpr SPACE+ baseDate
91 deltaDateExprRelativeToTodayImplied
92 : nFoosFromTodayAgoExpr
93 | deltaRelativeToTodayExpr
96 deltaRelativeToTodayExpr
97 : thisNextLast SPACE+ deltaUnit
100 nFoosFromTodayAgoExpr
101 : unsignedInt SPACE+ deltaUnit SPACE+ AGO_FROM_NOW
104 baseDate: singleDateExpr ;
106 baseAndOffsetTimeExpr
107 : deltaPlusMinusTimeExpr SPACE+ baseTime
108 | baseTime SPACE+ deltaPlusMinusTimeExpr
111 baseTime: singleTimeExpr ;
114 : nth SPACE+ deltaUnit (SPACE+ deltaBeforeAfter)?
120 deltaPlusMinusTimeExpr
121 : countUnitsBeforeAfterTimeExpr
122 | fractionBeforeAfterTimeExpr
125 countUnitsBeforeAfterTimeExpr
126 : nth (SPACE+ deltaTimeUnit)? SPACE+ deltaTimeBeforeAfter
129 fractionBeforeAfterTimeExpr
130 : deltaTimeFraction SPACE+ deltaTimeBeforeAfter
133 deltaUnit: (YEAR|MONTH|WEEK|DAY|WEEKDAY|WORKDAY) ;
135 deltaTimeUnit: (SECOND|MINUTE|HOUR|WORKDAY) ;
137 deltaBeforeAfter: (BEFORE|AFTER) ;
139 deltaTimeBeforeAfter: (BEFORE|AFTER) ;
146 year: DIGIT DIGIT DIGIT DIGIT ;
148 specialDate: SPECIAL_DATE ;
151 : DIGIT DIGIT? ('st'|'ST'|'nd'|'ND'|'rd'|'RD'|'th'|'TH')?
152 | KALENDS (SPACE+ 'of')?
153 | IDES (SPACE+ 'of')?
154 | NONES (SPACE+ 'of')?
157 firstOrLast: (FIRST|LAST) ;
159 nth: (DASH|PLUS)? DIGIT+ ('st'|'ST'|'nd'|'ND'|'rd'|'RD'|'th'|'TH')? ;
161 unsignedInt: DIGIT+ ;
163 deltaTimeFraction: DELTA_TIME_FRACTION ;
165 specialTimeExpr: specialTime (SPACE+ tzExpr)? ;
167 specialTime: SPECIAL_TIME ;
171 monthName: MONTH_NAME ;
173 monthNumber: DIGIT DIGIT? ;
177 minute: DIGIT DIGIT ;
179 second: DIGIT DIGIT ;
181 micros: DIGIT DIGIT? DIGIT? DIGIT? DIGIT? DIGIT? DIGIT? ;
183 ddiv: (SLASH|DASH|',') ;
187 dtdiv: ('T'|'t'|'at'|'AT'|','|';') ;
189 tddiv: ('on'|'ON'|','|';') ;
196 ntz: (PLUS|DASH) DIGIT DIGIT COLON? DIGIT DIGIT ;
198 ltz: UPPERCASE_STRING ;
200 // ----------------------------------
204 COMMENT: '#' ~[\r\n]* -> skip ;
206 THE: ('the'|'The') SPACE* -> skip ;
218 MONTH_NAME: (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) ;
305 WEEKDAY: (SUN|MON|TUE|WED|THU|FRI|SAT) ;
464 | MARTIN_LUTHER_KING_DAY
484 : ('noon'|'Noon'|'midday'|'Midday')
488 : ('midnight'|'Midnight')
493 : ('today'|'Today'|'now'|'Now')
498 : ('yesterday'|'Yesterday')
503 : ('tomorrow'|'Tomorrow')
555 MARTIN_LUTHER_KING_DAY
556 : 'martin luther king day'
557 | 'Martin Luther King Day'
608 | 'indiginous peoples day'
609 | 'indiginous peoples\' day'
612 | 'Indiginous Peoples Day'
613 | 'Indiginous Peoples\' Day'
640 FIRST: ('first'|'First') ;
642 LAST: ('last'|'Last'|'this past') ;
644 THIS: ('this'|'This'|'this coming') ;
646 NEXT: ('next'|'Next') ;
648 AGO_FROM_NOW: (AGO|FROM_NOW) ;
650 AGO: ('ago'|'Ago'|'back'|'Back') ;
652 FROM_NOW: ('from now'|'From Now') ;
654 BEFORE: ('to'|'To'|'before'|'Before'|'til'|'until'|'Until') ;
656 AFTER: ('after'|'After'|'from'|'From'|'past'|'Past') ;
658 DELTA_TIME_FRACTION: ('quarter'|'Quarter'|'half'|'Half') ;
662 IDES: ('ides'|'Ides') ;
664 NONES: ('nones'|'Nones') ;
666 KALENDS: ('kalends'|'Kalends') ;
679 UPPERCASE_STRING: [A-Z]+ ;