-
Notifications
You must be signed in to change notification settings - Fork 47
/
parsing.py
69 lines (44 loc) · 1.43 KB
/
parsing.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
from pyparsing import Keyword, Word, alphanums
class ConditionOperand(object):
def __init__(self, t, check_cond_fn):
self.label = t[0]
self.check_condition_fn = check_cond_fn
assert self.check_condition_fn is not None, 'The check_condition_fn should should be set'
assert callable(self.check_condition_fn), 'The check_condition_fn should should be callable'
def __bool__(self):
return self.check_condition_fn(self.label)
def __str__(self):
return self.label
__repr__ = __str__
__nonzero__ = __bool__
class BoolBinOp(object):
def __init__(self, t):
self.args = t[0][0::2]
def __str__(self):
sep = " %s " % self.reprsymbol
return "(" + sep.join(map(str, self.args)) + ")"
def __bool__(self):
return self.evalop(bool(a) for a in self.args)
__nonzero__ = __bool__
__repr__ = __str__
class BoolAnd(BoolBinOp):
reprsymbol = '&'
evalop = all
class BoolOr(BoolBinOp):
reprsymbol = '|'
evalop = any
class BoolNot(object):
def __init__(self, t):
self.arg = t[0][1]
def __bool__(self):
v = bool(self.arg)
return not v
def __str__(self):
return "~" + str(self.arg)
__repr__ = __str__
__nonzero__ = __bool__
TRUE = Keyword("True")
FALSE = Keyword("False")
class BoolOperand(object):
def __new__(cls):
return TRUE | FALSE | Word(alphanums + '_:.*', max=256)