dump.cpp 923 B

123456789101112131415161718192021222324252627282930313233343536
  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 "common/raw_string_ostream.h"
  7. #include "toolchain/lex/dump.h"
  8. namespace Carbon::Parse {
  9. LLVM_DUMP_METHOD auto Dump(const Tree& tree, Lex::TokenIndex token)
  10. -> std::string {
  11. return Lex::Dump(tree.tokens(), token);
  12. }
  13. LLVM_DUMP_METHOD auto Dump(const Tree& tree, NodeId node_id) -> std::string {
  14. RawStringOstream out;
  15. if (!node_id.has_value()) {
  16. out << "NodeId(<none>)";
  17. return out.TakeStr();
  18. }
  19. auto kind = tree.node_kind(node_id);
  20. auto token = tree.node_token(node_id);
  21. out << "NodeId(kind: " << kind
  22. << ", token: " << Lex::Dump(tree.tokens(), token) << ")";
  23. return out.TakeStr();
  24. }
  25. } // namespace Carbon::Parse
  26. #endif // NDEBUG