aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mparser.py
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2023-03-01 23:28:43 -0500
committerEli Schwartz <eschwartz@archlinux.org>2023-03-01 23:28:43 -0500
commitcc23996266846890fea708a7eb5dcff66d44c8b6 (patch)
treedad42b453895975643e623c9264944f1ae189c2a /mesonbuild/mparser.py
parent9423631b76093480e619beb2281caa017c981f7a (diff)
downloadmeson-cc23996266846890fea708a7eb5dcff66d44c8b6.zip
meson-cc23996266846890fea708a7eb5dcff66d44c8b6.tar.gz
meson-cc23996266846890fea708a7eb5dcff66d44c8b6.tar.bz2
mparser: Add partial AST to exceptions
Surprisingly enough we need to do this twice. In some cases (failing-meson/72 triggers this) we can error out after parsing the codeblock, but without getting the expected eof. We need to catch both exceptions as either one can interrupt the built codeblock object. Co-authored-by: Xavier Claessens <xavier.claessens@collabora.com>
Diffstat (limited to 'mesonbuild/mparser.py')
-rw-r--r--mesonbuild/mparser.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
index dbb6a45..fb0ec95 100644
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -41,6 +41,9 @@ def decode_match(match: T.Match[str]) -> str:
return codecs.decode(match.group(0).encode(), 'unicode_escape')
class ParseException(MesonException):
+
+ ast: T.Optional[CodeBlockNode] = None
+
def __init__(self, text: str, line: str, lineno: int, colno: int) -> None:
# Format as error message, followed by the line with the error, followed by a caret to show the error column.
super().__init__(mlog.code_line(text, line, colno))
@@ -548,7 +551,11 @@ class Parser:
def parse(self) -> CodeBlockNode:
block = self.codeblock()
- self.expect('eof')
+ try:
+ self.expect('eof')
+ except ParseException as e:
+ e.ast = block
+ raise
return block
def statement(self) -> BaseNode:
@@ -838,9 +845,13 @@ class Parser:
def codeblock(self) -> CodeBlockNode:
block = CodeBlockNode(self.current)
cond = True
- while cond:
- curline = self.line()
- if not isinstance(curline, EmptyNode):
- block.lines.append(curline)
- cond = self.accept('eol')
+ try:
+ while cond:
+ curline = self.line()
+ if not isinstance(curline, EmptyNode):
+ block.lines.append(curline)
+ cond = self.accept('eol')
+ except ParseException as e:
+ e.ast = block
+ raise
return block