heads.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. All of the specific kinds of canned parsers for Python 3.7
  3. These are derived from "compile-modes" but we have others that
  4. can be used to parse common part of a larger grammar.
  5. For example:
  6. * a basic-block expression (no branching)
  7. * an unadorned expression (no POP_TOP needed afterwards)
  8. * A non-compound statement
  9. """
  10. from decompyle3.parsers.p37.full import Python37Parser
  11. from decompyle3.parsers.p37.lambda_expr import Python37LambdaParser
  12. from decompyle3.parsers.parse_heads import (
  13. PythonParserEval,
  14. PythonParserExec,
  15. PythonParserExpr,
  16. PythonParserLambda,
  17. PythonParserSingle,
  18. # FIXME: add
  19. # PythonParserSimpleStmt
  20. # PythonParserStmt
  21. )
  22. from decompyle3.scanners.tok import Token
  23. # Make sure to list Python37... classes first so we prefer to inherit methods from that first.
  24. # In particular methods like reduce_is_invalid() need to come from there rather than
  25. # a more generic place.
  26. class Python37ParserEval(Python37LambdaParser, PythonParserEval):
  27. def __init__(self, debug_parser):
  28. PythonParserEval.__init__(self, debug_parser)
  29. class Python37ParserExec(Python37Parser, PythonParserExec):
  30. def __init__(self, debug_parser):
  31. PythonParserExec.__init__(self, debug_parser)
  32. class Python37ParserExpr(Python37Parser, PythonParserExpr):
  33. def __init__(self, debug_parser):
  34. PythonParserExpr.__init__(self, debug_parser)
  35. class Python37ParserLambda(Python37LambdaParser, PythonParserLambda):
  36. def __init__(self, debug_parser):
  37. PythonParserLambda.__init__(self, debug_parser)
  38. def reduce_is_invalid(self, rule, ast, tokens, first, last):
  39. if rule[0] == "call_kw":
  40. # Make sure we don't derive call_kw
  41. nt = ast[0]
  42. while not isinstance(nt, Token):
  43. if nt[0] == "call_kw":
  44. return True
  45. nt = nt[0]
  46. pass
  47. pass
  48. return False
  49. # These classes are here just to get parser doc-strings for the
  50. # various classes inherited properly and start_symbols set pproperly
  51. class Python37ParserSingle(Python37Parser, PythonParserSingle):
  52. def __init__(self, debug_parser):
  53. PythonParserSingle.__init__(self, debug_parser)