Make DoesSomething apply to labels optionally.

Allows detection of empty events, for example, even if they have labels.

Also, it is OK if there's a label inserted in a nested {}; that case wasn't contemplated.
This commit is contained in:
Sei Lisa 2017-10-29 13:18:35 +01:00
parent f2a6243695
commit e13f210695

View file

@ -131,17 +131,20 @@ class foldconst(object):
else: else:
idx += 1 idx += 1
def DoesSomething(self, node): def DoesSomething(self, node, labels = True):
"""Tell if a subtree does something or is just empty statements """Tell if a subtree does something or is just empty statements
(a pure combination of ';' and '{}') (a pure combination of ';' and '{}'). Labels are the top level are
considered to do something if labels is True, and vice versa.
Not to be confused with lslparse.does_something which includes labels, Not to be confused with lslparse.does_something which always includes
and applies to a block's statement list, not to a node. labels, and applies to a block's statement list, not to a node.
""" """
if node['nt'] != ';': maybe_label = ';' if labels else '@'
if maybe_label != node['nt'] != ';':
if node['nt'] == '{}': if node['nt'] == '{}':
for subnode in node['ch']: for subnode in node['ch']:
if self.DoesSomething(subnode): # Labels embedded in {} are not reachable. They do nothing.
if self.DoesSomething(subnode, labels = False):
return True return True
else: else:
return True return True