annotate expr.py @ 5:e55489813783

put tests in sseparate file
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 03 Jun 2011 08:23:19 -0700
parents a42bb6dc2fa7
children 325dccc38308
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
3 # 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
4 # 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
5 #
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
6 # Rough grammar:
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
7 # expr := literal
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
8 # | '(' 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 # | expr '==' expr
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
12 # | expr '!=' expr
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
13 # literal := BOOL
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
14 # | INT
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
15 # | STRING
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
16 # | IDENT
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
17 # BOOL := true|false
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
18 # INT := [0-9]+
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
19 # STRING := "[^"]*"
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
20 # IDENT := [A-Za-z_]\w*
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
21
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
22 # 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
23 # argument.
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
24
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
25 __all__ = ['parse', 'ParseError']
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
26 import re
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
27
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
28 # token classes
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
29 class ident_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
30 def __init__(self, value):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
31 self.value = value
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
32 def nud(self, parser):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
33 # identifiers take their value from the value mappings passed
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
34 # to the parser
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
35 return parser.value(self.value)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
36
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
37 class literal_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
38 def __init__(self, value):
3
5ac8eed85684 consolidate token classes
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 2
diff changeset
39 self.value = value
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
40 def nud(self, parser):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
41 return self.value
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
42
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
43 class eq_op_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
44 "=="
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
45 lbp = 20
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
46 def led(self, parser, left):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
47 return left == parser.expression(self.lbp)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
48
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
49 class neq_op_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
50 "!="
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
51 lbp = 20
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 and_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 lbp = 11
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
58 def led(self, parser, left):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
59 right = parser.expression(self.lbp)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
60 return left and right
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
61
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
62 class or_op_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
63 "||"
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
64 lbp = 10
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
65 def led(self, parser, left):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
66 right = parser.expression(self.lbp)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
67 return left or right
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
68
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
69 class lparen_token(object):
0
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 lbp = 50
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
72 def nud(self, parser):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
73 expr = parser.expression()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
74 parser.advance(rparen_token)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
75 return expr
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 rparen_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 lbp = 0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
80
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
81 class end_token(object):
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
82 # lowest left binding power, always ends parsing
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
83 lbp = 0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
84
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
85 precedence = [(end_token, rparen_token),
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
86 (or_op_token,),
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
87 (and_op_token,),
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
88 (eq_op_token, neq_op_token),
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
89 (lparen_token,),
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
90 ]
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
91
2
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
92 class ParseError(Exception):
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
93 """errror parsing conditional expression"""
2
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
94
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
95 class ExpressionParser(object):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
96 def __init__(self, text, valuemapping):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
97 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
98 Initialize the parser with input |text|, and |valuemapping| as
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
99 a dict mapping identifier names to values.
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
100 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
101 self.text = text
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
102 self.valuemapping = valuemapping
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
103
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
104 def _tokenize(self):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
105 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
106 Lex the input text into tokens and yield them in sequence.
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
107 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
108 # scanner callbacks
3
5ac8eed85684 consolidate token classes
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 2
diff changeset
109 def bool_(scanner, t): return literal_token({'true':True, 'false':False}[t])
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
110 def identifier(scanner, t): return ident_token(t)
3
5ac8eed85684 consolidate token classes
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 2
diff changeset
111 def integer(scanner, t): return literal_token(int(t))
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
112 def eq(scanner, t): return eq_op_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
113 def neq(scanner, t): return neq_op_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
114 def or_(scanner, t): return or_op_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
115 def and_(scanner, t): return and_op_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
116 def lparen(scanner, t): return lparen_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
117 def rparen(scanner, t): return rparen_token()
3
5ac8eed85684 consolidate token classes
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 2
diff changeset
118 def string_(scanner, t): return literal_token(t[1:-1])
0
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 scanner = re.Scanner([
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
121 (r"true|false", bool_),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
122 (r"[a-zA-Z_]\w*", identifier),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
123 (r"[0-9]+", integer),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
124 (r'"[^"]*"', string_),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
125 (r"==", eq),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
126 (r"!=", neq),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
127 (r"\|\|", or_),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
128 (r"&&", and_),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
129 (r"\(", lparen),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
130 (r"\)", rparen),
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
131 (r"\s+", None), # skip whitespace
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
132 ])
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
133 tokens, remainder = scanner.scan(self.text)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
134 for t in tokens:
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
135 yield t
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
136 yield end_token()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
137
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
138 def value(self, ident):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
139 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
140 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
141 constructor.
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
142 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
143 return self.valuemapping[ident]
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
144
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
145 def advance(self, expected):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
146 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
147 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
148 to the next token.
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 if not isinstance(self.token, expected):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
151 raise Exception, "Unexpected token!"
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
152 self.token = self.iter.next()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
153
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
154 def expression(self, rbp=0):
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
155 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
156 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
157 right binding power greater than rbp is encountered.
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
158 """
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
159 t = self.token
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
160 self.token = self.iter.next()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
161 left = t.nud(self)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
162 while rbp < self.token.lbp:
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
163 t = self.token
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
164 self.token = self.iter.next()
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
165 left = t.led(self, left)
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
166 return left
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 def parse(self):
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 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
171 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
172 could not be parsed.
0
ae57e69e4b15 simple expression parser
Ted Mielczarek <ted.mielczarek@gmail.com>
parents:
diff changeset
173 """
2
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
174 try:
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
175 self.iter = self._tokenize()
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
176 self.token = self.iter.next()
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
177 return self.expression()
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
178 except:
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
179 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
180
4
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
181 __call__ = parse
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
182
a42bb6dc2fa7 change parse mapping to **kwargs
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
183 def parse(text, **values):
2
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
184 """
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
185 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
186 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
187 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
188 """
94a293b914af add documentation, clean up interface slightly, tweak tests
Ted Mielczarek <ted.mielczarek@gmail.com>
parents: 1
diff changeset
189 return ExpressionParser(text, values).parse()