Fix: handle zero-length children correctly

This commit is contained in:
John Doty 2024-06-09 07:11:39 -07:00
parent bea7903686
commit c74a36463e

View file

@ -322,21 +322,29 @@ class Parser:
case parser.Reduce(name=name, count=size, transparent=transparent): case parser.Reduce(name=name, count=size, transparent=transparent):
children: list[TokenValue | Tree] = [] children: list[TokenValue | Tree] = []
for _, c in stack[-size:]: if size > 0:
if c is None: for _, c in stack[-size:]:
continue if c is None:
elif isinstance(c, Tree) and c.name is None: continue
children.extend(c.children) elif isinstance(c, Tree) and c.name is None:
else: children.extend(c.children)
children.append(c) else:
children.append(c)
del stack[-size:]
start = children[0].start
end = children[-1].end
else:
start = end = current_token.start
value = Tree( value = Tree(
name=name if not transparent else None, name=name if not transparent else None,
start=children[0].start, start=start,
end=children[-1].end, end=end,
children=tuple(children), children=tuple(children),
) )
del stack[-size:]
goto = self.table.gotos[stack[-1][0]].get(name) goto = self.table.gotos[stack[-1][0]].get(name)
assert goto is not None assert goto is not None
stack.append((goto, value)) stack.append((goto, value))