2024-07-12 16:09:51 +00:00
|
|
|
# Copyright (c) 2016-2017, 2022, 2024 Rocky Bernstein
|
2016-10-26 01:46:46 +00:00
|
|
|
"""
|
|
|
|
spark grammar differences over Python 3.2 for Python 3.1.
|
|
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from uncompyle6.parser import PythonParserSingle
|
2016-10-28 11:07:18 +00:00
|
|
|
from uncompyle6.parsers.parse32 import Python32Parser
|
2016-10-26 01:46:46 +00:00
|
|
|
|
|
|
|
|
2022-12-01 22:24:17 +00:00
|
|
|
class Python31Parser(Python32Parser):
|
2016-10-26 01:46:46 +00:00
|
|
|
def p_31(self, args):
|
|
|
|
"""
|
2017-11-29 05:08:08 +00:00
|
|
|
subscript2 ::= expr expr DUP_TOPX BINARY_SUBSCR
|
2016-10-26 01:46:46 +00:00
|
|
|
|
|
|
|
setupwith ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0 POP_TOP
|
|
|
|
setupwithas ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0 store
|
2020-04-01 13:48:34 +00:00
|
|
|
with ::= expr setupwith SETUP_FINALLY
|
2016-10-26 01:46:46 +00:00
|
|
|
suite_stmts_opt
|
|
|
|
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
|
2020-09-02 11:14:56 +00:00
|
|
|
load delete WITH_CLEANUP END_FINALLY
|
2016-10-26 01:46:46 +00:00
|
|
|
|
2022-12-01 22:24:17 +00:00
|
|
|
# Keeps Python 3.1 "with .. as" designator in the same position as it is in other version.
|
2020-09-02 11:14:56 +00:00
|
|
|
setupwithas31 ::= setupwithas SETUP_FINALLY load delete
|
2016-10-26 01:46:46 +00:00
|
|
|
|
2024-03-08 09:09:47 +00:00
|
|
|
with_as ::= expr setupwithas31 store
|
2016-10-26 01:46:46 +00:00
|
|
|
suite_stmts_opt
|
|
|
|
POP_BLOCK LOAD_CONST COME_FROM_FINALLY
|
2020-09-02 11:14:56 +00:00
|
|
|
load delete WITH_CLEANUP END_FINALLY
|
2016-10-26 01:46:46 +00:00
|
|
|
|
|
|
|
store ::= STORE_NAME
|
|
|
|
load ::= LOAD_FAST
|
|
|
|
load ::= LOAD_NAME
|
|
|
|
"""
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2019-11-17 06:01:52 +00:00
|
|
|
def remove_rules_31(self):
|
2022-12-01 22:24:17 +00:00
|
|
|
self.remove_rules(
|
|
|
|
"""
|
2019-11-17 06:01:52 +00:00
|
|
|
# DUP_TOP_TWO is DUP_TOPX in 3.1 and earlier
|
|
|
|
subscript2 ::= expr expr DUP_TOP_TWO BINARY_SUBSCR
|
2019-11-19 03:46:27 +00:00
|
|
|
|
|
|
|
# The were found using grammar coverage
|
|
|
|
list_if ::= expr jmp_false list_iter COME_FROM
|
|
|
|
list_if_not ::= expr jmp_true list_iter COME_FROM
|
2022-12-01 22:24:17 +00:00
|
|
|
"""
|
|
|
|
)
|
2016-10-26 01:46:46 +00:00
|
|
|
|
2017-12-13 22:25:19 +00:00
|
|
|
def customize_grammar_rules(self, tokens, customize):
|
|
|
|
super(Python31Parser, self).customize_grammar_rules(tokens, customize)
|
2019-11-17 06:01:52 +00:00
|
|
|
self.remove_rules_31()
|
2017-01-07 08:03:53 +00:00
|
|
|
return
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2017-01-07 08:03:53 +00:00
|
|
|
pass
|
|
|
|
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2016-10-26 01:46:46 +00:00
|
|
|
class Python31ParserSingle(Python31Parser, PythonParserSingle):
|
|
|
|
pass
|
2019-11-17 06:01:52 +00:00
|
|
|
|
2022-12-01 22:24:17 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-11-17 06:01:52 +00:00
|
|
|
# Check grammar
|
|
|
|
p = Python31Parser()
|
|
|
|
p.remove_rules_31()
|
|
|
|
p.check_grammar()
|
2022-12-01 22:24:17 +00:00
|
|
|
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
|
|
|
|
|
2022-11-05 21:17:24 +00:00
|
|
|
if PYTHON_VERSION_TRIPLE[:2] == (3, 1):
|
2019-11-17 06:01:52 +00:00
|
|
|
lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
|
|
|
|
from uncompyle6.scanner import get_scanner
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2022-11-05 21:17:24 +00:00
|
|
|
s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
|
2022-12-01 22:24:17 +00:00
|
|
|
opcode_set = set(s.opc.opname).union(
|
|
|
|
set(
|
|
|
|
"""JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
|
2019-11-17 06:01:52 +00:00
|
|
|
LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
|
|
|
|
LAMBDA_MARKER RETURN_LAST
|
2022-12-01 22:24:17 +00:00
|
|
|
""".split()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# FIXME: try this
|
2019-11-17 06:01:52 +00:00
|
|
|
remain_tokens = set(tokens) - opcode_set
|
|
|
|
import re
|
2022-12-01 22:24:17 +00:00
|
|
|
|
|
|
|
remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
|
|
|
|
remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
|
2019-11-17 06:01:52 +00:00
|
|
|
remain_tokens = set(remain_tokens) - opcode_set
|
|
|
|
print(remain_tokens)
|
|
|
|
import sys
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2019-11-17 06:01:52 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
from spark_parser.spark import rule2str
|
2022-12-01 22:24:17 +00:00
|
|
|
|
2019-11-17 06:01:52 +00:00
|
|
|
for rule in sorted(p.rule2name.items()):
|
|
|
|
print(rule2str(rule[0]))
|