3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 from antlr3
.compat
import set, frozenset
26 allOrdinals
= set([1, 2, 3, 4, 5])
27 numOrdinals
= len(allOrdinals
)
32 HIDDEN
= BaseRecognizer
.HIDDEN
74 "<invalid>", "<EOR>", "<DOWN>", "<UP>",
75 "OF", "TIME", "EVERY", "DIGIT", "DIGITS", "COMMA", "FIRST", "SECOND",
76 "THIRD", "FOURTH", "FIFTH", "FOURTH_OR_FIFTH", "HOURS", "MINUTES", "DAY",
77 "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY",
78 "SUNDAY", "MONTH", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
79 "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER", "QUARTER",
86 class GrocParser(Parser
):
87 grammarFileName
= "Groc.g"
88 antlr_version
= version_str_to_tuple("3.1.1")
89 antlr_version_str
= "3.1.1"
90 tokenNames
= tokenNames
92 def __init__(self
, input, state
=None):
94 state
= RecognizerSharedState()
96 Parser
.__init
__(self
, input, state
)
99 self
.dfa3
= self
.DFA3(
105 accept
= self
.DFA3_accept
,
106 special
= self
.DFA3_special
,
107 transition
= self
.DFA3_transition
113 self
.ordinal_set
= set()
114 self
.weekday_set
= set()
115 self
.month_set
= set()
116 self
.time_string
= '';
117 self
.interval_mins
= 0;
118 self
.period_string
= '';
156 def ValueOf(self
, token_type
):
157 return self
.valuesDict
.get(token_type
, -1)
162 def timespec(self
, ):
168 LA1_0
= self
.input.LA(1)
170 if (LA1_0
== EVERY
) :
171 LA1_1
= self
.input.LA(2)
173 if ((DIGIT
<= LA1_1
<= DIGITS
)) :
175 elif ((DAY
<= LA1_1
<= SUNDAY
)) :
178 nvae
= NoViableAltException("", 1, 1, self
.input)
182 elif ((FIRST
<= LA1_0
<= FOURTH_OR_FIFTH
)) :
185 nvae
= NoViableAltException("", 1, 0, self
.input)
191 self
._state
.following
.append(self
.FOLLOW_specifictime_in_timespec44
)
194 self
._state
.following
.pop()
199 self
._state
.following
.append(self
.FOLLOW_interval_in_timespec48
)
202 self
._state
.following
.pop()
210 except RecognitionException
, re
:
212 self
.recover(self
.input, re
)
221 def specifictime(self
, ):
230 alt3
= self
.dfa3
.predict(self
.input)
235 self
._state
.following
.append(self
.FOLLOW_ordinals_in_specifictime69
)
238 self
._state
.following
.pop()
239 self
._state
.following
.append(self
.FOLLOW_weekdays_in_specifictime71
)
242 self
._state
.following
.pop()
246 self
.match(self
.input, OF
, self
.FOLLOW_OF_in_specifictime74
)
248 LA2_0
= self
.input.LA(1)
250 if ((MONTH
<= LA2_0
<= DECEMBER
)) :
252 elif ((FIRST
<= LA2_0
<= THIRD
) or LA2_0
== QUARTER
) :
255 nvae
= NoViableAltException("", 2, 0, self
.input)
261 self
._state
.following
.append(self
.FOLLOW_monthspec_in_specifictime77
)
264 self
._state
.following
.pop()
269 self
._state
.following
.append(self
.FOLLOW_quarterspec_in_specifictime79
)
272 self
._state
.following
.pop()
284 self
._state
.following
.append(self
.FOLLOW_ordinals_in_specifictime96
)
287 self
._state
.following
.pop()
288 self
._state
.following
.append(self
.FOLLOW_weekdays_in_specifictime98
)
291 self
._state
.following
.pop()
292 self
.month_set
= set(range(1,13))
299 TIME1
=self
.match(self
.input, TIME
, self
.FOLLOW_TIME_in_specifictime112
)
300 self
.time_string
= TIME1
.text
308 except RecognitionException
, re
:
310 self
.recover(self
.input, re
)
319 def interval(self
, ):
329 self
.match(self
.input, EVERY
, self
.FOLLOW_EVERY_in_interval131
)
330 intervalnum
= self
.input.LT(1)
331 if (DIGIT
<= self
.input.LA(1) <= DIGITS
):
333 self
._state
.errorRecovery
= False
336 mse
= MismatchedSetException(None, self
.input)
341 self
.interval_mins
= int(intervalnum
.text
)
343 self
._state
.following
.append(self
.FOLLOW_period_in_interval157
)
344 period2
= self
.period()
346 self
._state
.following
.pop()
348 if ((period2
is not None) and [self
.input.toString(period2
.start
,period2
.stop
)] or [None])[0] == "hours":
349 self
.period_string
= "hours"
351 self
.period_string
= "minutes"
360 except RecognitionException
, re
:
362 self
.recover(self
.input, re
)
371 def ordinals(self
, ):
377 LA5_0
= self
.input.LA(1)
379 if (LA5_0
== EVERY
) :
381 elif ((FIRST
<= LA5_0
<= FOURTH_OR_FIFTH
)) :
384 nvae
= NoViableAltException("", 5, 0, self
.input)
390 self
.match(self
.input, EVERY
, self
.FOLLOW_EVERY_in_ordinals176
)
391 self
.ordinal_set
= self
.ordinal_set
.union(allOrdinals
)
397 self
._state
.following
.append(self
.FOLLOW_ordinal_in_ordinals192
)
400 self
._state
.following
.pop()
403 LA4_0
= self
.input.LA(1)
405 if (LA4_0
== COMMA
) :
411 self
.match(self
.input, COMMA
, self
.FOLLOW_COMMA_in_ordinals195
)
412 self
._state
.following
.append(self
.FOLLOW_ordinal_in_ordinals197
)
415 self
._state
.following
.pop()
432 except RecognitionException
, re
:
434 self
.recover(self
.input, re
)
450 ord = self
.input.LT(1)
451 if (FIRST
<= self
.input.LA(1) <= FOURTH_OR_FIFTH
):
453 self
._state
.errorRecovery
= False
456 mse
= MismatchedSetException(None, self
.input)
461 self
.ordinal_set
.add(self
.ValueOf(ord.type));
467 except RecognitionException
, re
:
469 self
.recover(self
.input, re
)
477 class period_return(ParserRuleReturnScope
):
479 ParserRuleReturnScope
.__init
__(self
)
487 retval
= self
.period_return()
488 retval
.start
= self
.input.LT(1)
493 if (HOURS
<= self
.input.LA(1) <= MINUTES
):
495 self
._state
.errorRecovery
= False
498 mse
= MismatchedSetException(None, self
.input)
505 retval
.stop
= self
.input.LT(-1)
508 except RecognitionException
, re
:
510 self
.recover(self
.input, re
)
519 def weekdays(self
, ):
525 LA7_0
= self
.input.LA(1)
529 elif ((MONDAY
<= LA7_0
<= SUNDAY
)) :
532 nvae
= NoViableAltException("", 7, 0, self
.input)
538 self
.match(self
.input, DAY
, self
.FOLLOW_DAY_in_weekdays280
)
540 self
.weekday_set
= set([self
.ValueOf(SUNDAY
), self
.ValueOf(MONDAY
),
541 self
.ValueOf(TUESDAY
), self
.ValueOf(WEDNESDAY
),
542 self
.ValueOf(THURSDAY
), self
.ValueOf(FRIDAY
),
543 self
.ValueOf(SATURDAY
), self
.ValueOf(SUNDAY
)])
550 self
._state
.following
.append(self
.FOLLOW_weekday_in_weekdays288
)
553 self
._state
.following
.pop()
556 LA6_0
= self
.input.LA(1)
558 if (LA6_0
== COMMA
) :
564 self
.match(self
.input, COMMA
, self
.FOLLOW_COMMA_in_weekdays291
)
565 self
._state
.following
.append(self
.FOLLOW_weekday_in_weekdays293
)
568 self
._state
.following
.pop()
585 except RecognitionException
, re
:
587 self
.recover(self
.input, re
)
603 dayname
= self
.input.LT(1)
604 if (MONDAY
<= self
.input.LA(1) <= SUNDAY
):
606 self
._state
.errorRecovery
= False
609 mse
= MismatchedSetException(None, self
.input)
614 self
.weekday_set
.add(self
.ValueOf(dayname
.type))
620 except RecognitionException
, re
:
622 self
.recover(self
.input, re
)
631 def monthspec(self
, ):
637 LA8_0
= self
.input.LA(1)
639 if (LA8_0
== MONTH
) :
641 elif ((JANUARY
<= LA8_0
<= DECEMBER
)) :
644 nvae
= NoViableAltException("", 8, 0, self
.input)
650 self
.match(self
.input, MONTH
, self
.FOLLOW_MONTH_in_monthspec373
)
652 self
.month_set
= self
.month_set
.union(set([
653 self
.ValueOf(JANUARY
), self
.ValueOf(FEBRUARY
), self
.ValueOf(MARCH
),
654 self
.ValueOf(APRIL
), self
.ValueOf(MAY
), self
.ValueOf(JUNE
),
655 self
.ValueOf(JULY
), self
.ValueOf(AUGUST
), self
.ValueOf(SEPTEMBER
),
656 self
.ValueOf(OCTOBER
), self
.ValueOf(NOVEMBER
),
657 self
.ValueOf(DECEMBER
)]))
663 self
._state
.following
.append(self
.FOLLOW_months_in_monthspec383
)
666 self
._state
.following
.pop()
674 except RecognitionException
, re
:
676 self
.recover(self
.input, re
)
691 self
._state
.following
.append(self
.FOLLOW_month_in_months400
)
694 self
._state
.following
.pop()
697 LA9_0
= self
.input.LA(1)
699 if (LA9_0
== COMMA
) :
705 self
.match(self
.input, COMMA
, self
.FOLLOW_COMMA_in_months403
)
706 self
._state
.following
.append(self
.FOLLOW_month_in_months405
)
709 self
._state
.following
.pop()
723 except RecognitionException
, re
:
725 self
.recover(self
.input, re
)
741 monthname
= self
.input.LT(1)
742 if (JANUARY
<= self
.input.LA(1) <= DECEMBER
):
744 self
._state
.errorRecovery
= False
747 mse
= MismatchedSetException(None, self
.input)
751 self
.month_set
.add(self
.ValueOf(monthname
.type));
756 except RecognitionException
, re
:
758 self
.recover(self
.input, re
)
767 def quarterspec(self
, ):
773 LA10_0
= self
.input.LA(1)
775 if (LA10_0
== QUARTER
) :
777 elif ((FIRST
<= LA10_0
<= THIRD
)) :
780 nvae
= NoViableAltException("", 10, 0, self
.input)
786 self
.match(self
.input, QUARTER
, self
.FOLLOW_QUARTER_in_quarterspec497
)
788 self
.month_set
= self
.month_set
.union(set([
789 self
.ValueOf(JANUARY
), self
.ValueOf(APRIL
), self
.ValueOf(JULY
),
790 self
.ValueOf(OCTOBER
)]))
796 self
._state
.following
.append(self
.FOLLOW_quarter_ordinals_in_quarterspec509
)
797 self
.quarter_ordinals()
799 self
._state
.following
.pop()
800 self
.match(self
.input, MONTH
, self
.FOLLOW_MONTH_in_quarterspec511
)
801 self
.match(self
.input, OF
, self
.FOLLOW_OF_in_quarterspec513
)
802 self
.match(self
.input, QUARTER
, self
.FOLLOW_QUARTER_in_quarterspec515
)
813 except RecognitionException
, re
:
815 self
.recover(self
.input, re
)
824 def quarter_ordinals(self
, ):
830 self
._state
.following
.append(self
.FOLLOW_month_of_quarter_ordinal_in_quarter_ordinals534
)
831 self
.month_of_quarter_ordinal()
833 self
._state
.following
.pop()
836 LA11_0
= self
.input.LA(1)
838 if (LA11_0
== COMMA
) :
844 self
.match(self
.input, COMMA
, self
.FOLLOW_COMMA_in_quarter_ordinals537
)
845 self
._state
.following
.append(self
.FOLLOW_month_of_quarter_ordinal_in_quarter_ordinals539
)
846 self
.month_of_quarter_ordinal()
848 self
._state
.following
.pop()
862 except RecognitionException
, re
:
864 self
.recover(self
.input, re
)
873 def month_of_quarter_ordinal(self
, ):
880 offset
= self
.input.LT(1)
881 if (FIRST
<= self
.input.LA(1) <= THIRD
):
883 self
._state
.errorRecovery
= False
886 mse
= MismatchedSetException(None, self
.input)
891 jOffset
= self
.ValueOf(offset
.type) - 1
892 self
.month_set
= self
.month_set
.union(set([
893 jOffset
+ self
.ValueOf(JANUARY
), jOffset
+ self
.ValueOf(APRIL
),
894 jOffset
+ self
.ValueOf(JULY
), jOffset
+ self
.ValueOf(OCTOBER
)]))
899 except RecognitionException
, re
:
901 self
.recover(self
.input, re
)
913 DFA3_eot
= DFA
.unpack(
917 DFA3_eof
= DFA
.unpack(
921 DFA3_min
= DFA
.unpack(
922 u
"\1\6\1\22\1\11\2\4\1\12\2\uffff\1\23\1\11\1\4"
925 DFA3_max
= DFA
.unpack(
926 u
"\1\17\2\31\1\5\1\11\1\17\2\uffff\2\31\1\11"
929 DFA3_accept
= DFA
.unpack(
930 u
"\6\uffff\1\1\1\2\3\uffff"
933 DFA3_special
= DFA
.unpack(
939 DFA
.unpack(u
"\1\1\3\uffff\6\2"),
940 DFA
.unpack(u
"\1\3\7\4"),
941 DFA
.unpack(u
"\1\5\10\uffff\1\3\7\4"),
942 DFA
.unpack(u
"\1\6\1\7"),
943 DFA
.unpack(u
"\1\6\1\7\3\uffff\1\10"),
944 DFA
.unpack(u
"\6\11"),
947 DFA
.unpack(u
"\7\12"),
948 DFA
.unpack(u
"\1\5\10\uffff\1\3\7\4"),
949 DFA
.unpack(u
"\1\6\1\7\3\uffff\1\10")
956 FOLLOW_specifictime_in_timespec44
= frozenset([1])
957 FOLLOW_interval_in_timespec48
= frozenset([1])
958 FOLLOW_ordinals_in_specifictime69
= frozenset([18, 19, 20, 21, 22, 23, 24, 25])
959 FOLLOW_weekdays_in_specifictime71
= frozenset([4])
960 FOLLOW_OF_in_specifictime74
= frozenset([10, 11, 12, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])
961 FOLLOW_monthspec_in_specifictime77
= frozenset([5])
962 FOLLOW_quarterspec_in_specifictime79
= frozenset([5])
963 FOLLOW_ordinals_in_specifictime96
= frozenset([18, 19, 20, 21, 22, 23, 24, 25])
964 FOLLOW_weekdays_in_specifictime98
= frozenset([5])
965 FOLLOW_TIME_in_specifictime112
= frozenset([1])
966 FOLLOW_EVERY_in_interval131
= frozenset([7, 8])
967 FOLLOW_set_in_interval141
= frozenset([16, 17])
968 FOLLOW_period_in_interval157
= frozenset([1])
969 FOLLOW_EVERY_in_ordinals176
= frozenset([1])
970 FOLLOW_ordinal_in_ordinals192
= frozenset([1, 9])
971 FOLLOW_COMMA_in_ordinals195
= frozenset([10, 11, 12, 13, 14, 15])
972 FOLLOW_ordinal_in_ordinals197
= frozenset([1, 9])
973 FOLLOW_set_in_ordinal218
= frozenset([1])
974 FOLLOW_set_in_period257
= frozenset([1])
975 FOLLOW_DAY_in_weekdays280
= frozenset([1])
976 FOLLOW_weekday_in_weekdays288
= frozenset([1, 9])
977 FOLLOW_COMMA_in_weekdays291
= frozenset([18, 19, 20, 21, 22, 23, 24, 25])
978 FOLLOW_weekday_in_weekdays293
= frozenset([1, 9])
979 FOLLOW_set_in_weekday314
= frozenset([1])
980 FOLLOW_MONTH_in_monthspec373
= frozenset([1])
981 FOLLOW_months_in_monthspec383
= frozenset([1])
982 FOLLOW_month_in_months400
= frozenset([1, 9])
983 FOLLOW_COMMA_in_months403
= frozenset([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38])
984 FOLLOW_month_in_months405
= frozenset([1, 9])
985 FOLLOW_set_in_month424
= frozenset([1])
986 FOLLOW_QUARTER_in_quarterspec497
= frozenset([1])
987 FOLLOW_quarter_ordinals_in_quarterspec509
= frozenset([26])
988 FOLLOW_MONTH_in_quarterspec511
= frozenset([4])
989 FOLLOW_OF_in_quarterspec513
= frozenset([39])
990 FOLLOW_QUARTER_in_quarterspec515
= frozenset([1])
991 FOLLOW_month_of_quarter_ordinal_in_quarter_ordinals534
= frozenset([1, 9])
992 FOLLOW_COMMA_in_quarter_ordinals537
= frozenset([10, 11, 12, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])
993 FOLLOW_month_of_quarter_ordinal_in_quarter_ordinals539
= frozenset([1, 9])
994 FOLLOW_set_in_month_of_quarter_ordinal558
= frozenset([1])
998 def main(argv
, stdin
=sys
.stdin
, stdout
=sys
.stdout
, stderr
=sys
.stderr
):
999 from antlr3
.main
import ParserMain
1000 main
= ParserMain("GrocLexer", GrocParser
)
1002 main
.stdout
= stdout
1003 main
.stderr
= stderr
1007 if __name__
== '__main__':