visit full ast for AstConditionLevel

Use the new `FullAstVisitor` for `AstConditionLevel`. This will allow
proper formatting of `if / else / endif` and `foreach / endforeach`
blocks.
pull/13040/merge
Charles Brunet 1 year ago committed by Dylan Baker
parent be9e9af15a
commit 2a962ffd63
  1. 41
      mesonbuild/ast/postprocess.py

@ -5,7 +5,7 @@
# or an interpreter-based tool # or an interpreter-based tool
from __future__ import annotations from __future__ import annotations
from .visitor import AstVisitor from .visitor import AstVisitor, FullAstVisitor
import typing as T import typing as T
if T.TYPE_CHECKING: if T.TYPE_CHECKING:
@ -78,32 +78,41 @@ class AstIDGenerator(AstVisitor):
node.ast_id = name + '#' + str(self.counter[name]) node.ast_id = name + '#' + str(self.counter[name])
self.counter[name] += 1 self.counter[name] += 1
class AstConditionLevel(AstVisitor): class AstConditionLevel(FullAstVisitor):
def __init__(self) -> None: def __init__(self) -> None:
self.condition_level = 0 self.condition_level = 0
def visit_default_func(self, node: mparser.BaseNode) -> None: def enter_node(self, node: mparser.BaseNode) -> None:
node.condition_level = self.condition_level node.condition_level = self.condition_level
def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None: def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
self.visit_default_func(node) self.enter_node(node)
self.condition_level += 1 node.foreach_.accept(self)
for varname in node.varnames:
varname.accept(self)
for comma in node.commas:
comma.accept(self)
node.colon.accept(self)
node.items.accept(self) node.items.accept(self)
self.condition_level += 1
node.block.accept(self) node.block.accept(self)
self.condition_level -= 1 self.condition_level -= 1
node.endforeach.accept(self)
def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None: self.exit_node(node)
self.visit_default_func(node)
for i in node.ifs:
i.accept(self)
if node.elseblock:
self.condition_level += 1
node.elseblock.accept(self)
self.condition_level -= 1
def visit_IfNode(self, node: mparser.IfNode) -> None: def visit_IfNode(self, node: mparser.IfNode) -> None:
self.visit_default_func(node) self.enter_node(node)
self.condition_level += 1 node.if_.accept(self)
node.condition.accept(self) node.condition.accept(self)
self.condition_level += 1
node.block.accept(self)
self.condition_level -= 1
self.exit_node(node)
def visit_ElseNode(self, node: mparser.ElseNode) -> None:
self.enter_node(node)
node.else_.accept(self)
self.condition_level += 1
node.block.accept(self) node.block.accept(self)
self.condition_level -= 1 self.condition_level -= 1
self.exit_node(node)

Loading…
Cancel
Save