parse34.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2017-2018, 2022-2024 Rocky Bernstein
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. """
  15. spark grammar differences over Python 3.3 for Python 3.4
  16. """
  17. from uncompyle6.parser import PythonParserSingle
  18. from uncompyle6.parsers.parse33 import Python33Parser
  19. class Python34Parser(Python33Parser):
  20. def p_misc34(self, args):
  21. """
  22. expr ::= LOAD_ASSERT
  23. # passtmt is needed for semantic actions to add "pass"
  24. suite_stmts_opt ::= pass
  25. whilestmt ::= SETUP_LOOP testexpr returns come_froms POP_BLOCK COME_FROM_LOOP
  26. # Seems to be needed starting 3.4.4 or so
  27. while1stmt ::= SETUP_LOOP l_stmts
  28. COME_FROM JUMP_BACK POP_BLOCK COME_FROM_LOOP
  29. while1stmt ::= SETUP_LOOP l_stmts
  30. POP_BLOCK COME_FROM_LOOP
  31. # FIXME the below masks a bug in not detecting COME_FROM_LOOP
  32. # grammar rules with COME_FROM -> COME_FROM_LOOP already exist
  33. whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
  34. else_suitel COME_FROM
  35. while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK _come_froms POP_BLOCK else_suitel
  36. COME_FROM_LOOP
  37. # Python 3.4+ optimizes the trailing two JUMPS away
  38. # This is 3.4 only
  39. yield_from ::= expr GET_ITER LOAD_CONST YIELD_FROM
  40. _ifstmts_jump ::= c_stmts_opt JUMP_ABSOLUTE JUMP_FORWARD COME_FROM
  41. genexpr_func ::= LOAD_ARG _come_froms FOR_ITER store comp_iter JUMP_BACK
  42. if_exp_lambda ::= expr jmp_false expr return_if_lambda return_stmt_lambda LAMBDA_MARKER
  43. return_if_lambda ::= RETURN_END_IF_LAMBDA come_froms
  44. return_if_stmt ::= return_expr RETURN_END_IF POP_BLOCK
  45. """
  46. def customize_grammar_rules(self, tokens, customize):
  47. self.remove_rules(
  48. """
  49. yield_from ::= expr expr YIELD_FROM
  50. # 3.4.2 has this. 3.4.4 may now
  51. # while1stmt ::= SETUP_LOOP l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
  52. """
  53. )
  54. super(Python34Parser, self).customize_grammar_rules(tokens, customize)
  55. return
  56. class Python34ParserSingle(Python34Parser, PythonParserSingle):
  57. pass
  58. if __name__ == "__main__":
  59. # Check grammar
  60. p = Python34Parser()
  61. p.check_grammar()
  62. from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
  63. if PYTHON_VERSION_TRIPLE[:2] == (3, 4):
  64. lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
  65. from uncompyle6.scanner import get_scanner
  66. s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
  67. opcode_set = set(s.opc.opname).union(
  68. set(
  69. """JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
  70. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  71. LAMBDA_MARKER RETURN_LAST
  72. """.split()
  73. )
  74. )
  75. remain_tokens = set(tokens) - opcode_set
  76. import re
  77. remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
  78. remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
  79. remain_tokens = set(remain_tokens) - opcode_set
  80. print(remain_tokens)
  81. # print(sorted(p.rule2name.items()))