From 486e636da18f0f812367d150b82a6e2a575a2f16 Mon Sep 17 00:00:00 2001 From: Markus Rosskopf Date: Wed, 23 Aug 2023 11:40:24 +0200 Subject: [PATCH] Add location pointing to the end of a source --- .gitignore | 3 +++ trlc/errors.py | 3 +++ trlc/lexer.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/.gitignore b/.gitignore index bbee371a..b2b07b35 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ dist docs .coverage* *.lobster +.DS_Store +.venv/ +.idea \ No newline at end of file diff --git a/trlc/errors.py b/trlc/errors.py index 37e50664..c150ce6b 100644 --- a/trlc/errors.py +++ b/trlc/errors.py @@ -74,6 +74,9 @@ def to_string(self, include_column=True): def context_lines(self): return [] + def get_end_location(self): + return self + class TRLC_Error(Exception): """ The universal exception that TRLC raises if something goes wrong diff --git a/trlc/lexer.py b/trlc/lexer.py index a6b23109..c4f25c12 100644 --- a/trlc/lexer.py +++ b/trlc/lexer.py @@ -108,6 +108,20 @@ def context_lines(self): return [stripped_line, " " * stripped_offset + "^" * min(tlen, maxtrail)] + def get_end_location(self): + """Get location pointing to the end of a source.""" + + lines_in_between = self.lexer.content[self.start_pos : self.end_pos + 1].count("\n") + end_line = self.line_no + lines_in_between + n = self.end_pos + while n > self.start_pos: + if self.lexer.content[n] == "\n": + break + n -= 1 + end_col = min(self.col_no + self.end_pos - n, n + 1) + + return Location(self.file_name, end_line, end_col) + class Token_Base: def __init__(self, location, kind, value):