show.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2018, 2020, 2023 Rocky Bernstein <rocky@gnu.org>
  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. import sys
  16. def maybe_show_asm(showasm, tokens):
  17. """
  18. Show the asm based on the showasm flag (or file object), writing to the
  19. appropriate stream depending on the type of the flag.
  20. :param showasm: Flag which determines whether the ingested code is
  21. written to sys.stdout or not. (It is also to pass a file
  22. like object, into which the asm will be written).
  23. :param tokens: The asm tokens to show.
  24. """
  25. if showasm:
  26. stream = showasm if hasattr(showasm, "write") else sys.stdout
  27. for t in tokens:
  28. stream.write(str(t))
  29. stream.write("\n")
  30. def maybe_show_tree(walker, ast):
  31. """
  32. Show the ast based on the showast flag (or file object), writing to the
  33. appropriate stream depending on the type of the flag.
  34. :param show_tree: Flag which determines whether the parse tree is
  35. written to sys.stdout or not. (It is also to pass a file
  36. like object, into which the ast will be written).
  37. :param ast: The ast to show.
  38. """
  39. if walker.showast:
  40. if hasattr(walker.showast, "write"):
  41. stream = walker.showast
  42. else:
  43. stream = sys.stdout
  44. if (
  45. isinstance(walker.showast, dict)
  46. and walker.showast.get("after", False)
  47. and hasattr(walker, "str_with_template")
  48. ):
  49. walker.str_with_template(ast)
  50. else:
  51. stream.write(str(ast))
  52. stream.write("\n")
  53. def maybe_show_tree_param_default(show_tree, name, default):
  54. """
  55. Show a function parameter with default for an grammar-tree based on the show_tree flag
  56. (or file object), writing to the appropriate stream depending on the type
  57. of the flag.
  58. :param show_tree: Flag which determines whether the function parameter with
  59. default is written to sys.stdout or not. (It is also to
  60. pass a file like object, into which the ast will be
  61. written).
  62. :param name: The function parameter name.
  63. :param default: The function parameter default.
  64. """
  65. if show_tree:
  66. stream = show_tree if hasattr(show_tree, "write") else sys.stdout
  67. stream.write("\n")
  68. stream.write("--" + name)
  69. stream.write("\n")
  70. stream.write(str(default))
  71. stream.write("\n")
  72. stream.write("--")
  73. stream.write("\n")