source: avoid printing source file twice if it is the same for beginning and end (which is very likely).

This commit is contained in:
Antoine Viallon 2024-01-11 00:54:35 +01:00
parent ff2435171c
commit a69788ca52
Signed by: aviallon
GPG key ID: 186FC35EDEB25716

View file

@ -17,10 +17,16 @@ class Location:
file: str = "<none>" file: str = "<none>"
internal: bool = False internal: bool = False
def to_string(self, show_file: bool = True) -> str:
filename = "<internal>" if self.internal else self.file
message = ""
if show_file:
message = f"{filename}:"
message += f"{self.line}:{self.character}"
return message
def __str__(self) -> str: def __str__(self) -> str:
if self.internal: return self.to_string()
return "<internal>"
return f"{self.file}:{self.line}:{self.character}"
@typecheck @typecheck
def __lt__(self, other: Location) -> bool: def __lt__(self, other: Location) -> bool:
@ -76,7 +82,7 @@ class SourceLocation:
def __str__(self): def __str__(self):
if self.begin == self.end: if self.begin == self.end:
return str(self.begin) return str(self.begin)
return f"{str(self.begin)} - {str(self.end)}" return f"{str(self.begin)} - {self.end.to_string(self.begin.file != self.end.file)}"
@typecheck @typecheck
def __lt__(self, other: SourceLocation) -> bool: def __lt__(self, other: SourceLocation) -> bool: