changeset 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
files expr.py
diffstat 1 files changed, 5 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/expr.py	Thu Jun 02 07:44:25 2011 -0400
+++ b/expr.py	Thu Jun 02 07:47:33 2011 -0400
@@ -34,15 +34,9 @@
         # to the parser
         return parser.value(self.value)
 
-class int_token:
+class literal_token:
     def __init__(self, value):
-        self.value = int(value)
-    def nud(self, parser):
-        return self.value
-
-class bool_token:
-    def __init__(self, value):
-        self.value = {'true':True, 'false':False}[value]
+        self.value = value
     def nud(self, parser):
         return self.value
 
@@ -84,12 +78,6 @@
     ")"
     lbp = 0
 
-class string_token:
-    def __init__(self, value):
-        self.value = value
-    def nud(self, parser):
-        return self.value
-
 class end_token:
     # lowest left binding power, always ends parsing
     lbp = 0
@@ -111,16 +99,16 @@
         Lex the input text into tokens and yield them in sequence.
         """
         # scanner callbacks
-        def bool_(scanner, t): return bool_token(t)
+        def bool_(scanner, t): return literal_token({'true':True, 'false':False}[t])
         def identifier(scanner, t): return ident_token(t)
-        def integer(scanner, t): return int_token(t)
+        def integer(scanner, t): return literal_token(int(t))
         def eq(scanner, t): return eq_op_token()
         def neq(scanner, t): return neq_op_token()
         def or_(scanner, t): return or_op_token()
         def and_(scanner, t): return and_op_token()
         def lparen(scanner, t): return lparen_token()
         def rparen(scanner, t): return rparen_token()
-        def string_(scanner, t): return string_token(t[1:-1])
+        def string_(scanner, t): return literal_token(t[1:-1])
 
         scanner = re.Scanner([
             (r"true|false", bool_),