summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenedikt Böhm <bb@xnull.de>2009-05-20 10:17:07 +0200
committerBenedikt Böhm <bb@xnull.de>2009-05-20 10:17:07 +0200
commitad57a52ac92e0745a0006897374357d449189293 (patch)
tree88db74e0292a05acfdd1bfe4fb72284da9c27c87
parent90f4e57ee5c4b2649ab01f96ed6ae886734b3eee (diff)
downloadswppy-ad57a52ac92e0745a0006897374357d449189293.tar.gz
swppy-ad57a52ac92e0745a0006897374357d449189293.tar.xz
swppy-ad57a52ac92e0745a0006897374357d449189293.zip
convert tags to a typesafe enum so we can print strings instead of ints
-rw-r--r--src/front/token.py92
1 files changed, 50 insertions, 42 deletions
diff --git a/src/front/token.py b/src/front/token.py
index def0236..4cf646a 100644
--- a/src/front/token.py
+++ b/src/front/token.py
@@ -2,46 +2,55 @@
# date: 20 Mai 2009
class Tag:
- # lexer tokens
- NUMBER = 1
- TRUE = 2
- FALSE = 3
- IDENT = 4
- WHILE = 5
- IF = 6
- ELSE = 7
- END = 8
- LBRAK = 9
- RBRAK = 10
- LPAREN = 11
- RPAREN = 12
- NEWLINE = 13
- COMMA = 14
- FUN = 15
- ASSIGNMENT = 16
- RETURN = 17
- OPERATOR = 18
+ def __init__(self, name):
+ self.name = name
- # parser tokens
- BOOL = 19
- JOIN = 20
- EQUALITY = 21
- RELATION = 22
- EXPRESSION = 23
- TERM = 24
- UNARY = 25
- FACTOR = 26
- IDENT_LIST = 27
- EXPRESSION_LIST = 28
- PROGRAM = 29
- FUNCTION = 30
- STATEMENT = 31
- STATEMENTS = 32
- IF_STATEMENT = 33
- WHILE_STATEMENT = 34
- RETURN_STATEMENT = 35
- ASSIGN_STATEMENT = 36
- FUNCTION_CALL = 37
+ def __str__(self):
+ return self.name
+
+ def __repr__(self):
+ return "<Tag: %s>" % self
+
+# lexer tokens
+Tag.NUMBER = Tag("NUMBER")
+Tag.TRUE = Tag("TRUE")
+Tag.FALSE = Tag("FALSE")
+Tag.IDENT = Tag("IDENT")
+Tag.WHILE = Tag("WHILE")
+Tag.IF = Tag("IF")
+Tag.ELSE = Tag("ELSE")
+Tag.END = Tag("END")
+Tag.LBRAK = Tag("LBRAK")
+Tag.RBRAK = Tag("RBRAK")
+Tag.LPAREN = Tag("LPAREN")
+Tag.RPAREN = Tag("RPAREN")
+Tag.NEWLINE = Tag("NEWLINE")
+Tag.COMMA = Tag("COMMA")
+Tag.FUN = Tag("FUN")
+Tag.ASSIGNMENT = Tag("ASSIGNMENT")
+Tag.RETURN = Tag("RETURN")
+Tag.OPERATOR = Tag("OPERATOR")
+
+# parser tokens
+Tag.BOOL = Tag("BOOL")
+Tag.JOIN = Tag("JOIN")
+Tag.EQUALITY = Tag("EQUALITY")
+Tag.RELATION = Tag("RELATION")
+Tag.EXPRESSION = Tag("EXPRESSION")
+Tag.TERM = Tag("TERM")
+Tag.UNARY = Tag("UNARY")
+Tag.FACTOR = Tag("FACTOR")
+Tag.IDENT = Tag("IDENT")
+Tag.EXPRESSION = Tag("EXPRESSION")
+Tag.PROGRAM = Tag("PROGRAM")
+Tag.FUNCTION = Tag("FUNCTION")
+Tag.STATEMENT = Tag("STATEMENT")
+Tag.STATEMENTS = Tag("STATEMENTS")
+Tag.IF = Tag("IF")
+Tag.WHILE = Tag("WHILE")
+Tag.RETURN = Tag("RETURN")
+Tag.ASSIGN = Tag("ASSIGN")
+Tag.FUNCTION = Tag("FUNCTION")
class Token:
tag = None
@@ -51,7 +60,7 @@ class Token:
return
def __repr__(self):
- return "<Token: %d>" % self.tag
+ return "<Token: %s>" % self.tag
class ValueToken(Token):
value = None
@@ -62,5 +71,4 @@ class ValueToken(Token):
return
def __repr__(self):
- return "<Token: %d Value: %s>" % (self.tag, self.value.__str__())
-
+ return "<Token: %s, Value: %s>" % (self.tag, self.value.__str__())