dump.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef NDEBUG
  5. #include "toolchain/parse/dump.h"
  6. #include <string>
  7. #include "common/raw_string_ostream.h"
  8. #include "toolchain/lex/dump.h"
  9. #include "toolchain/parse/context.h"
  10. namespace Carbon::Parse {
  11. static LLVM_DUMP_METHOD auto Dump(const Tree& tree) -> std::string {
  12. return PrintToString(tree);
  13. }
  14. LLVM_DUMP_METHOD auto Dump(const Tree& tree, Lex::TokenIndex token)
  15. -> std::string {
  16. return Lex::Dump(tree.tokens(), token);
  17. }
  18. LLVM_DUMP_METHOD auto Dump(const Tree& tree, NodeId node_id) -> std::string {
  19. RawStringOstream out;
  20. if (!node_id.has_value()) {
  21. out << "NodeId(<none>)";
  22. return out.TakeStr();
  23. }
  24. auto kind = tree.node_kind(node_id);
  25. auto token = tree.node_token(node_id);
  26. out << "NodeId(kind: " << kind
  27. << ", token: " << Lex::Dump(tree.tokens(), token) << ")";
  28. return out.TakeStr();
  29. }
  30. static LLVM_DUMP_METHOD auto Dump(const Context& context, Lex::TokenIndex token)
  31. -> std::string {
  32. return Dump(context.tree(), token);
  33. }
  34. static LLVM_DUMP_METHOD auto Dump(const Context& context, NodeId node_id)
  35. -> std::string {
  36. return Dump(context.tree(), node_id);
  37. }
  38. } // namespace Carbon::Parse
  39. #endif // NDEBUG