parse23.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (c) 2016-2018, 2020 Rocky Bernstein
  2. # Copyright (c) 2000-2002 by hartmut Goebel <hartmut@goebel.noris.de>
  3. # Copyright (c) 1999 John Aycock
  4. from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
  5. from uncompyle6.parser import PythonParserSingle
  6. from uncompyle6.parsers.parse24 import Python24Parser
  7. class Python23Parser(Python24Parser):
  8. def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
  9. super(Python23Parser, self).__init__(debug_parser)
  10. self.customized = {}
  11. def p_misc23(self, args):
  12. '''
  13. # Python 2.4 only adds something like the below for if 1:
  14. # However we will just treat it as a noop (which of course messes up
  15. # simple verify of bytecode.
  16. # See also below in reduce_is_invalid where we check that the JUMP_FORWARD
  17. # target matches the COME_FROM target
  18. stmt ::= if1_stmt
  19. if1_stmt ::= JUMP_FORWARD JUMP_IF_FALSE THEN POP_TOP COME_FROM
  20. stmts
  21. JUMP_FORWARD COME_FROM POP_TOP COME_FROM
  22. # Used to keep semantic positions the same across later versions
  23. # of Python
  24. _while1test ::= SETUP_LOOP JUMP_FORWARD JUMP_IF_FALSE POP_TOP COME_FROM
  25. while1stmt ::= _while1test l_stmts_opt JUMP_BACK
  26. POP_TOP POP_BLOCK COME_FROM
  27. while1stmt ::= _while1test l_stmts_opt JUMP_BACK COME_FROM
  28. POP_TOP POP_BLOCK COME_FROM
  29. # Python 2.3
  30. # The following has no "JUMP_BACK" after l_stmts because
  31. # l_stmts ends in a "break", "return", or "continue"
  32. while1stmt ::= _while1test l_stmts
  33. POP_TOP POP_BLOCK
  34. # The following has a "COME_FROM" at the end which comes from
  35. # a "break" inside "l_stmts".
  36. while1stmt ::= _while1test l_stmts COME_FROM JUMP_BACK
  37. POP_TOP POP_BLOCK COME_FROM
  38. while1stmt ::= _while1test l_stmts JUMP_BACK
  39. POP_TOP POP_BLOCK
  40. list_comp ::= BUILD_LIST_0 DUP_TOP LOAD_ATTR store list_iter delete
  41. list_for ::= expr for_iter store list_iter JUMP_BACK come_froms POP_TOP JUMP_BACK
  42. lc_body ::= LOAD_NAME expr CALL_FUNCTION_1 POP_TOP
  43. lc_body ::= LOAD_FAST expr CALL_FUNCTION_1 POP_TOP
  44. lc_body ::= LOAD_NAME expr LIST_APPEND
  45. lc_body ::= LOAD_FAST expr LIST_APPEND
  46. # "and" where the first part of the and is true,
  47. # so there is only the 2nd part to evaluate
  48. expr ::= and2
  49. and2 ::= _jump jmp_false COME_FROM expr COME_FROM
  50. alias ::= IMPORT_NAME attributes store
  51. if_exp ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM
  52. '''
  53. def customize_grammar_rules(self, tokens, customize):
  54. super(Python23Parser, self).customize_grammar_rules(tokens, customize)
  55. def reduce_is_invalid(self, rule, ast, tokens, first, last):
  56. invalid = super(Python24Parser,
  57. self).reduce_is_invalid(rule, ast,
  58. tokens, first, last)
  59. if invalid:
  60. return invalid
  61. # FiXME: this code never gets called...
  62. lhs = rule[0]
  63. if lhs == 'nop_stmt':
  64. return not int(tokens[first].pattr) == tokens[last].offset
  65. return False
  66. class Python23ParserSingle(Python23Parser, PythonParserSingle):
  67. pass
  68. if __name__ == '__main__':
  69. # Check grammar
  70. p = Python23Parser()
  71. p.check_grammar()
  72. p.dump_grammar()
  73. # local variables:
  74. # tab-width: 4