27
27
"""
28
28
29
29
import re
30
- from uncompyle6 .scanners .tok import Token
30
+
31
+ from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
32
+
31
33
from uncompyle6 .parser import PythonParser , PythonParserSingle , nop_func
32
34
from uncompyle6 .parsers .reducecheck import (
33
35
and_invalid ,
34
36
except_handler_else ,
35
37
ifelsestmt ,
36
- ifstmt ,
37
38
iflaststmt ,
39
+ ifstmt ,
38
40
or_check ,
39
41
testtrue ,
40
42
tryelsestmtl3 ,
41
43
tryexcept ,
42
- while1stmt
44
+ while1stmt ,
43
45
)
44
46
from uncompyle6 .parsers .treenode import SyntaxTree
45
- from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
47
+ from uncompyle6 . scanners . tok import Token
46
48
47
49
48
50
class Python3Parser (PythonParser ):
@@ -98,7 +100,7 @@ def p_comprehension3(self, args):
98
100
"""
99
101
100
102
def p_dict_comp3 (self , args ):
101
- """"
103
+ """ "
102
104
expr ::= dict_comp
103
105
stmt ::= dict_comp_func
104
106
dict_comp_func ::= BUILD_MAP_0 LOAD_ARG FOR_ITER store
@@ -519,7 +521,7 @@ def custom_build_class_rule(self, opname, i, token, tokens, customize, is_pypy):
519
521
expr
520
522
call
521
523
CALL_FUNCTION_3
522
- """
524
+ """
523
525
# FIXME: I bet this can be simplified
524
526
# look for next MAKE_FUNCTION
525
527
for i in range (i + 1 , len (tokens )):
@@ -625,7 +627,11 @@ def custom_classfunc_rule(self, opname, token, customize, next_token, is_pypy):
625
627
self .add_unique_rule (rule , token .kind , uniq_param , customize )
626
628
627
629
if "LOAD_BUILD_CLASS" in self .seen_ops :
628
- if next_token == "CALL_FUNCTION" and next_token .attr == 1 and pos_args_count > 1 :
630
+ if (
631
+ next_token == "CALL_FUNCTION"
632
+ and next_token .attr == 1
633
+ and pos_args_count > 1
634
+ ):
629
635
rule = "classdefdeco2 ::= LOAD_BUILD_CLASS mkfunc %s%s_%d" % (
630
636
("expr " * (pos_args_count - 1 )),
631
637
opname ,
@@ -764,18 +770,24 @@ def customize_grammar_rules(self, tokens, customize):
764
770
765
771
elif opname in ("BUILD_CONST_LIST" , "BUILD_CONST_DICT" , "BUILD_CONST_SET" ):
766
772
if opname == "BUILD_CONST_DICT" :
767
- rule = """
773
+ rule = (
774
+ """
768
775
add_consts ::= ADD_VALUE*
769
776
const_list ::= COLLECTION_START add_consts %s
770
777
dict ::= const_list
771
778
expr ::= dict
772
- """ % opname
779
+ """
780
+ % opname
781
+ )
773
782
else :
774
- rule = """
783
+ rule = (
784
+ """
775
785
add_consts ::= ADD_VALUE*
776
786
const_list ::= COLLECTION_START add_consts %s
777
787
expr ::= const_list
778
- """ % opname
788
+ """
789
+ % opname
790
+ )
779
791
self .addRule (rule , nop_func )
780
792
781
793
elif opname .startswith ("BUILD_DICT_OLDER" ):
@@ -854,18 +866,24 @@ def customize_grammar_rules(self, tokens, customize):
854
866
855
867
elif opname in ("BUILD_CONST_LIST" , "BUILD_CONST_DICT" , "BUILD_CONST_SET" ):
856
868
if opname == "BUILD_CONST_DICT" :
857
- rule = """
869
+ rule = (
870
+ """
858
871
add_consts ::= ADD_VALUE*
859
872
const_list ::= COLLECTION_START add_consts %s
860
873
dict ::= const_list
861
874
expr ::= dict
862
- """ % opname
875
+ """
876
+ % opname
877
+ )
863
878
else :
864
- rule = """
879
+ rule = (
880
+ """
865
881
add_consts ::= ADD_VALUE*
866
882
const_list ::= COLLECTION_START add_consts %s
867
883
expr ::= const_list
868
- """ % opname
884
+ """
885
+ % opname
886
+ )
869
887
self .addRule (rule , nop_func )
870
888
871
889
elif opname_base in (
@@ -946,7 +964,6 @@ def customize_grammar_rules(self, tokens, customize):
946
964
"CALL_FUNCTION_VAR_KW" ,
947
965
)
948
966
) or opname .startswith ("CALL_FUNCTION_KW" ):
949
-
950
967
if opname == "CALL_FUNCTION" and token .attr == 1 :
951
968
rule = """
952
969
dict_comp ::= LOAD_DICTCOMP LOAD_STR MAKE_FUNCTION_0 expr
@@ -1122,7 +1139,8 @@ def customize_grammar_rules(self, tokens, customize):
1122
1139
if has_get_iter_call_function1 :
1123
1140
rule_pat = (
1124
1141
"generator_exp ::= %sload_closure load_genexpr %%s%s expr "
1125
- "GET_ITER CALL_FUNCTION_1" % ("pos_arg " * pos_args_count , opname )
1142
+ "GET_ITER CALL_FUNCTION_1"
1143
+ % ("pos_arg " * pos_args_count , opname )
1126
1144
)
1127
1145
self .add_make_function_rule (rule_pat , opname , token .attr , customize )
1128
1146
@@ -1190,6 +1208,8 @@ def customize_grammar_rules(self, tokens, customize):
1190
1208
self .add_unique_rule (rule , opname , token .attr , customize )
1191
1209
1192
1210
elif (3 , 3 ) <= self .version < (3 , 6 ):
1211
+ # FIXME move this into version-specific custom rules.
1212
+ # In fact, some of this has been done for 3.3.
1193
1213
if annotate_args > 0 :
1194
1214
rule = (
1195
1215
"mkfunc_annotate ::= %s%s%sannotate_tuple load_closure LOAD_CODE LOAD_STR %s"
@@ -1208,7 +1228,6 @@ def customize_grammar_rules(self, tokens, customize):
1208
1228
)
1209
1229
self .add_unique_rule (rule , opname , token .attr , customize )
1210
1230
1211
-
1212
1231
if self .version >= (3 , 4 ):
1213
1232
if not self .is_pypy :
1214
1233
load_op = "LOAD_STR"
@@ -1292,14 +1311,16 @@ def customize_grammar_rules(self, tokens, customize):
1292
1311
if has_get_iter_call_function1 :
1293
1312
rule_pat = (
1294
1313
"generator_exp ::= %sload_genexpr %%s%s expr "
1295
- "GET_ITER CALL_FUNCTION_1" % ("pos_arg " * pos_args_count , opname )
1314
+ "GET_ITER CALL_FUNCTION_1"
1315
+ % ("pos_arg " * pos_args_count , opname )
1296
1316
)
1297
1317
self .add_make_function_rule (
1298
1318
rule_pat , opname , token .attr , customize
1299
1319
)
1300
1320
rule_pat = (
1301
1321
"generator_exp ::= %sload_closure load_genexpr %%s%s expr "
1302
- "GET_ITER CALL_FUNCTION_1" % ("pos_arg " * pos_args_count , opname )
1322
+ "GET_ITER CALL_FUNCTION_1"
1323
+ % ("pos_arg " * pos_args_count , opname )
1303
1324
)
1304
1325
self .add_make_function_rule (
1305
1326
rule_pat , opname , token .attr , customize
@@ -1351,7 +1372,8 @@ def customize_grammar_rules(self, tokens, customize):
1351
1372
if has_get_iter_call_function1 :
1352
1373
rule_pat = (
1353
1374
"generator_exp ::= %sload_genexpr %%s%s expr "
1354
- "GET_ITER CALL_FUNCTION_1" % ("pos_arg " * pos_args_count , opname )
1375
+ "GET_ITER CALL_FUNCTION_1"
1376
+ % ("pos_arg " * pos_args_count , opname )
1355
1377
)
1356
1378
self .add_make_function_rule (rule_pat , opname , token .attr , customize )
1357
1379
@@ -1363,7 +1385,8 @@ def customize_grammar_rules(self, tokens, customize):
1363
1385
# Todo: For Pypy we need to modify this slightly
1364
1386
rule_pat = (
1365
1387
"listcomp ::= %sLOAD_LISTCOMP %%s%s expr "
1366
- "GET_ITER CALL_FUNCTION_1" % ("expr " * pos_args_count , opname )
1388
+ "GET_ITER CALL_FUNCTION_1"
1389
+ % ("expr " * pos_args_count , opname )
1367
1390
)
1368
1391
self .add_make_function_rule (
1369
1392
rule_pat , opname , token .attr , customize
@@ -1450,9 +1473,6 @@ def customize_grammar_rules(self, tokens, customize):
1450
1473
)
1451
1474
)
1452
1475
if self .version >= (3 , 3 ):
1453
- # Normally we remove EXTENDED_ARG from the opcodes, but in the case of
1454
- # annotated functions can use the EXTENDED_ARG tuple to signal we have an annotated function.
1455
- # Yes this is a little hacky
1456
1476
if self .version == (3 , 3 ):
1457
1477
# 3.3 puts kwargs before pos_arg
1458
1478
pos_kw_tuple = (
@@ -1466,17 +1486,17 @@ def customize_grammar_rules(self, tokens, customize):
1466
1486
("kwargs " * kw_args_count ),
1467
1487
)
1468
1488
rule = (
1469
- "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR EXTENDED_ARG %s"
1489
+ "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR %s"
1470
1490
% (
1471
1491
pos_kw_tuple [0 ],
1472
1492
pos_kw_tuple [1 ],
1473
- ("call " * annotate_args ),
1493
+ ("annotate_arg " * annotate_args ),
1474
1494
opname ,
1475
1495
)
1476
1496
)
1477
1497
self .add_unique_rule (rule , opname , token .attr , customize )
1478
1498
rule = (
1479
- "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR EXTENDED_ARG %s"
1499
+ "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR %s"
1480
1500
% (
1481
1501
pos_kw_tuple [0 ],
1482
1502
pos_kw_tuple [1 ],
@@ -1485,9 +1505,8 @@ def customize_grammar_rules(self, tokens, customize):
1485
1505
)
1486
1506
)
1487
1507
else :
1488
- # See above comment about use of EXTENDED_ARG
1489
1508
rule = (
1490
- "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE EXTENDED_ARG %s"
1509
+ "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE %s"
1491
1510
% (
1492
1511
("kwargs " * kw_args_count ),
1493
1512
("pos_arg " * (pos_args_count )),
@@ -1497,7 +1516,7 @@ def customize_grammar_rules(self, tokens, customize):
1497
1516
)
1498
1517
self .add_unique_rule (rule , opname , token .attr , customize )
1499
1518
rule = (
1500
- "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE EXTENDED_ARG %s"
1519
+ "mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE %s"
1501
1520
% (
1502
1521
("kwargs " * kw_args_count ),
1503
1522
("pos_arg " * pos_args_count ),
@@ -1594,7 +1613,7 @@ def customize_grammar_rules(self, tokens, customize):
1594
1613
}
1595
1614
1596
1615
if self .version == (3 , 6 ):
1597
- self .reduce_check_table ["and" ] = and_invalid
1616
+ self .reduce_check_table ["and" ] = and_invalid
1598
1617
self .check_reduce ["and" ] = "AST"
1599
1618
1600
1619
self .check_reduce ["annotate_tuple" ] = "noAST"
@@ -1624,7 +1643,7 @@ def customize_grammar_rules(self, tokens, customize):
1624
1643
def reduce_is_invalid (self , rule , ast , tokens , first , last ):
1625
1644
lhs = rule [0 ]
1626
1645
n = len (tokens )
1627
- last = min (last , n - 1 )
1646
+ last = min (last , n - 1 )
1628
1647
fn = self .reduce_check_table .get (lhs , None )
1629
1648
if fn :
1630
1649
if fn (self , lhs , n , rule , ast , tokens , first , last ):
@@ -1650,13 +1669,18 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
1650
1669
condition_jump2 = tokens [min (last - 1 , len (tokens ) - 1 )]
1651
1670
# If there are two *distinct* condition jumps, they should not jump to the
1652
1671
# same place. Otherwise we have some sort of "and"/"or".
1653
- if condition_jump2 .kind .startswith ("POP_JUMP_IF" ) and condition_jump != condition_jump2 :
1672
+ if (
1673
+ condition_jump2 .kind .startswith ("POP_JUMP_IF" )
1674
+ and condition_jump != condition_jump2
1675
+ ):
1654
1676
return condition_jump .attr == condition_jump2 .attr
1655
1677
1656
- if tokens [last ] == "COME_FROM" and tokens [last ].off2int () != condition_jump .attr :
1678
+ if (
1679
+ tokens [last ] == "COME_FROM"
1680
+ and tokens [last ].off2int () != condition_jump .attr
1681
+ ):
1657
1682
return False
1658
1683
1659
-
1660
1684
# if condition_jump.attr < condition_jump2.off2int():
1661
1685
# print("XXX", first, last)
1662
1686
# for t in range(first, last): print(tokens[t])
@@ -1678,7 +1702,6 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
1678
1702
< tokens [last ].off2int ()
1679
1703
)
1680
1704
elif lhs == "while1stmt" :
1681
-
1682
1705
if while1stmt (self , lhs , n , rule , ast , tokens , first , last ):
1683
1706
return True
1684
1707
@@ -1700,7 +1723,6 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
1700
1723
return True
1701
1724
return False
1702
1725
elif lhs == "while1elsestmt" :
1703
-
1704
1726
n = len (tokens )
1705
1727
if last == n :
1706
1728
# Adjust for fuzziness in parsing
0 commit comments