Files
python-uncompyle6/uncompyle6/semantics/check_ast.py
rocky 63a43d0c93 Profiling workarounds, more coverage ...
test/Makefile: more grammar checking. Update python versions
10_del.pyc add test of DEL_GLOBAL

check_ast.py, pysource.py: Profileing workarounds
2017-11-15 03:16:54 -05:00

29 lines
1.1 KiB
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):
if ast is None:
return
in_loop = in_loop or ast.kind in ('while1stmt', 'whileTruestmt',
'whilestmt', 'whileelsestmt', 'while1elsestmt',
'for_block')
if ast.kind in ('augassign1', 'augassign2') and ast[0][0] == 'and':
text = str(ast)
error_text = '\n# improper augmented assigment (e.g. +=, *=, ...):\n#\t' + '\n# '.join(text.split("\n")) + '\n'
errors.append(error_text)
for node in ast:
if not in_loop and node.kind 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)