dump.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (c) 2020-2022 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. """Common grammar dump and check routine"""
  16. def dump_and_check(p, version: tuple, modified_tokens: set) -> None:
  17. p.dump_grammar()
  18. print("=" * 50, "\n")
  19. p.check_grammar()
  20. from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY
  21. if PYTHON_VERSION_TRIPLE[:2] == version[:2]:
  22. lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
  23. from decompyle3.scanner import get_scanner
  24. s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
  25. modified_tokens = set(
  26. """JUMP_LOOP CONTINUE RETURN_END_IF COME_FROM
  27. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  28. LAMBDA_MARKER RETURN_LAST
  29. """.split()
  30. )
  31. print("\nModified opcodes:", modified_tokens)
  32. opcode_set = set(s.opc.opname).union(modified_tokens)
  33. pseudo_tokens = set(tokens) - opcode_set
  34. import re
  35. pseudo_tokens = set([re.sub(r"_\d+$", "", t) for t in pseudo_tokens])
  36. pseudo_tokens = set([re.sub("_CONT$", "", t) for t in pseudo_tokens])
  37. pseudo_tokens = set(pseudo_tokens) - opcode_set
  38. print("\nPseudo tokens:")
  39. print(pseudo_tokens)
  40. import sys
  41. if len(sys.argv) > 1:
  42. from spark_parser.spark import rule2str
  43. for rule in sorted(p.rule2name.items()):
  44. print(rule2str(rule[0]))