comparison expr.py @ 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
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 03 Jun 2011 08:31:29 -0700
parents a42bb6dc2fa7
children 9b2bf000aeed
comparison
equal deleted inserted replaced
6:4b07bb71c218 7:325dccc38308
80 80
81 class end_token(object): 81 class end_token(object):
82 # lowest left binding power, always ends parsing 82 # lowest left binding power, always ends parsing
83 lbp = 0 83 lbp = 0
84 84
85 class bool_token(literal_token):
86 def __init__(self, value):
87 value = {'true':True, 'false':False}[value]
88 literal_token.__init__(self, value)
89
85 precedence = [(end_token, rparen_token), 90 precedence = [(end_token, rparen_token),
86 (or_op_token,), 91 (or_op_token,),
87 (and_op_token,), 92 (and_op_token,),
88 (eq_op_token, neq_op_token), 93 (eq_op_token, neq_op_token),
89 (lparen_token,), 94 (lparen_token,),
104 def _tokenize(self): 109 def _tokenize(self):
105 """ 110 """
106 Lex the input text into tokens and yield them in sequence. 111 Lex the input text into tokens and yield them in sequence.
107 """ 112 """
108 # scanner callbacks 113 # scanner callbacks
109 def bool_(scanner, t): return literal_token({'true':True, 'false':False}[t]) 114 def bool_(scanner, t): return bool_token(t)
110 def identifier(scanner, t): return ident_token(t) 115 def identifier(scanner, t): return ident_token(t)
111 def integer(scanner, t): return literal_token(int(t)) 116 def integer(scanner, t): return literal_token(int(t))
112 def eq(scanner, t): return eq_op_token() 117 def eq(scanner, t): return eq_op_token()
113 def neq(scanner, t): return neq_op_token() 118 def neq(scanner, t): return neq_op_token()
114 def or_(scanner, t): return or_op_token() 119 def or_(scanner, t): return or_op_token()