-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmoment_ast.py
143 lines (113 loc) · 3.23 KB
/
moment_ast.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
import arrow
class ASTSyntaxError(Exception):
def __init__(self, args, errorIdx):
self.args = args
self.errorIdx = errorIdx
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def is_shift(s):
if s[0] != '+' and s[0] != '-':
return False
return is_number(s[1:])
timeKeywords = {
'timezone': 'timezone',
'tz': 'timezone',
'year': 'year',
'years': 'year',
'y': 'year',
'month': 'month',
'months': 'month',
'M': 'month',
'day': 'day',
'days': 'day',
'd': 'day',
'hour': 'hour',
'hours': 'hour',
'h': 'hour',
'minute': 'minute',
'minutes': 'minute',
'm': 'minute',
'second': 'second',
'seconds': 'second',
's': 'second',
'microsecond': 'microsecond',
'microseconds': 'microsecond',
'ms': 'microsecond'
}
def is_time_keywords(s):
return s in timeKeywords
def syntax_error(args, idx):
raise ASTSyntaxError(args, idx)
KEYWORD_SET = 'set'
KEYWORD_TO = 'to'
KEYWORD_START = 'start'
KEYWORD_OF = 'of'
KEYWORD_END = 'end'
KEYWORD_FORMAT = 'format'
STATUS_INIT = 0
STATUS_SHIFT = 1
STATUS_SET = 2
STATUS_SET_UNIT = 3
STATUS_START = 4
STATUS_END = 5
STATUS_OF = 6
def parse(args):
ast = []
status = STATUS_INIT
for idx, query in enumerate(args):
if is_shift(query):
if status == STATUS_SET_UNIT:
ast[-1].append(float(query))
status = STATUS_INIT
continue
elif status != STATUS_INIT:
syntax_error(args, idx)
ast.append(['shift', float(query)])
status = STATUS_SHIFT
elif is_time_keywords(query):
if status == STATUS_SHIFT:
ast[-1].append(timeKeywords[query])
status = STATUS_INIT
elif status == STATUS_SET:
ast[-1].append(timeKeywords[query])
status = STATUS_SET_UNIT
elif status == STATUS_OF:
ast[-1].append(timeKeywords[query])
status = STATUS_INIT
else:
syntax_error(args, idx)
elif is_number(query):
if status == STATUS_INIT:
ast.append(['reset', float(query)])
elif status == STATUS_SET_UNIT:
ast[-1].append(float(query))
status = STATUS_INIT
elif query == KEYWORD_SET:
if status != STATUS_INIT:
syntax_error(args, idx)
ast.append(['set'])
status = STATUS_SET
elif query == KEYWORD_START:
ast.append(['start_of'])
status = STATUS_START
elif query == KEYWORD_END:
ast.append(['end_of'])
status = STATUS_END
elif query == KEYWORD_OF:
if status != STATUS_START and status != STATUS_END:
syntax_error(args, idx)
status = STATUS_OF
elif query == KEYWORD_FORMAT:
if status != STATUS_INIT:
syntax_error(args, idx)
ast.append(['format'] + args[idx + 1:])
break
else:
syntax_error(args, idx)
if status != STATUS_INIT:
syntax_error(args, -1)
return ast