lambda_expr.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (c) 2020-2024 Rocky Bernstein
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. """
  16. Differences over Python 3.7 for Python 3.8 in the Earley-algorithm lambda grammar
  17. """
  18. from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
  19. from decompyle3.parsers.p37.lambda_expr import Python37LambdaParser
  20. from decompyle3.parsers.p38pypy.lambda_custom import Python38PyPyLambdaCustom
  21. from decompyle3.parsers.parse_heads import PythonBaseParser, PythonParserLambda
  22. class Python38PyPyLambdaParser(
  23. Python38PyPyLambdaCustom, Python37LambdaParser, PythonParserLambda
  24. ):
  25. def p_38walrus(self, args):
  26. """
  27. # named_expr is also known as the "walrus op" :=
  28. expr ::= named_expr
  29. named_expr ::= expr DUP_TOP store
  30. """
  31. def p_lambda_start(self, args):
  32. """
  33. return_expr_lambda ::= genexpr_func LOAD_CONST RETURN_VALUE_LAMBDA
  34. """
  35. def p_pypy38_comprehension(self, args):
  36. """
  37. list_comp ::= LOAD_ARG
  38. BUILD_LIST_FROM_ARG
  39. COME_FROM FOR_ITER
  40. store lc_body
  41. JUMP_LOOP _come_froms
  42. list_afor2 ::= async_iter store list_iter
  43. JUMP_LOOP COME_FROM_EXCEPT
  44. END_ASYNC_FOR
  45. lc_body ::= expr LIST_APPEND
  46. """
  47. def p_expr38(self, args):
  48. """
  49. expr ::= if_exp_compare38
  50. if_exp_compare38 ::= or_in_ifexp jump_if_false_cf expr jf_cfs expr come_froms
  51. list_iter ::= list_if_not38
  52. list_if_not38 ::= expr pjump_ift expr pjump_ift _come_froms list_iter
  53. come_from_opt
  54. or_in_ifexp ::= expr_pjit expr
  55. or_in_ifexp ::= or_in_ifexp POP_JUMP_IF_TRUE expr
  56. """
  57. def __init__(
  58. self,
  59. start_symbol: str = "lambda_start",
  60. debug_parser: dict = PARSER_DEFAULT_DEBUG,
  61. ):
  62. PythonParserLambda.__init__(
  63. self, debug_parser=debug_parser, start_symbol=start_symbol
  64. )
  65. PythonBaseParser.__init__(
  66. self, start_symbol=start_symbol, debug_parser=debug_parser
  67. )
  68. Python38PyPyLambdaCustom.__init__(self)
  69. def customize_grammar_rules(self, tokens, customize):
  70. self.customize_grammar_rules_lambda38(tokens, customize)
  71. if __name__ == "__main__":
  72. # Check grammar
  73. from decompyle3.parsers.dump import dump_and_check
  74. p = Python38PyPyLambdaParser()
  75. modified_tokens = set(
  76. """JUMP_LOOP CONTINUE RETURN_END_IF COME_FROM
  77. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  78. LAMBDA_MARKER RETURN_LAST
  79. """.split()
  80. )
  81. dump_and_check(p, (3, 8), modified_tokens)