34 lines
684 B
Python
34 lines
684 B
Python
from __future__ import annotations
|
|
from typing import NamedTuple, TypeVar, Generic
|
|
|
|
class Position(NamedTuple):
|
|
index: int
|
|
line: int
|
|
col: int
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.line}:{self.col}"
|
|
|
|
class Span(NamedTuple):
|
|
begin: Position
|
|
end: Position
|
|
|
|
def to(self, end: Span) -> Span:
|
|
return Span(self.begin, end.end)
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.begin} to {self.end}"
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class Node(Generic[T]):
|
|
def __init__(self, value: T, span: Span) -> None:
|
|
super().__init__()
|
|
self.value = value
|
|
self.span = span
|
|
|
|
def __str__(self) -> str:
|
|
return str(self.value)
|