|
1 | 1 | #include <sourcemeta/codegen/ir.h> |
2 | 2 |
|
3 | | -namespace sourcemeta::codegen {} // namespace sourcemeta::codegen |
| 3 | +#include <algorithm> // std::ranges::sort |
| 4 | +#include <functional> // std::reference_wrapper |
| 5 | +#include <map> // std::map |
| 6 | +#include <vector> // std::vector |
| 7 | + |
| 8 | +namespace sourcemeta::codegen { |
| 9 | + |
| 10 | +auto compile( |
| 11 | + const sourcemeta::core::JSON &schema, |
| 12 | + const sourcemeta::core::SchemaWalker &walker, |
| 13 | + const sourcemeta::core::SchemaResolver &resolver, |
| 14 | + const std::optional<sourcemeta::core::JSON::String> &default_dialect, |
| 15 | + const std::optional<sourcemeta::core::JSON::String> &default_id) |
| 16 | + -> IRResult { |
| 17 | + sourcemeta::core::SchemaFrame frame{ |
| 18 | + sourcemeta::core::SchemaFrame::Mode::Instances}; |
| 19 | + frame.analyse(schema, walker, resolver, default_dialect, default_id); |
| 20 | + std::map<sourcemeta::core::PointerTemplate, |
| 21 | + std::vector<std::reference_wrapper< |
| 22 | + const sourcemeta::core::SchemaFrame::Location>>> |
| 23 | + instance_to_locations; |
| 24 | + for (const auto &[key, location] : frame.locations()) { |
| 25 | + if (location.type == |
| 26 | + sourcemeta::core::SchemaFrame::LocationType::Resource || |
| 27 | + location.type == |
| 28 | + sourcemeta::core::SchemaFrame::LocationType::Subschema) { |
| 29 | + for (const auto &instance_location : frame.instance_locations(location)) { |
| 30 | + instance_to_locations[instance_location].emplace_back( |
| 31 | + std::cref(location)); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + IRResult result; |
| 37 | + |
| 38 | + // Process each instance location group |
| 39 | + for (const auto &[instance_location, locations] : instance_to_locations) { |
| 40 | + for (const auto &location_ref : locations) { |
| 41 | + const auto &location{location_ref.get()}; |
| 42 | + const auto &subschema{sourcemeta::core::get(schema, location.pointer)}; |
| 43 | + if (!subschema.is_object()) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + const auto vocabularies{frame.vocabularies(location, resolver)}; |
| 48 | + |
| 49 | + if (subschema.defines("type")) { |
| 50 | + const auto &type_result{walker("type", vocabularies)}; |
| 51 | + if (type_result.type != |
| 52 | + sourcemeta::core::SchemaKeywordType::Assertion) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + const auto &type_value{subschema.at("type")}; |
| 57 | + if (!type_value.is_string()) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + const auto &type_string{type_value.to_string()}; |
| 62 | + |
| 63 | + if (type_string == "string") { |
| 64 | + result.emplace_back(IRScalar{.pointer = instance_location, |
| 65 | + .value = IRScalarType::String}); |
| 66 | + } else if (type_string == "object") { |
| 67 | + IRObject object; |
| 68 | + object.pointer = instance_location; |
| 69 | + |
| 70 | + // Find child instance locations (one property token deeper) |
| 71 | + for (const auto &[child_instance, child_locations] : |
| 72 | + instance_to_locations) { |
| 73 | + if (!child_instance.trivial() || child_instance.empty()) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // Check if child is exactly one property token deeper |
| 78 | + auto child_size{ |
| 79 | + std::distance(child_instance.begin(), child_instance.end())}; |
| 80 | + auto parent_size{std::distance(instance_location.begin(), |
| 81 | + instance_location.end())}; |
| 82 | + if (child_size != parent_size + 1) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + // Verify parent prefix matches |
| 87 | + auto matches{true}; |
| 88 | + auto child_iter{child_instance.begin()}; |
| 89 | + for (const auto &parent_token : instance_location) { |
| 90 | + if (*child_iter != parent_token) { |
| 91 | + matches = false; |
| 92 | + break; |
| 93 | + } |
| 94 | + ++child_iter; |
| 95 | + } |
| 96 | + |
| 97 | + if (!matches) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + // Get the property name from the last token |
| 102 | + const auto &last_token{*child_instance.rbegin()}; |
| 103 | + if (!std::holds_alternative<sourcemeta::core::Pointer::Token>( |
| 104 | + last_token)) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + const auto &property_token{ |
| 109 | + std::get<sourcemeta::core::Pointer::Token>(last_token)}; |
| 110 | + if (!property_token.is_property()) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + object.members.emplace(property_token.to_property(), |
| 115 | + IRObjectValue{.required = false, |
| 116 | + .immutable = false, |
| 117 | + .pointer = child_instance}); |
| 118 | + } |
| 119 | + |
| 120 | + result.emplace_back(std::move(object)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Sort by pointer template (longer paths come first, so dependencies |
| 127 | + // appear before their parents) |
| 128 | + const auto get_pointer{ |
| 129 | + [](const auto &entry) -> const sourcemeta::core::PointerTemplate & { |
| 130 | + return entry.pointer; |
| 131 | + }}; |
| 132 | + std::ranges::sort( |
| 133 | + result, [&get_pointer](const IREntity &a, const IREntity &b) -> bool { |
| 134 | + return std::visit(get_pointer, b) < std::visit(get_pointer, a); |
| 135 | + }); |
| 136 | + |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace sourcemeta::codegen |
0 commit comments