From 400943bb6a76cb1f42214d6f4925318dc72fe049 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 13 Apr 2019 22:22:37 -0400 Subject: [PATCH] 3.8 async for/with... More is needed though. --- uncompyle6/parsers/parse35.py | 2 +- uncompyle6/parsers/parse38.py | 49 +++++++++++++++++++++++------- uncompyle6/semantics/customize3.py | 19 +++++++++--- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/uncompyle6/parsers/parse35.py b/uncompyle6/parsers/parse35.py index 1ef38743..4ff4b94b 100644 --- a/uncompyle6/parsers/parse35.py +++ b/uncompyle6/parsers/parse35.py @@ -160,7 +160,7 @@ class Python35Parser(Python34Parser): call_token = tokens[i+1] rule = 'call ::= expr unmapexpr ' + call_token.kind self.addRule(rule, nop_func) - elif opname == 'BEFORE_ASYNC_WITH': + elif opname == 'BEFORE_ASYNC_WITH' and self.version < 3.8: # Some Python 3.5+ async additions rules_str = """ async_with_stmt ::= expr diff --git a/uncompyle6/parsers/parse38.py b/uncompyle6/parsers/parse38.py index 969706db..afe112af 100644 --- a/uncompyle6/parsers/parse38.py +++ b/uncompyle6/parsers/parse38.py @@ -35,21 +35,40 @@ class Python38Parser(Python37Parser): stmt ::= try_except38 stmt ::= whilestmt38 stmt ::= whileTruestmt38 + stmt ::= call # FIXME this should be restricted to being inside a try block stmt ::= except_ret38 - async_for_stmt38 ::= expr - GET_AITER - SETUP_FINALLY - GET_ANEXT - LOAD_CONST - YIELD_FROM - POP_BLOCK - store for_block - COME_FROM_FINALLY - END_ASYNC_FOR + # FIXME this should be added only when seeing GET_AITER or YIELD_FROM + async_for_stmt38 ::= expr + GET_AITER + SETUP_FINALLY + GET_ANEXT + LOAD_CONST + YIELD_FROM + POP_BLOCK + store for_block + COME_FROM_FINALLY + END_ASYNC_FOR + async_with_stmt ::= expr BEFORE_ASYNC_WITH GET_AWAITABLE LOAD_CONST YIELD_FROM + SETUP_ASYNC_WITH POP_TOP + suite_stmts + POP_TOP POP_BLOCK + BEGIN_FINALLY + WITH_CLEANUP_START + GET_AWAITABLE LOAD_CONST YIELD_FROM + WITH_CLEANUP_FINISH END_FINALLY + + async_with_as_stmt ::= expr BEFORE_ASYNC_WITH GET_AWAITABLE LOAD_CONST YIELD_FROM + SETUP_ASYNC_WITH store + suite_stmts + POP_TOP POP_BLOCK + BEGIN_FINALLY + WITH_CLEANUP_START + GET_AWAITABLE LOAD_CONST YIELD_FROM + WITH_CLEANUP_FINISH END_FINALLY return ::= ret_expr ROT_TWO POP_TOP RETURN_VALUE @@ -132,7 +151,8 @@ class Python38Parser(Python37Parser): END_FINALLY for_block COME_FROM POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP POP_BLOCK - COME_FROM_LOOP + COME_FROM_LOOP + for ::= SETUP_LOOP expr for_iter store for_block POP_BLOCK for ::= SETUP_LOOP expr for_iter store for_block POP_BLOCK NOP @@ -154,6 +174,7 @@ class Python38Parser(Python37Parser): """) super(Python37Parser, self).customize_grammar_rules(tokens, customize) self.check_reduce['ifstmt'] = 'tokens' + self.check_reduce['whileTruestmt38'] = 'tokens' def reduce_is_invalid(self, rule, ast, tokens, first, last): invalid = super(Python38Parser, @@ -178,6 +199,12 @@ class Python38Parser(Python37Parser): pass pass pass + elif rule[0] == 'whileTruestmt38': + t = tokens[last-1] + if t.kind == 'JUMP_BACK': + return t.attr != tokens[first].offset + pass + return False class Python38ParserSingle(Python38Parser, PythonParserSingle): diff --git a/uncompyle6/semantics/customize3.py b/uncompyle6/semantics/customize3.py index 7763bce4..e000e172 100644 --- a/uncompyle6/semantics/customize3.py +++ b/uncompyle6/semantics/customize3.py @@ -285,14 +285,16 @@ def customize_for_version3(self, version): 'await_expr': ( 'await %c', 0), 'await_stmt': ( '%|%c\n', 0), 'async_for_stmt': ( - '%|async for %c in %c:\n%+%c%-\n\n', 9, 1, 25 ), + '%|async for %c in %c:\n%+%|%c%-\n\n', 9, 1, 25 ), 'async_forelse_stmt': ( '%|async for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n', 9, 1, 25, (27, 'else_suite') ), 'async_with_stmt': ( - '%|async with %c:\n%+%c%-', 0, 7), + '%|async with %c:\n%+%|%c%-', + (0, 'expr'), 7 ), 'async_with_as_stmt': ( - '%|async with %c as %c:\n%+%c%-', 0, 6, 7), + '%|async with %c as %c:\n%+%|%c%-', + (0, 'expr'), (6, 'store'), 7), 'unmap_dict': ( '{**%C}', (0, -1, ', **') ), # 'unmapexpr': ( '{**%c}', 0), # done by n_unmapexpr @@ -952,7 +954,16 @@ def customize_for_version3(self, version): TABLE_DIRECT.update({ 'async_for_stmt38': ( '%|async for %c in %c:\n%+%c%-%-\n\n', - (0, 'expr'), (7, 'store'), (8, 'for_block') ), + (7, 'store'), (0, 'expr'), (8, 'for_block') ), + + 'async_with_stmt38': ( + '%|async with %c:\n%+%|%c%-', + (0, 'expr'), 7), + + 'async_with_as_stmt38': ( + '%|async with %c as %c:\n%+%|%c%-', + (0, 'expr'), (6, 'store'), + (7, 'suite_stmts') ), 'except_handler38a': ( '%c', (-2, 'stmts') ),