Initial revision
[python_utils.git] / dateparse / dateparse_utils.g4
1 // antlr4 -Dlanguage=Python3 ./dateparse_utils.g4
2
3 // Hi, self.  In ANTLR grammars, there are two separate types of symbols: those
4 // for the lexer and those for the parser.  The former begin with a CAPITAL
5 // whereas the latter begin with lowercase.  The order of the lexer symbols
6 // is the order that the lexer will recognize them in.  There's a good tutorial
7 // on this shit at:
8 //
9 //    https://tomassetti.me/antlr-mega-tutorial/
10 //
11 // There are also a zillion premade grammars at:
12 //
13 //    https://github.com/antlr/grammars-v4
14
15 grammar dateparse_utils;
16
17 parse: dateExpr ;
18
19 dateExpr
20     : singleDateExpr
21     | baseAndOffsetDateExpr
22     ;
23
24 singleDateExpr
25     : monthDayMaybeYearExpr
26     | dayMonthMaybeYearExpr
27     | yearMonthDayExpr
28     | specialDateMaybeYearExpr
29     | nthWeekdayInMonthMaybeYearExpr
30     | firstLastWeekdayInMonthMaybeYearExpr
31     ;
32
33 monthDayMaybeYearExpr
34     : monthExpr DIV* dayOfMonth (DIV* year)?
35     ;
36
37 dayMonthMaybeYearExpr
38     : dayOfMonth DIV* monthName (DIV* year)?
39     ;
40
41 yearMonthDayExpr
42     : year DIV* monthName DIV* dayOfMonth
43     ;
44
45 nthWeekdayInMonthMaybeYearExpr
46     : nth dayName ('in'|'of') monthName (DIV* year)?
47     ;
48
49 firstLastWeekdayInMonthMaybeYearExpr
50     : firstOrLast dayName ('in'|'of'|DIV)? monthName (DIV* year)?
51     ;
52
53 specialDateMaybeYearExpr
54     : specialDate (DIV* year)?
55     ;
56
57 baseAndOffsetDateExpr
58     : baseDate deltaPlusMinusExpr
59     | deltaPlusMinusExpr baseDate
60     ;
61
62 baseDate: singleDateExpr ;
63
64 deltaPlusMinusExpr: deltaInt deltaUnit deltaBeforeAfter? ;
65
66 deltaUnit: (WEEK|DAY|SUN|WEEKDAY) ;
67
68 deltaBeforeAfter: (BEFORE|AFTER) ;
69
70 monthExpr
71     : monthName
72     | monthNumber
73     ;
74
75 year: DIGIT DIGIT DIGIT DIGIT ;
76
77 specialDate: SPECIAL_DATE ;
78
79 dayOfMonth: DIGIT? DIGIT ('st'|'nd'|'rd'|'th')? ;
80
81 firstOrLast: (FIRST|LAST) ;
82
83 nth: DIGIT ('st'|'nd'|'rd'|'th')? ;
84
85 deltaInt: ('+'|'-')? DIGIT+ ;
86
87 dayName: WEEKDAY ;
88
89 monthName: MONTH ;
90
91 monthNumber: DIGIT? DIGIT ;
92
93 // ----------------------------------
94
95 COMMENT: '#' ~[\r\n]* -> skip ;
96
97 SPACE: [ \t\r\n] -> skip ;
98
99 THE: 'the' -> skip ;
100
101 DIV: ('/'|','|'.') ;
102
103 MONTH: (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) ;
104
105 JAN : 'jan'
106     | 'january'
107     ;
108
109 FEB : 'feb'
110     | 'february'
111     ;
112
113 MAR : 'mar'
114     | 'march'
115     ;
116
117 APR : 'apr'
118     | 'april'
119     ;
120
121 MAY : 'may'
122     ;
123
124 JUN : 'jun'
125     | 'june'
126     ;
127
128 JUL : 'jul'
129     | 'july'
130     ;
131
132 AUG : 'aug'
133     | 'august'
134     ;
135
136 SEP : 'sep'
137     | 'sept'
138     | 'september'
139     ;
140
141 OCT : 'oct'
142     | 'october'
143     ;
144
145 NOV : 'nov'
146     | 'november'
147     ;
148
149 DEC : 'dec'
150     | 'december'
151     ;
152
153 WEEKDAY: (SUN|MON|TUE|WED|THU|FRI|SAT) ;
154
155 SUN : 'sun'
156     | 'suns'
157     | 'sunday'
158     | 'sundays'
159     ;
160
161 MON : 'mon'
162     | 'mons'
163     | 'monday'
164     | 'mondays'
165     ;
166
167 TUE : 'tue'
168     | 'tues'
169     | 'tuesday'
170     | 'tuesdays'
171     ;
172
173 WED : 'wed'
174     | 'weds'
175     | 'wednesday'
176     | 'wednesdays'
177     ;
178
179 THU : 'thu'
180     | 'thur'
181     | 'thurs'
182     | 'thursday'
183     | 'thursdays'
184     ;
185
186 FRI : 'fri'
187     | 'fris'
188     | 'friday'
189     | 'fridays'
190     ;
191
192 SAT : 'sat'
193     | 'sats'
194     | 'saturday'
195     | 'saturdays'
196     ;
197
198 WEEK
199     : 'week'
200     | 'weeks'
201     ;
202
203 DAY
204     : 'day'
205     | 'days'
206     ;
207
208 SPECIAL_DATE
209     : TODAY
210     | NEW_YEARS_EVE
211     | NEW_YEARS_DAY
212     | MARTIN_LUTHER_KING_DAY
213     | PRESIDENTS_DAY
214     | EASTER
215     | MEMORIAL_DAY
216     | INDEPENDENCE_DAY
217     | LABOR_DAY
218     | COLUMBUS_DAY
219     | VETERANS_DAY
220     | THANKSGIVING_DAY
221     | CHRISTMAS_EVE
222     | CHRISTMAS
223     ;
224
225 // today
226 TODAY
227     : 'today'
228     ;
229
230 // easte
231 EASTER
232     : 'easter'
233     | 'easter sunday'
234     ;
235
236 // newye
237 NEW_YEARS_DAY
238     : 'new years'
239     | 'new years day'
240     | 'new year\'s'
241     | 'new year\'s day'
242     ;
243
244 // newyeeve
245 NEW_YEARS_EVE
246     : 'nye'
247     | 'new years eve'
248     | 'new year\'s eve'
249     ;
250
251 // chris
252 CHRISTMAS
253     : 'christmas'
254     | 'christmas day'
255     | 'xmas'
256     | 'xmas day'
257     ;
258
259 // chriseve
260 CHRISTMAS_EVE
261     : 'christmas eve'
262     | 'xmas eve'
263     ;
264
265 // mlk
266 MARTIN_LUTHER_KING_DAY
267     : 'martin luther king day'
268     | 'mlk day'
269     | 'mlk'
270     ;
271
272 // memor
273 MEMORIAL_DAY
274     : 'memorial'
275     | 'memorial day'
276     ;
277
278 // indep
279 INDEPENDENCE_DAY
280     : 'independence day'
281     ;
282
283 // labor
284 LABOR_DAY
285     : 'labor'
286     | 'labor day'
287     ;
288
289 // presi
290 PRESIDENTS_DAY
291     : 'presidents\' day'
292     | 'president\'s day'
293     | 'presidents day'
294     | 'presidents'
295     | 'president\'s'
296     | 'presidents\''
297     ;
298
299 // colum
300 COLUMBUS_DAY
301     : 'columbus'
302     | 'columbus day'
303     | 'indiginous peoples day'
304     | 'indiginous peoples\' day'
305     ;
306
307 // veter
308 VETERANS_DAY
309     : 'veterans'
310     | 'veterans day'
311     | 'veterans\' day'
312     ;
313
314 // thank
315 THANKSGIVING_DAY
316     : 'thanksgiving'
317     | 'thanksgiving day'
318     ;
319
320 FIRST: 'first' ;
321
322 LAST: 'last' ;
323
324 BEFORE: 'before' ;
325
326 AFTER: ('after'|'from') ;
327
328 DIGIT: ('0'..'9') ;