Skip to content

Commit e6ff603

Browse files
authored
Merge pull request #489 from rocky/withasstmt-to-with_as
withasstmt -> with_as
2 parents 3724e02 + 156188f commit e6ff603

File tree

13 files changed

+348
-249
lines changed

13 files changed

+348
-249
lines changed

uncompyle6/parser.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2023 Rocky Bernstein
1+
# Copyright (c) 2015-2024 Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
# Copyright (c) 1999 John Aycock
@@ -21,10 +21,11 @@
2121

2222
import sys
2323

24-
from spark_parser import GenericASTBuilder, DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
25-
from uncompyle6.show import maybe_show_asm
24+
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG, GenericASTBuilder
2625
from xdis import iscode
2726

27+
from uncompyle6.show import maybe_show_asm
28+
2829

2930
class ParserError(Exception):
3031
def __init__(self, token, offset, debug=PARSER_DEFAULT_DEBUG):
@@ -91,7 +92,14 @@ def __init__(self, syntax_tree_class, start, debug):
9192
# singleton reduction that we can simplify. It also happens to be optional
9293
# in its other derivation
9394
self.optional_nt |= frozenset(
94-
("come_froms", "suite_stmts", "l_stmts_opt", "c_stmts_opt", "stmts_opt", "stmt")
95+
(
96+
"come_froms",
97+
"suite_stmts",
98+
"l_stmts_opt",
99+
"c_stmts_opt",
100+
"stmts_opt",
101+
"stmt",
102+
)
95103
)
96104

97105
# Reduce singleton reductions in these nonterminals:
@@ -113,10 +121,10 @@ def ast_first_offset(self, ast):
113121

114122
def add_unique_rule(self, rule, opname, arg_count, customize):
115123
"""Add rule to grammar, but only if it hasn't been added previously
116-
opname and stack_count are used in the customize() semantic
117-
the actions to add the semantic action rule. Stack_count is
118-
used in custom opcodes like MAKE_FUNCTION to indicate how
119-
many arguments it has. Often it is not used.
124+
opname and stack_count are used in the customize() semantic
125+
the actions to add the semantic action rule. Stack_count is
126+
used in custom opcodes like MAKE_FUNCTION to indicate how
127+
many arguments it has. Often it is not used.
120128
"""
121129
if rule not in self.new_rules:
122130
# print("XXX ", rule) # debug
@@ -223,7 +231,9 @@ def get_pos_kw(self, token):
223231
"""
224232
# Low byte indicates number of positional parameters,
225233
# high byte number of keyword parameters
226-
assert token.kind.startswith("CALL_FUNCTION") or token.kind.startswith("CALL_METHOD")
234+
assert token.kind.startswith("CALL_FUNCTION") or token.kind.startswith(
235+
"CALL_METHOD"
236+
)
227237
args_pos = token.attr & 0xFF
228238
args_kw = (token.attr >> 8) & 0xFF
229239
return args_pos, args_kw
@@ -364,7 +374,7 @@ def p_stmt(self, args):
364374
stmt ::= tryelsestmt
365375
stmt ::= tryfinallystmt
366376
stmt ::= with
367-
stmt ::= withasstmt
377+
stmt ::= with_as
368378
369379
stmt ::= delete
370380
delete ::= DELETE_FAST
@@ -907,7 +917,7 @@ def python_parser(
907917
if __name__ == "__main__":
908918

909919
def parse_test(co):
910-
from xdis import PYTHON_VERSION_TRIPLE, IS_PYPY
920+
from xdis import IS_PYPY, PYTHON_VERSION_TRIPLE
911921

912922
ast = python_parser(PYTHON_VERSION_TRIPLE, co, showasm=True, is_pypy=IS_PYPY)
913923
print(ast)

uncompyle6/parsers/parse24.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2016-2018, 2020, 2022-2023 Rocky Bernstein
1+
# Copyright (c) 2016-2018, 2020, 2022-2024 Rocky Bernstein
22
"""
33
spark grammar differences over Python2.5 for Python 2.4.
44
"""
@@ -89,12 +89,14 @@ def customize_grammar_rules(self, tokens, customize):
8989
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM
9090
while1stmt ::= SETUP_LOOP returns COME_FROM
9191
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK COME_FROM
92+
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt POP_BLOCK
93+
LOAD_CONST COME_FROM with_cleanup
94+
with_as ::= expr setupwithas store suite_stmts_opt POP_BLOCK
95+
LOAD_CONST COME_FROM with_cleanup
9296
with_cleanup ::= LOAD_FAST DELETE_FAST WITH_CLEANUP END_FINALLY
9397
with_cleanup ::= LOAD_NAME DELETE_NAME WITH_CLEANUP END_FINALLY
94-
withasstmt ::= expr setupwithas store suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM with_cleanup
95-
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM with_cleanup
96-
stmt ::= with
97-
stmt ::= withasstmt
98+
stmt ::= with
99+
stmt ::= with_as
98100
"""
99101
)
100102
super(Python24Parser, self).customize_grammar_rules(tokens, customize)

uncompyle6/parsers/parse25.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2016-2018, 2020, 2022 Rocky Bernstein
1+
# Copyright (c) 2016-2018, 2020, 2022, 2024 Rocky Bernstein
22
"""
33
spark grammar differences over Python2.6 for Python 2.5.
44
"""
@@ -33,7 +33,7 @@ def p_misc25(self, args):
3333
POP_BLOCK LOAD_CONST COME_FROM with_cleanup
3434
3535
# Semantic actions want store to be at index 2
36-
withasstmt ::= expr setupwithas store suite_stmts_opt
36+
with_as ::= expr setupwithas store suite_stmts_opt
3737
POP_BLOCK LOAD_CONST COME_FROM with_cleanup
3838
3939
@@ -48,7 +48,7 @@ def p_misc25(self, args):
4848
4949
# Python 2.6 omits the LOAD_FAST DELETE_FAST below
5050
# withas is allowed as a "from future" in 2.5
51-
withasstmt ::= expr setupwithas store suite_stmts_opt
51+
with_as ::= expr setupwithas store suite_stmts_opt
5252
POP_BLOCK LOAD_CONST COME_FROM
5353
with_cleanup
5454
@@ -67,7 +67,7 @@ def customize_grammar_rules(self, tokens, customize):
6767
setupwith ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 POP_TOP
6868
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt
6969
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
70-
withasstmt ::= expr setupwithas store suite_stmts_opt
70+
with_as ::= expr setupwithas store suite_stmts_opt
7171
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
7272
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
7373
classdefdeco ::= classdefdeco1 store

uncompyle6/parsers/parse26.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2017-2023 Rocky Bernstein
1+
# Copyright (c) 2017-2024 Rocky Bernstein
22
"""
33
spark grammar differences over Python2 for Python 2.6.
44
"""
@@ -136,7 +136,7 @@ def p_stmt26(self, args):
136136
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
137137
138138
# Semantic actions want store to be at index 2
139-
withasstmt ::= expr setupwithas store suite_stmts_opt
139+
with_as ::= expr setupwithas store suite_stmts_opt
140140
POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
141141
142142
# This is truly weird. 2.7 does this (not including POP_TOP) with
@@ -352,9 +352,9 @@ def p_misc26(self, args):
352352
def customize_grammar_rules(self, tokens, customize):
353353
self.remove_rules(
354354
"""
355-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
356-
POP_BLOCK LOAD_CONST COME_FROM_WITH
357-
WITH_CLEANUP END_FINALLY
355+
with_as ::= expr SETUP_WITH store suite_stmts_opt
356+
POP_BLOCK LOAD_CONST COME_FROM_WITH
357+
WITH_CLEANUP END_FINALLY
358358
"""
359359
)
360360
super(Python26Parser, self).customize_grammar_rules(tokens, customize)
@@ -391,7 +391,6 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
391391
("and", ("expr", "jmp_false", "expr", "come_from_opt")),
392392
("assert_expr_and", ("assert_expr", "jmp_false", "expr")),
393393
):
394-
395394
# FIXME: workaround profiling bug
396395
if ast[1] is None:
397396
return False
@@ -491,7 +490,6 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
491490
("JUMP_FORWARD", "RETURN_VALUE")
492491
) or (tokens[last - 3] == "JUMP_FORWARD" and tokens[last - 3].attr != 2)
493492
elif lhs == "tryelsestmt":
494-
495493
# We need to distinguish "try_except" from "tryelsestmt"; we do that
496494
# by making sure that the jump before the except handler jumps to
497495
# code somewhere before the end of the construct.

uncompyle6/parsers/parse27.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ def p_stmt27(self, args):
161161
POP_BLOCK LOAD_CONST COME_FROM_WITH
162162
WITH_CLEANUP END_FINALLY
163163
164-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
165-
POP_BLOCK LOAD_CONST COME_FROM_WITH
166-
WITH_CLEANUP END_FINALLY
164+
with_as ::= expr SETUP_WITH store suite_stmts_opt
165+
POP_BLOCK LOAD_CONST COME_FROM_WITH
166+
WITH_CLEANUP END_FINALLY
167167
168168
whilestmt ::= SETUP_LOOP testexpr returns
169169
_come_froms POP_BLOCK COME_FROM

uncompyle6/parsers/parse3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ def p_grammar(self, args):
287287
POP_BLOCK LOAD_CONST COME_FROM_WITH
288288
WITH_CLEANUP END_FINALLY
289289
290-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt
291-
POP_BLOCK LOAD_CONST COME_FROM_WITH
292-
WITH_CLEANUP END_FINALLY
290+
with_as ::= expr SETUP_WITH store suite_stmts_opt
291+
POP_BLOCK LOAD_CONST COME_FROM_WITH
292+
WITH_CLEANUP END_FINALLY
293293
294294
expr_jt ::= expr jmp_true
295295
expr_jitop ::= expr JUMP_IF_TRUE_OR_POP

uncompyle6/parsers/parse30.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def p_30(self, args):
6666
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE COME_FROM POP_TOP
6767
6868
69-
withasstmt ::= expr setupwithas store suite_stmts_opt
69+
with_as ::= expr setupwithas store suite_stmts_opt
7070
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
7171
LOAD_FAST DELETE_FAST WITH_CLEANUP END_FINALLY
7272
setupwithas ::= DUP_TOP LOAD_ATTR STORE_FAST LOAD_ATTR CALL_FUNCTION_0 setup_finally
@@ -222,12 +222,17 @@ def remove_rules_30(self):
222222
# The were found using grammar coverage
223223
while1stmt ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
224224
whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM_LOOP
225-
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK else_suitel COME_FROM_LOOP
226-
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM_LOOP
227-
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK JUMP_BACK COME_FROM_LOOP
225+
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
226+
else_suitel COME_FROM_LOOP
227+
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
228+
COME_FROM_LOOP
229+
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK JUMP_BACK
230+
COME_FROM_LOOP
228231
whilestmt ::= SETUP_LOOP testexpr returns POP_TOP POP_BLOCK COME_FROM_LOOP
229-
withasstmt ::= expr SETUP_WITH store suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM_WITH WITH_CLEANUP END_FINALLY
230-
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM_WITH WITH_CLEANUP END_FINALLY
232+
with_as ::= expr SETUP_WITH store suite_stmts_opt POP_BLOCK LOAD_CONST
233+
COME_FROM_WITH WITH_CLEANUP END_FINALLY
234+
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST
235+
COME_FROM_WITH WITH_CLEANUP END_FINALLY
231236
232237
# lc_body ::= LOAD_FAST expr LIST_APPEND
233238
# lc_body ::= LOAD_NAME expr LIST_APPEND

uncompyle6/parsers/parse31.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def p_31(self, args):
2323
# Keeps Python 3.1 "with .. as" designator in the same position as it is in other version.
2424
setupwithas31 ::= setupwithas SETUP_FINALLY load delete
2525
26-
withasstmt ::= expr setupwithas31 store
26+
with_as ::= expr setupwithas31 store
2727
suite_stmts_opt
2828
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
2929
load delete WITH_CLEANUP END_FINALLY

0 commit comments

Comments
 (0)