parser: add ElseNode

pull/12152/head
Charles Brunet 1 year ago
parent 4b7a56caa2
commit f13260dd43
  1. 2
      mesonbuild/ast/interpreter.py
  2. 4
      mesonbuild/ast/visitor.py
  3. 2
      mesonbuild/interpreterbase/interpreterbase.py
  4. 17
      mesonbuild/mparser.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

@ -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)

@ -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]:

@ -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:

Loading…
Cancel
Save