Mercurial > hg > expressionparser
comparison expr.py @ 8:9b2bf000aeed
add an int token
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 03 Jun 2011 08:32:57 -0700 |
parents | 325dccc38308 |
children | 421e26c0299f |
comparison
equal
deleted
inserted
replaced
7:325dccc38308 | 8:9b2bf000aeed |
---|---|
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 ### derived literal tokens | |
86 | |
85 class bool_token(literal_token): | 87 class bool_token(literal_token): |
86 def __init__(self, value): | 88 def __init__(self, value): |
87 value = {'true':True, 'false':False}[value] | 89 value = {'true':True, 'false':False}[value] |
88 literal_token.__init__(self, value) | 90 literal_token.__init__(self, value) |
91 | |
92 class int_token(literal_token): | |
93 def __init__(self, value): | |
94 literal_token.__init__(self, int(value)) | |
89 | 95 |
90 precedence = [(end_token, rparen_token), | 96 precedence = [(end_token, rparen_token), |
91 (or_op_token,), | 97 (or_op_token,), |
92 (and_op_token,), | 98 (and_op_token,), |
93 (eq_op_token, neq_op_token), | 99 (eq_op_token, neq_op_token), |
111 Lex the input text into tokens and yield them in sequence. | 117 Lex the input text into tokens and yield them in sequence. |
112 """ | 118 """ |
113 # scanner callbacks | 119 # scanner callbacks |
114 def bool_(scanner, t): return bool_token(t) | 120 def bool_(scanner, t): return bool_token(t) |
115 def identifier(scanner, t): return ident_token(t) | 121 def identifier(scanner, t): return ident_token(t) |
116 def integer(scanner, t): return literal_token(int(t)) | 122 def integer(scanner, t): return int_token(t) |
117 def eq(scanner, t): return eq_op_token() | 123 def eq(scanner, t): return eq_op_token() |
118 def neq(scanner, t): return neq_op_token() | 124 def neq(scanner, t): return neq_op_token() |
119 def or_(scanner, t): return or_op_token() | 125 def or_(scanner, t): return or_op_token() |
120 def and_(scanner, t): return and_op_token() | 126 def and_(scanner, t): return and_op_token() |
121 def lparen(scanner, t): return lparen_token() | 127 def lparen(scanner, t): return lparen_token() |