Files
python-uncompyle6/uncompyle6/semantics/check_ast.py
rocky 6fb879d0d8 Detect some erroneous decompilations
Until we can actually prevent these in grammar rules, we will warn of
improper decompilations.

Also, we now stop when we hit a decompile error. Previously we were not.
2016-11-10 22:29:39 -05:00

22 lines
856 B
Python

"""
Python AST grammar checker.
Our rules sometimes give erroneous results. Until we have perfect rules,
This checker will catch mistakes in decompilation we've made.
FIXME idea: extend parsing system to do same kinds of checks or nonterminal
before reduction and don't reduce when there is a problem.
"""
def checker(ast, in_loop, errors):
in_loop = in_loop or ast.type in ('while1stmt', 'whileTruestmt',
'whilestmt', 'whileelsestmt',
'for_block')
for node in ast:
if not in_loop and node.type in ('continue_stmt', 'break_stmt'):
text = str(node)
error_text = '\n# not in loop:\n#\t' + '\n# '.join(text.split("\n"))
errors.append(error_text)
if hasattr(node, '__repr1__'):
checker(node, in_loop, errors)