-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransformer.py
More file actions
42 lines (32 loc) · 1.47 KB
/
transformer.py
File metadata and controls
42 lines (32 loc) · 1.47 KB
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
from lark import Transformer, Tree
from undate import Undate, Calendar
class GregorianDateTransformer(Transformer):
"""Transform a Gregorian date parse tree and return an Undate."""
# Currently parser should not result in intervals
calendar = Calendar.GREGORIAN
def gregorian_date(self, items):
parts = {}
for child in items:
if child.data in ["year", "month", "day"]:
# in each case we expect one integer value;
# anonymous tokens convert to their value and cast as int
value = int(child.children[0])
parts[str(child.data)] = value
# initialize and return an undate with year, month, day and
# Gregorian calendar
return Undate(**parts, calendar=self.calendar)
def year(self, items):
# combine multiple parts into a single string
value = "".join([str(i) for i in items])
return Tree(data="year", children=[value])
def month(self, items):
# month has a nested tree for the rule and the value
# the name of the rule (month_1, month_2, etc) gives us the
# number of the month needed for converting the date
tree = items[0]
month_n = tree.data.split("_")[-1]
return Tree(data="month", children=[month_n])
def day(self, items):
# combine multiple parts into a single string
value = "".join([str(i) for i in items])
return Tree(data="day", children=[value])