parser: use IdNode for foreach varnames

pull/12152/head
Charles Brunet 1 year ago
parent 306562b466
commit 8d357ba62c
  1. 4
      mesonbuild/ast/printer.py
  2. 2
      mesonbuild/ast/visitor.py
  3. 4
      mesonbuild/interpreterbase/interpreterbase.py
  4. 8
      mesonbuild/mparser.py

@ -185,7 +185,7 @@ class AstPrinter(AstVisitor):
def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
node.lineno = self.curr_line or node.lineno
self.append_padded('foreach', node)
self.append_padded(', '.join(node.varnames), node)
self.append_padded(', '.join(varname.value for varname in node.varnames), node)
self.append_padded(':', node)
node.items.accept(self)
self.newline()
@ -373,7 +373,7 @@ class AstJSONPrinter(AstVisitor):
def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
self._accept('items', node.items)
self._accept('block', node.block)
self.current['varnames'] = node.varnames
self.current['varnames'] = [varname.value for varname in node.varnames]
self.setbase(node)
def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:

@ -123,6 +123,8 @@ class AstVisitor:
def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
self.visit_default_func(node)
for varname in node.varnames:
varname.accept(self)
node.items.accept(self)
node.block.accept(self)

@ -459,14 +459,14 @@ class InterpreterBase:
if tsize is None:
if isinstance(i, tuple):
raise mesonlib.MesonBugException(f'Iteration of {items} returned a tuple even though iter_tuple_size() is None')
self.set_variable(node.varnames[0], self._holderify(i))
self.set_variable(node.varnames[0].value, self._holderify(i))
else:
if not isinstance(i, tuple):
raise mesonlib.MesonBugException(f'Iteration of {items} did not return a tuple even though iter_tuple_size() is {tsize}')
if len(i) != tsize:
raise mesonlib.MesonBugException(f'Iteration of {items} did not return a tuple even though iter_tuple_size() is {tsize}')
for j in range(tsize):
self.set_variable(node.varnames[j], self._holderify(i[j]))
self.set_variable(node.varnames[j].value, self._holderify(i[j]))
try:
self.evaluate_codeblock(node.block)
except ContinueRequest:

@ -518,11 +518,11 @@ class PlusAssignmentNode(BaseNode):
@dataclass(unsafe_hash=True)
class ForeachClauseNode(BaseNode):
varnames: T.List[str] = field(hash=False)
varnames: T.List[IdNode] = field(hash=False)
items: BaseNode
block: CodeBlockNode
def __init__(self, token: Token, varnames: T.List[str], items: BaseNode, block: CodeBlockNode):
def __init__(self, token: Token, varnames: T.List[IdNode], items: BaseNode, block: CodeBlockNode):
super().__init__(token.lineno, token.colno, token.filename)
self.varnames = varnames
self.items = items
@ -898,13 +898,13 @@ class Parser:
self.expect('id')
assert isinstance(t.value, str)
varname = t
varnames = [t.value]
varnames = [IdNode(t)]
if self.accept('comma'):
t = self.current
self.expect('id')
assert isinstance(t.value, str)
varnames.append(t.value)
varnames.append(IdNode(t))
self.expect('colon')
items = self.statement()

Loading…
Cancel
Save