-
Notifications
You must be signed in to change notification settings - Fork 19
/
grammar.py
353 lines (278 loc) · 9.97 KB
/
grammar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# ruff: noqa: E402 I001
# It's recommended to `enablePackrat()` immediately after importing pyparsing
# https://github.com/pyparsing/pyparsing/wiki/Performance-Tips
# TODO: uncomment this once benchmark testing has run once in CI
# import pyparsing
# pyparsing.ParserElement.enablePackrat()
from pyparsing import (
Combine,
NotAny,
OneOrMore,
Opt,
Optional,
ParseException,
Regex,
Word,
ZeroOrMore,
nums,
oneOf,
)
from pyparsing import Literal as L
from edtf.parser.edtf_exceptions import EDTFParseException
# (* ************************** Level 0 *************************** *)
from edtf.parser.parser_classes import (
UA,
Consecutives,
Date,
DateAndTime,
EarlierConsecutives,
ExponentialYear,
Interval,
LaterConsecutives,
Level1Interval,
Level2Interval,
Level2Season,
LongYear,
MultipleDates,
OneOfASet,
PartialUncertainOrApproximate,
PartialUnspecified,
Season,
UncertainOrApproximate,
Unspecified,
)
oneThru12 = oneOf(["%.2d" % i for i in range(1, 13)])
oneThru13 = oneOf(["%.2d" % i for i in range(1, 14)])
oneThru23 = oneOf(["%.2d" % i for i in range(1, 24)])
zeroThru23 = oneOf(["%.2d" % i for i in range(0, 24)])
oneThru29 = oneOf(["%.2d" % i for i in range(1, 30)])
oneThru30 = oneOf(["%.2d" % i for i in range(1, 31)])
oneThru31 = oneOf(["%.2d" % i for i in range(1, 32)])
oneThru59 = oneOf(["%.2d" % i for i in range(1, 60)])
zeroThru59 = oneOf(["%.2d" % i for i in range(0, 60)])
digit = Word(nums, exact=1)
positiveDigit = Word(nums, exact=1, excludeChars="0")
positiveInteger = Combine(positiveDigit + ZeroOrMore(digit))
second = zeroThru59
minute = zeroThru59
hour = zeroThru23
day = oneThru31("day")
month = oneThru12("month")
monthDay = (
(oneOf("01 03 05 07 08 10 12")("month") + "-" + oneThru31("day"))
^ (oneOf("04 06 09 11")("month") + "-" + oneThru30("day"))
^ (L("02")("month") + "-" + oneThru29("day"))
)
# Significant digits suffix
significantDigits = "S" + Word(nums)("significant_digits")
# 4 digits, 0 to 9
positiveYear = Word(nums, exact=4)
# Negative version of positive year, but "-0000" is illegal
negativeYear = NotAny(L("-0000")) + ("-" + positiveYear)
year = Combine(positiveYear ^ negativeYear)("year") + Optional(significantDigits)
# simple version for Consecutives
year_basic = Combine(positiveYear ^ negativeYear)("year")
yearMonth = year + "-" + month
yearMonthDay = year + "-" + monthDay # o hai iso date
date = Combine(year ^ yearMonth ^ yearMonthDay)("date")
Date.set_parser(date)
zoneOffsetHour = oneThru13
zoneOffset = L("Z") ^ (
Regex("[+-]")
+ (zoneOffsetHour + Optional(":" + minute) ^ L("14:00") ^ ("00:" + oneThru59))
)
baseTime = Combine(hour + ":" + minute + ":" + second ^ "24:00:00")
time = Combine(baseTime + Optional(zoneOffset))("time")
dateAndTime = date + "T" + time
DateAndTime.set_parser(dateAndTime)
l0Interval = date("lower") + "/" + date("upper")
Interval.set_parser(l0Interval)
level0Expression = date ^ dateAndTime ^ l0Interval
# (* ************************** Level 1 *************************** *)
# (* ** Auxiliary Assignments for Level 1 ** *)
UASymbol = Combine(oneOf("? ~ %"))
UA.set_parser(UASymbol)
seasonNumber = oneOf("21 22 23 24")
# (* *** Season (unqualified) *** *)
season = year + "-" + seasonNumber("season")
Season.set_parser(season)
dateOrSeason = date("") ^ season
# (* *** Long Year - Simple Form *** *)
longYearSimple = (
"Y"
+ Combine(Optional("-") + positiveDigit + digit + digit + digit + OneOrMore(digit))(
"year"
)
+ Optional(significantDigits)
)
LongYear.set_parser(longYearSimple)
# (* *** L1Interval *** *)
uaDateOrSeason = dateOrSeason + Optional(UASymbol)
# bit of a kludge here to get the all the relevant tokens into the parse action
# cleanly otherwise the parameter names are overlapped.
def f(toks):
try:
return {"date": toks[0], "ua": toks[1]}
except IndexError:
return {"date": toks[0], "ua": None}
l1Start = ".." ^ uaDateOrSeason
l1Start.addParseAction(f)
l1End = uaDateOrSeason ^ ".."
l1End.addParseAction(f)
level1Interval = Optional(l1Start)("lower") + "/" + l1End("upper") ^ l1Start(
"lower"
) + "/" + Optional(l1End("upper"))
Level1Interval.set_parser(level1Interval)
# (* *** unspecified *** *)
yearWithOneOrTwoUnspecifedDigits = Combine(digit + digit + (digit ^ "X") + "X")("year")
monthUnspecified = year + "-" + L("XX")("month")
dayUnspecified = yearMonth + "-" + L("XX")("day")
dayAndMonthUnspecified = year + "-" + L("XX")("month") + "-" + L("XX")("day")
unspecified = (
yearWithOneOrTwoUnspecifedDigits
^ monthUnspecified
^ dayUnspecified
^ dayAndMonthUnspecified
)
Unspecified.set_parser(unspecified)
# (* *** uncertainOrApproxDate *** *)
uncertainOrApproxDate = date("date") + UASymbol("ua")
UncertainOrApproximate.set_parser(uncertainOrApproxDate)
level1Expression = (
uncertainOrApproxDate ^ unspecified ^ level1Interval ^ longYearSimple ^ season
)
# (* ************************** Level 2 *************************** *)
# (* ** Internal Unspecified** *)
digitOrX = Word(nums + "X", exact=1)
# 2-digit day with at least one 'X' present
dayWithX = Combine(("X" + digitOrX) ^ (digitOrX + "X"))("day")
# 2-digit month with at least one 'X' present
monthWithX = Combine(oneOf("0X 1X") ^ ("X" + digitOrX))("month")
# 4-digit year with at least one 'X' present
yearWithX = Combine(
("X" + digitOrX + digitOrX + digitOrX)
^ (digitOrX + "X" + digitOrX + digitOrX)
^ (digitOrX + digitOrX + "X" + digitOrX)
^ (digitOrX + digitOrX + digitOrX + "X")
)("year")
yearMonthWithX = (Combine(year("") ^ yearWithX(""))("year") + "-" + monthWithX) ^ (
yearWithX + "-" + month
)
monthDayWithX = (Combine(month("") ^ monthWithX(""))("month") + "-" + dayWithX) ^ (
monthWithX + "-" + day
)
yearMonthDayWithX = (
(
yearWithX
+ "-"
+ Combine(month("") ^ monthWithX(""))("month")
+ "-"
+ Combine(day("") ^ dayWithX(""))("day")
)
^ (year + "-" + monthWithX + "-" + Combine(day("") ^ dayWithX(""))("day"))
^ (year + "-" + month + "-" + dayWithX)
)
partialUnspecified = yearWithX ^ yearMonthWithX ^ yearMonthDayWithX
PartialUnspecified.set_parser(partialUnspecified)
# (* ** Internal Uncertain or Approximate** *)
# group qualification
# qualifier right of a component(date, month, day) applies to all components to the left
group_qual = yearMonth + UASymbol("year_month_ua") + "-" + day ^ year + UASymbol(
"year_ua"
) + "-" + month + Opt("-" + day)
# component qualification
# qualifier immediate left of a component (date, month, day) applies to that component only
qual_year = year ^ UASymbol("year_ua_b") + year ^ year + UASymbol("year_ua")
qual_month = month ^ UASymbol("month_ua") + month
qual_day = day ^ UASymbol("day_ua") + day
indi_qual = (
UASymbol("year_ua_b") + year + Opt("-" + qual_month + Opt("-" + qual_day))
^ qual_year + "-" + UASymbol("month_ua") + month + Opt("-" + qual_day)
^ qual_year + "-" + qual_month + "-" + UASymbol("day_ua") + day
)
partialUncertainOrApproximate = group_qual ^ indi_qual
PartialUncertainOrApproximate.set_parser(partialUncertainOrApproximate)
dateWithInternalUncertainty = partialUncertainOrApproximate ^ partialUnspecified
qualifyingString = Regex(r"\S") # any nonwhitespace char
# (* ** SeasonQualified ** *)
seasonQualifier = qualifyingString
seasonQualified = season + "^" + seasonQualifier
# (* ** Long Year - Scientific Form ** *)
longYearScientific = (
"Y"
+ Combine(Optional("-") + positiveInteger)("base")
+ "E"
+ positiveInteger("exponent")
+ Optional(significantDigits)
)
ExponentialYear.set_parser(longYearScientific)
# (* ** level2Interval ** *)
level2Interval = (
(dateOrSeason("lower") + "/" + dateWithInternalUncertainty("upper"))
^ (dateWithInternalUncertainty("lower") + "/" + dateOrSeason("upper"))
^ (
dateWithInternalUncertainty("lower")
+ "/"
+ dateWithInternalUncertainty("upper")
)
)
Level2Interval.set_parser(level2Interval)
# (* ** Inclusive list and choice list** *)
consecutives = (
(yearMonthDay("lower") + ".." + yearMonthDay("upper"))
^ (yearMonth("lower") + ".." + yearMonth("upper"))
^ (
year_basic("lower") + ".." + year_basic("upper")
) # using year_basic because some tests were throwing `'list' object has no attribute 'expandtabs'` - somewhere, pyparsing.parse_string() was being passed a list
)
Consecutives.set_parser(consecutives)
listElement = (
date
^ dateWithInternalUncertainty
^ uncertainOrApproxDate
^ unspecified
^ consecutives
)
earlier = L("..").addParseAction(f)("lower") + date("upper").addParseAction(f)
later = date("lower").addParseAction(f) + L("..").addParseAction(f)("upper")
EarlierConsecutives.set_parser(earlier)
LaterConsecutives.set_parser(later)
listContent = (
(earlier + ZeroOrMore("," + listElement))
^ (Optional(earlier + ",") + ZeroOrMore(listElement + ",") + later)
^ (listElement + OneOrMore("," + listElement))
^ consecutives
)
choiceList = "[" + listContent + "]"
OneOfASet.set_parser(choiceList)
inclusiveList = "{" + listContent + "}"
MultipleDates.set_parser(inclusiveList)
# (* *** L2 Season *** *)
seasonL2Number = oneOf("21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41")
l2season = year + "-" + seasonL2Number("season")
Level2Season.set_parser(l2season)
level2Expression = (
partialUncertainOrApproximate
^ partialUnspecified
^ choiceList
^ inclusiveList
^ level2Interval
^ longYearScientific
^ l2season
^ seasonQualified
)
# putting it all together
edtfParser = (
level0Expression("level0") ^ level1Expression("level1") ^ level2Expression("level2")
)
def parse_edtf(str, parseAll=True, fail_silently=False):
try:
if not str:
raise ParseException("You must supply some input text")
p = edtfParser.parseString(str.strip(), parseAll)
if p:
return p[0]
except ParseException as err:
if fail_silently:
return None
raise EDTFParseException(err) from err