64 lines
1.2 KiB
Python
64 lines
1.2 KiB
Python
from typing import Any, Generic, TypeVar, Union
|
|
from decimal import Decimal
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class Token(Generic[T]):
|
|
__slots__ = ["pos", "size", "value"]
|
|
|
|
def __init__(self, pos: int, size: int, value: T) -> None:
|
|
self.size = size
|
|
self.pos = pos
|
|
self.value = value
|
|
|
|
def __repr__(self) -> str:
|
|
return "{cls}(pos={pos}, size={size}, value={value})".format(
|
|
cls=self.__class__.__name__, pos=self.pos, value=self.value, size=self.size
|
|
)
|
|
|
|
def __eq__(self, other: Any):
|
|
if isinstance(other, self.__class__):
|
|
return (
|
|
(other.value == self.value)
|
|
and (other.pos == self.pos)
|
|
and (other.size == self.size)
|
|
)
|
|
else:
|
|
return False
|
|
|
|
|
|
class TokenComment(Token[bytes]):
|
|
pass
|
|
|
|
|
|
class TokenHex(Token[bytes]):
|
|
pass
|
|
|
|
|
|
class TokenKeyword(Token[bytes]):
|
|
pass
|
|
|
|
|
|
class TokenLiteral(Token[str]):
|
|
pass
|
|
|
|
|
|
class TokenBoolean(Token[bool]):
|
|
pass
|
|
|
|
|
|
class TokenString(Token[str]):
|
|
pass
|
|
|
|
|
|
class TokenInteger(Token[int]):
|
|
pass
|
|
|
|
|
|
class TokenDecimal(Token[Decimal]):
|
|
pass
|
|
|
|
|
|
TokenNumber = Union[TokenInteger, TokenDecimal]
|