dump.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. LLVM_DUMP_METHOD auto Dump(const Tree& tree, Lex::TokenIndex token)
  12. -> std::string {
  13. return Lex::Dump(tree.tokens(), token);
  14. }
  15. LLVM_DUMP_METHOD auto Dump(const Tree& tree, NodeId node_id) -> std::string {
  16. RawStringOstream out;
  17. if (!node_id.has_value()) {
  18. out << "NodeId(<none>)";
  19. return out.TakeStr();
  20. }
  21. auto kind = tree.node_kind(node_id);
  22. auto token = tree.node_token(node_id);
  23. out << "NodeId(kind: " << kind
  24. << ", token: " << Lex::Dump(tree.tokens(), token) << ")";
  25. return out.TakeStr();
  26. }
  27. static LLVM_DUMP_METHOD auto Dump(const Context& context, Lex::TokenIndex token)
  28. -> std::string {
  29. return Dump(context.tree(), token);
  30. }
  31. static LLVM_DUMP_METHOD auto Dump(const Context& context, NodeId node_id)
  32. -> std::string {
  33. return Dump(context.tree(), node_id);
  34. }
  35. } // namespace Carbon::Parse
  36. #endif // NDEBUG