Better lexer error handling
This commit is contained in:
parent
b47eb7ce10
commit
03c24a2074
@ -1,6 +1,7 @@
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
from .lexer import LexToken
|
if typing.TYPE_CHECKING:
|
||||||
|
from .lexer import LexToken
|
||||||
|
|
||||||
|
|
||||||
class CxxParseError(Exception):
|
class CxxParseError(Exception):
|
||||||
|
@ -7,6 +7,12 @@ import sys
|
|||||||
from ._ply import lex
|
from ._ply import lex
|
||||||
from ._ply.lex import TOKEN
|
from ._ply.lex import TOKEN
|
||||||
|
|
||||||
|
from .errors import CxxParseError
|
||||||
|
|
||||||
|
|
||||||
|
class LexError(CxxParseError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info >= (3, 8):
|
if sys.version_info >= (3, 8):
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
@ -249,7 +255,11 @@ class Lexer:
|
|||||||
return t
|
return t
|
||||||
|
|
||||||
def t_error(self, t: LexToken) -> None:
|
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
|
_lexer = None
|
||||||
lex: lex.Lexer
|
lex: lex.Lexer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user