omlish 0.0.0.dev45__py3-none-any.whl → 0.0.0.dev47__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omlish/.manifests.json +12 -0
- omlish/__about__.py +2 -2
- omlish/specs/__init__.py +0 -1
- omlish/specs/jmespath/LICENSE +16 -0
- omlish/specs/jmespath/__init__.py +20 -0
- omlish/specs/jmespath/__main__.py +11 -0
- omlish/specs/jmespath/ast.py +90 -0
- omlish/specs/jmespath/cli.py +64 -0
- omlish/specs/jmespath/exceptions.py +116 -0
- omlish/specs/jmespath/functions.py +372 -0
- omlish/specs/jmespath/lexer.py +312 -0
- omlish/specs/jmespath/parser.py +587 -0
- omlish/specs/jmespath/visitor.py +344 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/RECORD +19 -9
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/LICENSE +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,587 @@
|
|
1
|
+
"""
|
2
|
+
Top down operator precedence parser.
|
3
|
+
|
4
|
+
This is an implementation of Vaughan R. Pratt's "Top Down Operator Precedence" parser.
|
5
|
+
(http://dl.acm.org/citation.cfm?doid=512927.512931).
|
6
|
+
|
7
|
+
These are some additional resources that help explain the general idea behind a Pratt parser:
|
8
|
+
|
9
|
+
* http://effbot.org/zone/simple-top-down-parsing.htm
|
10
|
+
* http://javascript.crockford.com/tdop/tdop.html
|
11
|
+
|
12
|
+
A few notes on the implementation.
|
13
|
+
|
14
|
+
* All the nud/led tokens are on the Parser class itself, and are dispatched using getattr(). This keeps all the parsing
|
15
|
+
logic contained to a single class.
|
16
|
+
* We use two passes through the data. One to create a list of token, then one pass through the tokens to create the
|
17
|
+
AST. While the lexer actually yields tokens, we convert it to a list so we can easily implement two tokens of
|
18
|
+
lookahead. A previous implementation used a fixed circular buffer, but it was significantly slower. Also, the
|
19
|
+
average jmespath expression typically does not have a large amount of token so this is not an issue. And
|
20
|
+
interestingly enough, creating a token list first is actually faster than consuming from the token iterator one token
|
21
|
+
at a time.
|
22
|
+
"""
|
23
|
+
import random
|
24
|
+
import typing as ta
|
25
|
+
|
26
|
+
from . import ast
|
27
|
+
from . import exceptions
|
28
|
+
from . import lexer
|
29
|
+
from . import visitor
|
30
|
+
|
31
|
+
|
32
|
+
class Parser:
|
33
|
+
BINDING_POWER: ta.Mapping[str, int] = {
|
34
|
+
'eof': 0,
|
35
|
+
'unquoted_identifier': 0,
|
36
|
+
'quoted_identifier': 0,
|
37
|
+
'literal': 0,
|
38
|
+
'rbracket': 0,
|
39
|
+
'rparen': 0,
|
40
|
+
'comma': 0,
|
41
|
+
'rbrace': 0,
|
42
|
+
'number': 0,
|
43
|
+
'current': 0,
|
44
|
+
'expref': 0,
|
45
|
+
'colon': 0,
|
46
|
+
'pipe': 1,
|
47
|
+
'or': 2,
|
48
|
+
'and': 3,
|
49
|
+
'eq': 5,
|
50
|
+
'gt': 5,
|
51
|
+
'lt': 5,
|
52
|
+
'gte': 5,
|
53
|
+
'lte': 5,
|
54
|
+
'ne': 5,
|
55
|
+
'flatten': 9,
|
56
|
+
# Everything above stops a projection.
|
57
|
+
'star': 20,
|
58
|
+
'filter': 21,
|
59
|
+
'dot': 40,
|
60
|
+
'not': 45,
|
61
|
+
'lbrace': 50,
|
62
|
+
'lbracket': 55,
|
63
|
+
'lparen': 60,
|
64
|
+
}
|
65
|
+
|
66
|
+
# The maximum binding power for a token that can stop a projection.
|
67
|
+
_PROJECTION_STOP = 10
|
68
|
+
|
69
|
+
# The _MAX_SIZE most recent expressions are cached in _CACHE dict.
|
70
|
+
_CACHE: dict = {} # noqa
|
71
|
+
_MAX_SIZE = 128
|
72
|
+
|
73
|
+
def __init__(self, lookahead=2):
|
74
|
+
self.tokenizer = None
|
75
|
+
self._tokens = [None] * lookahead
|
76
|
+
self._buffer_size = lookahead
|
77
|
+
self._index = 0
|
78
|
+
|
79
|
+
def parse(self, expression):
|
80
|
+
cached = self._CACHE.get(expression)
|
81
|
+
if cached is not None:
|
82
|
+
return cached
|
83
|
+
|
84
|
+
parsed_result = self._do_parse(expression)
|
85
|
+
|
86
|
+
self._CACHE[expression] = parsed_result
|
87
|
+
if len(self._CACHE) > self._MAX_SIZE:
|
88
|
+
self._free_cache_entries()
|
89
|
+
|
90
|
+
return parsed_result
|
91
|
+
|
92
|
+
def _do_parse(self, expression):
|
93
|
+
try:
|
94
|
+
return self._parse(expression)
|
95
|
+
|
96
|
+
except exceptions.LexerError as e:
|
97
|
+
e.expression = expression
|
98
|
+
raise
|
99
|
+
|
100
|
+
except exceptions.IncompleteExpressionError as e:
|
101
|
+
e.set_expression(expression)
|
102
|
+
raise
|
103
|
+
|
104
|
+
except exceptions.ParseError as e:
|
105
|
+
e.expression = expression
|
106
|
+
raise
|
107
|
+
|
108
|
+
def _parse(self, expression):
|
109
|
+
self.tokenizer = lexer.Lexer().tokenize(expression)
|
110
|
+
self._tokens = list(self.tokenizer)
|
111
|
+
self._index = 0
|
112
|
+
|
113
|
+
parsed = self._expression(binding_power=0)
|
114
|
+
|
115
|
+
if self._current_token() != 'eof':
|
116
|
+
t = self._lookahead_token(0)
|
117
|
+
raise exceptions.ParseError(
|
118
|
+
t['start'],
|
119
|
+
t['value'],
|
120
|
+
t['type'],
|
121
|
+
f'Unexpected token: {t["value"]}',
|
122
|
+
)
|
123
|
+
|
124
|
+
return ParsedResult(expression, parsed)
|
125
|
+
|
126
|
+
def _expression(self, binding_power=0):
|
127
|
+
left_token = self._lookahead_token(0)
|
128
|
+
|
129
|
+
self._advance()
|
130
|
+
|
131
|
+
nud_function = getattr(
|
132
|
+
self,
|
133
|
+
f'_token_nud_{left_token["type"]}',
|
134
|
+
self._error_nud_token,
|
135
|
+
)
|
136
|
+
|
137
|
+
left = nud_function(left_token)
|
138
|
+
|
139
|
+
current_token = self._current_token()
|
140
|
+
while binding_power < self.BINDING_POWER[current_token]:
|
141
|
+
led = getattr(
|
142
|
+
self,
|
143
|
+
f'_token_led_{current_token}',
|
144
|
+
None,
|
145
|
+
)
|
146
|
+
if led is None:
|
147
|
+
error_token = self._lookahead_token(0)
|
148
|
+
self._error_led_token(error_token)
|
149
|
+
|
150
|
+
else:
|
151
|
+
self._advance()
|
152
|
+
left = led(left)
|
153
|
+
current_token = self._current_token()
|
154
|
+
|
155
|
+
return left
|
156
|
+
|
157
|
+
def _token_nud_literal(self, token):
|
158
|
+
return ast.literal(token['value'])
|
159
|
+
|
160
|
+
def _token_nud_unquoted_identifier(self, token):
|
161
|
+
return ast.field(token['value'])
|
162
|
+
|
163
|
+
def _token_nud_quoted_identifier(self, token):
|
164
|
+
field = ast.field(token['value'])
|
165
|
+
|
166
|
+
# You can't have a quoted identifier as a function name.
|
167
|
+
if self._current_token() == 'lparen':
|
168
|
+
t = self._lookahead_token(0)
|
169
|
+
raise exceptions.ParseError(
|
170
|
+
0,
|
171
|
+
t['value'],
|
172
|
+
t['type'],
|
173
|
+
'Quoted identifier not allowed for function names.',
|
174
|
+
)
|
175
|
+
|
176
|
+
return field
|
177
|
+
|
178
|
+
def _token_nud_star(self, token):
|
179
|
+
left = ast.identity()
|
180
|
+
if self._current_token() == 'rbracket':
|
181
|
+
right = ast.identity()
|
182
|
+
else:
|
183
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['star'])
|
184
|
+
return ast.value_projection(left, right)
|
185
|
+
|
186
|
+
def _token_nud_filter(self, token):
|
187
|
+
return self._token_led_filter(ast.identity())
|
188
|
+
|
189
|
+
def _token_nud_lbrace(self, token):
|
190
|
+
return self._parse_multi_select_hash()
|
191
|
+
|
192
|
+
def _token_nud_lparen(self, token):
|
193
|
+
expression = self._expression()
|
194
|
+
self._match('rparen')
|
195
|
+
return expression
|
196
|
+
|
197
|
+
def _token_nud_flatten(self, token):
|
198
|
+
left = ast.flatten(ast.identity())
|
199
|
+
right = self._parse_projection_rhs(
|
200
|
+
self.BINDING_POWER['flatten'])
|
201
|
+
return ast.projection(left, right)
|
202
|
+
|
203
|
+
def _token_nud_not(self, token):
|
204
|
+
expr = self._expression(self.BINDING_POWER['not'])
|
205
|
+
return ast.not_expression(expr)
|
206
|
+
|
207
|
+
def _token_nud_lbracket(self, token):
|
208
|
+
if self._current_token() in ['number', 'colon']:
|
209
|
+
right = self._parse_index_expression()
|
210
|
+
# We could optimize this and remove the identity() node. We don't really need an index_expression node, we
|
211
|
+
# can just use emit an index node here if we're not dealing with a slice.
|
212
|
+
return self._project_if_slice(ast.identity(), right)
|
213
|
+
|
214
|
+
elif self._current_token() == 'star' and self._lookahead(1) == 'rbracket':
|
215
|
+
self._advance()
|
216
|
+
self._advance()
|
217
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['star'])
|
218
|
+
return ast.projection(ast.identity(), right)
|
219
|
+
|
220
|
+
else:
|
221
|
+
return self._parse_multi_select_list()
|
222
|
+
|
223
|
+
def _parse_index_expression(self):
|
224
|
+
# We're here:
|
225
|
+
# [<current>
|
226
|
+
# ^
|
227
|
+
# | current token
|
228
|
+
if (self._lookahead(0) == 'colon' or self._lookahead(1) == 'colon'):
|
229
|
+
return self._parse_slice_expression()
|
230
|
+
|
231
|
+
else:
|
232
|
+
# Parse the syntax [number]
|
233
|
+
node = ast.index(self._lookahead_token(0)['value'])
|
234
|
+
self._advance()
|
235
|
+
self._match('rbracket')
|
236
|
+
return node
|
237
|
+
|
238
|
+
def _parse_slice_expression(self):
|
239
|
+
# [start:end:step]
|
240
|
+
# Where start, end, and step are optional. The last colon is optional as well.
|
241
|
+
parts = [None, None, None]
|
242
|
+
index = 0
|
243
|
+
current_token = self._current_token()
|
244
|
+
while current_token != 'rbracket' and index < 3: # noqa
|
245
|
+
if current_token == 'colon': # noqa
|
246
|
+
index += 1
|
247
|
+
if index == 3:
|
248
|
+
self._raise_parse_error_for_token(self._lookahead_token(0), 'syntax error')
|
249
|
+
self._advance()
|
250
|
+
|
251
|
+
elif current_token == 'number': # noqa
|
252
|
+
parts[index] = self._lookahead_token(0)['value']
|
253
|
+
self._advance()
|
254
|
+
|
255
|
+
else:
|
256
|
+
self._raise_parse_error_for_token(self._lookahead_token(0), 'syntax error')
|
257
|
+
|
258
|
+
current_token = self._current_token()
|
259
|
+
|
260
|
+
self._match('rbracket')
|
261
|
+
return ast.slice(*parts)
|
262
|
+
|
263
|
+
def _token_nud_current(self, token):
|
264
|
+
return ast.current_node()
|
265
|
+
|
266
|
+
def _token_nud_expref(self, token):
|
267
|
+
expression = self._expression(self.BINDING_POWER['expref'])
|
268
|
+
return ast.expref(expression)
|
269
|
+
|
270
|
+
def _token_led_dot(self, left):
|
271
|
+
if self._current_token() != 'star':
|
272
|
+
right = self._parse_dot_rhs(self.BINDING_POWER['dot'])
|
273
|
+
if left['type'] == 'subexpression':
|
274
|
+
left['children'].append(right)
|
275
|
+
return left
|
276
|
+
|
277
|
+
else:
|
278
|
+
return ast.subexpression([left, right])
|
279
|
+
|
280
|
+
else:
|
281
|
+
# We're creating a projection.
|
282
|
+
self._advance()
|
283
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['dot'])
|
284
|
+
return ast.value_projection(left, right)
|
285
|
+
|
286
|
+
def _token_led_pipe(self, left):
|
287
|
+
right = self._expression(self.BINDING_POWER['pipe'])
|
288
|
+
return ast.pipe(left, right)
|
289
|
+
|
290
|
+
def _token_led_or(self, left):
|
291
|
+
right = self._expression(self.BINDING_POWER['or'])
|
292
|
+
return ast.or_expression(left, right)
|
293
|
+
|
294
|
+
def _token_led_and(self, left):
|
295
|
+
right = self._expression(self.BINDING_POWER['and'])
|
296
|
+
return ast.and_expression(left, right)
|
297
|
+
|
298
|
+
def _token_led_lparen(self, left):
|
299
|
+
if left['type'] != 'field':
|
300
|
+
# 0 - first func arg or closing paren.
|
301
|
+
# -1 - '(' token
|
302
|
+
# -2 - invalid function "name".
|
303
|
+
prev_t = self._lookahead_token(-2)
|
304
|
+
raise exceptions.ParseError(
|
305
|
+
prev_t['start'],
|
306
|
+
prev_t['value'],
|
307
|
+
prev_t['type'],
|
308
|
+
f"Invalid function name '{prev_t['value']}'",
|
309
|
+
)
|
310
|
+
|
311
|
+
name = left['value']
|
312
|
+
args = []
|
313
|
+
while self._current_token() != 'rparen':
|
314
|
+
expression = self._expression()
|
315
|
+
if self._current_token() == 'comma':
|
316
|
+
self._match('comma')
|
317
|
+
args.append(expression)
|
318
|
+
self._match('rparen')
|
319
|
+
|
320
|
+
function_node = ast.function_expression(name, args)
|
321
|
+
return function_node
|
322
|
+
|
323
|
+
def _token_led_filter(self, left):
|
324
|
+
# Filters are projections.
|
325
|
+
condition = self._expression(0)
|
326
|
+
self._match('rbracket')
|
327
|
+
if self._current_token() == 'flatten':
|
328
|
+
right = ast.identity()
|
329
|
+
else:
|
330
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['filter'])
|
331
|
+
return ast.filter_projection(left, right, condition)
|
332
|
+
|
333
|
+
def _token_led_eq(self, left):
|
334
|
+
return self._parse_comparator(left, 'eq')
|
335
|
+
|
336
|
+
def _token_led_ne(self, left):
|
337
|
+
return self._parse_comparator(left, 'ne')
|
338
|
+
|
339
|
+
def _token_led_gt(self, left):
|
340
|
+
return self._parse_comparator(left, 'gt')
|
341
|
+
|
342
|
+
def _token_led_gte(self, left):
|
343
|
+
return self._parse_comparator(left, 'gte')
|
344
|
+
|
345
|
+
def _token_led_lt(self, left):
|
346
|
+
return self._parse_comparator(left, 'lt')
|
347
|
+
|
348
|
+
def _token_led_lte(self, left):
|
349
|
+
return self._parse_comparator(left, 'lte')
|
350
|
+
|
351
|
+
def _token_led_flatten(self, left):
|
352
|
+
left = ast.flatten(left)
|
353
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['flatten'])
|
354
|
+
return ast.projection(left, right)
|
355
|
+
|
356
|
+
def _token_led_lbracket(self, left):
|
357
|
+
token = self._lookahead_token(0)
|
358
|
+
if token['type'] in ['number', 'colon']:
|
359
|
+
right = self._parse_index_expression()
|
360
|
+
if left['type'] == 'index_expression':
|
361
|
+
# Optimization: if the left node is an index expr, we can avoid creating another node and instead just
|
362
|
+
# add the right node as a child of the left.
|
363
|
+
left['children'].append(right)
|
364
|
+
return left
|
365
|
+
|
366
|
+
else:
|
367
|
+
return self._project_if_slice(left, right)
|
368
|
+
|
369
|
+
else:
|
370
|
+
# We have a projection
|
371
|
+
self._match('star')
|
372
|
+
self._match('rbracket')
|
373
|
+
right = self._parse_projection_rhs(self.BINDING_POWER['star'])
|
374
|
+
return ast.projection(left, right)
|
375
|
+
|
376
|
+
def _project_if_slice(self, left, right):
|
377
|
+
index_expr = ast.index_expression([left, right])
|
378
|
+
if right['type'] == 'slice':
|
379
|
+
return ast.projection(
|
380
|
+
index_expr,
|
381
|
+
self._parse_projection_rhs(self.BINDING_POWER['star']),
|
382
|
+
)
|
383
|
+
else:
|
384
|
+
return index_expr
|
385
|
+
|
386
|
+
def _parse_comparator(self, left, comparator):
|
387
|
+
right = self._expression(self.BINDING_POWER[comparator])
|
388
|
+
return ast.comparator(comparator, left, right)
|
389
|
+
|
390
|
+
def _parse_multi_select_list(self):
|
391
|
+
expressions = []
|
392
|
+
while True:
|
393
|
+
expression = self._expression()
|
394
|
+
expressions.append(expression)
|
395
|
+
if self._current_token() == 'rbracket':
|
396
|
+
break
|
397
|
+
else:
|
398
|
+
self._match('comma')
|
399
|
+
self._match('rbracket')
|
400
|
+
return ast.multi_select_list(expressions)
|
401
|
+
|
402
|
+
def _parse_multi_select_hash(self):
|
403
|
+
pairs = []
|
404
|
+
while True:
|
405
|
+
key_token = self._lookahead_token(0)
|
406
|
+
|
407
|
+
# Before getting the token value, verify it's an identifier.
|
408
|
+
self._match_multiple_tokens(token_types=['quoted_identifier', 'unquoted_identifier'])
|
409
|
+
key_name = key_token['value']
|
410
|
+
|
411
|
+
self._match('colon')
|
412
|
+
value = self._expression(0)
|
413
|
+
|
414
|
+
node = ast.key_val_pair(key_name=key_name, node=value)
|
415
|
+
|
416
|
+
pairs.append(node)
|
417
|
+
if self._current_token() == 'comma':
|
418
|
+
self._match('comma')
|
419
|
+
|
420
|
+
elif self._current_token() == 'rbrace':
|
421
|
+
self._match('rbrace')
|
422
|
+
break
|
423
|
+
|
424
|
+
return ast.multi_select_dict(nodes=pairs)
|
425
|
+
|
426
|
+
def _parse_projection_rhs(self, binding_power):
|
427
|
+
# Parse the right hand side of the projection.
|
428
|
+
if self.BINDING_POWER[self._current_token()] < self._PROJECTION_STOP:
|
429
|
+
# BP of 10 are all the tokens that stop a projection.
|
430
|
+
right = ast.identity()
|
431
|
+
|
432
|
+
elif self._current_token() == 'lbracket':
|
433
|
+
right = self._expression(binding_power)
|
434
|
+
|
435
|
+
elif self._current_token() == 'filter':
|
436
|
+
right = self._expression(binding_power)
|
437
|
+
|
438
|
+
elif self._current_token() == 'dot':
|
439
|
+
self._match('dot')
|
440
|
+
right = self._parse_dot_rhs(binding_power)
|
441
|
+
|
442
|
+
else:
|
443
|
+
self._raise_parse_error_for_token(self._lookahead_token(0), 'syntax error')
|
444
|
+
|
445
|
+
return right
|
446
|
+
|
447
|
+
def _parse_dot_rhs(self, binding_power):
|
448
|
+
# From the grammar:
|
449
|
+
# expression '.' ( identifier /
|
450
|
+
# multi-select-list /
|
451
|
+
# multi-select-hash /
|
452
|
+
# function-expression /
|
453
|
+
# *
|
454
|
+
# In terms of tokens that means that after a '.', you can have:
|
455
|
+
lookahead = self._current_token()
|
456
|
+
|
457
|
+
# Common case "foo.bar", so first check for an identifier.
|
458
|
+
if lookahead in ['quoted_identifier', 'unquoted_identifier', 'star']:
|
459
|
+
return self._expression(binding_power)
|
460
|
+
|
461
|
+
elif lookahead == 'lbracket':
|
462
|
+
self._match('lbracket')
|
463
|
+
return self._parse_multi_select_list()
|
464
|
+
|
465
|
+
elif lookahead == 'lbrace':
|
466
|
+
self._match('lbrace')
|
467
|
+
return self._parse_multi_select_hash()
|
468
|
+
|
469
|
+
else:
|
470
|
+
t = self._lookahead_token(0)
|
471
|
+
allowed = ['quoted_identifier', 'unquoted_identifier', 'lbracket', 'lbrace']
|
472
|
+
msg = f'Expecting: {allowed}, got: {t["type"]}'
|
473
|
+
self._raise_parse_error_for_token(t, msg)
|
474
|
+
raise RuntimeError # noqa
|
475
|
+
|
476
|
+
def _error_nud_token(self, token):
|
477
|
+
if token['type'] == 'eof':
|
478
|
+
raise exceptions.IncompleteExpressionError(
|
479
|
+
token['start'],
|
480
|
+
token['value'],
|
481
|
+
token['type'],
|
482
|
+
)
|
483
|
+
|
484
|
+
self._raise_parse_error_for_token(token, 'invalid token')
|
485
|
+
|
486
|
+
def _error_led_token(self, token):
|
487
|
+
self._raise_parse_error_for_token(token, 'invalid token')
|
488
|
+
|
489
|
+
def _match(self, token_type=None):
|
490
|
+
# inline'd self._current_token()
|
491
|
+
if self._current_token() == token_type:
|
492
|
+
# inline'd self._advance()
|
493
|
+
self._advance()
|
494
|
+
else:
|
495
|
+
self._raise_parse_error_maybe_eof(token_type, self._lookahead_token(0))
|
496
|
+
|
497
|
+
def _match_multiple_tokens(self, token_types):
|
498
|
+
if self._current_token() not in token_types:
|
499
|
+
self._raise_parse_error_maybe_eof(token_types, self._lookahead_token(0))
|
500
|
+
self._advance()
|
501
|
+
|
502
|
+
def _advance(self):
|
503
|
+
self._index += 1
|
504
|
+
|
505
|
+
def _current_token(self):
|
506
|
+
return self._tokens[self._index]['type'] # type: ignore
|
507
|
+
|
508
|
+
def _lookahead(self, number):
|
509
|
+
return self._tokens[self._index + number]['type'] # noqa
|
510
|
+
|
511
|
+
def _lookahead_token(self, number):
|
512
|
+
return self._tokens[self._index + number]
|
513
|
+
|
514
|
+
def _raise_parse_error_for_token(self, token, reason) -> ta.NoReturn:
|
515
|
+
lex_position = token['start']
|
516
|
+
actual_value = token['value']
|
517
|
+
actual_type = token['type']
|
518
|
+
raise exceptions.ParseError(
|
519
|
+
lex_position,
|
520
|
+
actual_value,
|
521
|
+
actual_type,
|
522
|
+
reason,
|
523
|
+
)
|
524
|
+
|
525
|
+
def _raise_parse_error_maybe_eof(self, expected_type, token):
|
526
|
+
lex_position = token['start']
|
527
|
+
actual_value = token['value']
|
528
|
+
actual_type = token['type']
|
529
|
+
if actual_type == 'eof':
|
530
|
+
raise exceptions.IncompleteExpressionError(
|
531
|
+
lex_position,
|
532
|
+
actual_value,
|
533
|
+
actual_type,
|
534
|
+
)
|
535
|
+
|
536
|
+
message = f'Expecting: {expected_type}, got: {actual_type}'
|
537
|
+
raise exceptions.ParseError(
|
538
|
+
lex_position,
|
539
|
+
actual_value,
|
540
|
+
actual_type,
|
541
|
+
message,
|
542
|
+
)
|
543
|
+
|
544
|
+
def _free_cache_entries(self):
|
545
|
+
for key in random.sample(list(self._CACHE.keys()), int(self._MAX_SIZE / 2)):
|
546
|
+
self._CACHE.pop(key, None)
|
547
|
+
|
548
|
+
@classmethod
|
549
|
+
def purge(cls):
|
550
|
+
"""Clear the expression compilation cache."""
|
551
|
+
|
552
|
+
cls._CACHE.clear()
|
553
|
+
|
554
|
+
|
555
|
+
class ParsedResult:
|
556
|
+
def __init__(self, expression, parsed):
|
557
|
+
self.expression = expression
|
558
|
+
self.parsed = parsed
|
559
|
+
|
560
|
+
def search(self, value, options=None):
|
561
|
+
interpreter = visitor.TreeInterpreter(options)
|
562
|
+
result = interpreter.visit(self.parsed, value)
|
563
|
+
return result
|
564
|
+
|
565
|
+
def _render_dot_file(self):
|
566
|
+
"""
|
567
|
+
Render the parsed AST as a dot file.
|
568
|
+
|
569
|
+
Note that this is marked as an internal method because the AST is an implementation detail and is subject to
|
570
|
+
change. This method can be used to help troubleshoot or for development purposes, but is not considered part of
|
571
|
+
the public supported API. Use at your own risk.
|
572
|
+
"""
|
573
|
+
|
574
|
+
renderer = visitor.GraphvizVisitor()
|
575
|
+
contents = renderer.visit(self.parsed)
|
576
|
+
return contents
|
577
|
+
|
578
|
+
def __repr__(self):
|
579
|
+
return repr(self.parsed)
|
580
|
+
|
581
|
+
|
582
|
+
def compile(expression): # noqa
|
583
|
+
return Parser().parse(expression)
|
584
|
+
|
585
|
+
|
586
|
+
def search(expression, data, options=None):
|
587
|
+
return Parser().parse(expression).search(data, options=options)
|