yaml_test_helpers.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "toolchain/testing/yaml_test_helpers.h"
  5. #include <iomanip>
  6. #include <optional>
  7. #include <ostream>
  8. #include <string>
  9. #include <utility>
  10. #include <variant>
  11. #include "common/raw_string_ostream.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/Support/YAMLParser.h"
  14. namespace Carbon::Testing::Yaml {
  15. // This is for tests, so we should be okay with the recursion here.
  16. // NOLINTNEXTLINE(misc-no-recursion)
  17. static auto Parse(llvm::yaml::Node* node) -> Value {
  18. CARBON_CHECK(node != nullptr);
  19. // getType returns an unsigned int which should map to the enum.
  20. switch (static_cast<llvm::yaml::Node::NodeKind>(node->getType())) {
  21. case llvm::yaml::Node::NK_Null:
  22. return Value(NullValue());
  23. case llvm::yaml::Node::NK_Scalar: {
  24. llvm::SmallString<128> storage;
  25. return Value{
  26. llvm::cast<llvm::yaml::ScalarNode>(*node).getValue(storage).str()};
  27. }
  28. case llvm::yaml::Node::NK_BlockScalar:
  29. return Value{
  30. llvm::cast<llvm::yaml::BlockScalarNode>(*node).getValue().str()};
  31. case llvm::yaml::Node::NK_Mapping: {
  32. MappingValue v;
  33. for (llvm::yaml::KeyValueNode& kv :
  34. llvm::cast<llvm::yaml::MappingNode>(*node)) {
  35. Value key = Parse(kv.getKey());
  36. Value value = Parse(kv.getValue());
  37. v.emplace_back(std::move(key), std::move(value));
  38. }
  39. return Value(std::move(v));
  40. }
  41. case llvm::yaml::Node::NK_Sequence: {
  42. SequenceValue v;
  43. for (llvm::yaml::Node& n : llvm::cast<llvm::yaml::SequenceNode>(*node)) {
  44. v.push_back(Parse(&n));
  45. }
  46. return Value(std::move(v));
  47. }
  48. case llvm::yaml::Node::NK_Alias:
  49. return Value(AliasValue());
  50. case llvm::yaml::Node::NK_KeyValue:
  51. llvm_unreachable("should only exist as child of mapping");
  52. }
  53. llvm_unreachable("unknown yaml node kind");
  54. }
  55. auto Value::FromText(llvm::StringRef text) -> ErrorOr<SequenceValue> {
  56. llvm::SourceMgr sm;
  57. std::optional<std::string> error_message;
  58. sm.setDiagHandler(
  59. [](const llvm::SMDiagnostic& diag, void* context) -> void {
  60. auto* error_message = static_cast<std::optional<std::string>*>(context);
  61. RawStringOstream stream;
  62. diag.print(/*ProgName=*/nullptr, stream, /*ShowColors=*/false);
  63. *error_message = stream.TakeStr();
  64. },
  65. &error_message);
  66. llvm::yaml::Stream yaml_stream(text, sm);
  67. SequenceValue result;
  68. for (llvm::yaml::Document& document : yaml_stream) {
  69. result.push_back(Parse(document.getRoot()));
  70. if (error_message) {
  71. return Error(*error_message);
  72. }
  73. }
  74. return result;
  75. }
  76. auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
  77. // Variant visitor that prints the value in the form of code to recreate the
  78. // value.
  79. struct Printer {
  80. auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
  81. auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
  82. auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
  83. auto operator()(const MappingValue& v) -> void {
  84. out << "Yaml::MappingValue{";
  85. bool first = true;
  86. for (const auto& [key, value] : v) {
  87. if (first) {
  88. first = false;
  89. } else {
  90. out << ", ";
  91. }
  92. out << "{" << key << ", " << value << "}";
  93. }
  94. out << "}";
  95. }
  96. auto operator()(const SequenceValue& v) -> void {
  97. out << "Yaml::SequenceValue{";
  98. bool first = true;
  99. for (const auto& value : v) {
  100. if (first) {
  101. first = false;
  102. } else {
  103. out << ", ";
  104. }
  105. out << value;
  106. }
  107. out << "}";
  108. }
  109. std::ostream& out;
  110. };
  111. std::visit(Printer{.out = os}, v);
  112. return os;
  113. }
  114. } // namespace Carbon::Testing::Yaml