dump.cpp 942 B

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