lambda_expr.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (c) 2020-2023 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.p38.lambda_custom import Python38LambdaCustom
  21. from decompyle3.parsers.parse_heads import PythonBaseParser, PythonParserLambda
  22. class Python38LambdaParser(
  23. Python38LambdaCustom, 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_expr38(self, args):
  36. """
  37. expr ::= if_exp_compare38
  38. if_exp_compare38 ::= or_in_ifexp jump_if_false_cf expr jf_cfs expr come_froms
  39. list_iter ::= list_if_not38
  40. list_if_not38 ::= expr pjump_ift expr pjump_ift _come_froms list_iter
  41. come_from_opt
  42. or_in_ifexp ::= expr_pjit expr
  43. or_in_ifexp ::= or_in_ifexp POP_JUMP_IF_TRUE expr
  44. """
  45. def __init__(
  46. self,
  47. start_symbol: str = "lambda_start",
  48. debug_parser: dict = PARSER_DEFAULT_DEBUG,
  49. ):
  50. PythonParserLambda.__init__(
  51. self, debug_parser=debug_parser, start_symbol=start_symbol
  52. )
  53. PythonBaseParser.__init__(
  54. self, start_symbol=start_symbol, debug_parser=debug_parser
  55. )
  56. Python38LambdaCustom.__init__(self)
  57. def customize_grammar_rules(self, tokens, customize):
  58. self.customize_grammar_rules_lambda38(tokens, customize)
  59. if __name__ == "__main__":
  60. # Check grammar
  61. from decompyle3.parsers.dump import dump_and_check
  62. p = Python38LambdaParser()
  63. modified_tokens = set(
  64. """JUMP_LOOP CONTINUE RETURN_END_IF COME_FROM
  65. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  66. LAMBDA_MARKER RETURN_LAST
  67. """.split()
  68. )
  69. dump_and_check(p, (3, 8), modified_tokens)