-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToken.py
79 lines (68 loc) · 1.91 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from __future__ import annotations
from enum import Enum
from typing import Any
class Position:
"""A class representing the exact Position of a token for error logging"""
def __init__(self, index: int, line: int, column: int, length: int) -> None:
self.index = index
self.line = line
self.column = column
self.len = length
def advance(self, char: str) -> None:
self.index += 1
self.column += 1
if char == '\n':
self.line += 1
self.column = 0
def copy(self) -> Position:
"""Has to always be called, when giving into token objects in the Lexer, else further advances will also affect this position, invalidating data"""
return Position(self.index, self.line, self.column, self.len)
class Token:
"""Tokens representing the smallest possible units a program is made of"""
def __init__(self, token_type: TT, value: Any | None, pos: Position):
self.type = token_type
self.value = value
self.pos = pos
def __str__(self) -> str:
return f'{self.type.value}: {self.value}' if self.value else f'{self.type.value}'
def __repr__(self) -> str:
return str(self)
class TT(Enum):
"""Enum for Token types mapping name to representation"""
INT = 'int'
FLOAT = 'float'
PLUS = '+'
MINUS = '-'
MUL = '*'
DIV = '/'
MOD = '%'
POW = '^'
LPAREN = '('
RPAREN = ')'
IDENTIFIER = 'identifier'
KEYWORD = 'keyword'
ASSIGN = '<-'
EQUALS = '='
UNEQUALS = '<>'
LESS = '<'
GREATER = '>'
LESSEQUAL = '<='
GREATEREQUAL = '>='
NOT = '!'
AND = '&'
OR = '|'
XOR = '~'
COMMA = ','
STRING = 'string'
LSQUARE = '['
RSQUARE = ']'
GET = '[...]'
COLON = ':'
NEWLINE = '\n'
LCURLY = '{'
RCURLY = '}'
DOT = '.'
TO = '..'
TYPE = 'type'
ARROW = '->'
EOF = 'end of file'