Blaze 0.0.1
The ultra high-performance JSON Schema evaluator
 
Loading...
Searching...
No Matches
Compiler

Compile a JSON Schema into a set of low-level instructions for fast evaluation. More...

Classes

struct  sourcemeta::blaze::SchemaContext
 
struct  sourcemeta::blaze::DynamicContext
 
struct  sourcemeta::blaze::Tweaks
 
struct  sourcemeta::blaze::Context
 
struct  sourcemeta::blaze::SchemaUnevaluatedEntry
 

Typedefs

using sourcemeta::blaze::Compiler = std::function< Instructions(const Context &, const SchemaContext &, const DynamicContext &, const Instructions &)>
 
using sourcemeta::blaze::SchemaUnevaluatedEntries = std::map< sourcemeta::core::JSON::String, SchemaUnevaluatedEntry >
 

Functions

auto sourcemeta::blaze::default_schema_compiler (const Context &, const SchemaContext &, const DynamicContext &, const Instructions &) -> Instructions
 
auto sourcemeta::blaze::compile (const sourcemeta::core::JSON &schema, const sourcemeta::blaze::SchemaWalker &walker, const sourcemeta::blaze::SchemaResolver &resolver, const Compiler &compiler, const Mode mode=Mode::FastValidation, const std::string_view default_dialect="", const std::string_view default_id="", const std::string_view entrypoint="", const std::optional< Tweaks > &tweaks=std::nullopt) -> Template
 
auto sourcemeta::blaze::compile (const sourcemeta::core::JSON &schema, const sourcemeta::blaze::SchemaWalker &walker, const sourcemeta::blaze::SchemaResolver &resolver, const Compiler &compiler, const sourcemeta::blaze::SchemaFrame &frame, const std::string_view entrypoint, const Mode mode=Mode::FastValidation, const std::optional< Tweaks > &tweaks=std::nullopt) -> Template
 
auto sourcemeta::blaze::compile (const Context &context, const SchemaContext &schema_context, const DynamicContext &dynamic_context, const sourcemeta::core::WeakPointer &schema_suffix, const sourcemeta::core::WeakPointer &instance_suffix=sourcemeta::core::empty_weak_pointer, std::optional< std::string_view > uri=std::nullopt) -> Instructions
 
auto sourcemeta::blaze::to_json (const Template &schema_template) -> sourcemeta::core::JSON
 

Detailed Description

Compile a JSON Schema into a set of low-level instructions for fast evaluation.


Class Documentation

◆ sourcemeta::blaze::SchemaContext

struct sourcemeta::blaze::SchemaContext

The schema compiler context is the current subschema information you have at your disposal to implement a keyword

Public Attributes

const sourcemeta::core::WeakPointer & relative_pointer
 The schema location relative to the base URI.
 
const sourcemeta::core::JSON & schema
 The current subschema.
 
const sourcemeta::blaze::Vocabulariesvocabularies
 The schema vocabularies in use.
 
const sourcemeta::core::URI & base
 The schema base URI.
 
bool is_property_name
 Whether the current schema targets a property name.
 

◆ sourcemeta::blaze::DynamicContext

struct sourcemeta::blaze::DynamicContext

The dynamic compiler context is the read-write information you have at your disposal to implement a keyword

Public Attributes

const sourcemeta::core::JSON::String & keyword
 The schema keyword.
 
const sourcemeta::core::WeakPointer & base_schema_location
 The schema base keyword path.
 
const sourcemeta::core::WeakPointer & base_instance_location
 The base instance location that the keyword must be evaluated to.
 

◆ sourcemeta::blaze::Tweaks

struct sourcemeta::blaze::Tweaks

Advanced knobs that you can tweak for higher control and optimisations

Public Attributes

bool properties_always_unroll {false}
 Always unroll properties in a logical AND operation.
 
bool properties_reorder {true}
 Attempt to re-order properties subschemas to evaluate cheaper ones first.
 
std::size_t target_inline_threshold {50}
 Inline jump targets with fewer instructions than this threshold.
 
bool format_assertion {false}
 When set, force format to be compiled as an assertion.
 

◆ sourcemeta::blaze::Context

struct sourcemeta::blaze::Context

The static compiler context is the information you have at your disposal to implement a keyword that will never change throughout the compilation process

Public Attributes

const sourcemeta::core::JSON & root
 The root schema resource.
 
const sourcemeta::blaze::SchemaFrameframe
 The reference frame of the entire schema.
 
const std::vector< std::string > resources
 The set of all schema resources in the schema without duplicates.
 
const sourcemeta::blaze::SchemaWalkerwalker
 The schema walker in use.
 
const sourcemeta::blaze::SchemaResolverresolver
 The schema resolver in use.
 
const Compilercompiler
 The schema compiler in use.
 
const Mode mode
 The mode of the schema compiler.
 
const bool uses_dynamic_scopes
 Whether the schema makes use of dynamic scoping.
 
const SchemaUnevaluatedEntries unevaluated
 The list of unevaluated entries and their dependencies.
 
const Tweaks tweaks
 The set of tweaks for the compiler.
 
const std::map< std::tuple< sourcemeta::blaze::SchemaReferenceType, std::string_view, bool >, std::pair< std::size_t, const sourcemeta::core::WeakPointer * > > targets
 All possible reference targets (key includes is_property_name context)
 
std::vector< InstructionExtra > & extra
 Accumulator for instruction extra data during compilation.
 

◆ sourcemeta::blaze::SchemaUnevaluatedEntry

struct sourcemeta::blaze::SchemaUnevaluatedEntry

Public Attributes

std::set< sourcemeta::core::WeakPointer > static_dependencies
 The absolute pointers of the static keyword dependencies.
 
std::set< sourcemeta::core::WeakPointer > dynamic_dependencies
 The absolute pointers of the static keyword dependencies.
 
bool unresolved {false}
 

Member Data Documentation

◆ unresolved

bool sourcemeta::blaze::SchemaUnevaluatedEntry::unresolved {false}

Whether the entry cannot be fully resolved, which means there might be unknown dynamic dependencies

Typedef Documentation

◆ Compiler

using sourcemeta::blaze::Compiler = typedef std::function<Instructions(const Context &, const SchemaContext &, const DynamicContext &, const Instructions &)>

A compiler is represented as a function that maps a keyword compiler contexts into a compiler template. You can provide your own to implement your own keywords

◆ SchemaUnevaluatedEntries

using sourcemeta::blaze::SchemaUnevaluatedEntries = typedef std::map<sourcemeta::core::JSON::String, SchemaUnevaluatedEntry>

The flattened set of unevaluated cases in the schema by absolute URI

Function Documentation

◆ compile() [1/3]

auto sourcemeta::blaze::compile ( const Context context,
const SchemaContext schema_context,
const DynamicContext dynamic_context,
const sourcemeta::core::WeakPointer &  schema_suffix,
const sourcemeta::core::WeakPointer &  instance_suffix = sourcemeta::core::empty_weak_pointer,
std::optional< std::string_view >  uri = std::nullopt 
) -> Instructions

This function compiles a single subschema into a compiler template as determined by the given pointer. If a URI is given, the compiler will attempt to jump to that corresponding frame entry. Otherwise, it will navigate within the current keyword. This function is not meant to be used directly, but instead as a building block for supporting applicators on compiler functions.

◆ compile() [2/3]

auto sourcemeta::blaze::compile ( const sourcemeta::core::JSON &  schema,
const sourcemeta::blaze::SchemaWalker walker,
const sourcemeta::blaze::SchemaResolver resolver,
const Compiler compiler,
const Mode  mode = Mode::FastValidation,
const std::string_view  default_dialect = "",
const std::string_view  default_id = "",
const std::string_view  entrypoint = "",
const std::optional< Tweaks > &  tweaks = std::nullopt 
) -> Template

This function compiles an input JSON Schema into a template that can be later evaluated. For example:

#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/blaze/foundation.h>
const sourcemeta::core::JSON schema =
sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");
const auto schema_template{sourcemeta::blaze::compile(
// Evaluate or encode
auto default_schema_compiler(const Context &, const SchemaContext &, const DynamicContext &, const Instructions &) -> Instructions
SOURCEMETA_BLAZE_FOUNDATION_EXPORT auto schema_walker(const std::string_view keyword, const Vocabularies &vocabularies) -> const SchemaWalkerResult &
SOURCEMETA_BLAZE_FOUNDATION_EXPORT auto schema_resolver(const std::string_view identifier) -> std::optional< sourcemeta::core::JSON >

◆ compile() [3/3]

auto sourcemeta::blaze::compile ( const sourcemeta::core::JSON &  schema,
const sourcemeta::blaze::SchemaWalker walker,
const sourcemeta::blaze::SchemaResolver resolver,
const Compiler compiler,
const sourcemeta::blaze::SchemaFrame frame,
const std::string_view  entrypoint,
const Mode  mode = Mode::FastValidation,
const std::optional< Tweaks > &  tweaks = std::nullopt 
) -> Template

This function compiles an input JSON Schema into a template that can be later evaluated, but given an existing schema frame. The schema frame must contain reference information for the given schema and the input schema must be bundled. If those pre-conditions are not met, you will hit undefined behavior.

Don't use this function unless you know what you are doing.

◆ default_schema_compiler()

auto sourcemeta::blaze::default_schema_compiler ( const Context ,
const SchemaContext ,
const DynamicContext ,
const Instructions  
) -> Instructions

A default compiler that aims to implement every keyword for official JSON Schema dialects.

◆ to_json()

auto sourcemeta::blaze::to_json ( const Template schema_template) -> sourcemeta::core::JSON

Serialise a template as JSON