29 lines
542 B
Python
29 lines
542 B
Python
|
from __future__ import annotations
|
||
|
from typing import NamedTuple, TypeVar, Generic
|
||
|
|
||
|
|
||
|
class Position(NamedTuple):
|
||
|
index: int
|
||
|
line: int
|
||
|
col: int
|
||
|
|
||
|
class Span(NamedTuple):
|
||
|
begin: Position
|
||
|
end: Position
|
||
|
|
||
|
def to(self, end: Span) -> Span:
|
||
|
return Span(self.begin, end.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 self.value.__str__()
|