comparison expr.py @ 11:e17a3464a0b9

get precedence from a list position vs magic constants
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 03 Jun 2011 10:51:36 -0700
parents 15fb1081784f
children 835efd8acb04
comparison
equal deleted inserted replaced
10:15fb1081784f 11:e17a3464a0b9
43 def nud(self, parser): 43 def nud(self, parser):
44 return self.value 44 return self.value
45 45
46 class eq_op_token(object): 46 class eq_op_token(object):
47 "==" 47 "=="
48 lbp = 20
49 def led(self, parser, left): 48 def led(self, parser, left):
50 return left == parser.expression(self.lbp) 49 return left == parser.expression(self.lbp)
51 50
52 class neq_op_token(object): 51 class neq_op_token(object):
53 "!=" 52 "!="
54 lbp = 20
55 def led(self, parser, left): 53 def led(self, parser, left):
56 return left != parser.expression(self.lbp) 54 return left != parser.expression(self.lbp)
57 55
58 class and_op_token(object): 56 class and_op_token(object):
59 "&&" 57 "&&"
60 lbp = 11
61 def led(self, parser, left): 58 def led(self, parser, left):
62 right = parser.expression(self.lbp) 59 right = parser.expression(self.lbp)
63 return left and right 60 return left and right
64 61
65 class or_op_token(object): 62 class or_op_token(object):
66 "||" 63 "||"
67 lbp = 10
68 def led(self, parser, left): 64 def led(self, parser, left):
69 right = parser.expression(self.lbp) 65 right = parser.expression(self.lbp)
70 return left or right 66 return left or right
71 67
72 class lparen_token(object): 68 class lparen_token(object):
73 "(" 69 "("
74 lbp = 50
75 def nud(self, parser): 70 def nud(self, parser):
76 expr = parser.expression() 71 expr = parser.expression()
77 parser.advance(rparen_token) 72 parser.advance(rparen_token)
78 return expr 73 return expr
79 74
80 class rparen_token(object): 75 class rparen_token(object):
81 ")" 76 ")"
82 lbp = 0
83 77
84 class end_token(object): 78 class end_token(object):
85 # lowest left binding power, always ends parsing 79 """always ends parsing"""
86 lbp = 0
87 80
88 ### derived literal tokens 81 ### derived literal tokens
89 82
90 class bool_token(literal_token): 83 class bool_token(literal_token):
91 def __init__(self, value): 84 def __init__(self, value):
104 (or_op_token,), 97 (or_op_token,),
105 (and_op_token,), 98 (and_op_token,),
106 (eq_op_token, neq_op_token), 99 (eq_op_token, neq_op_token),
107 (lparen_token,), 100 (lparen_token,),
108 ] 101 ]
102 for index, rank in enumerate(precedence):
103 for token in rank:
104 token.lbp = index # lbp = lowest left binding power
109 105
110 class ParseError(Exception): 106 class ParseError(Exception):
111 """errror parsing conditional expression""" 107 """errror parsing conditional expression"""
112 108
113 class ExpressionParser(object): 109 class ExpressionParser(object):