class TokenType(IntEnum):
IDENT = auto()
STRING = auto()
+
LEFT_PAREN = auto()
RIGHT_PAREN = auto()
MINUS = auto()
SEMICOLON = auto()
LEFT_SQUARE_BRACE = auto()
RIGHT_SQUARE_BRACE = auto()
+
+ NUMBER = auto()
COUNT = auto()
token_type_as_str_map: { TokenType : str } = {
TokenType.SEMICOLON : "Semicolon",
TokenType.LEFT_SQUARE_BRACE : "Left Square Brace",
TokenType.RIGHT_SQUARE_BRACE : "Right Square Brace",
+ TokenType.NUMBER : "Number"
}
# NOTE: TokenType.COUNT - 1 because auto() starts from 1
assert len(token_type_as_str_map) == TokenType.COUNT-1
return (ident, ident_loc)
+ def consume_number(self) -> (str, Loc):
+ assert self.current_char().isdigit(), "Called consume_number() at the wrong character!"
+ number: str = self.consume_char()
+
+ # TODO: Handle floating point numbers
+ # TODO: Handle numbers in other formats (Eg, binary, hexadecimal, etc)
+ number_loc: Loc = Loc(self.filename, self.line, self.row())
+
+ while self.current_char().isdigit() and not self.eof():
+ number += self.consume_char()
+
+ return (number, number_loc)
+
+
def left_trim(self):
while not self.eof() and self.current_char().isspace():
if self.current_char() == '\n':
elif c == ']':
loc = Loc(self.filename, self.line, self.row())
return Token(TokenType.RIGHT_SQUARE_BRACE, self.consume_char(), loc)
+ elif c.isdigit():
+ num, loc = self.consume_number()
+ return Token(TokenType.NUMBER, num, loc)
else:
fatal(f"Unrecognized character '{c}'")