yaml_test_helpers.cpp 3.2 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/base/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. // 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) -> SequenceValue {
  49. llvm::SourceMgr sm;
  50. llvm::yaml::Stream yaml_stream(text, sm);
  51. SequenceValue result;
  52. for (llvm::yaml::Document& document : yaml_stream) {
  53. result.push_back(Parse(document.getRoot()));
  54. }
  55. return result;
  56. }
  57. auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
  58. // Variant visitor that prints the value in the form of code to recreate the
  59. // value.
  60. struct Printer {
  61. auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
  62. auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
  63. auto operator()(ErrorValue /*v*/) -> void { out << "Yaml::ErrorValue()"; }
  64. auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
  65. auto operator()(const MappingValue& v) -> void {
  66. out << "Yaml::MappingValue{";
  67. bool first = true;
  68. for (const auto& [key, value] : v) {
  69. if (first) {
  70. first = false;
  71. } else {
  72. out << ", ";
  73. }
  74. out << "{" << key << ", " << value << "}";
  75. }
  76. out << "}";
  77. }
  78. auto operator()(const SequenceValue& v) -> void {
  79. out << "Yaml::SequenceValue{";
  80. bool first = true;
  81. for (const auto& value : v) {
  82. if (first) {
  83. first = false;
  84. } else {
  85. out << ", ";
  86. }
  87. out << value;
  88. }
  89. out << "}";
  90. }
  91. std::ostream& out;
  92. };
  93. std::visit(Printer{os}, v);
  94. return os;
  95. }
  96. } // namespace Carbon::Testing::Yaml