Skip to content

Commit

Permalink
Add location pointing to the end of a source
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Rosskopf authored and markusrosskopf committed Aug 23, 2023
1 parent c4eab07 commit 486e636
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ dist
docs
.coverage*
*.lobster
.DS_Store
.venv/
.idea
3 changes: 3 additions & 0 deletions trlc/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions trlc/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 486e636

Please sign in to comment.