top-level context manager
This commit is contained in:
parent
291c308cad
commit
babda0a037
1 changed files with 40 additions and 36 deletions
|
|
@ -27,8 +27,10 @@ class _Parser:
|
||||||
def __init__(self, text: str):
|
def __init__(self, text: str):
|
||||||
self._text = text
|
self._text = text
|
||||||
self._pos = 0
|
self._pos = 0
|
||||||
|
self._error_highlight_pos = 0
|
||||||
self._ast: List[ArgumentType] = []
|
self._ast: List[ArgumentType] = []
|
||||||
|
|
||||||
|
with self._error_formatting():
|
||||||
self._syntax_tree = self._parse()
|
self._syntax_tree = self._parse()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -42,14 +44,16 @@ class _Parser:
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def _error_formatting(self) -> None:
|
def _error_formatting(self) -> None:
|
||||||
parked_pos = self._pos
|
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
except NonFormattedInvalidSyntaxException as exc:
|
except NonFormattedInvalidSyntaxException as exc:
|
||||||
raise ParserExceptionFormatter(
|
raise ParserExceptionFormatter(
|
||||||
self._text, parked_pos, self._pos, exc
|
self._text, self._error_highlight_pos, self._pos, exc
|
||||||
).highlight() from exc
|
).highlight() from exc
|
||||||
|
|
||||||
|
def _set_highlight_position(self) -> None:
|
||||||
|
self._error_highlight_pos = self._pos
|
||||||
|
|
||||||
def _read(self, increment_pos: bool = True, length: int = 1) -> Optional[str]:
|
def _read(self, increment_pos: bool = True, length: int = 1) -> Optional[str]:
|
||||||
try:
|
try:
|
||||||
ch = self._text[self._pos : (self._pos + length)]
|
ch = self._text[self._pos : (self._pos + length)]
|
||||||
|
|
@ -240,7 +244,7 @@ class _Parser:
|
||||||
key: Optional[ArgumentType] = None
|
key: Optional[ArgumentType] = None
|
||||||
in_comma = False
|
in_comma = False
|
||||||
|
|
||||||
with self._error_formatting():
|
self._set_highlight_position()
|
||||||
while ch := self._read(increment_pos=False):
|
while ch := self._read(increment_pos=False):
|
||||||
if ch == "}":
|
if ch == "}":
|
||||||
if key is not None:
|
if key is not None:
|
||||||
|
|
@ -258,14 +262,14 @@ class _Parser:
|
||||||
in_comma = True
|
in_comma = True
|
||||||
self._pos += 1
|
self._pos += 1
|
||||||
elif key is None:
|
elif key is None:
|
||||||
with self._error_formatting():
|
self._set_highlight_position()
|
||||||
in_comma = False
|
in_comma = False
|
||||||
key_args = self._parse_args(breaking_chars=":")
|
key_args = self._parse_args(breaking_chars=":")
|
||||||
if len(key_args) != 1:
|
if len(key_args) != 1:
|
||||||
raise StringFormattingException("Lazy parsing but got mlutiple args")
|
raise StringFormattingException("Lazy parsing but got mlutiple args")
|
||||||
key = key_args[0]
|
key = key_args[0]
|
||||||
elif key is not None and ch == ":":
|
elif key is not None and ch == ":":
|
||||||
with self._error_formatting():
|
self._set_highlight_position()
|
||||||
self._pos += 1
|
self._pos += 1
|
||||||
value_args = self._parse_args(breaking_chars=",}")
|
value_args = self._parse_args(breaking_chars=",}")
|
||||||
if len(value_args) != 1:
|
if len(value_args) != 1:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue