2024-08-08 21:54:03 +10:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
class Keyword(Enum):
|
|
|
|
Enum = 'enum'
|
|
|
|
Struct = 'struct'
|
|
|
|
Func = 'func'
|
|
|
|
If = 'if'
|
|
|
|
Else = 'else'
|
|
|
|
While = 'while'
|
|
|
|
Break = 'break'
|
|
|
|
Continue = 'continue'
|
|
|
|
Do = 'do'
|
|
|
|
For = 'for'
|
|
|
|
To = 'to'
|
|
|
|
In = 'in'
|
|
|
|
Match = 'match'
|
|
|
|
Case = 'case'
|
|
|
|
Assert = 'assert'
|
|
|
|
Return = 'return'
|
|
|
|
Lambda = 'lambda'
|
|
|
|
Import = 'import'
|
|
|
|
Type = 'type'
|
2024-08-11 12:59:19 +10:00
|
|
|
Defer = 'defer'
|
2024-08-08 21:54:03 +10:00
|
|
|
|
2024-10-01 11:51:54 +10:00
|
|
|
def __str__(self) -> str: return self._value_
|
|
|
|
|
2024-08-08 21:54:03 +10:00
|
|
|
class Symbol(Enum):
|
|
|
|
Open = '('
|
|
|
|
Close = ')'
|
|
|
|
OpenCurly = '{'
|
|
|
|
CloseCurly = '}'
|
|
|
|
Comma = ','
|
|
|
|
OpenSquare = '['
|
|
|
|
CloseSquare = ']'
|
|
|
|
Colon = ':'
|
|
|
|
Left = '<'
|
|
|
|
Right = '>'
|
|
|
|
Arrow = '->'
|
|
|
|
EqualArrow = '=>'
|
|
|
|
Semicolon = ';'
|
|
|
|
Equal = '='
|
|
|
|
Dequal = '=='
|
|
|
|
Exclamation = '!'
|
|
|
|
NotEqual = '!='
|
|
|
|
Dot = '.'
|
|
|
|
Plus = '+'
|
|
|
|
Dash = '-'
|
|
|
|
Asterisk = '*'
|
|
|
|
Dasterisk = '**'
|
|
|
|
Slash = '/'
|
|
|
|
QuestionMark = '?'
|
|
|
|
Ampersand = '&'
|
|
|
|
Dampersand = '&&'
|
|
|
|
Pipe = '|'
|
|
|
|
Dpipe = '||'
|
|
|
|
Dleft = '<<'
|
|
|
|
Dright = '>>'
|
|
|
|
GreaterEqual = '>='
|
|
|
|
LesserEqual = '<='
|
|
|
|
Percent = '%'
|
|
|
|
Tilde = '~'
|
|
|
|
Carot = '^'
|
|
|
|
|
2024-10-01 12:13:27 +10:00
|
|
|
def __str__(self) -> str: return self._value_
|
|
|
|
|
2024-08-08 21:54:03 +10:00
|
|
|
@dataclass
|
|
|
|
class KeywordToken:
|
|
|
|
keyword: Keyword
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class IdentifierToken:
|
|
|
|
identifier: str
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class NumberToken:
|
|
|
|
number: int
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class StringToken:
|
|
|
|
string: str
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SymbolToken:
|
|
|
|
symbol: Symbol
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
return hash(('symbol', self.symbol))
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EofToken: pass
|
|
|
|
|
2024-08-13 12:45:42 +10:00
|
|
|
TokenContents = KeywordToken | IdentifierToken | NumberToken | StringToken | SymbolToken | EofToken
|
2024-08-08 21:54:03 +10:00
|
|
|
|
|
|
|
@dataclass
|
2024-10-01 11:28:10 +10:00
|
|
|
class Location:
|
|
|
|
file: str
|
2024-08-08 21:54:03 +10:00
|
|
|
line: int
|
|
|
|
col: int
|
2024-10-01 11:28:10 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self.file}:{self.line}:{self.col+1}"
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Token:
|
|
|
|
loc: Location
|
2024-08-08 21:54:03 +10:00
|
|
|
value: str
|
|
|
|
contents: TokenContents
|
2024-10-01 19:02:20 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self.loc}: {self.contents}"
|