1#ifndef SOURCEMETA_BLAZE_EVALUATOR_H_
2#define SOURCEMETA_BLAZE_EVALUATOR_H_
4#ifndef SOURCEMETA_BLAZE_EVALUATOR_EXPORT
5#include <sourcemeta/blaze/evaluator_export.h>
8#include <sourcemeta/blaze/evaluator_error.h>
9#include <sourcemeta/blaze/evaluator_instruction.h>
11#include <sourcemeta/core/json.h>
12#include <sourcemeta/core/jsonpointer.h>
13#include <sourcemeta/core/regex.h>
31namespace sourcemeta::blaze {
38 std::vector<Instructions> targets;
43 std::vector<std::vector<std::pair<std::size_t, std::size_t>>>
bindings;
46 std::vector<InstructionExtra> extra;
56constexpr std::size_t JSON_VERSION{20};
60auto SOURCEMETA_BLAZE_EVALUATOR_EXPORT
61from_json(
const sourcemeta::core::JSON &json) -> std::optional<Template>;
84 const sourcemeta::core::WeakPointer &,
85 const sourcemeta::core::WeakPointer &,
const sourcemeta::core::JSON &)>;
120 assert(this->evaluate_path.empty());
121 assert(this->instance_location.empty());
122 assert(this->binding_undo.empty());
124 this->evaluated.clear();
125 return this->evaluate_impl<false>(schema, instance,
nullptr);
190 assert(this->evaluate_path.empty());
191 assert(this->instance_location.empty());
192 assert(this->binding_undo.empty());
193 this->evaluated.clear();
194 return this->evaluate_impl<true>(schema, instance, &callback);
198 template <
bool HasCallback>
199 auto evaluate_impl(
const Template &schema,
200 const sourcemeta::core::JSON &instance,
204 static inline const sourcemeta::core::JSON NULL_VALUE{
nullptr};
205 static inline const sourcemeta::core::JSON EMPTY_STRING{
""};
208 auto evaluate(
const sourcemeta::core::JSON *target) ->
void {
209 this->evaluated.push_back(target);
212 [[nodiscard]]
auto is_evaluated(
const sourcemeta::core::JSON *target,
213 const std::size_t frame)
const ->
bool {
214 assert(frame <= this->evaluated.size());
215 for (
auto index = this->evaluated.size(); index > frame; index--) {
216 if (this->evaluated[index - 1] == target) {
230 [[nodiscard]]
auto checkpoint() const -> std::
size_t {
231 return this->evaluated.size();
234 auto rewind(
const std::size_t checkpoint) ->
void {
235 assert(checkpoint <= this->evaluated.size());
236 this->evaluated.resize(checkpoint);
243 auto enter_entrypoint_resource(
const Template &schema) -> void;
250 const std::vector<std::pair<std::size_t, std::size_t>> &bindings)
252 for (
const auto &binding : bindings) {
253 assert(binding.first < this->dynamic_bindings.size());
254 auto &slot{this->dynamic_bindings[binding.first]};
255 this->binding_undo.push_back(slot);
257 slot = binding.second;
263 const std::vector<std::pair<std::size_t, std::size_t>> &bindings)
265 for (
const auto &binding : std::views::reverse(bindings)) {
266 assert(!this->binding_undo.empty());
267 assert(binding.first < this->dynamic_bindings.size());
268 this->dynamic_bindings[binding.first] = this->binding_undo.back();
269 this->binding_undo.pop_back();
275 [[nodiscard]]
auto dynamic_target(
const std::size_t anchor,
276 const std::size_t fallback)
const
278 assert(anchor < this->dynamic_bindings.size());
279 const auto slot{this->dynamic_bindings[anchor]};
280 return slot == 0 ? fallback : slot;
284#pragma warning(disable : 4251 4275)
286 sourcemeta::core::WeakPointer evaluate_path;
287 sourcemeta::core::WeakPointer instance_location;
292 std::vector<std::size_t> dynamic_bindings;
296 std::vector<std::size_t> binding_undo;
298 std::vector<const sourcemeta::core::JSON *> evaluated;
300#pragma warning(default : 4251 4275)
309#include <sourcemeta/blaze/evaluator_dispatch.h>
311template <
bool HasCallback>
312auto sourcemeta::blaze::Evaluator::evaluate_impl(
313 const Template &schema,
const sourcemeta::core::JSON &instance,
314 const Callback *callback) ->
bool {
315 assert(!schema.targets.empty());
316 assert(schema.target_bindings.size() == schema.targets.size());
317 dispatch::DispatchContext<HasCallback> context{.schema = &schema,
318 .callback = callback,
320 .property_target =
nullptr,
323 if (!schema.anchors.empty()) [[unlikely]] {
324 this->enter_entrypoint_resource(schema);
328 for (
const auto &instruction : schema.targets[0]) {
329 if (!dispatch::evaluate_instruction(instruction, instance, 0, context))
336 if constexpr (HasCallback) {
337 assert(this->evaluate_path.empty());
338 assert(this->instance_location.empty());
341 assert(this->binding_undo.empty());
auto validate(const Template &schema, const sourcemeta::core::JSON &instance) -> bool
Definition evaluator.h:118
std::vector< std::size_t > target_bindings
What each target binds on entry, as an index into Template::bindings.
Definition evaluator.h:45
auto validate(const Template &schema, const sourcemeta::core::JSON &instance, const Callback &callback) -> bool
Definition evaluator.h:188
std::vector< std::string > messages
The message templates that instructions refer to, without duplicates.
Definition evaluator.h:52
std::vector< std::string > vocabularies
The vocabulary URIs that instructions refer to, without duplicates.
Definition evaluator.h:50
std::vector< std::string > anchors
The dynamic anchor names that instructions refer to, without duplicates.
Definition evaluator.h:48
std::vector< std::vector< std::pair< std::size_t, std::size_t > > > bindings
Definition evaluator.h:43
EvaluationType
Definition evaluator.h:65
std::function< void( const EvaluationType, bool, const Instruction &, const InstructionExtra &, const sourcemeta::core::WeakPointer &, const sourcemeta::core::WeakPointer &, const sourcemeta::core::JSON &)> Callback
Definition evaluator.h:82
auto from_json(const sourcemeta::core::JSON &json) -> std::optional< Template >