Mercurial > hg > expressionparser
annotate expr.py @ 15:6f0c032b65f5 default tip
add a not token, i hope!
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 03 Jun 2011 13:01:22 -0700 |
parents | ec0e83ec329f |
children |
rev | line source |
---|---|
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
1 # Implements a top-down parser/evaluator for simple boolean expressions. |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
2 # ideas taken from http://effbot.org/zone/simple-top-down-parsing.htm |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
3 # |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
4 # Rough grammar: |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
5 # expr := literal |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
6 # | '(' expr ')' |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
7 # | expr '&&' expr |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
8 # | expr '||' expr |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
9 # | expr '==' expr |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
10 # | expr '!=' expr |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
11 # literal := BOOL |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
12 # | INT |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
13 # | STRING |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
14 # | IDENT |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
15 # BOOL := true|false |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
16 # INT := [0-9]+ |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
17 # STRING := "[^"]*" |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
18 # IDENT := [A-Za-z_]\w* |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
19 |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
20 # Identifiers take their values from a mapping dictionary passed as the second |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
21 # argument. |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
22 |
12 | 23 # Glossary (see above URL for details): |
24 # - nud: null denotation | |
25 # - led: left detonation | |
26 # - lbp: left binding power | |
27 # - rbp: right binding power | |
28 | |
10
15fb1081784f
add ExpressionParser to the exports
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
29 __all__ = ['parse', 'ParseError', 'ExpressionParser'] |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
30 import re |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
31 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
32 # token classes |
10
15fb1081784f
add ExpressionParser to the exports
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
33 class token(object): |
15fb1081784f
add ExpressionParser to the exports
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
34 pass |
15fb1081784f
add ExpressionParser to the exports
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
35 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
36 class ident_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
37 def __init__(self, value): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
38 self.value = value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
39 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
40 # identifiers take their value from the value mappings passed |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
41 # to the parser |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
42 return parser.value(self.value) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
43 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
44 class literal_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
45 def __init__(self, value): |
3
5ac8eed85684
consolidate token classes
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
2
diff
changeset
|
46 self.value = value |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
47 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
48 return self.value |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
49 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
50 class eq_op_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
51 "==" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
52 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
53 return left == parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
54 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
55 class neq_op_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
56 "!=" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
57 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
58 return left != parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
59 |
15 | 60 class not_op_token(object): |
61 "!" | |
62 def nud(self, parser): | |
63 return not parser.expression() | |
64 | |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
65 class and_op_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
66 "&&" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
67 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
68 right = parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
69 return left and right |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
70 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
71 class or_op_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
72 "||" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
73 def led(self, parser, left): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
74 right = parser.expression(self.lbp) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
75 return left or right |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
76 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
77 class lparen_token(object): |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
78 "(" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
79 def nud(self, parser): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
80 expr = parser.expression() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
81 parser.advance(rparen_token) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
82 return expr |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
83 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
84 class rparen_token(object): |
0
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 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
87 class end_token(object): |
11
e17a3464a0b9
get precedence from a list position vs magic constants
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
88 """always ends parsing""" |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
89 |
8 | 90 ### derived literal tokens |
91 | |
7
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 class bool_token(literal_token): |
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
93 def __init__(self, value): |
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
94 value = {'true':True, 'false':False}[value] |
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
95 literal_token.__init__(self, value) |
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
96 |
8 | 97 class int_token(literal_token): |
98 def __init__(self, value): | |
99 literal_token.__init__(self, int(value)) | |
100 | |
9 | 101 class string_token(literal_token): |
102 def __init__(self, value): | |
103 literal_token.__init__(self, value[1:-1]) | |
104 | |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
105 precedence = [(end_token, rparen_token), |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
106 (or_op_token,), |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
107 (and_op_token,), |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
108 (eq_op_token, neq_op_token), |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
109 (lparen_token,), |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
110 ] |
11
e17a3464a0b9
get precedence from a list position vs magic constants
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
111 for index, rank in enumerate(precedence): |
e17a3464a0b9
get precedence from a list position vs magic constants
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
112 for token in rank: |
e17a3464a0b9
get precedence from a list position vs magic constants
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
113 token.lbp = index # lbp = lowest left binding power |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
114 |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
115 class ParseError(Exception): |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
116 """errror parsing conditional expression""" |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
117 |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
118 class ExpressionParser(object): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
119 def __init__(self, text, valuemapping): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
120 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
121 Initialize the parser with input |text|, and |valuemapping| as |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
122 a dict mapping identifier names to values. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
123 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
124 self.text = text |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
125 self.valuemapping = valuemapping |
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 def _tokenize(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
128 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
129 Lex the input text into tokens and yield them in sequence. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
130 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
131 # scanner callbacks |
7
325dccc38308
begin using subclasses for tokens; the eventual goal is that a token class will know everything it can about what it is and the parser just knows about what tokens are
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
132 def bool_(scanner, t): return bool_token(t) |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
133 def identifier(scanner, t): return ident_token(t) |
8 | 134 def integer(scanner, t): return int_token(t) |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
135 def eq(scanner, t): return eq_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
136 def neq(scanner, t): return neq_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
137 def or_(scanner, t): return or_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
138 def and_(scanner, t): return and_op_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
139 def lparen(scanner, t): return lparen_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
140 def rparen(scanner, t): return rparen_token() |
9 | 141 def string_(scanner, t): return string_token(t) |
15 | 142 def not_(scanner, t): return not_op_token() |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
143 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
144 scanner = re.Scanner([ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
145 (r"true|false", bool_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
146 (r"[a-zA-Z_]\w*", identifier), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
147 (r"[0-9]+", integer), |
13
ec0e83ec329f
allow both kinds of strings
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
148 (r'("[^"]*")|(\'[^\']*\')', string_), |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
149 (r"==", eq), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
150 (r"!=", neq), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
151 (r"\|\|", or_), |
15 | 152 (r"!", not_), |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
153 (r"&&", and_), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
154 (r"\(", lparen), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
155 (r"\)", rparen), |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
156 (r"\s+", None), # skip whitespace |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
157 ]) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
158 tokens, remainder = scanner.scan(self.text) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
159 for t in tokens: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
160 yield t |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
161 yield end_token() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
162 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
163 def value(self, ident): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
164 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
165 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
|
166 constructor. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
167 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
168 return self.valuemapping[ident] |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
169 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
170 def advance(self, expected): |
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 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
|
173 to the next token. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
174 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
175 if not isinstance(self.token, expected): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
176 raise Exception, "Unexpected token!" |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
177 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
178 |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
179 def expression(self, rbp=0): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
180 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
181 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
|
182 right binding power greater than rbp is encountered. |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
183 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
184 t = self.token |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
185 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
186 left = t.nud(self) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
187 while rbp < self.token.lbp: |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
188 t = self.token |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
189 self.token = self.iter.next() |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
190 left = t.led(self, left) |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
191 return left |
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 parse(self): |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
194 """ |
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
195 Parse and return the value of the expression in the text |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
196 passed to the constructor. Raises a ParseError if the expression |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
197 could not be parsed. |
0
ae57e69e4b15
simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff
changeset
|
198 """ |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
199 try: |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
200 self.iter = self._tokenize() |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
201 self.token = self.iter.next() |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
202 return self.expression() |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
203 except: |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
204 raise ParseError("could not parse: %s" % self.text) |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
205 |
4
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
206 __call__ = parse |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
207 |
a42bb6dc2fa7
change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
208 def parse(text, **values): |
2
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
209 """ |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
210 Parse and evaluate a boolean expression in |text|. Use |values| to look |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
211 up the value of identifiers referenced in the expression. Returns the final |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
212 value of the expression. A ParseError will be raised if parsing fails. |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
213 """ |
94a293b914af
add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
1
diff
changeset
|
214 return ExpressionParser(text, values).parse() |