From f13260dd43771bd7e9594cbad942b7d9aa4292fc Mon Sep 17 00:00:00 2001 From: Charles Brunet Date: Fri, 25 Aug 2023 09:18:21 -0400 Subject: [PATCH] parser: add ElseNode --- mesonbuild/ast/interpreter.py | 2 +- mesonbuild/ast/visitor.py | 4 ++++ mesonbuild/interpreterbase/interpreterbase.py | 2 +- mesonbuild/mparser.py | 17 +++++++++++++---- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py index 1dea817b1..da2119c89 100644 --- a/mesonbuild/ast/interpreter.py +++ b/mesonbuild/ast/interpreter.py @@ -312,7 +312,7 @@ class AstInterpreter(InterpreterBase): for i in node.ifs: self.evaluate_codeblock(i.block) if not isinstance(node.elseblock, EmptyNode): - self.evaluate_codeblock(node.elseblock) + self.evaluate_codeblock(node.elseblock.block) def get_variable(self, varname: str) -> int: return 0 diff --git a/mesonbuild/ast/visitor.py b/mesonbuild/ast/visitor.py index 5b1fb6a09..a83782655 100644 --- a/mesonbuild/ast/visitor.py +++ b/mesonbuild/ast/visitor.py @@ -143,6 +143,10 @@ class AstVisitor: node.condition.accept(self) node.block.accept(self) + def visit_ElseNode(self, node: mparser.IfNode) -> None: + self.visit_default_func(node) + node.block.accept(self) + def visit_TernaryNode(self, node: mparser.TernaryNode) -> None: self.visit_default_func(node) node.condition.accept(self) diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py index 7e3f8da5a..5b07dc075 100644 --- a/mesonbuild/interpreterbase/interpreterbase.py +++ b/mesonbuild/interpreterbase/interpreterbase.py @@ -303,7 +303,7 @@ class InterpreterBase: mesonlib.project_meson_versions[self.subproject] = prev_meson_version return None if not isinstance(node.elseblock, mparser.EmptyNode): - self.evaluate_codeblock(node.elseblock) + self.evaluate_codeblock(node.elseblock.block) return None def evaluate_testcase(self, node: mparser.TestCaseClauseNode) -> T.Optional[Disabler]: diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index ce83cc283..6b578b5af 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -534,17 +534,25 @@ class IfNode(BaseNode): self.condition = condition self.block = block +@dataclass(unsafe_hash=True) +class ElseNode(BaseNode): + + block: CodeBlockNode + + def __init__(self, block: CodeBlockNode): + super().__init__(block.lineno, block.colno, block.filename) + self.block = block @dataclass(unsafe_hash=True) class IfClauseNode(BaseNode): ifs: T.List[IfNode] = field(hash=False) - elseblock: T.Union[EmptyNode, CodeBlockNode] + elseblock: T.Union[EmptyNode, ElseNode] def __init__(self, linenode: BaseNode): super().__init__(linenode.lineno, linenode.colno, linenode.filename) self.ifs = [] - self.elseblock = None + self.elseblock = EmptyNode(linenode.lineno, linenode.colno, linenode.filename) @dataclass(unsafe_hash=True) class TestCaseClauseNode(BaseNode): @@ -919,10 +927,11 @@ class Parser: b = self.codeblock() clause.ifs.append(IfNode(s, s, b)) - def elseblock(self) -> T.Union[CodeBlockNode, EmptyNode]: + def elseblock(self) -> T.Union[ElseNode, EmptyNode]: if self.accept('else'): self.expect('eol') - return self.codeblock() + block = self.codeblock() + return ElseNode(block) return EmptyNode(self.current.lineno, self.current.colno, self.current.filename) def testcaseblock(self) -> TestCaseClauseNode: