Add "ifelsestmt" reduce rule checking

This commit is contained in:
rocky
2020-01-21 16:01:19 -05:00
parent 8684137f80
commit afedf43ee1
2 changed files with 16 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2018 Rocky Bernstein # Copyright (c) 2016-2018, 2020 Rocky Bernstein
""" """
spark grammar differences over Python2.6 for Python 2.5. spark grammar differences over Python2.6 for Python 2.5.
""" """
@@ -6,6 +6,7 @@ spark grammar differences over Python2.6 for Python 2.5.
from uncompyle6.parser import PythonParserSingle from uncompyle6.parser import PythonParserSingle
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse26 import Python26Parser from uncompyle6.parsers.parse26 import Python26Parser
from uncompyle6.parsers.reducecheck import (ifelsestmt)
class Python25Parser(Python26Parser): class Python25Parser(Python26Parser):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG): def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
@@ -98,8 +99,9 @@ class Python25Parser(Python26Parser):
""") """)
super(Python25Parser, self).customize_grammar_rules(tokens, customize) super(Python25Parser, self).customize_grammar_rules(tokens, customize)
if self.version == 2.5: if self.version == 2.5:
self.check_reduce['try_except'] = 'tokens' self.check_reduce["try_except"] = "tokens"
self.check_reduce['aug_assign1'] = 'AST' self.check_reduce["aug_assign1"] = "AST"
self.check_reduce["ifelsestmt"] = "AST"
def reduce_is_invalid(self, rule, ast, tokens, first, last): def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python25Parser, invalid = super(Python25Parser,
@@ -107,15 +109,19 @@ class Python25Parser(Python26Parser):
tokens, first, last) tokens, first, last)
if invalid or tokens is None: if invalid or tokens is None:
return invalid return invalid
if rule == ('aug_assign1', ('expr', 'expr', 'inplace_op', 'store')): if rule == ("aug_assign1", ("expr", "expr", "inplace_op", "store")):
return ast[0][0] == 'and' return ast[0][0] == "and"
lhs = rule[0]
n = len(tokens)
if lhs == "ifelsestmt":
return ifelsestmt(self, lhs, n, rule, ast, tokens, first, last)
return False return False
class Python25ParserSingle(Python26Parser, PythonParserSingle): class Python25ParserSingle(Python26Parser, PythonParserSingle):
pass pass
if __name__ == '__main__': if __name__ == "__main__":
# Check grammar # Check grammar
p = Python25Parser() p = Python25Parser()
p.check_grammar() p.check_grammar()

View File

@@ -64,7 +64,6 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
), ),
): ):
return False return False
# Make sure all of the "come froms" offset at the # Make sure all of the "come froms" offset at the
# end of the "if" come from somewhere inside the "if". # end of the "if" come from somewhere inside the "if".
# Since the come_froms are ordered so that lowest # Since the come_froms are ordered so that lowest
@@ -115,8 +114,10 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
jf_cf_pop = ast[2] jf_cf_pop = ast[2]
if jf_cf_pop == "jf_cf_pop" and jf_cf_pop[0] == "JUMP_FORWARD": if jf_cf_pop == "jf_cf_pop" and jf_cf_pop[0] == "JUMP_FORWARD":
int(jf_cf_pop[0].pattr) jump_forward = jf_cf_pop[0]
if int(jf_cf_pop[0].pattr) < tokens[last-1].off2int(): endif_target = int(jump_forward.pattr)
last_offset = tokens[min(last, n-1)].off2int()
if endif_target != last_offset:
return True return True
# The jump inside "else" check below should be added. # The jump inside "else" check below should be added.