Better lexer error handling

This commit is contained in:
Dustin Spicuzza 2022-12-10 14:48:20 -05:00
parent b47eb7ce10
commit 03c24a2074
2 changed files with 13 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import typing
from .lexer import LexToken
if typing.TYPE_CHECKING:
from .lexer import LexToken
class CxxParseError(Exception):

View File

@ -7,6 +7,12 @@ import sys
from ._ply import lex
from ._ply.lex import TOKEN
from .errors import CxxParseError
class LexError(CxxParseError):
pass
if sys.version_info >= (3, 8):
from typing import Protocol
@ -249,7 +255,11 @@ class Lexer:
return t
def t_error(self, t: LexToken) -> None:
print("Lex error: ", t)
self._error(f"Illegal character {t.value!r}", t)
def _error(self, msg: str, tok: LexToken):
tok.location = self.current_location()
raise LexError(msg, tok)
_lexer = None
lex: lex.Lexer