Blaze 0.0.1
The ultra high-performance JSON Schema evaluator
Loading...
Searching...
No Matches
evaluator.h
1#ifndef SOURCEMETA_BLAZE_EVALUATOR_H_
2#define SOURCEMETA_BLAZE_EVALUATOR_H_
3
4#ifndef SOURCEMETA_BLAZE_EVALUATOR_EXPORT
5#include <sourcemeta/blaze/evaluator_export.h>
6#endif
7
8#include <sourcemeta/blaze/evaluator_error.h>
9#include <sourcemeta/blaze/evaluator_instruction.h>
10
11#include <sourcemeta/core/json.h>
12#include <sourcemeta/core/jsonpointer.h>
13#include <sourcemeta/core/regex.h>
14
15#include <cassert> // assert
16#include <cstdint> // std::uint8_t
17#include <functional> // std::function
18#include <ranges> // std::views::reverse
19#include <utility> // std::pair
20#include <vector> // std::vector
21
30
31namespace sourcemeta::blaze {
32
35struct Template {
36 bool dynamic;
37 bool frames;
38 std::vector<Instructions> targets;
43 std::vector<std::vector<std::pair<std::size_t, std::size_t>>> bindings;
45 std::vector<std::size_t> target_bindings;
46 std::vector<InstructionExtra> extra;
48 std::vector<std::string> anchors;
50 std::vector<std::string> vocabularies;
52 std::vector<std::string> messages;
53};
54
56constexpr std::size_t JSON_VERSION{20};
57
60auto SOURCEMETA_BLAZE_EVALUATOR_EXPORT
61from_json(const sourcemeta::core::JSON &json) -> std::optional<Template>;
62
65enum class EvaluationType : std::uint8_t { Pre, Post };
66
81// TODO(C++23): Use std::move_only_function when available in libc++
82using Callback = std::function<void(
83 const EvaluationType, bool, const Instruction &, const InstructionExtra &,
84 const sourcemeta::core::WeakPointer &,
85 const sourcemeta::core::WeakPointer &, const sourcemeta::core::JSON &)>;
86
88class SOURCEMETA_BLAZE_EVALUATOR_EXPORT Evaluator {
89public:
118 auto validate(const Template &schema, const sourcemeta::core::JSON &instance)
119 -> bool {
120 assert(this->evaluate_path.empty());
121 assert(this->instance_location.empty());
122 assert(this->binding_undo.empty());
123
124 this->evaluated.clear();
125 return this->evaluate_impl<false>(schema, instance, nullptr);
126 }
127
188 auto validate(const Template &schema, const sourcemeta::core::JSON &instance,
189 const Callback &callback) -> bool {
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);
195 }
196
197#ifndef DOXYGEN
198 template <bool HasCallback>
199 auto evaluate_impl(const Template &schema,
200 const sourcemeta::core::JSON &instance,
201 const Callback *callback) -> bool;
202
203 // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables,cert-err58-cpp,bugprone-throwing-static-initialization)
204 static inline const sourcemeta::core::JSON NULL_VALUE{nullptr};
205 static inline const sourcemeta::core::JSON EMPTY_STRING{""};
206 // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables,cert-err58-cpp,bugprone-throwing-static-initialization)
207
208 auto evaluate(const sourcemeta::core::JSON *target) -> void {
209 this->evaluated.push_back(target);
210 }
211
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) {
217 return true;
218 }
219 }
220
221 return false;
222 }
223
224 // A subschema that ends up failing must not contribute any of the marks it
225 // made along the way, so a disjunction records the length here before each
226 // branch and truncates back to it when the branch does not hold, as does a
227 // negation for its whole subtree. Marks are only ever appended, never
228 // inserted earlier, so everything a branch adds sits past the recorded
229 // length and nothing from outside it can be lost
230 [[nodiscard]] auto checkpoint() const -> std::size_t {
231 return this->evaluated.size();
232 }
233
234 auto rewind(const std::size_t checkpoint) -> void {
235 assert(checkpoint <= this->evaluated.size());
236 this->evaluated.resize(checkpoint);
237 }
238
239 // What the schema resource the entry point sits in declares, which no
240 // instruction stands for. This lives in a translation unit of its own, as
241 // inlining it pushes the evaluation path past what its caller will take on,
242 // which costs a call per evaluation on every schema, dynamic anchors or not
243 auto enter_entrypoint_resource(const Template &schema) -> void;
244
245 // A schema resource claims each dynamic anchor it declares that no enclosing
246 // resource already holds, so a name resolves to its outermost declaration.
247 // What the slots held goes on a stack of its own, as only a resource that
248 // declares an anchor ever reaches here
249 auto enter_resource(
250 const std::vector<std::pair<std::size_t, std::size_t>> &bindings)
251 -> void {
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);
256 if (slot == 0) {
257 slot = binding.second;
258 }
259 }
260 }
261
262 auto leave_resource(
263 const std::vector<std::pair<std::size_t, std::size_t>> &bindings)
264 -> void {
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();
270 }
271 }
272
273 // What a dynamic reference resolves to, given the name it carries and the
274 // static target it fell back on
275 [[nodiscard]] auto dynamic_target(const std::size_t anchor,
276 const std::size_t fallback) const
277 -> std::size_t {
278 assert(anchor < this->dynamic_bindings.size());
279 const auto slot{this->dynamic_bindings[anchor]};
280 return slot == 0 ? fallback : slot;
281 }
282
283#if defined(_MSC_VER)
284#pragma warning(disable : 4251 4275)
285#endif
286 sourcemeta::core::WeakPointer evaluate_path;
287 sourcemeta::core::WeakPointer instance_location;
288 // What dynamic anchor each name currently resolves to, as a one-based index
289 // into Template::targets where zero means the name is not in scope. A
290 // resource writes a name on entry only when it is not already taken, so a
291 // slot holds the outermost declaration in the dynamic scope
292 std::vector<std::size_t> dynamic_bindings;
293 // What the slots a resource wrote on entry held beforehand, so that leaving
294 // it puts them back. Only a resource that declares a dynamic anchor touches
295 // this, rather than every instruction
296 std::vector<std::size_t> binding_undo;
297
298 std::vector<const sourcemeta::core::JSON *> evaluated;
299#if defined(_MSC_VER)
300#pragma warning(default : 4251 4275)
301#endif
302#endif
303};
304
305} // namespace sourcemeta::blaze
306
307#ifndef DOXYGEN
308
309#include <sourcemeta/blaze/evaluator_dispatch.h>
310
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,
319 .evaluator = this,
320 .property_target = nullptr,
321 .frame = 0};
322
323 if (!schema.anchors.empty()) [[unlikely]] {
324 this->enter_entrypoint_resource(schema);
325 }
326
327 bool overall{true};
328 for (const auto &instruction : schema.targets[0]) {
329 if (!dispatch::evaluate_instruction(instruction, instance, 0, context))
330 [[unlikely]] {
331 overall = false;
332 break;
333 }
334 }
335
336 if constexpr (HasCallback) {
337 assert(this->evaluate_path.empty());
338 assert(this->instance_location.empty());
339 }
340
341 assert(this->binding_undo.empty());
342
343 return overall;
344}
345
346#endif // !DOXYGEN
347
348#endif
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
Definition evaluator.h:88
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 >
Definition evaluator_instruction.h:368
Definition evaluator_instruction.h:351
Definition evaluator.h:35