From ad57a52ac92e0745a0006897374357d449189293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20B=C3=B6hm?= Date: Wed, 20 May 2009 10:17:07 +0200 Subject: convert tags to a typesafe enum so we can print strings instead of ints --- src/front/token.py | 92 +++++++++++++++++++++++++++++------------------------- 1 file 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 "" % 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 "" % self.tag + return "" % self.tag class ValueToken(Token): value = None @@ -62,5 +71,4 @@ class ValueToken(Token): return def __repr__(self): - return "" % (self.tag, self.value.__str__()) - + return "" % (self.tag, self.value.__str__()) -- cgit v1.2.3