Skip to content

Commit

Permalink
Add support for str err
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidchu committed Jul 13, 2024
1 parent c19e293 commit f966ab7
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/compiler/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
class LolError:
def __init__(
self,
input_path: Path,
input_: Path | str,
start_position: int,
end_position: int,
message: str,
):
self.error_string = LolError.create_error_string(
input_path, start_position, end_position, message
input_, start_position, end_position, message
)

self.input_path = input_path
self.input_ = input_
self.start_position = start_position
self.end_position = end_position
self.message = message
Expand All @@ -30,7 +30,7 @@ def get_text_of_interest(self) -> str:
@note This intention of this function is for debugging.
"""
with self.input_path.open() as f:
with self.input_.open() as f:
text = f.read()

return text[self.start_position : self.end_position]
Expand Down Expand Up @@ -72,15 +72,18 @@ def create_single_line_error_string(
text_lines: list[str] = input_text.splitlines()
user_line = lineno + 1
return (
f"Error: {message}\n"
f"> Error: {message}\n"
f"> {user_line} | {text_lines[lineno]}\n"
# NOTE I just guessed for the number of spaces and '^'
f"> {' ' * len(str(user_line))} | {' ' * (start_col)}{'^' * (end_col-start_col)}\n"
)

@staticmethod
def create_error_string(
input_path: Path, start_position: int, end_position: int, message: str
input_: Path | str,
start_position: int,
end_position: int,
message: str,
) -> str:
"""
@brief Create a string with the error underlined and the error message displayed.
Expand All @@ -101,8 +104,11 @@ def create_error_string(
"""
assert start_position < end_position

with input_path.open() as f:
input_text: str = f.read()
if isinstance(input_, Path):
with input_.open() as f:
input_text: str = f.read()
elif isinstance(input_, str):
input_text = input_
start_line, start_col = LolError.get_line_and_column(
input_text, start_position
)
Expand Down

0 comments on commit f966ab7

Please sign in to comment.