yaml_test_helpers.cpp 3.1 KB

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