parse32.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2016-2017, 2022-2024 Rocky Bernstein
  2. """
  3. spark grammar differences over Python 3 for Python 3.2.
  4. """
  5. from __future__ import print_function
  6. from uncompyle6.parser import PythonParserSingle
  7. from uncompyle6.parsers.parse3 import Python3Parser
  8. class Python32Parser(Python3Parser):
  9. def p_30to33(self, args):
  10. """
  11. # Store locals is only in Python 3.0 to 3.3
  12. stmt ::= store_locals
  13. store_locals ::= LOAD_FAST STORE_LOCALS
  14. """
  15. def p_gen_comp32(self, args):
  16. """
  17. genexpr_func ::= LOAD_ARG FOR_ITER store comp_iter JUMP_BACK
  18. """
  19. def p_32to35(self, args):
  20. """
  21. if_exp ::= expr jmp_false expr jump_forward_else expr COME_FROM
  22. # compare_chained_right is used in a "chained_compare": x <= y <= z
  23. compare_chained_right ::= expr COMPARE_OP RETURN_VALUE
  24. compare_chained_right ::= expr COMPARE_OP RETURN_VALUE_LAMBDA
  25. # Python < 3.5 no POP BLOCK
  26. whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM_LOOP
  27. # Python 3.5+ has jump optimization to remove the redundant
  28. # jump_excepts. But in 3.3 we need them added
  29. try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
  30. except_handler
  31. jump_excepts come_from_except_clauses
  32. except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
  33. END_FINALLY
  34. tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
  35. except_handler else_suite
  36. jump_excepts come_from_except_clauses
  37. jump_excepts ::= jump_except+
  38. # Python 3.2+ has more loop optimization that removes
  39. # JUMP_FORWARD in some cases, and hence we also don't
  40. # see COME_FROM
  41. _ifstmts_jump ::= stmts_opt
  42. _ifstmts_jump ::= stmts_opt JUMP_FORWARD _come_froms
  43. _ifstmts_jumpl ::= c_stmts_opt
  44. _ifstmts_jumpl ::= c_stmts_opt JUMP_FORWARD _come_froms
  45. kv3 ::= expr expr STORE_MAP
  46. """
  47. pass
  48. def p_32on(self, args):
  49. """
  50. # In Python 3.2+, DUP_TOPX is DUP_TOP_TWO
  51. subscript2 ::= expr expr DUP_TOP_TWO BINARY_SUBSCR
  52. """
  53. pass
  54. def customize_grammar_rules(self, tokens, customize):
  55. self.remove_rules(
  56. """
  57. except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM
  58. except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM_EXCEPT
  59. except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts END_FINALLY COME_FROM_EXCEPT_CLAUSE
  60. except_handler ::= jmp_abs COME_FROM except_stmts END_FINALLY
  61. tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler else_suite come_from_except_clauses
  62. whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK NOP COME_FROM_LOOP
  63. whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK NOP COME_FROM_LOOP
  64. """
  65. )
  66. super(Python32Parser, self).customize_grammar_rules(tokens, customize)
  67. for i, token in enumerate(tokens):
  68. opname = token.kind
  69. if opname.startswith("MAKE_FUNCTION_A"):
  70. args_pos, _, annotate_args = token.attr
  71. # Check that there are 2 annotated params?
  72. rule = (
  73. "mkfunc_annotate ::= %s%sannotate_tuple "
  74. "LOAD_CONST LOAD_CODE EXTENDED_ARG %s"
  75. ) % (
  76. ("pos_arg " * args_pos),
  77. ("annotate_arg " * (annotate_args)),
  78. opname,
  79. )
  80. self.add_unique_rule(rule, opname, token.attr, customize)
  81. pass
  82. return
  83. pass
  84. class Python32ParserSingle(Python32Parser, PythonParserSingle):
  85. pass