1
- # Copyright (c) 2016-2017, 2022-2023 Rocky Bernstein
1
+ # Copyright (c) 2016-2017, 2022-2024 Rocky Bernstein
2
2
"""
3
3
spark grammar differences over Python 3.1 for Python 3.0.
4
4
"""
7
7
from uncompyle6 .parser import PythonParserSingle
8
8
from uncompyle6 .parsers .parse31 import Python31Parser
9
9
10
- class Python30Parser (Python31Parser ):
11
10
11
+ class Python30Parser (Python31Parser ):
12
12
def p_30 (self , args ):
13
13
"""
14
14
@@ -78,7 +78,6 @@ def p_30(self, args):
78
78
set_comp_func ::= set_comp_header
79
79
LOAD_ARG FOR_ITER store comp_iter
80
80
JUMP_BACK ending_return
81
- RETURN_VALUE RETURN_LAST
82
81
83
82
list_comp_header ::= BUILD_LIST_0 DUP_TOP STORE_FAST
84
83
list_comp ::= list_comp_header
@@ -118,7 +117,7 @@ def p_30(self, args):
118
117
# From Python 2.6
119
118
120
119
121
- lc_body ::= LOAD_FAST expr LIST_APPEND
120
+ lc_body ::= LOAD_FAST expr LIST_APPEND
122
121
lc_body ::= LOAD_NAME expr LIST_APPEND
123
122
list_if ::= expr jmp_false_then list_iter
124
123
list_if_not ::= expr jmp_true list_iter JUMP_BACK come_froms POP_TOP
@@ -216,9 +215,9 @@ def p_30(self, args):
216
215
compare_chained_right ::= expr COMPARE_OP RETURN_END_IF
217
216
"""
218
217
219
-
220
218
def remove_rules_30 (self ):
221
- self .remove_rules ("""
219
+ self .remove_rules (
220
+ """
222
221
223
222
# The were found using grammar coverage
224
223
while1stmt ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
@@ -286,37 +285,41 @@ def remove_rules_30(self):
286
285
or ::= expr JUMP_IF_TRUE_OR_POP expr COME_FROM
287
286
and ::= expr JUMP_IF_TRUE_OR_POP expr COME_FROM
288
287
and ::= expr JUMP_IF_FALSE_OR_POP expr COME_FROM
289
- """ )
288
+ """
289
+ )
290
290
291
291
def customize_grammar_rules (self , tokens , customize ):
292
292
super (Python30Parser , self ).customize_grammar_rules (tokens , customize )
293
293
self .remove_rules_30 ()
294
294
295
295
self .check_reduce ["iflaststmtl" ] = "AST"
296
- self .check_reduce [' ifstmt' ] = "AST"
296
+ self .check_reduce [" ifstmt" ] = "AST"
297
297
self .check_reduce ["ifelsestmtc" ] = "AST"
298
298
self .check_reduce ["ifelsestmt" ] = "AST"
299
299
# self.check_reduce["and"] = "stmt"
300
300
return
301
301
302
302
def reduce_is_invalid (self , rule , ast , tokens , first , last ):
303
- invalid = super (Python30Parser ,
304
- self ). reduce_is_invalid ( rule , ast ,
305
- tokens , first , last )
303
+ invalid = super (Python30Parser , self ). reduce_is_invalid (
304
+ rule , ast , tokens , first , last
305
+ )
306
306
if invalid :
307
307
return invalid
308
308
lhs = rule [0 ]
309
309
if (
310
- lhs in ("iflaststmtl" , "ifstmt" ,
311
- "ifelsestmt" , "ifelsestmtc" ) and ast [0 ] == "testexpr"
310
+ lhs in ("iflaststmtl" , "ifstmt" , "ifelsestmt" , "ifelsestmtc" )
311
+ and ast [0 ] == "testexpr"
312
312
):
313
313
testexpr = ast [0 ]
314
314
if testexpr [0 ] == "testfalse" :
315
315
testfalse = testexpr [0 ]
316
316
if lhs == "ifelsestmtc" and ast [2 ] == "jump_absolute_else" :
317
317
jump_absolute_else = ast [2 ]
318
318
come_from = jump_absolute_else [2 ]
319
- return come_from == "COME_FROM" and come_from .attr < tokens [first ].offset
319
+ return (
320
+ come_from == "COME_FROM"
321
+ and come_from .attr < tokens [first ].offset
322
+ )
320
323
pass
321
324
elif lhs in ("ifelsestmt" , "ifelsestmtc" ) and ast [2 ] == "jump_cf_pop" :
322
325
jump_cf_pop = ast [2 ]
@@ -339,20 +342,21 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
339
342
jmp_false = testfalse [1 ]
340
343
if last == len (tokens ):
341
344
last -= 1
342
- while ( isinstance (tokens [first ].offset , str ) and first < last ) :
345
+ while isinstance (tokens [first ].offset , str ) and first < last :
343
346
first += 1
344
347
if first == last :
345
348
return True
346
- while ( first < last and isinstance (tokens [last ].offset , str ) ):
349
+ while first < last and isinstance (tokens [last ].offset , str ):
347
350
last -= 1
348
351
if rule [0 ] == "iflaststmtl" :
349
352
return not (jmp_false [0 ].attr <= tokens [last ].offset )
350
353
else :
351
354
jmp_false_target = jmp_false [0 ].attr
352
355
if tokens [first ].offset > jmp_false_target :
353
356
return True
354
- return (
355
- (jmp_false_target > tokens [last ].offset ) and tokens [last ] != "JUMP_FORWARD" )
357
+ return (jmp_false_target > tokens [last ].offset ) and tokens [
358
+ last
359
+ ] != "JUMP_FORWARD"
356
360
pass
357
361
pass
358
362
pass
@@ -361,33 +365,43 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
361
365
362
366
pass
363
367
368
+
364
369
class Python30ParserSingle (Python30Parser , PythonParserSingle ):
365
370
pass
366
371
367
- if __name__ == '__main__' :
372
+
373
+ if __name__ == "__main__" :
368
374
# Check grammar
369
375
p = Python30Parser ()
370
376
p .remove_rules_30 ()
371
377
p .check_grammar ()
372
- from xdis .version_info import PYTHON_VERSION_TRIPLE , IS_PYPY
378
+ from xdis .version_info import IS_PYPY , PYTHON_VERSION_TRIPLE
379
+
373
380
if PYTHON_VERSION_TRIPLE [:2 ] == (3 , 0 ):
374
381
lhs , rhs , tokens , right_recursive , dup_rhs = p .check_sets ()
375
382
from uncompyle6 .scanner import get_scanner
383
+
376
384
s = get_scanner (PYTHON_VERSION_TRIPLE , IS_PYPY )
377
- opcode_set = set (s .opc .opname ).union (set (
378
- """JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
385
+ opcode_set = set (s .opc .opname ).union (
386
+ set (
387
+ """JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
379
388
LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
380
389
LAMBDA_MARKER RETURN_LAST
381
- """ .split ()))
390
+ """ .split ()
391
+ )
392
+ )
382
393
## FIXME: try this
383
394
remain_tokens = set (tokens ) - opcode_set
384
395
import re
385
- remain_tokens = set ([re .sub (r'_\d+$' , '' , t ) for t in remain_tokens ])
386
- remain_tokens = set ([re .sub ('_CONT$' , '' , t ) for t in remain_tokens ])
396
+
397
+ remain_tokens = set ([re .sub (r"_\d+$" , "" , t ) for t in remain_tokens ])
398
+ remain_tokens = set ([re .sub ("_CONT$" , "" , t ) for t in remain_tokens ])
387
399
remain_tokens = set (remain_tokens ) - opcode_set
388
400
print (remain_tokens )
389
401
import sys
402
+
390
403
if len (sys .argv ) > 1 :
391
404
from spark_parser .spark import rule2str
405
+
392
406
for rule in sorted (p .rule2name .items ()):
393
407
print (rule2str (rule [0 ]))
0 commit comments