-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlox_token.py
57 lines (51 loc) · 1.17 KB
/
lox_token.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from enum import Enum
class TokenType(Enum):
LEFT_PAREN = '('
RIGHT_PAREN = ')'
LEFT_BRACE = '['
RIGHT_BRACE = ']'
COMMA = ','
DOT = '.'
MINUS = '-'
PLUS = '+'
SEMICOLON = ';'
SLASH = '/'
STAR = '*'
BANG = '!'
BANG_EQUAL = '!='
EQUAL = '='
EQUAL_EQUAL = '=='
GREATER = '>'
GREATER_EQUAL = '>='
LESS = '<'
LESS_EQUAL = '<='
IDENTIFIER = 'identifier'
STRING = 'string'
NUMBER = 'number'
AND = 'and'
CLASS = 'class'
ELSE = 'else'
FALSE = 'false'
FUN = 'fun'
FOR = 'for'
IF = 'if'
NIL = 'nil'
OR = 'or'
PRINT = 'print'
RETURN = 'return'
SUPER = 'super'
THIS = 'this'
TRUE = 'true'
VAR = 'var'
WHILE = 'while'
EOF = 'end_of_file'
class Token:
def __init__(self, token_type: TokenType, lexeme: str, literal: dict | None, line: int):
self.token_type = token_type
self.lexeme = lexeme
self.literal = literal
self.line = line
def __str__(self):
return f'{self.lexeme}'
def __repr__(self):
return f"{self.token_type}(lexeme={self.lexeme}, literal={self.literal}, line={self.line})"