source: add ordering to Location

This commit is contained in:
Antoine Viallon 2023-05-08 23:10:02 +02:00
parent 5b3f0262a7
commit bd402beba7
Signed by: aviallon
GPG key ID: D126B13AB555E16F

View file

@ -6,6 +6,10 @@ from dataclasses import dataclass
from beartype import beartype
from beartype.typing import Optional
from .logger import Logger
logger = Logger(__name__)
@beartype
@dataclass
@ -17,6 +21,38 @@ class Location:
def __str__(self) -> str:
return f"{self.file}:{self.line}:{self.character}"
@beartype
def __lt__(self, other: Location) -> bool:
if self.file != other.file:
logger.trace(f"{self} is not in the same file as {other}")
return False
if self.line < other.line:
logger.trace(f"{self} lesser than {other}")
return True
if self.line > other.line:
logger.trace(f"{self} greater than {other}")
return False
if self.character < other.character:
logger.trace(f"{self} lesser than {other}")
return True
return False
@beartype
def __ge__(self, other: Location) -> bool:
return not self.__lt__(other)
@beartype
def __eq__(self, other: Location) -> bool:
return all((
self.file == other.file,
self.line == other.line,
self.character == other.character
))
@beartype
class SourceLocation: