yaml_test_helpers.cpp 3.7 KB

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