Adjustments to error reporting and other fixes

This commit is contained in:
Kyler 2024-03-07 00:15:51 -07:00
parent 5a5ed5bee6
commit c3af4ecfa5
4 changed files with 51 additions and 23 deletions

View File

@ -1,6 +1,8 @@
# Kyler Olsen
# Feb 2024
from textwrap import indent
class FileInfo:
@ -65,6 +67,8 @@ class FileInfo:
class CompilerError(Exception):
_compiler_error_type = "Compiler"
def __init__(self, message: str, file_info: FileInfo):
new_message = message
new_message += (
@ -84,3 +88,40 @@ class CompilerError(Exception):
file_info.col - 1) + '^' * file_info.length
super().__init__(new_message)
def compiler_error(self) -> str:
return (
f"[{self._compiler_error_type} Error] {type(self).__name__}:\n"
f"{indent(str(self), ' |', lambda _: True)}"
)
class CompilerWarning(Warning):
_compiler_warning_type = "Compiler"
def __init__(self, message: str, file_info: FileInfo):
new_message = message
new_message += (
f"\nIn file {file_info.filename} at line {file_info.line} "
)
if file_info.lines:
new_message += f"to line {file_info.line + file_info.lines}"
with open(file_info.filename, 'r') as file:
new_message += ''.join(
file.readlines()[
file_info.line-1:file_info.line + file_info.lines])
else:
new_message += f"col {file_info.col}\n\n"
with open(file_info.filename, 'r') as file:
new_message += file.readlines()[file_info.line-1]
new_message += ' ' * (
file_info.col - 1) + '^' * file_info.length
super().__init__(new_message)
def compiler_error(self) -> str:
return (
f"[{self._compiler_warning_type} Warning] {type(self).__name__}:\n"
f"{indent(str(self), ' |', lambda _: True)}"
)

View File

@ -7,7 +7,9 @@ from typing import ClassVar, Sequence
from .compiler_types import CompilerError, FileInfo
class LexerError(CompilerError): pass
class LexerError(CompilerError):
_compiler_error_type = "Lexical"
class _InterTokenType(Enum):

View File

@ -1,7 +1,6 @@
# Kyler Olsen
# Feb 2024
from textwrap import indent
from typing import Sequence
import argparse
@ -24,24 +23,7 @@ def _compile(args: argparse.Namespace):
def compile(args: argparse.Namespace):
try: _compile(args)
except LexerError as e:
print(
f"[Lexical Error] {type(e).__name__}:\n"
f"{indent(str(e), ' |', lambda _: True)}"
)
# raise
except SyntaxError as e:
print(
f"[Syntax Error] {type(e).__name__}:\n"
f"{indent(str(e), ' |', lambda _: True)}"
)
# raise
except CompilerError as e:
print(
f"[Compiler Error] {type(e).__name__}:\n"
f"{indent(str(e), ' |', lambda _: True)}"
)
# raise
except CompilerError as e: print(e.compiler_error())
except Exception as e:
raise Exception(
"You found an error in the compiler!\n"

View File

@ -8,7 +8,9 @@ from .compiler_types import CompilerError , FileInfo
from . import lexer
class SyntaxError(CompilerError): pass
class SyntaxError(CompilerError):
_compiler_error_type = "Syntax"
class UnexpectedEndOfTokenStream(SyntaxError): pass
@ -82,7 +84,7 @@ class _UnexpectedTokenBase(_ExpectedTokenBase):
s = ""
for i in expected[:-1]:
s += i + "', '"
s = s[:-1] + "or '" + i
s = s[:-1] + "or '" + expected[-1]
expected = s
else:
expected = expected[0]
@ -1620,7 +1622,8 @@ def _assert_token_literal(
)
if isinstance(token, lexer.Keyword):
if token.value not in BuiltInConstEnum:
raise UnexpectedKeyword(token, [i.value for i in BuiltInDataTypeEnum])
raise UnexpectedKeyword(
token, [i.value for i in BuiltInDataTypeEnum])
def _literal_map(literal: (
lexer.Keyword |