parse31.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2016-2017, 2022, 2024 Rocky Bernstein
  2. """
  3. spark grammar differences over Python 3.2 for Python 3.1.
  4. """
  5. from __future__ import print_function
  6. from uncompyle6.parser import PythonParserSingle
  7. from uncompyle6.parsers.parse32 import Python32Parser
  8. class Python31Parser(Python32Parser):
  9. def p_31(self, args):
  10. """
  11. subscript2 ::= expr expr DUP_TOPX BINARY_SUBSCR
  12. setupwith ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0 POP_TOP
  13. setupwithas ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0 store
  14. with ::= expr setupwith SETUP_FINALLY
  15. suite_stmts_opt
  16. POP_BLOCK LOAD_CONST COME_FROM_FINALLY
  17. load delete WITH_CLEANUP END_FINALLY
  18. # Keeps Python 3.1 "with .. as" designator in the same position as it is in other version.
  19. setupwithas31 ::= setupwithas SETUP_FINALLY load delete
  20. with_as ::= expr setupwithas31 store
  21. suite_stmts_opt
  22. POP_BLOCK LOAD_CONST COME_FROM_FINALLY
  23. load delete WITH_CLEANUP END_FINALLY
  24. store ::= STORE_NAME
  25. load ::= LOAD_FAST
  26. load ::= LOAD_NAME
  27. """
  28. def remove_rules_31(self):
  29. self.remove_rules(
  30. """
  31. # DUP_TOP_TWO is DUP_TOPX in 3.1 and earlier
  32. subscript2 ::= expr expr DUP_TOP_TWO BINARY_SUBSCR
  33. # The were found using grammar coverage
  34. list_if ::= expr jmp_false list_iter COME_FROM
  35. list_if_not ::= expr jmp_true list_iter COME_FROM
  36. """
  37. )
  38. def customize_grammar_rules(self, tokens, customize):
  39. super(Python31Parser, self).customize_grammar_rules(tokens, customize)
  40. self.remove_rules_31()
  41. return
  42. pass
  43. class Python31ParserSingle(Python31Parser, PythonParserSingle):
  44. pass
  45. if __name__ == "__main__":
  46. # Check grammar
  47. p = Python31Parser()
  48. p.remove_rules_31()
  49. p.check_grammar()
  50. from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
  51. if PYTHON_VERSION_TRIPLE[:2] == (3, 1):
  52. lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
  53. from uncompyle6.scanner import get_scanner
  54. s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
  55. opcode_set = set(s.opc.opname).union(
  56. set(
  57. """JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
  58. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  59. LAMBDA_MARKER RETURN_LAST
  60. """.split()
  61. )
  62. )
  63. # FIXME: try this
  64. remain_tokens = set(tokens) - opcode_set
  65. import re
  66. remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
  67. remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
  68. remain_tokens = set(remain_tokens) - opcode_set
  69. print(remain_tokens)
  70. import sys
  71. if len(sys.argv) > 1:
  72. from spark_parser.spark import rule2str
  73. for rule in sorted(p.rule2name.items()):
  74. print(rule2str(rule[0]))