Exemple #1
0
 def test_le(self):
     result = self.parser.parse("1 <= 2")[0]
     want = LE(Const(1), Const(2))
     self.assertEqual(result, want)
Exemple #2
0
 def test_mul(self):
     result = self.parser.parse("1 * 1")[0]
     want = Mul(Const(1), Const(1))
     self.assertEqual(result, want)
Exemple #3
0
 def test_plusmul(self):
     result = self.parser.parse("1 + 1 * 3")[0]
     want = Add(Const(1), Mul(Const(1), Const(3)))
     self.assertEqual(result, want)
Exemple #4
0
 def test_not_true(self):
     result = self.parser.parse("not True")[0]
     want = Not(Const(True))
     self.assertEqual(result, want)
Exemple #5
0
 def test_plusplus(self):
     result = self.parser.parse("1 + 1 + 3")[0]
     want = Add(Add(Const(1), Const(1)), Const(3))
     self.assertEqual(result, want)
Exemple #6
0
 def test_false(self):
     result = self.parser.parse("False")[0]
     want = Const(False)
     self.assertEqual(result, want)
Exemple #7
0
 def test_not_number(self):
     result = self.parser.parse("not 123")[0]
     want = Not(Const(123))
     self.assertEqual(result, want)
Exemple #8
0
 def test_and(self):
     result = self.parser.parse("True and True")[0]
     want = And(Const(True), Const(True))
     self.assertEqual(result, want)
Exemple #9
0
 def test_or(self):
     result = self.parser.parse("False or True")[0]
     want = Or(Const(False), Const(True))
     self.assertEqual(result, want)
Exemple #10
0
 def test_ne(self):
     result = self.parser.parse("2 != 1")[0]
     want = NE(Const(2), Const(1))
     self.assertEqual(result, want)
Exemple #11
0
 def test_plusnediv(self):
     result = self.parser.parse("2 + 2 != 8 / 2")[0]
     want = NE(Add(Const(2), Const(2)), Div(Const(8), Const(2)))
     self.assertEqual(result, want)
Exemple #12
0
 def test_eq(self):
     result = self.parser.parse("2 == 2")[0]
     want = EQ(Const(2), Const(2))
     self.assertEqual(result, want)
Exemple #13
0
 def test_gtstring(self):
     result = self.parser.parse("'foo and bar' lt 'baz'")[0]
     want = LT(Const('foo and bar'), Const('baz'))
     self.assertEqual(result, want)
Exemple #14
0
 def test_gt(self):
     result = self.parser.parse("2 > 1")[0]
     want = GT(Const(2), Const(1))
     self.assertEqual(result, want)
Exemple #15
0
 def test_bool(self):
     result = self.parser.parse("bool(1)")[0]
     want = Call(functions['bool'], Const(1))
     self.assertEqual(result, want)
Exemple #16
0
 def test_eqandgt(self):
     result = self.parser.parse("2 == 2 and 8 > 2")[0]
     want = And(EQ(Const(2), Const(2)), GT(Const(8), Const(2)))
     self.assertEqual(result, want)
Exemple #17
0
 def test_true(self):
     result = self.parser.parse("True")[0]
     want = Const(True)
     self.assertEqual(result, want)
Exemple #18
0
 def test_string(self):
     result = self.parser.parse("'xyz'")[0]
     want = Const("xyz")
     self.assertEqual(result, want)
Exemple #19
0
 def test_listing(self):
     result = self.parser.parse("[1,2,3]")[0]
     want = List([Const(1), Const(2), Const(3)])
     self.assertEqual(result, want)
Exemple #20
0
 def test_eqorgt(self):
     result = self.parser.parse("2 != 2 or 8 > 2")[0]
     want = Or(NE(Const(2), Const(2)), GT(Const(8), Const(2)))
     self.assertEqual(result, want)
Exemple #21
0
 def test_not_string(self):
     result = self.parser.parse("not 'xyz'")[0]
     want = Not(Const("xyz"))
     self.assertEqual(result, want)
Exemple #22
0
 def test_notand(self):
     result = self.parser.parse("not False and True")[0]
     want = And(Not(Const(False)), Const(True))
     self.assertEqual(result, want)
Exemple #23
0
 def test_not_false(self):
     result = self.parser.parse("not False")[0]
     want = Not(Const(False))
     self.assertEqual(result, want)
Exemple #24
0
 def test_in(self):
     result = self.parser.parse("'foo' in ['foo','bar']")[0]
     want = In(Const('foo'), List([Const('foo'), Const('bar')]))
     self.assertEqual(result, want)
Exemple #25
0
 def test_minus(self):
     result = self.parser.parse("1 - 1")[0]
     want = Sub(Const(1), Const(1))
     self.assertEqual(result, want)
Exemple #26
0
 def test_date_today(self):
     result = self.parser.parse("date('today')")[0]
     want = Call(functions['date'], Const('today'))
     self.assertEqual(result, want)
Exemple #27
0
 def test_div(self):
     result = self.parser.parse("1 / 1")[0]
     want = Div(Const(1), Const(1))
     self.assertEqual(result, want)
Exemple #28
0
 def test_varlttoday(self):
     result = self.parser.parse("$xxx < date('today')")[0]
     want = LT(Variable("xxx"), Call(functions['date'], Const('today')))
     self.assertEqual(result, want)
Exemple #29
0
    ('not', 1, opAssoc.RIGHT, _make_unary),
    (oneOf('* /'), 2, opAssoc.LEFT, _make_binary),
    (oneOf('+ -'), 2, opAssoc.LEFT, _make_binary),
    (oneOf('> gt >= ge < lt <= le != ne == eq'), 2, opAssoc.LEFT,
     _make_binary),
    ('and', 2, opAssoc.LEFT, _make_binary),
    ('or', 2, opAssoc.LEFT, _make_binary),
    ('in', 2, opAssoc.LEFT, _make_binary),
])
dellist = delimitedList(Optional(atom))
listing = lbr.suppress() + dellist + rbr.suppress()
function = identifier.setResultsName('name') + lpar.suppress() + Group(
    Optional(delimitedList(atom))).setResultsName("args") + rpar.suppress()
atom << (listing | number | string | variable | true | false | none | function)

_false = Const(False)
_true = Const(True)

number.setParseAction(lambda t: Const(_number(t[0])))
variable.setParseAction(lambda t: Variable(t[0].strip("$")))
string.setParseAction(lambda t: Const(_str(t[0])))
none.setParseAction(lambda t: _false)
false.setParseAction(lambda t: _false)
true.setParseAction(lambda t: _true)
dellist.setParseAction(lambda s, l, t: List(t[:]))
function.setParseAction(_make_func)
atom.setParseAction(lambda s, l, t: t[0])


class Parser(object):
    """Parser class for python expression."""
Exemple #30
0
 def test_number(self):
     result = self.parser.parse("1")[0]
     want = Const(1)
     self.assertEqual(result, want)