parrot/position.py

34 lines
684 B
Python
Raw Normal View History

2023-04-06 03:17:57 +01:00
from __future__ import annotations
from typing import NamedTuple, TypeVar, Generic
class Position(NamedTuple):
index: int
line: int
col: int
2023-04-10 03:20:05 +01:00
def __str__(self) -> str:
return f"{self.line}:{self.col}"
2023-04-06 03:17:57 +01:00
class Span(NamedTuple):
begin: Position
end: Position
def to(self, end: Span) -> Span:
return Span(self.begin, end.end)
2023-04-10 03:20:05 +01:00
def __str__(self) -> str:
return f"{self.begin} to {self.end}"
2023-04-06 03:17:57 +01:00
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:
2023-04-10 03:20:05 +01:00
return str(self.value)