Make smart futures avoid polling.
[python_utils.git] / dateparse / dateparse_utils.g4
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
6 // on this shit at:
7 //
8 //    https://tomassetti.me/antlr-mega-tutorial/
9 //
10 // There are also a zillion premade grammars at:
11 //
12 //    https://github.com/antlr/grammars-v4
13
14 grammar dateparse_utils;
15
16 parse
17     : SPACE* dateExpr
18     | SPACE* timeExpr
19     | SPACE* dateExpr SPACE* dtdiv? SPACE* timeExpr
20     | SPACE* timeExpr SPACE* tddiv? SPACE+ dateExpr
21     ;
22
23 dateExpr
24     : singleDateExpr
25     | baseAndOffsetDateExpr
26     ;
27
28 timeExpr
29     : singleTimeExpr
30     | baseAndOffsetTimeExpr
31     ;
32
33 singleTimeExpr
34     : twentyFourHourTimeExpr
35     | twelveHourTimeExpr
36     | specialTimeExpr
37     ;
38
39 twentyFourHourTimeExpr
40     : hour ((SPACE|tdiv)+ minute ((SPACE|tdiv)+ second ((SPACE|tdiv)+ micros)? )? )? SPACE* tzExpr?
41     ;
42
43 twelveHourTimeExpr
44     : hour ((SPACE|tdiv)+ minute ((SPACE|tdiv)+ second ((SPACE|tdiv)+ micros)? )? )? SPACE* ampm SPACE* tzExpr?
45     ;
46
47 ampm: ('a'|'am'|'p'|'pm'|'AM'|'PM'|'A'|'P');
48
49 singleDateExpr
50     : monthDayMaybeYearExpr
51     | dayMonthMaybeYearExpr
52     | yearMonthDayExpr
53     | specialDateMaybeYearExpr
54     | nthWeekdayInMonthMaybeYearExpr
55     | firstLastWeekdayInMonthMaybeYearExpr
56     | deltaDateExprRelativeToTodayImplied
57     | dayName
58     ;
59
60 monthDayMaybeYearExpr
61     : monthExpr (SPACE|ddiv)+ dayOfMonth ((SPACE|ddiv)+ year)?
62     ;
63
64 dayMonthMaybeYearExpr
65     : dayOfMonth (SPACE|ddiv)+ monthName ((SPACE|ddiv)+ year)?
66     ;
67
68 yearMonthDayExpr
69     : year (SPACE|ddiv)+ monthExpr (SPACE|ddiv)+ dayOfMonth
70     ;
71
72 nthWeekdayInMonthMaybeYearExpr
73     : nth SPACE+ dayName SPACE+ ('in'|'of') SPACE+ monthName ((ddiv|SPACE)+ year)?
74     ;
75
76 firstLastWeekdayInMonthMaybeYearExpr
77     : firstOrLast SPACE+ dayName (SPACE+ ('in'|'of'))? SPACE+ monthName ((ddiv|SPACE)+ year)?
78     ;
79
80 specialDateMaybeYearExpr
81     : (thisNextLast SPACE+)? specialDate ((SPACE|ddiv)+ year)?
82     ;
83
84 thisNextLast: (THIS|NEXT|LAST) ;
85
86 baseAndOffsetDateExpr
87     : baseDate SPACE+ deltaPlusMinusExpr
88     | deltaPlusMinusExpr SPACE+ baseDate
89     ;
90
91 deltaDateExprRelativeToTodayImplied
92     : nFoosFromTodayAgoExpr
93     | deltaRelativeToTodayExpr
94     ;
95
96 deltaRelativeToTodayExpr
97     : thisNextLast SPACE+ deltaUnit
98     ;
99
100 nFoosFromTodayAgoExpr
101     : unsignedInt SPACE+ deltaUnit SPACE+ AGO_FROM_NOW
102     ;
103
104 baseDate: singleDateExpr ;
105
106 baseAndOffsetTimeExpr
107     : deltaPlusMinusTimeExpr SPACE+ baseTime
108     | baseTime SPACE+ deltaPlusMinusTimeExpr
109     ;
110
111 baseTime: singleTimeExpr ;
112
113 deltaPlusMinusExpr
114     : nth SPACE+ deltaUnit (SPACE+ deltaBeforeAfter)?
115     ;
116
117 deltaNextLast
118     : (NEXT|LAST) ;
119
120 deltaPlusMinusTimeExpr
121     : countUnitsBeforeAfterTimeExpr
122     | fractionBeforeAfterTimeExpr
123     ;
124
125 countUnitsBeforeAfterTimeExpr
126     : nth (SPACE+ deltaTimeUnit)? SPACE+ deltaTimeBeforeAfter
127     ;
128
129 fractionBeforeAfterTimeExpr
130     : deltaTimeFraction SPACE+ deltaTimeBeforeAfter
131     ;
132
133 deltaUnit: (YEAR|MONTH|WEEK|DAY|WEEKDAY|WORKDAY) ;
134
135 deltaTimeUnit: (SECOND|MINUTE|HOUR|WORKDAY) ;
136
137 deltaBeforeAfter: (BEFORE|AFTER) ;
138
139 deltaTimeBeforeAfter: (BEFORE|AFTER) ;
140
141 monthExpr
142     : monthName
143     | monthNumber
144     ;
145
146 year: DIGIT DIGIT DIGIT DIGIT ;
147
148 specialDate: SPECIAL_DATE ;
149
150 dayOfMonth
151     : DIGIT DIGIT? ('st'|'ST'|'nd'|'ND'|'rd'|'RD'|'th'|'TH')?
152     | KALENDS (SPACE+ 'of')?
153     | IDES (SPACE+ 'of')?
154     | NONES (SPACE+ 'of')?
155     ;
156
157 firstOrLast: (FIRST|LAST) ;
158
159 nth: (DASH|PLUS)? DIGIT+ ('st'|'ST'|'nd'|'ND'|'rd'|'RD'|'th'|'TH')? ;
160
161 unsignedInt: DIGIT+ ;
162
163 deltaTimeFraction: DELTA_TIME_FRACTION ;
164
165 specialTimeExpr: specialTime (SPACE+ tzExpr)? ;
166
167 specialTime: SPECIAL_TIME ;
168
169 dayName: WEEKDAY ;
170
171 monthName: MONTH_NAME ;
172
173 monthNumber: DIGIT DIGIT? ;
174
175 hour: DIGIT DIGIT? ;
176
177 minute: DIGIT DIGIT ;
178
179 second: DIGIT DIGIT ;
180
181 micros: DIGIT DIGIT? DIGIT? DIGIT? DIGIT? DIGIT? DIGIT? ;
182
183 ddiv: (SLASH|DASH|',') ;
184
185 tdiv: (COLON|DOT) ;
186
187 dtdiv: ('T'|'t'|'at'|'AT'|','|';') ;
188
189 tddiv: ('on'|'ON'|','|';') ;
190
191 tzExpr
192     : ntz
193     | ltz
194     ;
195
196 ntz: (PLUS|DASH) DIGIT DIGIT COLON? DIGIT DIGIT ;
197
198 ltz: UPPERCASE_STRING ;
199
200 // ----------------------------------
201
202 SPACE: [ \t\r\n] ;
203
204 COMMENT: '#' ~[\r\n]* -> skip ;
205
206 THE: ('the'|'The') SPACE* -> skip ;
207
208 DASH: '-' ;
209
210 PLUS: '+' ;
211
212 SLASH: '/' ;
213
214 DOT: '.' ;
215
216 COLON: ':' ;
217
218 MONTH_NAME: (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) ;
219
220 JAN : 'jan'
221     | 'Jan'
222     | 'JAN'
223     | 'January'
224     | 'january'
225     ;
226
227 FEB : 'feb'
228     | 'Feb'
229     | 'FEB'
230     | 'February'
231     | 'february'
232     ;
233
234 MAR : 'mar'
235     | 'Mar'
236     | 'MAR'
237     | 'March'
238     | 'march'
239     ;
240
241 APR : 'apr'
242     | 'Apr'
243     | 'APR'
244     | 'April'
245     | 'april'
246     ;
247
248 MAY : 'may'
249     | 'May'
250     | 'MAY'
251     ;
252
253 JUN : 'jun'
254     | 'Jun'
255     | 'JUN'
256     | 'June'
257     | 'june'
258     ;
259
260 JUL : 'jul'
261     | 'Jul'
262     | 'JUL'
263     | 'July'
264     | 'july'
265     ;
266
267 AUG : 'aug'
268     | 'Aug'
269     | 'AUG'
270     | 'August'
271     | 'august'
272     ;
273
274 SEP : 'sep'
275     | 'Sep'
276     | 'SEP'
277     | 'sept'
278     | 'Sept'
279     | 'SEPT'
280     | 'September'
281     | 'september'
282     ;
283
284 OCT : 'oct'
285     | 'Oct'
286     | 'OCT'
287     | 'October'
288     | 'october'
289     ;
290
291 NOV : 'nov'
292     | 'Nov'
293     | 'NOV'
294     | 'November'
295     | 'november'
296     ;
297
298 DEC : 'dec'
299     | 'Dec'
300     | 'DEC'
301     | 'December'
302     | 'december'
303     ;
304
305 WEEKDAY: (SUN|MON|TUE|WED|THU|FRI|SAT) ;
306
307 SUN : 'sun'
308     | 'Sun'
309     | 'SUN'
310     | 'suns'
311     | 'Suns'
312     | 'SUNS'
313     | 'sunday'
314     | 'Sunday'
315     | 'sundays'
316     | 'Sundays'
317     ;
318
319 MON : 'mon'
320     | 'Mon'
321     | 'MON'
322     | 'mons'
323     | 'Mons'
324     | 'MONS'
325     | 'monday'
326     | 'Monday'
327     | 'mondays'
328     | 'Mondays'
329     ;
330
331 TUE : 'tue'
332     | 'Tue'
333     | 'TUE'
334     | 'tues'
335     | 'Tues'
336     | 'TUES'
337     | 'tuesday'
338     | 'Tuesday'
339     | 'tuesdays'
340     | 'Tuesdays'
341     ;
342
343 WED : 'wed'
344     | 'Wed'
345     | 'WED'
346     | 'weds'
347     | 'Weds'
348     | 'WEDS'
349     | 'wednesday'
350     | 'Wednesday'
351     | 'wednesdays'
352     | 'Wednesdays'
353     ;
354
355 THU : 'thu'
356     | 'Thu'
357     | 'THU'
358     | 'thur'
359     | 'Thur'
360     | 'THUR'
361     | 'thurs'
362     | 'Thurs'
363     | 'THURS'
364     | 'thursday'
365     | 'Thursday'
366     | 'thursdays'
367     | 'Thursdays'
368     ;
369
370 FRI : 'fri'
371     | 'Fri'
372     | 'FRI'
373     | 'fris'
374     | 'Fris'
375     | 'FRIS'
376     | 'friday'
377     | 'Friday'
378     | 'fridays'
379     | 'Fridays'
380     ;
381
382 SAT : 'sat'
383     | 'Sat'
384     | 'SAT'
385     | 'sats'
386     | 'Sats'
387     | 'SATS'
388     | 'saturday'
389     | 'Saturday'
390     | 'saturdays'
391     | 'Saturdays'
392     ;
393
394 WEEK
395     : 'week'
396     | 'Week'
397     | 'weeks'
398     | 'Weeks'
399     | 'wks'
400     ;
401
402 DAY
403     : 'day'
404     | 'Day'
405     | 'days'
406     | 'Days'
407     ;
408
409 HOUR
410     : 'hour'
411     | 'Hour'
412     | 'hours'
413     | 'Hours'
414     | 'hrs'
415     ;
416
417 MINUTE
418     : 'min'
419     | 'Min'
420     | 'MIN'
421     | 'mins'
422     | 'Mins'
423     | 'MINS'
424     | 'minute'
425     | 'Minute'
426     | 'minutes'
427     | 'Minutes'
428     ;
429
430 SECOND
431     : 'sec'
432     | 'Sec'
433     | 'SEC'
434     | 'secs'
435     | 'Secs'
436     | 'SECS'
437     | 'second'
438     | 'Second'
439     | 'seconds'
440     | 'Seconds'
441     ;
442
443 MONTH
444     : 'month'
445     | 'Month'
446     | 'months'
447     | 'Months'
448     ;
449
450 YEAR
451     : 'year'
452     | 'Year'
453     | 'years'
454     | 'Years'
455     | 'yrs'
456     ;
457
458 SPECIAL_DATE
459     : TODAY
460     | YESTERDAY
461     | TOMORROW
462     | NEW_YEARS_EVE
463     | NEW_YEARS_DAY
464     | MARTIN_LUTHER_KING_DAY
465     | PRESIDENTS_DAY
466     | EASTER
467     | MEMORIAL_DAY
468     | INDEPENDENCE_DAY
469     | LABOR_DAY
470     | COLUMBUS_DAY
471     | VETERANS_DAY
472     | HALLOWEEN
473     | THANKSGIVING_DAY
474     | CHRISTMAS_EVE
475     | CHRISTMAS
476     ;
477
478 SPECIAL_TIME
479     : NOON
480     | MIDNIGHT
481     ;
482
483 NOON
484     : ('noon'|'Noon'|'midday'|'Midday')
485     ;
486
487 MIDNIGHT
488     : ('midnight'|'Midnight')
489     ;
490
491 // today
492 TODAY
493     : ('today'|'Today'|'now'|'Now')
494     ;
495
496 // yeste
497 YESTERDAY
498     : ('yesterday'|'Yesterday')
499     ;
500
501 // tomor
502 TOMORROW
503     : ('tomorrow'|'Tomorrow')
504     ;
505
506 // easte
507 EASTER
508     : 'easter' SUN?
509     | 'Easter' SUN?
510     ;
511
512 // newye
513 NEW_YEARS_DAY
514     : 'new years'
515     | 'New Years'
516     | 'new years day'
517     | 'New Years Day'
518     | 'new year\'s'
519     | 'New Year\'s'
520     | 'new year\'s day'
521     | 'New year\'s Day'
522     ;
523
524 // newyeeve
525 NEW_YEARS_EVE
526     : 'nye'
527     | 'NYE'
528     | 'new years eve'
529     | 'New Years Eve'
530     | 'new year\'s eve'
531     | 'New Year\'s Eve'
532     ;
533
534 // chris
535 CHRISTMAS
536     : 'christmas'
537     | 'Christmas'
538     | 'christmas day'
539     | 'Christmas Day'
540     | 'xmas'
541     | 'Xmas'
542     | 'xmas day'
543     | 'Xmas Day'
544     ;
545
546 // chriseve
547 CHRISTMAS_EVE
548     : 'christmas eve'
549     | 'Christmas Eve'
550     | 'xmas eve'
551     | 'Xmas Eve'
552     ;
553
554 // mlk
555 MARTIN_LUTHER_KING_DAY
556     : 'martin luther king day'
557     | 'Martin Luther King Day'
558     | 'mlk day'
559     | 'MLK Day'
560     | 'MLK day'
561     | 'mlk'
562     | 'MLK'
563     ;
564
565 // memor
566 MEMORIAL_DAY
567     : 'memorial'
568     | 'Memorial'
569     | 'memorial day'
570     | 'Memorial Day'
571     ;
572
573 // indep
574 INDEPENDENCE_DAY
575     : 'independence day'
576     | 'Independence day'
577     | 'Independence Day'
578     ;
579
580 // labor
581 LABOR_DAY
582     : 'labor'
583     | 'Labor'
584     | 'labor day'
585     | 'Labor Day'
586     ;
587
588 // presi
589 PRESIDENTS_DAY
590     : 'presidents\' day'
591     | 'president\'s day'
592     | 'presidents day'
593     | 'presidents'
594     | 'president\'s'
595     | 'presidents\''
596     | 'Presidents\' Day'
597     | 'President\'s Day'
598     | 'Presidents Day'
599     | 'Presidents'
600     | 'President\'s'
601     | 'Presidents\''
602     ;
603
604 // colum
605 COLUMBUS_DAY
606     : 'columbus'
607     | 'columbus day'
608     | 'indiginous peoples day'
609     | 'indiginous peoples\' day'
610     | 'Columbus'
611     | 'Columbus Day'
612     | 'Indiginous Peoples Day'
613     | 'Indiginous Peoples\' Day'
614     ;
615
616 // veter
617 VETERANS_DAY
618     : 'veterans'
619     | 'veterans day'
620     | 'veterans\' day'
621     | 'Veterans'
622     | 'Veterans Day'
623     | 'Veterans\' Day'
624     ;
625
626 // hallo
627 HALLOWEEN
628     : 'halloween'
629     | 'Halloween'
630     ;
631
632 // thank
633 THANKSGIVING_DAY
634     : 'thanksgiving'
635     | 'thanksgiving day'
636     | 'Thanksgiving'
637     | 'Thanksgiving Day'
638     ;
639
640 FIRST: ('first'|'First') ;
641
642 LAST: ('last'|'Last'|'this past') ;
643
644 THIS: ('this'|'This'|'this coming') ;
645
646 NEXT: ('next'|'Next') ;
647
648 AGO_FROM_NOW: (AGO|FROM_NOW) ;
649
650 AGO: ('ago'|'Ago'|'back'|'Back') ;
651
652 FROM_NOW: ('from now'|'From Now') ;
653
654 BEFORE: ('to'|'To'|'before'|'Before'|'til'|'until'|'Until') ;
655
656 AFTER: ('after'|'After'|'from'|'From'|'past'|'Past') ;
657
658 DELTA_TIME_FRACTION: ('quarter'|'Quarter'|'half'|'Half') ;
659
660 DIGIT: ('0'..'9') ;
661
662 IDES: ('ides'|'Ides') ;
663
664 NONES: ('nones'|'Nones') ;
665
666 KALENDS: ('kalends'|'Kalends') ;
667
668 WORKDAY
669     : 'workday'
670     | 'workdays'
671     | 'work days'
672     | 'working days'
673     | 'Workday'
674     | 'Workdays'
675     | 'Work Days'
676     | 'Working Days'
677     ;
678
679 UPPERCASE_STRING: [A-Z]+ ;