-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
142 lines (109 loc) · 3.46 KB
/
parser.py
File metadata and controls
142 lines (109 loc) · 3.46 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
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
import re
from sly import Parser
from cf_cell_methods.lexer import CfcmLexer
from cf_cell_methods.representation import (
CellMethods, CellMethod, Method, ExtraInfo, SxiInterval,
)
class CfcmParser(Parser):
# Get the token list from the lexer (required)
tokens = CfcmLexer.tokens
# Grammar rules and actions
start = 'cell_methods'
# Start symbol
@_("cell_methods cell_method")
def cell_methods(self, p):
return p.cell_methods + CellMethods([p.cell_method])
@_("cell_method")
def cell_methods(self, p):
return CellMethods([p.cell_method])
# Cell method
@_("NAME COLON method opt_where_clause opt_over_clause opt_extra_info")
def cell_method(self, p):
return CellMethod(
name=p.NAME,
method=p.method,
where=p.opt_where_clause,
over=p.opt_over_clause,
extra_info=p.opt_extra_info,
)
@_("NAME COLON method opt_within_clause opt_extra_info")
def cell_method(self, p):
return CellMethod(
name=p.NAME,
method=p.method,
within=p.opt_within_clause,
extra_info=p.opt_extra_info,
)
# Method (with optional params)
@_("NAME opt_params")
def method(self, p):
return Method(p.NAME, p.opt_params)
@_("params")
def opt_params(self, p):
return p.params
@_("empty")
def opt_params(self, p):
return None
@_("LBRACKET param_list RBRACKET")
def params(self, p):
return p.param_list
@_("param_list COMMA param")
def param_list(self, p):
return p.param_list + (p.param,)
@_("param")
def param_list(self, p):
return (p.param,)
@_("NUM")
def param(self, p):
return p.NUM
# Statistics applying to portions of cells (`where`, `over`)
@_("WHERE NAME")
def opt_where_clause(self, p):
return p.NAME
@_("empty")
def opt_where_clause(self, p):
return None
@_("OVER NAME")
def opt_over_clause(self, p):
return p.NAME
@_("empty")
def opt_over_clause(self, p):
return None
@_("WITHIN NAME")
def opt_within_clause(self, p):
return p.NAME
@_("empty")
def opt_within_clause(self, p):
return None
# Extra method information
@_("extra_info")
def opt_extra_info(self, p):
return p.extra_info
@_("empty")
def opt_extra_info(self, p):
return None
# This is our workaround for the fact that the cell_methods grammar is
# not quite context-free due to the format for "extra information." We treat
# extra information like a quoted string, but the "quotes" are matching
# parentheses. The string between the parentheses is then parsed separately.
@_("EXTRA_INFO")
def extra_info(self, p):
# TODO: It would be better to parse this little sub-language with a real
# parser. regex FTW :-P
match = re.match(
r"(?P<interval>\s*interval:\s+(?P<value>\d+(\.\d+)?)\s+"
r"(?P<unit>\w+)(\s+comment: )?)?"
r"(?P<comment>.*)",
p.EXTRA_INFO
)
standardized = (
match.group('interval') and
SxiInterval(float(match.group('value')), match.group('unit'))
)
non_standardized = match.group('comment') or None
return ExtraInfo(standardized, non_standardized)
# Helper symbols
@_("")
def empty(self, p):
pass
parser = CfcmParser()