Mercurial > hg > expressionparser
annotate expr.py @ 1:c45135ec8c13
slight cleanup
author | Ted Mielczarek <ted.mielczarek@gmail.com> |
---|---|
date | Wed, 01 Jun 2011 20:00:51 -0400 |
parents | ae57e69e4b15 |
children | 94a293b914af |
rev | line source |
---|---|
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
2 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
3 import re, unittest |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
4 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
5 # ideas taken from http://effbot.org/zone/simple-top-down-parsing.htm |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
6 # token classes |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
7 class ident_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
8 def __init__(self, value): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
9 self.value = value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
10 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
11 # identifiers take their value from the value mappings passed |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
12 # to the parser |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
13 return parser.value(self.value) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
14 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
15 class int_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
16 def __init__(self, value): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
17 self.value = int(value) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
18 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
19 return self.value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
20 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
21 class bool_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
22 def __init__(self, value): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
23 self.value = {'true':True, 'false':False}[value] |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
24 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
25 return self.value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
26 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
27 class eq_op_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
28 "==" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
29 lbp = 20 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
30 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
31 return left == parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
32 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
33 class neq_op_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
34 "!=" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
35 lbp = 20 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
36 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
37 return left != parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
38 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
39 class and_op_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
40 "&&" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
41 lbp = 11 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
42 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
43 right = parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
44 return left and right |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
45 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
46 class or_op_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
47 "||" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
48 lbp = 10 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
49 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
50 right = parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
51 return left or right |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
52 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
53 class lparen_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
54 "(" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
55 lbp = 50 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
56 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
57 expr = parser.expression() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
58 parser.advance(rparen_token) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
59 return expr |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
60 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
61 class rparen_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
62 ")" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
63 lbp = 0 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
64 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
65 class string_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
66 def __init__(self, value): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
67 self.value = value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
68 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
69 return self.value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
70 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
71 class end_token: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
72 # lowest left binding power, always ends parsing |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
73 lbp = 0 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
74 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
75 class ExpressionParser(object): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
76 def __init__(self, text, valuemapping): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
77 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
78 Initialize the parser with input |text|, and |valuemapping| as |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
79 a dict mapping identifier names to values. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
80 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
81 self.text = text |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
82 self.valuemapping = valuemapping |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
83 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
84 def _tokenize(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
85 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
86 Lex the input text into tokens and yield them in sequence. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
87 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
88 # scanner callbacks |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
89 def bool_(scanner, t): return bool_token(t) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
90 def identifier(scanner, t): return ident_token(t) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
91 def integer(scanner, t): return int_token(t) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
92 def eq(scanner, t): return eq_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
93 def neq(scanner, t): return neq_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
94 def or_(scanner, t): return or_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
95 def and_(scanner, t): return and_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
96 def lparen(scanner, t): return lparen_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
97 def rparen(scanner, t): return rparen_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
98 def string_(scanner, t): return string_token(t[1:-1]) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
99 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
100 scanner = re.Scanner([ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
101 (r"true|false", bool_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
102 (r"[a-zA-Z_]\w*", identifier), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
103 (r"[0-9]+", integer), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
104 (r'"[^"]*"', string_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
105 (r"==", eq), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
106 (r"!=", neq), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
107 (r"\|\|", or_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
108 (r"&&", and_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
109 (r"\(", lparen), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
110 (r"\)", rparen), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
111 (r"\s+", None), # skip whitespace |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
112 ]) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
113 tokens, remainder = scanner.scan(self.text) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
114 for t in tokens: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
115 yield t |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
116 yield end_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
117 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
118 def value(self, ident): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
119 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
120 Look up the value of |ident| in the value mapping passed in the |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
121 constructor. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
122 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
123 return self.valuemapping[ident] |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
124 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
125 def advance(self, expected): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
126 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
127 Assert that the next token is an instance of |expected|, and advance |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
128 to the next token. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
129 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
130 if not isinstance(self.token, expected): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
131 raise Exception, "Unexpected token!" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
132 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
133 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
134 def expression(self, rbp=0): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
135 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
136 Parse and return the value of an expression until a token with |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
137 right binding power greater than rbp is encountered. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
138 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
139 t = self.token |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
140 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
141 left = t.nud(self) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
142 while rbp < self.token.lbp: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
143 t = self.token |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
144 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
145 left = t.led(self, left) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
146 return left |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
147 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
148 def parse(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
149 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
150 Parse and return the value of the expression in the text |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
151 passed to the constructor. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
152 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
153 self.iter = self._tokenize() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
154 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
155 return self.expression() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
156 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
157 class ExpressionParserUnittest(unittest.TestCase): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
158 def parse(self, text, values): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
159 return ExpressionParser(text, values).parse() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
160 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
161 def test_BasicValues(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
162 self.assertEqual(1, self.parse("1", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
163 self.assertEqual(100, self.parse("100", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
164 self.assertEqual(True, self.parse("true", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
165 self.assertEqual(False, self.parse("false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
166 self.assertEqual("", self.parse('""', {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
167 self.assertEqual("foo bar", self.parse('"foo bar"', {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
168 self.assertEqual(1, self.parse("foo", {'foo':1})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
169 self.assertEqual(True, self.parse("bar", {'bar':True})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
170 self.assertEqual("xyz", self.parse("abc123", {'abc123':"xyz"})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
171 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
172 def test_Equality(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
173 self.assertTrue(self.parse("true == true", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
174 self.assertTrue(self.parse("false == false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
175 self.assertTrue(self.parse("false == false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
176 self.assertTrue(self.parse("1 == 1", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
177 self.assertTrue(self.parse("100 == 100", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
178 self.assertTrue(self.parse('"some text" == "some text"', {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
179 self.assertTrue(self.parse("true != false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
180 self.assertTrue(self.parse("1 != 2", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
181 self.assertTrue(self.parse('"text" != "other text"', {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
182 self.assertTrue(self.parse("foo == true", {'foo': True})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
183 self.assertTrue(self.parse("foo == 1", {'foo': 1})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
184 self.assertTrue(self.parse('foo == "bar"', {'foo': 'bar'})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
185 self.assertTrue(self.parse("foo == bar", {'foo': True, 'bar': True})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
186 self.assertTrue(self.parse("true == foo", {'foo': True})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
187 self.assertTrue(self.parse("foo != true", {'foo': False})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
188 self.assertTrue(self.parse("foo != 2", {'foo': 1})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
189 self.assertTrue(self.parse('foo != "bar"', {'foo': 'abc'})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
190 self.assertTrue(self.parse("foo != bar", {'foo': True, 'bar': False})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
191 self.assertTrue(self.parse("true != foo", {'foo': False})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
192 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
193 def test_Conjunctions(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
194 self.assertTrue(self.parse("true && true", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
195 self.assertTrue(self.parse("true || false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
196 self.assertFalse(self.parse("false || false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
197 self.assertFalse(self.parse("true && false", {})) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
198 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
199 if __name__ == '__main__': |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
200 unittest.main() |