@@ -138,7 +138,7 @@ def evaluate(marker, *, env, strict = True, **kwargs):
138
138
"""
139
139
tokens = tokenize (marker )
140
140
141
- ast = _new_expr (** kwargs )
141
+ ast = _new_expr (marker = marker , ** kwargs )
142
142
for _ in range (len (tokens ) * 2 ):
143
143
if not tokens :
144
144
break
@@ -219,17 +219,20 @@ def _not_fn(x):
219
219
return not x
220
220
221
221
def _new_expr (
222
+ * ,
223
+ marker ,
222
224
and_fn = _and_fn ,
223
225
or_fn = _or_fn ,
224
226
not_fn = _not_fn ):
225
227
# buildifier: disable=uninitialized
226
228
self = struct (
229
+ marker = marker ,
227
230
tree = [],
228
231
parse = lambda ** kwargs : _parse (self , ** kwargs ),
229
232
value = lambda : _value (self ),
230
233
# This is a way for us to have a handle to the currently constructed
231
234
# expression tree branch.
232
- current = lambda : self ._current [0 ] if self ._current else None ,
235
+ current = lambda : self ._current [- 1 ] if self ._current else None ,
233
236
_current = [],
234
237
_and = and_fn ,
235
238
_or = or_fn ,
@@ -393,20 +396,26 @@ def _append(self, value):
393
396
current .tree .append (value )
394
397
elif hasattr (current .tree [- 1 ], "append" ):
395
398
current .tree [- 1 ].append (value )
396
- else :
399
+ elif hasattr ( current . tree , "_append" ) :
397
400
current .tree ._append (value )
401
+ else :
402
+ fail ("Cannot evaluate '{}' in '{}', current: {}" .format (value , self .marker , current ))
398
403
399
404
def _open_parenthesis (self ):
400
405
"""Add an extra node into the tree to perform evaluate inside parenthesis."""
401
406
self ._current .append (_new_expr (
407
+ marker = self .marker ,
402
408
and_fn = self ._and ,
403
409
or_fn = self ._or ,
404
410
not_fn = self ._not ,
405
411
))
406
412
407
413
def _close_parenthesis (self ):
408
414
"""Backtrack and evaluate the expression within parenthesis."""
409
- value = self ._current .pop ().value ()
415
+ current = self .current ()
416
+ value = current .value ()
417
+ self ._current .pop ()
418
+
410
419
if type (value ) == type ("" ):
411
420
return "({})" .format (value )
412
421
else :
0 commit comments