yaml_test_helpers.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/common/yaml_test_helpers.h"
  5. #include "llvm/ADT/SmallString.h"
  6. #include "llvm/Support/YAMLParser.h"
  7. namespace Carbon::Testing::Yaml {
  8. static auto Parse(llvm::yaml::Node* node) -> Value {
  9. if (!node) {
  10. return Value{ErrorValue()};
  11. }
  12. switch (node->getType()) {
  13. case llvm::yaml::Node::NK_Null:
  14. return Value{NullValue()};
  15. case llvm::yaml::Node::NK_Scalar: {
  16. llvm::SmallString<128> storage;
  17. return Value{
  18. llvm::cast<llvm::yaml::ScalarNode>(*node).getValue(storage).str()};
  19. }
  20. case llvm::yaml::Node::NK_BlockScalar:
  21. return Value{
  22. llvm::cast<llvm::yaml::BlockScalarNode>(*node).getValue().str()};
  23. case llvm::yaml::Node::NK_Mapping: {
  24. MappingValue v;
  25. for (llvm::yaml::KeyValueNode& kv :
  26. llvm::cast<llvm::yaml::MappingNode>(*node)) {
  27. Value key = Parse(kv.getKey());
  28. Value value = Parse(kv.getValue());
  29. v.emplace_back(std::move(key), std::move(value));
  30. }
  31. return Value{std::move(v)};
  32. }
  33. case llvm::yaml::Node::NK_Sequence: {
  34. SequenceValue v;
  35. for (llvm::yaml::Node& n : llvm::cast<llvm::yaml::SequenceNode>(*node)) {
  36. v.push_back(Parse(&n));
  37. }
  38. return Value{std::move(v)};
  39. }
  40. case llvm::yaml::Node::NK_Alias:
  41. return Value{AliasValue()};
  42. case llvm::yaml::Node::NK_KeyValue:
  43. llvm_unreachable("should only exist as child of mapping");
  44. }
  45. llvm_unreachable("unknown yaml node kind");
  46. }
  47. auto Value::FromText(llvm::StringRef text) -> SequenceValue {
  48. llvm::SourceMgr sm;
  49. llvm::yaml::Stream yaml_stream(text, sm);
  50. SequenceValue result;
  51. for (llvm::yaml::Document& document : yaml_stream) {
  52. result.push_back(Parse(document.getRoot()));
  53. }
  54. return result;
  55. }
  56. auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
  57. // Variant visitor that prints the value in the form of code to recreate the
  58. // value.
  59. struct Printer {
  60. auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
  61. auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
  62. auto operator()(ErrorValue /*v*/) -> void { out << "Yaml::ErrorValue()"; }
  63. auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
  64. auto operator()(const MappingValue& v) -> void {
  65. out << "Yaml::MappingValue{";
  66. bool first = true;
  67. for (const auto& [key, value] : v) {
  68. if (first) {
  69. first = false;
  70. } else {
  71. out << ", ";
  72. }
  73. out << "{" << key << ", " << value << "}";
  74. }
  75. out << "}";
  76. }
  77. auto operator()(const SequenceValue& v) -> void {
  78. out << "Yaml::SequenceValue{";
  79. bool first = true;
  80. for (const auto& value : v) {
  81. if (first) {
  82. first = false;
  83. } else {
  84. out << ", ";
  85. }
  86. out << value;
  87. }
  88. out << "}";
  89. }
  90. std::ostream& out;
  91. };
  92. std::visit(Printer{os}, v);
  93. return os;
  94. }
  95. } // namespace Carbon::Testing::Yaml