Mercurial > hg > expressionparser
comparison expr.py @ 3:5ac8eed85684
consolidate token classes
author | Ted Mielczarek <ted.mielczarek@gmail.com> |
---|---|
date | Thu, 02 Jun 2011 07:47:33 -0400 |
parents | 94a293b914af |
children | a42bb6dc2fa7 |
comparison
equal
deleted
inserted
replaced
2:94a293b914af | 3:5ac8eed85684 |
---|---|
32 def nud(self, parser): | 32 def nud(self, parser): |
33 # identifiers take their value from the value mappings passed | 33 # identifiers take their value from the value mappings passed |
34 # to the parser | 34 # to the parser |
35 return parser.value(self.value) | 35 return parser.value(self.value) |
36 | 36 |
37 class int_token: | 37 class literal_token: |
38 def __init__(self, value): | 38 def __init__(self, value): |
39 self.value = int(value) | 39 self.value = value |
40 def nud(self, parser): | |
41 return self.value | |
42 | |
43 class bool_token: | |
44 def __init__(self, value): | |
45 self.value = {'true':True, 'false':False}[value] | |
46 def nud(self, parser): | 40 def nud(self, parser): |
47 return self.value | 41 return self.value |
48 | 42 |
49 class eq_op_token: | 43 class eq_op_token: |
50 "==" | 44 "==" |
82 | 76 |
83 class rparen_token: | 77 class rparen_token: |
84 ")" | 78 ")" |
85 lbp = 0 | 79 lbp = 0 |
86 | 80 |
87 class string_token: | |
88 def __init__(self, value): | |
89 self.value = value | |
90 def nud(self, parser): | |
91 return self.value | |
92 | |
93 class end_token: | 81 class end_token: |
94 # lowest left binding power, always ends parsing | 82 # lowest left binding power, always ends parsing |
95 lbp = 0 | 83 lbp = 0 |
96 | 84 |
97 class ParseError(Exception): | 85 class ParseError(Exception): |
109 def _tokenize(self): | 97 def _tokenize(self): |
110 """ | 98 """ |
111 Lex the input text into tokens and yield them in sequence. | 99 Lex the input text into tokens and yield them in sequence. |
112 """ | 100 """ |
113 # scanner callbacks | 101 # scanner callbacks |
114 def bool_(scanner, t): return bool_token(t) | 102 def bool_(scanner, t): return literal_token({'true':True, 'false':False}[t]) |
115 def identifier(scanner, t): return ident_token(t) | 103 def identifier(scanner, t): return ident_token(t) |
116 def integer(scanner, t): return int_token(t) | 104 def integer(scanner, t): return literal_token(int(t)) |
117 def eq(scanner, t): return eq_op_token() | 105 def eq(scanner, t): return eq_op_token() |
118 def neq(scanner, t): return neq_op_token() | 106 def neq(scanner, t): return neq_op_token() |
119 def or_(scanner, t): return or_op_token() | 107 def or_(scanner, t): return or_op_token() |
120 def and_(scanner, t): return and_op_token() | 108 def and_(scanner, t): return and_op_token() |
121 def lparen(scanner, t): return lparen_token() | 109 def lparen(scanner, t): return lparen_token() |
122 def rparen(scanner, t): return rparen_token() | 110 def rparen(scanner, t): return rparen_token() |
123 def string_(scanner, t): return string_token(t[1:-1]) | 111 def string_(scanner, t): return literal_token(t[1:-1]) |
124 | 112 |
125 scanner = re.Scanner([ | 113 scanner = re.Scanner([ |
126 (r"true|false", bool_), | 114 (r"true|false", bool_), |
127 (r"[a-zA-Z_]\w*", identifier), | 115 (r"[a-zA-Z_]\w*", identifier), |
128 (r"[0-9]+", integer), | 116 (r"[0-9]+", integer), |