python-plus-plus/ppp_tokens.py

112 lines
1.7 KiB
Python
Raw Normal View History

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'
def __str__(self) -> str: return self._value_
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 = '^'
def __str__(self) -> str: return self._value_
@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
TokenContents = KeywordToken | IdentifierToken | NumberToken | StringToken | SymbolToken | EofToken
@dataclass
2024-10-01 11:28:10 +10:00
class Location:
file: str
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
value: str
contents: TokenContents
2024-10-01 19:02:20 +10:00
def __repr__(self) -> str:
return f"{self.loc}: {self.contents}"