/
parser.py
257 lines (218 loc) · 6.12 KB
/
parser.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
"""
This module makes use of the Python PLY package
to parse the grammars
"""
###
# Implements the lexer
###
import ply.lex as lex
reserved = {
'is': 'IS_EQUALS',
'and': 'AND',
'or': 'OR',
'not': 'NOT',
'contains': 'CONTAINS',
'matches': 'MATCHES',
'true': 'TRUE',
'false': 'FALSE',
'undefined': 'UNDEFINED',
'null': 'NULL',
'empty': 'EMPTY'
}
tokens = (
'AND',
'OR',
'NOT',
'GREATER_THAN',
'GREATER_THAN_EQUALS',
'LESS_THAN',
'LESS_THAN_EQUALS',
'EQUALS',
'DBL_EQUALS',
'NOT_EQUALS',
'IS_EQUALS',
'IS_NOT_EQUALS',
'LPAREN',
'RPAREN',
'LBRACK',
'RBRACK',
'CONTAINS',
'MATCHES',
'NUMBER',
'STRING',
'TRUE',
'FALSE',
'UNDEFINED',
'NULL',
'EMPTY'
)
# Regex rules for tokens
t_GREATER_THAN = r'>'
t_GREATER_THAN_EQUALS = r'>='
t_LESS_THAN = r'<'
t_LESS_THAN_EQUALS = r'<='
t_EQUALS = r'='
t_DBL_EQUALS = r'=='
t_NOT_EQUALS = r'!='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACK = r'{'
t_RBRACK = r'}'
# Ignore any comments
t_ignore_COMMENT = r'\#.*'
def t_NUMBER(t):
r'-?\d+(\.\d+)?'
return t
# Matches either a sequence of non-whitespace
# or anything that is quoted
def t_STRING(t):
r'([\w_\-.:;]+|"[^"]*"|\'[^\']*\'|/[^/]*/[simluSIMLU]*)'
l = t.value.lower()
if l in reserved:
t.value = l
# Check for reserved words
t.type = reserved.get(t.value, 'STRING')
return t
# Track the newlines
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# The ignore characters
t_ignore = ' \t\r'
# Compute column.
# lexer is the lexer instance
# lexpos is the token position
def compute_column(lexer, lexpos):
return lexpos - lexer.lexdata.rfind('\n', 0, lexpos)
# Error handler
def t_error(t):
if " " in t.value:
idx = t.value.index(" ")
error_loc = (t.value[:idx], compute_column(t.lexer, t.lexer.lexpos), t.lexer.lineno)
t.lexer.errors.append(error_loc)
t.lexer.skip(idx)
else:
error_loc = (t.value, compute_column(t.lexer, t.lexer.lexpos), t.lexer.lineno)
t.lexer.errors.append(error_loc)
t.lexer.skip(1)
# Build the lexer
def get_lexer():
"Returns a new instance of the lexer"
l = lex.lex()
l.errors = []
return l
###
# Implements the parser
###
import ply.yacc as yacc
from . import ast
precedence = (
('right', 'AND', 'OR'),
('right', 'NOT'),
)
def p_expression_binop(p):
"""expression : expression AND expression
| expression OR expression"""
p[0] = ast.LogicalOperator(p[2], p[1], p[3])
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_expression_not(p):
"expression : NOT expression"
p[0] = ast.NegateOperator(p[2])
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
def p_expression_term(p):
"expression : term"
p[0] = p[1]
def p_term_is_not(p):
"term : factor IS_EQUALS NOT factor"
p[0] = ast.CompareOperator("!=", p[1], p[4])
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_term_comparison(p):
"""term : factor GREATER_THAN factor
| factor GREATER_THAN_EQUALS factor
| factor LESS_THAN factor
| factor LESS_THAN_EQUALS factor
| factor EQUALS factor
| factor NOT_EQUALS factor
| factor IS_NOT_EQUALS factor
| factor IS_EQUALS factor"""
p[0] = ast.CompareOperator(p[2], p[1], p[3])
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_term_dbl_equals(p):
"term : factor DBL_EQUALS factor"
p[0] = ast.CompareOperator("=", p[1], p[3])
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_contains(p):
"term : factor CONTAINS factor"
p[0] = ast.ContainsOperator(p[1], p[3])
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_matchse(p):
"term : factor MATCHES factor"
p[0] = ast.MatchOperator(p[1], ast.Regex(p[3]))
p[0].set_position(p.lineno(2), compute_column(p.lexer, p.lexpos(2)))
def p_term_factor(p):
"term : factor"
p[0] = p[1]
def p_factor_string(p):
"factor : STRING"
p[0] = ast.Literal(p[1])
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
def p_factor_number(p):
"factor : NUMBER"
p[0] = ast.Number(p[1])
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
def p_factor_constants(p):
"""factor : TRUE
| FALSE
| UNDEFINED
| NULL
| EMPTY"""
if p[1] == "true":
p[0] = ast.Constant(True)
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
elif p[1] == "false":
p[0] = ast.Constant(False)
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
elif p[1] == "null":
p[0] = ast.Constant(None)
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
elif p[1] == "undefined":
p[0] = ast.Undefined()
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
elif p[1] == "empty":
p[0] = ast.Empty()
p[0].set_position(p.lineno(1), compute_column(p.lexer, p.lexpos(1)))
else:
raise SyntaxError
def p_factor_parens(p):
"factor : LPAREN expression RPAREN"
p[0] = p[2]
def p_empty(p):
"empty : "
pass
def p_factor_list(p):
"""factor_list : factor factor_list
| empty"""
if len(p) == 2 and p[1] is None:
p[0] = []
else:
p[0] = [p[1]] + p[2]
def p_factor_sets(p):
"factor : LBRACK factor_list RBRACK"
p[0] = ast.LiteralSet(p[2])
def p_error(p):
"Handles errors"
if p is None:
raise SyntaxError("Unexpected end of predicate!")
else:
err = ("Syntax error at token", p.type, p.value, compute_column(p.lexer, p.lexpos), p.lineno)
parser = p.lexer.parser
parser.errors.append(err)
parser.errok()
def get_parser(lexer=None, debug=0):
"Returns a new instance of the parser"
p = yacc.yacc(debug=debug)
p.errors = []
if lexer:
lexer.parser = p
p.lexer = lexer
return p