From 7b4cba5d4ac8088ff3d9df1a9c316534385fbf19 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 15 Oct 2024 15:10:48 -0400 Subject: [PATCH] Simplify interface member names Signed-off-by: Juan Cruz Viotti --- benchmark/validator_helper.cc | 5 +- src/compiler/compile.cc | 97 ++- src/compiler/compile_describe.cc | 191 ++--- src/compiler/compile_helpers.h | 23 +- src/compiler/compile_json.cc | 188 ++--- src/compiler/default_compiler.cc | 10 +- src/compiler/default_compiler_2019_09.h | 194 ++--- src/compiler/default_compiler_2020_12.h | 44 +- src/compiler/default_compiler_draft4.h | 715 ++++++++---------- src/compiler/default_compiler_draft6.h | 125 ++- src/compiler/default_compiler_draft7.h | 30 +- .../include/sourcemeta/blaze/compiler.h | 66 +- .../include/sourcemeta/blaze/compiler_error.h | 10 +- src/evaluator/context.cc | 8 +- src/evaluator/evaluator.cc | 472 ++++++------ .../include/sourcemeta/blaze/evaluator.h | 23 +- .../sourcemeta/blaze/evaluator_context.h | 10 +- .../sourcemeta/blaze/evaluator_error.h | 4 +- .../sourcemeta/blaze/evaluator_template.h | 440 +++++------ .../sourcemeta/blaze/evaluator_value.h | 53 +- test/compiler/compiler_json_test.cc | 85 +-- .../compiler_output_error_trace_test.cc | 36 +- .../compiler/compiler_template_format_test.cc | 4 +- test/evaluator/evaluator_draft4_test.cc | 10 +- test/evaluator/evaluator_test.cc | 4 +- test/evaluator/evaluator_utils.h | 80 +- test/evaluator/officialsuite.cc | 25 +- 27 files changed, 1311 insertions(+), 1641 deletions(-) diff --git a/benchmark/validator_helper.cc b/benchmark/validator_helper.cc index 17b70a06..0d899e64 100644 --- a/benchmark/validator_helper.cc +++ b/benchmark/validator_helper.cc @@ -35,9 +35,8 @@ auto main(int argc, char **argv) noexcept -> int { const sourcemeta::jsontoolkit::JSON template_json{ sourcemeta::blaze::to_json(schema_template)}; - sourcemeta::jsontoolkit::prettify( - template_json, std::cout, - sourcemeta::blaze::compiler_template_format_compare); + sourcemeta::jsontoolkit::prettify(template_json, std::cout, + sourcemeta::blaze::template_format_compare); std::cout << std::endl; std::cout << compile_duration.count() << "ms\n"; diff --git a/src/compiler/compile.cc b/src/compiler/compile.cc index 79a07ad8..ce1e63ab 100644 --- a/src/compiler/compile.cc +++ b/src/compiler/compile.cc @@ -12,12 +12,11 @@ namespace { -auto compile_subschema( - const sourcemeta::blaze::SchemaCompilerContext &context, - const sourcemeta::blaze::SchemaCompilerSchemaContext &schema_context, - const sourcemeta::blaze::SchemaCompilerDynamicContext &dynamic_context, - const std::optional &default_dialect) - -> sourcemeta::blaze::SchemaCompilerTemplate { +auto compile_subschema(const sourcemeta::blaze::Context &context, + const sourcemeta::blaze::SchemaContext &schema_context, + const sourcemeta::blaze::DynamicContext &dynamic_context, + const std::optional &default_dialect) + -> sourcemeta::blaze::Template { using namespace sourcemeta::blaze; assert(is_schema(schema_context.schema)); @@ -27,13 +26,12 @@ auto compile_subschema( if (schema_context.schema.to_boolean()) { return {}; } else { - return {make(true, context, schema_context, - dynamic_context, - SchemaCompilerValueNone{})}; + return {make(true, context, schema_context, + dynamic_context, ValueNone{})}; } } - SchemaCompilerTemplate steps; + Template steps; for (const auto &entry : sourcemeta::jsontoolkit::SchemaKeywordIterator{ schema_context.schema, context.walker, context.resolver, default_dialect}) { @@ -68,9 +66,8 @@ namespace sourcemeta::blaze { auto compile(const sourcemeta::jsontoolkit::JSON &schema, const sourcemeta::jsontoolkit::SchemaWalker &walker, const sourcemeta::jsontoolkit::SchemaResolver &resolver, - const SchemaCompiler &compiler, const SchemaCompilerMode mode, - const std::optional &default_dialect) - -> SchemaCompilerTemplate { + const Compiler &compiler, const Mode mode, + const std::optional &default_dialect) -> Template { assert(is_schema(schema)); // Make sure the input schema is bundled, otherwise we won't be able to @@ -110,7 +107,7 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, } } - SchemaCompilerSchemaContext schema_context{ + SchemaContext schema_context{ sourcemeta::jsontoolkit::empty_pointer, result, vocabularies(schema, resolver, root_frame_entry.dialect), @@ -172,19 +169,19 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, assert(resources.size() == std::set(resources.cbegin(), resources.cend()).size()); - const SchemaCompilerContext context{result, - frame, - references, - std::move(resources), - walker, - resolver, - compiler, - mode, - uses_dynamic_scopes, - uses_unevaluated_properties, - uses_unevaluated_items}; - const SchemaCompilerDynamicContext dynamic_context{relative_dynamic_context}; - SchemaCompilerTemplate compiler_template; + const Context context{result, + frame, + references, + std::move(resources), + walker, + resolver, + compiler, + mode, + uses_dynamic_scopes, + uses_unevaluated_properties, + uses_unevaluated_items}; + const DynamicContext dynamic_context{relative_dynamic_context}; + Template compiler_template; if (uses_dynamic_scopes && (schema_context.vocabularies.contains( @@ -212,17 +209,16 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, auto subschema{get(result, entry.second.pointer)}; auto nested_vocabularies{ vocabularies(subschema, resolver, entry.second.dialect)}; - const SchemaCompilerSchemaContext nested_schema_context{ - entry.second.relative_pointer, - std::move(subschema), - std::move(nested_vocabularies), - entry.second.base, - {}, - {}}; - - compiler_template.push_back(make( + const SchemaContext nested_schema_context{entry.second.relative_pointer, + std::move(subschema), + std::move(nested_vocabularies), + entry.second.base, + {}, + {}}; + + compiler_template.push_back(make( true, context, nested_schema_context, dynamic_context, - SchemaCompilerValueUnsignedInteger{label}, + ValueUnsignedInteger{label}, compile(context, nested_schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer, entry.first.second))); @@ -241,12 +237,11 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, } } -auto compile(const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, +auto compile(const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const sourcemeta::jsontoolkit::Pointer &schema_suffix, const sourcemeta::jsontoolkit::Pointer &instance_suffix, - const std::optional &uri) -> SchemaCompilerTemplate { + const std::optional &uri) -> Template { // Determine URI of the destination after recursion const std::string destination{ uri.has_value() @@ -295,44 +290,44 @@ auto compile(const SchemaCompilerContext &context, entry.dialect); } -SchemaCompilerErrorTraceOutput::SchemaCompilerErrorTraceOutput( +ErrorTraceOutput::ErrorTraceOutput( const sourcemeta::jsontoolkit::JSON &instance, const sourcemeta::jsontoolkit::WeakPointer &base) : instance_{instance}, base_{base} {} -auto SchemaCompilerErrorTraceOutput::begin() const -> const_iterator { +auto ErrorTraceOutput::begin() const -> const_iterator { return this->output.begin(); } -auto SchemaCompilerErrorTraceOutput::end() const -> const_iterator { +auto ErrorTraceOutput::end() const -> const_iterator { return this->output.end(); } -auto SchemaCompilerErrorTraceOutput::cbegin() const -> const_iterator { +auto ErrorTraceOutput::cbegin() const -> const_iterator { return this->output.cbegin(); } -auto SchemaCompilerErrorTraceOutput::cend() const -> const_iterator { +auto ErrorTraceOutput::cend() const -> const_iterator { return this->output.cend(); } -auto SchemaCompilerErrorTraceOutput::operator()( - const SchemaCompilerEvaluationType type, const bool result, - const SchemaCompilerTemplate::value_type &step, +auto ErrorTraceOutput::operator()( + const EvaluationType type, const bool result, + const Template::value_type &step, const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, const sourcemeta::jsontoolkit::WeakPointer &instance_location, const sourcemeta::jsontoolkit::JSON &annotation) -> void { assert(!evaluate_path.empty()); assert(evaluate_path.back().is_property()); - if (type == SchemaCompilerEvaluationType::Pre) { + if (type == EvaluationType::Pre) { assert(result); const auto &keyword{evaluate_path.back().to_property()}; // To ease the output if (keyword == "oneOf" || keyword == "not") { this->mask.insert(evaluate_path); } - } else if (type == SchemaCompilerEvaluationType::Post && + } else if (type == EvaluationType::Post && this->mask.contains(evaluate_path)) { this->mask.erase(evaluate_path); } diff --git a/src/compiler/compile_describe.cc b/src/compiler/compile_describe.cc index 62b41a66..62060632 100644 --- a/src/compiler/compile_describe.cc +++ b/src/compiler/compile_describe.cc @@ -134,7 +134,7 @@ struct DescribeVisitor { const sourcemeta::jsontoolkit::JSON ⌖ const sourcemeta::jsontoolkit::JSON &annotation; - auto operator()(const SchemaCompilerAssertionFail &) const -> std::string { + auto operator()(const AssertionFail &) const -> std::string { if (this->keyword == "contains") { return "The constraints declared for this keyword were not satisfiable"; } @@ -153,7 +153,7 @@ struct DescribeVisitor { return "No instance is expected to succeed against the false schema"; } - auto operator()(const SchemaCompilerLogicalOr &step) const -> std::string { + auto operator()(const LogicalOr &step) const -> std::string { assert(!step.children.empty()); std::ostringstream message; message << "The " << to_string(this->target.type()) @@ -168,7 +168,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLogicalAnd &step) const -> std::string { + auto operator()(const LogicalAnd &step) const -> std::string { if (this->keyword == "allOf") { assert(!step.children.empty()); std::ostringstream message; @@ -207,7 +207,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerLogicalXor &step) const -> std::string { + auto operator()(const LogicalXor &step) const -> std::string { assert(!step.children.empty()); std::ostringstream message; message << "The " << to_string(this->target.type()) @@ -222,11 +222,11 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLogicalCondition &) const -> std::string { + auto operator()(const LogicalCondition &) const -> std::string { return unknown(); } - auto operator()(const SchemaCompilerLogicalNot &) const -> std::string { + auto operator()(const LogicalNot &) const -> std::string { std::ostringstream message; message << "The " << to_string(this->target.type()) @@ -238,7 +238,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAnnotationNot &) const -> std::string { + auto operator()(const AnnotationNot &) const -> std::string { std::ostringstream message; message << "The " << to_string(this->target.type()) @@ -250,20 +250,19 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerControlLabel &) const -> std::string { + auto operator()(const ControlLabel &) const -> std::string { return describe_reference(this->target); } - auto operator()(const SchemaCompilerControlMark &) const -> std::string { + auto operator()(const ControlMark &) const -> std::string { return describe_reference(this->target); } - auto operator()(const SchemaCompilerControlJump &) const -> std::string { + auto operator()(const ControlJump &) const -> std::string { return describe_reference(this->target); } - auto operator()(const SchemaCompilerControlDynamicAnchorJump &step) const - -> std::string { + auto operator()(const ControlDynamicAnchorJump &step) const -> std::string { if (this->keyword == "$dynamicRef") { const auto &value{step_value(step)}; std::ostringstream message; @@ -282,7 +281,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAnnotationEmit &) const -> std::string { + auto operator()(const AnnotationEmit &) const -> std::string { if (this->keyword == "properties") { assert(this->annotation.is_string()); std::ostringstream message; @@ -491,8 +490,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAnnotationWhenArraySizeEqual &) const - -> std::string { + auto operator()(const AnnotationWhenArraySizeEqual &) const -> std::string { if (this->keyword == "items" && this->annotation.is_boolean() && this->annotation.to_boolean()) { assert(this->target.is_array()); @@ -514,8 +512,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAnnotationWhenArraySizeGreater &) const - -> std::string { + auto operator()(const AnnotationWhenArraySizeGreater &) const -> std::string { if ((this->keyword == "prefixItems" || this->keyword == "items") && this->annotation.is_integer()) { assert(this->target.is_array()); @@ -538,8 +535,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAnnotationToParent &) const - -> std::string { + auto operator()(const AnnotationToParent &) const -> std::string { if (this->keyword == "unevaluatedItems" && this->annotation.is_boolean() && this->annotation.to_boolean()) { assert(this->target.is_array()); @@ -552,8 +548,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAnnotationBasenameToParent &) const - -> std::string { + auto operator()(const AnnotationBasenameToParent &) const -> std::string { if (this->keyword == "patternProperties") { assert(this->annotation.is_string()); std::ostringstream message; @@ -597,13 +592,11 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerLoopProperties &step) const - -> std::string { + auto operator()(const LoopProperties &step) const -> std::string { assert(this->keyword == "additionalProperties"); std::ostringstream message; if (step.children.size() == 1 && - std::holds_alternative( - step.children.front())) { + std::holds_alternative(step.children.front())) { message << "The object value was not expected to define additional " "properties"; } else { @@ -614,13 +607,12 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAnnotationLoopPropertiesUnevaluated &step) - const -> std::string { + auto operator()(const AnnotationLoopPropertiesUnevaluated &step) const + -> std::string { if (this->keyword == "unevaluatedProperties") { std::ostringstream message; if (!step.children.empty() && - std::holds_alternative( - step.children.front())) { + std::holds_alternative(step.children.front())) { message << "The object value was not expected to define unevaluated " "properties"; } else { @@ -634,13 +626,11 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerLoopPropertiesExcept &step) const - -> std::string { + auto operator()(const LoopPropertiesExcept &step) const -> std::string { assert(this->keyword == "additionalProperties"); std::ostringstream message; if (step.children.size() == 1 && - std::holds_alternative( - step.children.front())) { + std::holds_alternative(step.children.front())) { message << "The object value was not expected to define additional " "properties"; } else { @@ -651,23 +641,21 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLoopPropertiesType &step) const - -> std::string { + auto operator()(const LoopPropertiesType &step) const -> std::string { std::ostringstream message; message << "The object properties were expected to be of type " << to_string(step.value); return message.str(); } - auto operator()(const SchemaCompilerLoopPropertiesTypeStrict &step) const - -> std::string { + auto operator()(const LoopPropertiesTypeStrict &step) const -> std::string { std::ostringstream message; message << "The object properties were expected to be of type " << to_string(step.value); return message.str(); } - auto operator()(const SchemaCompilerLoopKeys &) const -> std::string { + auto operator()(const LoopKeys &) const -> std::string { assert(this->keyword == "propertyNames"); assert(this->target.is_object()); std::ostringstream message; @@ -697,7 +685,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLoopItems &step) const -> std::string { + auto operator()(const LoopItems &step) const -> std::string { assert(this->target.is_array()); const auto &value{step_value(step)}; std::ostringstream message; @@ -712,13 +700,11 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAnnotationLoopItemsUnmarked &) const - -> std::string { + auto operator()(const AnnotationLoopItemsUnmarked &) const -> std::string { return unknown(); } - auto - operator()(const SchemaCompilerAnnotationLoopItemsUnevaluated &step) const + auto operator()(const AnnotationLoopItemsUnevaluated &step) const -> std::string { assert(this->keyword == "unevaluatedItems"); const auto &value{step_value(step)}; @@ -729,7 +715,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLoopContains &step) const -> std::string { + auto operator()(const LoopContains &step) const -> std::string { assert(this->target.is_array()); std::ostringstream message; const auto &value{step_value(step)}; @@ -773,16 +759,14 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionDefines &step) const - -> std::string { + auto operator()(const AssertionDefines &step) const -> std::string { std::ostringstream message; message << "The object value was expected to define the property " << escape_string(step_value(step)); return message.str(); } - auto operator()(const SchemaCompilerAssertionDefinesAll &step) const - -> std::string { + auto operator()(const AssertionDefinesAll &step) const -> std::string { const auto &value{step_value(step)}; assert(value.size() > 1); std::ostringstream message; @@ -826,16 +810,14 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionType &step) const - -> std::string { + auto operator()(const AssertionType &step) const -> std::string { std::ostringstream message; describe_type_check(this->valid, this->target.type(), step_value(step), message); return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeStrict &step) const - -> std::string { + auto operator()(const AssertionTypeStrict &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && @@ -855,24 +837,21 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeAny &step) const - -> std::string { + auto operator()(const AssertionTypeAny &step) const -> std::string { std::ostringstream message; describe_types_check(this->valid, this->target.type(), step_value(step), message); return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeStrictAny &step) const - -> std::string { + auto operator()(const AssertionTypeStrictAny &step) const -> std::string { std::ostringstream message; describe_types_check(this->valid, this->target.type(), step_value(step), message); return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeStringBounded &step) const - -> std::string { + auto operator()(const AssertionTypeStringBounded &step) const -> std::string { std::ostringstream message; const auto minimum{std::get<0>(step.value)}; @@ -893,8 +872,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeArrayBounded &step) const - -> std::string { + auto operator()(const AssertionTypeArrayBounded &step) const -> std::string { std::ostringstream message; const auto minimum{std::get<0>(step.value)}; @@ -914,8 +892,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionTypeObjectBounded &step) const - -> std::string { + auto operator()(const AssertionTypeObjectBounded &step) const -> std::string { std::ostringstream message; const auto minimum{std::get<0>(step.value)}; @@ -936,8 +913,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionRegex &step) const - -> std::string { + auto operator()(const AssertionRegex &step) const -> std::string { assert(this->target.is_string()); std::ostringstream message; message << "The string value " << escape_string(this->target.to_string()) @@ -946,8 +922,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionStringSizeLess &step) const - -> std::string { + auto operator()(const AssertionStringSizeLess &step) const -> std::string { if (this->keyword == "maxLength") { std::ostringstream message; const auto maximum{step_value(step) - 1}; @@ -988,8 +963,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionStringSizeGreater &step) const - -> std::string { + auto operator()(const AssertionStringSizeGreater &step) const -> std::string { if (this->keyword == "minLength") { std::ostringstream message; const auto minimum{step_value(step) + 1}; @@ -1030,8 +1004,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionArraySizeLess &step) const - -> std::string { + auto operator()(const AssertionArraySizeLess &step) const -> std::string { if (this->keyword == "maxItems") { assert(this->target.is_array()); std::ostringstream message; @@ -1063,8 +1036,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionArraySizeGreater &step) const - -> std::string { + auto operator()(const AssertionArraySizeGreater &step) const -> std::string { if (this->keyword == "minItems") { assert(this->target.is_array()); std::ostringstream message; @@ -1096,8 +1068,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionObjectSizeLess &step) const - -> std::string { + auto operator()(const AssertionObjectSizeLess &step) const -> std::string { if (this->keyword == "maxProperties") { assert(this->target.is_object()); std::ostringstream message; @@ -1145,8 +1116,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionObjectSizeGreater &step) const - -> std::string { + auto operator()(const AssertionObjectSizeGreater &step) const -> std::string { if (this->keyword == "minProperties") { assert(this->target.is_object()); std::ostringstream message; @@ -1194,8 +1164,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionEqual &step) const - -> std::string { + auto operator()(const AssertionEqual &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1206,7 +1175,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionGreaterEqual &step) const { + auto operator()(const AssertionGreaterEqual &step) const { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1217,8 +1186,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionLessEqual &step) const - -> std::string { + auto operator()(const AssertionLessEqual &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1229,8 +1197,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionGreater &step) const - -> std::string { + auto operator()(const AssertionGreater &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1245,8 +1212,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionLess &step) const - -> std::string { + auto operator()(const AssertionLess &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1261,7 +1227,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionUnique &) const -> std::string { + auto operator()(const AssertionUnique &) const -> std::string { assert(this->target.is_array()); auto array{this->target.as_array()}; std::ostringstream message; @@ -1302,8 +1268,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionDivisible &step) const - -> std::string { + auto operator()(const AssertionDivisible &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1314,8 +1279,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionEqualsAny &step) const - -> std::string { + auto operator()(const AssertionEqualsAny &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; message << "The " << to_string(this->target.type()) << " value "; @@ -1348,14 +1312,13 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionStringType &step) const - -> std::string { + auto operator()(const AssertionStringType &step) const -> std::string { assert(this->target.is_string()); std::ostringstream message; message << "The string value " << escape_string(this->target.to_string()) << " was expected to represent a valid"; switch (step_value(step)) { - case SchemaCompilerValueStringType::URI: + case ValueStringType::URI: message << " URI"; break; default: @@ -1365,8 +1328,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionPropertyType &step) const - -> std::string { + auto operator()(const AssertionPropertyType &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && @@ -1386,7 +1348,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerAssertionPropertyTypeStrict &step) const + auto operator()(const AssertionPropertyTypeStrict &step) const -> std::string { std::ostringstream message; const auto &value{step_value(step)}; @@ -1407,8 +1369,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLoopPropertiesMatch &step) const - -> std::string { + auto operator()(const LoopPropertiesMatch &step) const -> std::string { assert(!step.children.empty()); assert(this->target.is_object()); std::ostringstream message; @@ -1422,8 +1383,7 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const SchemaCompilerLogicalWhenType &step) const - -> std::string { + auto operator()(const LogicalWhenType &step) const -> std::string { if (this->keyword == "patternProperties") { assert(!step.children.empty()); assert(this->target.is_object()); @@ -1466,9 +1426,8 @@ struct DescribeVisitor { for (const auto &child : step.children) { // Schema - if (std::holds_alternative(child)) { - const auto &substep{ - std::get(child)}; + if (std::holds_alternative(child)) { + const auto &substep{std::get(child)}; const auto &property{step_value(substep)}; all_dependencies.insert(property); if (!this->target.defines(property)) { @@ -1480,10 +1439,8 @@ struct DescribeVisitor { // Properties } else { - assert(std::holds_alternative< - SchemaCompilerAssertionPropertyDependencies>(child)); - const auto &substep{ - std::get(child)}; + assert(std::holds_alternative(child)); + const auto &substep{std::get(child)}; for (const auto &[property, dependencies] : substep.value) { all_dependencies.insert(property); @@ -1591,8 +1548,8 @@ struct DescribeVisitor { std::set present; std::set all_dependencies; for (const auto &child : step.children) { - assert(std::holds_alternative(child)); - const auto &substep{std::get(child)}; + assert(std::holds_alternative(child)); + const auto &substep{std::get(child)}; const auto &property{step_value(substep)}; all_dependencies.insert(property); if (!this->target.defines(property)) { @@ -1650,7 +1607,7 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const SchemaCompilerAssertionPropertyDependencies &step) const + auto operator()(const AssertionPropertyDependencies &step) const -> std::string { if (this->keyword == "dependentRequired") { assert(this->target.is_object()); @@ -1731,20 +1688,16 @@ struct DescribeVisitor { // These steps are never described, at least not right now - auto operator()(const SchemaCompilerLogicalWhenArraySizeGreater &) const - -> std::string { + auto operator()(const LogicalWhenArraySizeGreater &) const -> std::string { return unknown(); } - auto operator()(const SchemaCompilerLogicalWhenArraySizeEqual &) const - -> std::string { + auto operator()(const LogicalWhenArraySizeEqual &) const -> std::string { return unknown(); } - auto operator()(const SchemaCompilerLogicalWhenDefines &) const - -> std::string { + auto operator()(const LogicalWhenDefines &) const -> std::string { return unknown(); } - auto operator()(const SchemaCompilerLoopPropertiesRegex &) const - -> std::string { + auto operator()(const LoopPropertiesRegex &) const -> std::string { return unknown(); } }; @@ -1755,7 +1708,7 @@ namespace sourcemeta::blaze { // TODO: What will unlock even better error messages is being able to // get the subschema being evaluated along with the keyword -auto describe(const bool valid, const SchemaCompilerTemplate::value_type &step, +auto describe(const bool valid, const Template::value_type &step, const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, const sourcemeta::jsontoolkit::WeakPointer &instance_location, const sourcemeta::jsontoolkit::JSON &instance, diff --git a/src/compiler/compile_helpers.h b/src/compiler/compile_helpers.h index 875672da..8f0ad4f5 100644 --- a/src/compiler/compile_helpers.h +++ b/src/compiler/compile_helpers.h @@ -11,11 +11,11 @@ namespace sourcemeta::blaze { -static const SchemaCompilerDynamicContext relative_dynamic_context{ +static const DynamicContext relative_dynamic_context{ "", sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer}; -inline auto schema_resource_id(const SchemaCompilerContext &context, +inline auto schema_resource_id(const Context &context, const std::string &resource) -> std::size_t { const auto iterator{std::find( context.resources.cbegin(), context.resources.cend(), @@ -31,9 +31,9 @@ inline auto schema_resource_id(const SchemaCompilerContext &context, // Instantiate a value-oriented step template -auto make(const bool report, const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, +auto make(const bool report, const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context, // Take the value type from the "type" property of the step struct const decltype(std::declval().value) &value) -> Step { return { @@ -51,12 +51,12 @@ auto make(const bool report, const SchemaCompilerContext &context, // Instantiate an applicator step template -auto make(const bool report, const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, +auto make(const bool report, const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context, // Take the value type from the "value" property of the step struct - decltype(std::declval().value) &&value, - SchemaCompilerTemplate &&children) -> Step { + decltype(std::declval().value) &&value, Template &&children) + -> Step { return { dynamic_context.keyword.empty() ? dynamic_context.base_schema_location @@ -72,8 +72,7 @@ auto make(const bool report, const SchemaCompilerContext &context, } template -auto unroll(const SchemaCompilerDynamicContext &dynamic_context, - const Step &step, +auto unroll(const DynamicContext &dynamic_context, const Step &step, const sourcemeta::jsontoolkit::Pointer &base_instance_location = sourcemeta::jsontoolkit::empty_pointer) -> Type { assert(std::holds_alternative(step)); diff --git a/src/compiler/compile_json.cc b/src/compiler/compile_json.cc index 6cf29488..0bb5face 100644 --- a/src/compiler/compile_json.cc +++ b/src/compiler/compile_json.cc @@ -16,25 +16,25 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { sourcemeta::jsontoolkit::JSON result{ sourcemeta::jsontoolkit::JSON::make_object()}; result.assign("category", sourcemeta::jsontoolkit::JSON{"value"}); - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"json"}); result.assign("value", sourcemeta::jsontoolkit::JSON{value}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"boolean"}); result.assign("value", sourcemeta::jsontoolkit::JSON{value}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"regex"}); result.assign("value", sourcemeta::jsontoolkit::JSON{value.second}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"type"}); std::ostringstream type_string; type_string << value; result.assign("value", sourcemeta::jsontoolkit::JSON{type_string.str()}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"types"}); sourcemeta::jsontoolkit::JSON types{ sourcemeta::jsontoolkit::JSON::make_array()}; @@ -46,11 +46,11 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(types)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"string"}); result.assign("value", sourcemeta::jsontoolkit::JSON{value}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"strings"}); sourcemeta::jsontoolkit::JSON items{ sourcemeta::jsontoolkit::JSON::make_array()}; @@ -60,7 +60,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(items)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"array"}); sourcemeta::jsontoolkit::JSON items{ sourcemeta::jsontoolkit::JSON::make_array()}; @@ -70,11 +70,11 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(items)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"unsigned-integer"}); result.assign("value", sourcemeta::jsontoolkit::JSON{value}); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"range"}); sourcemeta::jsontoolkit::JSON values{ sourcemeta::jsontoolkit::JSON::make_array()}; @@ -87,7 +87,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { values.push_back(sourcemeta::jsontoolkit::JSON{std::get<2>(range)}); result.assign("value", std::move(values)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"named-indexes"}); sourcemeta::jsontoolkit::JSON values{ sourcemeta::jsontoolkit::JSON::make_object()}; @@ -97,7 +97,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(values)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"string-map"}); sourcemeta::jsontoolkit::JSON map{ sourcemeta::jsontoolkit::JSON::make_object()}; @@ -113,8 +113,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(map)); return result; - } else if constexpr (std::is_same_v< - SchemaCompilerValueItemsAnnotationKeywords, T>) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"items-annotation-keywords"}); sourcemeta::jsontoolkit::JSON data{ @@ -137,7 +136,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(data)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"indexed-json"}); sourcemeta::jsontoolkit::JSON data{ sourcemeta::jsontoolkit::JSON::make_object()}; @@ -145,7 +144,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { data.assign("value", value.second); result.assign("value", std::move(data)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"property-filter"}); sourcemeta::jsontoolkit::JSON data{ sourcemeta::jsontoolkit::JSON::make_object()}; @@ -163,10 +162,10 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(data)); return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"string-type"}); switch (value) { - case SchemaCompilerValueStringType::URI: + case ValueStringType::URI: result.assign("value", sourcemeta::jsontoolkit::JSON{"uri"}); break; default: @@ -175,7 +174,7 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { } return result; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { result.assign("type", sourcemeta::jsontoolkit::JSON{"index-pair"}); sourcemeta::jsontoolkit::JSON data{ sourcemeta::jsontoolkit::JSON::make_array()}; @@ -184,14 +183,13 @@ auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { result.assign("value", std::move(data)); return result; } else { - static_assert(std::is_same_v); + static_assert(std::is_same_v); return sourcemeta::jsontoolkit::JSON{nullptr}; } } template -auto step_to_json( - const sourcemeta::blaze::SchemaCompilerTemplate::value_type &step) +auto step_to_json(const sourcemeta::blaze::Template::value_type &step) -> sourcemeta::jsontoolkit::JSON { static V visitor; return std::visit(visitor, step); @@ -235,88 +233,71 @@ struct StepVisitor { return encode_step(category, type, step); \ } - HANDLE_STEP("assertion", "fail", SchemaCompilerAssertionFail) - HANDLE_STEP("assertion", "defines", SchemaCompilerAssertionDefines) - HANDLE_STEP("assertion", "defines-all", SchemaCompilerAssertionDefinesAll) + HANDLE_STEP("assertion", "fail", AssertionFail) + HANDLE_STEP("assertion", "defines", AssertionDefines) + HANDLE_STEP("assertion", "defines-all", AssertionDefinesAll) HANDLE_STEP("assertion", "property-dependencies", - SchemaCompilerAssertionPropertyDependencies) - HANDLE_STEP("assertion", "type", SchemaCompilerAssertionType) - HANDLE_STEP("assertion", "type-any", SchemaCompilerAssertionTypeAny) - HANDLE_STEP("assertion", "type-strict", SchemaCompilerAssertionTypeStrict) - HANDLE_STEP("assertion", "type-strict-any", - SchemaCompilerAssertionTypeStrictAny) - HANDLE_STEP("assertion", "type-string-bounded", - SchemaCompilerAssertionTypeStringBounded) - HANDLE_STEP("assertion", "type-array-bounded", - SchemaCompilerAssertionTypeArrayBounded) - HANDLE_STEP("assertion", "type-object-bounded", - SchemaCompilerAssertionTypeObjectBounded) - HANDLE_STEP("assertion", "regex", SchemaCompilerAssertionRegex) - HANDLE_STEP("assertion", "string-size-less", - SchemaCompilerAssertionStringSizeLess) - HANDLE_STEP("assertion", "string-size-greater", - SchemaCompilerAssertionStringSizeGreater) - HANDLE_STEP("assertion", "array-size-less", - SchemaCompilerAssertionArraySizeLess) - HANDLE_STEP("assertion", "array-size-greater", - SchemaCompilerAssertionArraySizeGreater) - HANDLE_STEP("assertion", "object-size-less", - SchemaCompilerAssertionObjectSizeLess) - HANDLE_STEP("assertion", "object-size-greater", - SchemaCompilerAssertionObjectSizeGreater) - HANDLE_STEP("assertion", "equal", SchemaCompilerAssertionEqual) - HANDLE_STEP("assertion", "greater-equal", SchemaCompilerAssertionGreaterEqual) - HANDLE_STEP("assertion", "less-equal", SchemaCompilerAssertionLessEqual) - HANDLE_STEP("assertion", "greater", SchemaCompilerAssertionGreater) - HANDLE_STEP("assertion", "less", SchemaCompilerAssertionLess) - HANDLE_STEP("assertion", "unique", SchemaCompilerAssertionUnique) - HANDLE_STEP("assertion", "divisible", SchemaCompilerAssertionDivisible) - HANDLE_STEP("assertion", "string-type", SchemaCompilerAssertionStringType) - HANDLE_STEP("assertion", "property-type", SchemaCompilerAssertionPropertyType) - HANDLE_STEP("assertion", "property-type-strict", - SchemaCompilerAssertionPropertyTypeStrict) - HANDLE_STEP("assertion", "equals-any", SchemaCompilerAssertionEqualsAny) - HANDLE_STEP("annotation", "emit", SchemaCompilerAnnotationEmit) + AssertionPropertyDependencies) + HANDLE_STEP("assertion", "type", AssertionType) + HANDLE_STEP("assertion", "type-any", AssertionTypeAny) + HANDLE_STEP("assertion", "type-strict", AssertionTypeStrict) + HANDLE_STEP("assertion", "type-strict-any", AssertionTypeStrictAny) + HANDLE_STEP("assertion", "type-string-bounded", AssertionTypeStringBounded) + HANDLE_STEP("assertion", "type-array-bounded", AssertionTypeArrayBounded) + HANDLE_STEP("assertion", "type-object-bounded", AssertionTypeObjectBounded) + HANDLE_STEP("assertion", "regex", AssertionRegex) + HANDLE_STEP("assertion", "string-size-less", AssertionStringSizeLess) + HANDLE_STEP("assertion", "string-size-greater", AssertionStringSizeGreater) + HANDLE_STEP("assertion", "array-size-less", AssertionArraySizeLess) + HANDLE_STEP("assertion", "array-size-greater", AssertionArraySizeGreater) + HANDLE_STEP("assertion", "object-size-less", AssertionObjectSizeLess) + HANDLE_STEP("assertion", "object-size-greater", AssertionObjectSizeGreater) + HANDLE_STEP("assertion", "equal", AssertionEqual) + HANDLE_STEP("assertion", "greater-equal", AssertionGreaterEqual) + HANDLE_STEP("assertion", "less-equal", AssertionLessEqual) + HANDLE_STEP("assertion", "greater", AssertionGreater) + HANDLE_STEP("assertion", "less", AssertionLess) + HANDLE_STEP("assertion", "unique", AssertionUnique) + HANDLE_STEP("assertion", "divisible", AssertionDivisible) + HANDLE_STEP("assertion", "string-type", AssertionStringType) + HANDLE_STEP("assertion", "property-type", AssertionPropertyType) + HANDLE_STEP("assertion", "property-type-strict", AssertionPropertyTypeStrict) + HANDLE_STEP("assertion", "equals-any", AssertionEqualsAny) + HANDLE_STEP("annotation", "emit", AnnotationEmit) HANDLE_STEP("annotation", "when-array-size-equal", - SchemaCompilerAnnotationWhenArraySizeEqual) + AnnotationWhenArraySizeEqual) HANDLE_STEP("annotation", "when-array-size-greater", - SchemaCompilerAnnotationWhenArraySizeGreater) - HANDLE_STEP("annotation", "to-parent", SchemaCompilerAnnotationToParent) - HANDLE_STEP("annotation", "basename-to-parent", - SchemaCompilerAnnotationBasenameToParent) + AnnotationWhenArraySizeGreater) + HANDLE_STEP("annotation", "to-parent", AnnotationToParent) + HANDLE_STEP("annotation", "basename-to-parent", AnnotationBasenameToParent) HANDLE_STEP("annotation", "loop-properties-unevaluated", - SchemaCompilerAnnotationLoopPropertiesUnevaluated) - HANDLE_STEP("annotation", "loop-items-unmarked", - SchemaCompilerAnnotationLoopItemsUnmarked) + AnnotationLoopPropertiesUnevaluated) + HANDLE_STEP("annotation", "loop-items-unmarked", AnnotationLoopItemsUnmarked) HANDLE_STEP("annotation", "loop-items-unevaluated", - SchemaCompilerAnnotationLoopItemsUnevaluated) - HANDLE_STEP("annotation", "not", SchemaCompilerAnnotationNot) - HANDLE_STEP("logical", "not", SchemaCompilerLogicalNot) - HANDLE_STEP("logical", "or", SchemaCompilerLogicalOr) - HANDLE_STEP("logical", "and", SchemaCompilerLogicalAnd) - HANDLE_STEP("logical", "xor", SchemaCompilerLogicalXor) - HANDLE_STEP("logical", "condition", SchemaCompilerLogicalCondition) - HANDLE_STEP("logical", "when-type", SchemaCompilerLogicalWhenType) - HANDLE_STEP("logical", "when-defines", SchemaCompilerLogicalWhenDefines) - HANDLE_STEP("logical", "when-array-size-greater", - SchemaCompilerLogicalWhenArraySizeGreater) - HANDLE_STEP("logical", "when-array-size-equal", - SchemaCompilerLogicalWhenArraySizeEqual) - HANDLE_STEP("loop", "properties-match", SchemaCompilerLoopPropertiesMatch) - HANDLE_STEP("loop", "properties", SchemaCompilerLoopProperties) - HANDLE_STEP("loop", "properties-regex", SchemaCompilerLoopPropertiesRegex) - HANDLE_STEP("loop", "properties-except", SchemaCompilerLoopPropertiesExcept) - HANDLE_STEP("loop", "properties-type", SchemaCompilerLoopPropertiesType) - HANDLE_STEP("loop", "properties-type-strict", - SchemaCompilerLoopPropertiesTypeStrict) - HANDLE_STEP("loop", "keys", SchemaCompilerLoopKeys) - HANDLE_STEP("loop", "items", SchemaCompilerLoopItems) - HANDLE_STEP("loop", "contains", SchemaCompilerLoopContains) - HANDLE_STEP("control", "label", SchemaCompilerControlLabel) - HANDLE_STEP("control", "mark", SchemaCompilerControlMark) - HANDLE_STEP("control", "jump", SchemaCompilerControlJump) - HANDLE_STEP("control", "dynamic-anchor-jump", - SchemaCompilerControlDynamicAnchorJump) + AnnotationLoopItemsUnevaluated) + HANDLE_STEP("annotation", "not", AnnotationNot) + HANDLE_STEP("logical", "not", LogicalNot) + HANDLE_STEP("logical", "or", LogicalOr) + HANDLE_STEP("logical", "and", LogicalAnd) + HANDLE_STEP("logical", "xor", LogicalXor) + HANDLE_STEP("logical", "condition", LogicalCondition) + HANDLE_STEP("logical", "when-type", LogicalWhenType) + HANDLE_STEP("logical", "when-defines", LogicalWhenDefines) + HANDLE_STEP("logical", "when-array-size-greater", LogicalWhenArraySizeGreater) + HANDLE_STEP("logical", "when-array-size-equal", LogicalWhenArraySizeEqual) + HANDLE_STEP("loop", "properties-match", LoopPropertiesMatch) + HANDLE_STEP("loop", "properties", LoopProperties) + HANDLE_STEP("loop", "properties-regex", LoopPropertiesRegex) + HANDLE_STEP("loop", "properties-except", LoopPropertiesExcept) + HANDLE_STEP("loop", "properties-type", LoopPropertiesType) + HANDLE_STEP("loop", "properties-type-strict", LoopPropertiesTypeStrict) + HANDLE_STEP("loop", "keys", LoopKeys) + HANDLE_STEP("loop", "items", LoopItems) + HANDLE_STEP("loop", "contains", LoopContains) + HANDLE_STEP("control", "label", ControlLabel) + HANDLE_STEP("control", "mark", ControlMark) + HANDLE_STEP("control", "jump", ControlJump) + HANDLE_STEP("control", "dynamic-anchor-jump", ControlDynamicAnchorJump) #undef HANDLE_STEP }; @@ -325,8 +306,7 @@ struct StepVisitor { namespace sourcemeta::blaze { -auto to_json(const SchemaCompilerTemplate &steps) - -> sourcemeta::jsontoolkit::JSON { +auto to_json(const Template &steps) -> sourcemeta::jsontoolkit::JSON { sourcemeta::jsontoolkit::JSON result{ sourcemeta::jsontoolkit::JSON::make_array()}; for (const auto &step : steps) { @@ -336,9 +316,9 @@ auto to_json(const SchemaCompilerTemplate &steps) return result; } -auto compiler_template_format_compare( - const sourcemeta::jsontoolkit::JSON::String &left, - const sourcemeta::jsontoolkit::JSON::String &right) -> bool { +auto template_format_compare(const sourcemeta::jsontoolkit::JSON::String &left, + const sourcemeta::jsontoolkit::JSON::String &right) + -> bool { using Rank = std::map< sourcemeta::jsontoolkit::JSON::String, std::uint64_t, std::less, diff --git a/src/compiler/default_compiler.cc b/src/compiler/default_compiler.cc index 383e6c82..1c77ece5 100644 --- a/src/compiler/default_compiler.cc +++ b/src/compiler/default_compiler.cc @@ -11,10 +11,10 @@ #include // std::string auto sourcemeta::blaze::default_schema_compiler( - const sourcemeta::blaze::SchemaCompilerContext &context, - const sourcemeta::blaze::SchemaCompilerSchemaContext &schema_context, - const sourcemeta::blaze::SchemaCompilerDynamicContext &dynamic_context) - -> sourcemeta::blaze::SchemaCompilerTemplate { + const sourcemeta::blaze::Context &context, + const sourcemeta::blaze::SchemaContext &schema_context, + const sourcemeta::blaze::DynamicContext &dynamic_context) + -> sourcemeta::blaze::Template { assert(!dynamic_context.keyword.empty()); static std::set SUPPORTED_VOCABULARIES{ @@ -510,7 +510,7 @@ auto sourcemeta::blaze::default_schema_compiler( return {}; } - if (context.mode == SchemaCompilerMode::FastValidation) { + if (context.mode == Mode::FastValidation) { return {}; } diff --git a/src/compiler/default_compiler_2019_09.h b/src/compiler/default_compiler_2019_09.h index 9ed1c896..9de486e3 100644 --- a/src/compiler/default_compiler_2019_09.h +++ b/src/compiler/default_compiler_2019_09.h @@ -10,10 +10,8 @@ namespace internal { using namespace sourcemeta::blaze; auto compiler_2019_09_applicator_dependentschemas( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_object()); if (schema_context.schema.defines("type") && @@ -22,7 +20,7 @@ auto compiler_2019_09_applicator_dependentschemas( return {}; } - SchemaCompilerTemplate children; + Template children; // To guarantee order std::vector dependents; @@ -40,24 +38,22 @@ auto compiler_2019_09_applicator_dependentschemas( } if (!dependency.is_boolean() || !dependency.to_boolean()) { - children.push_back(make( + children.push_back(make( false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueString{dependent}, + ValueString{dependent}, compile(context, schema_context, relative_dynamic_context, {dependent}, sourcemeta::jsontoolkit::empty_pointer))); } } - return {make( - true, context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Object, std::move(children))}; + return {make(true, context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Object, + std::move(children))}; } auto compiler_2019_09_validation_dependentrequired( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { if (!schema_context.schema.at(dynamic_context.keyword).is_object()) { return {}; } @@ -68,7 +64,7 @@ auto compiler_2019_09_validation_dependentrequired( return {}; } - SchemaCompilerValueStringMap dependencies; + ValueStringMap dependencies; for (const auto &entry : schema_context.schema.at(dynamic_context.keyword).as_object()) { if (!entry.second.is_array()) { @@ -90,26 +86,23 @@ auto compiler_2019_09_validation_dependentrequired( return {}; } - return {make( + return {make( true, context, schema_context, dynamic_context, std::move(dependencies))}; } -auto compiler_2019_09_core_annotation( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { - return {make( +auto compiler_2019_09_core_annotation(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON{ schema_context.schema.at(dynamic_context.keyword)})}; } auto compiler_2019_09_applicator_contains_conditional_annotate( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, const bool annotate) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const bool annotate) -> Template { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "array") { @@ -141,24 +134,21 @@ auto compiler_2019_09_applicator_contains_conditional_annotate( } if (maximum.has_value() && minimum > maximum.value()) { - return {make(true, context, schema_context, - dynamic_context, - SchemaCompilerValueNone{})}; + return {make(true, context, schema_context, dynamic_context, + ValueNone{})}; } if (minimum == 0 && !maximum.has_value()) { return {}; } - SchemaCompilerTemplate children{ - compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Template children{compile(context, schema_context, relative_dynamic_context, + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (annotate) { - children.push_back(make( - true, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{})); + children.push_back(make( + true, context, schema_context, relative_dynamic_context, ValueNone{})); // TODO: If after emitting the above annotation, the number of annotations // for the current schema location + instance location is equal to the @@ -166,113 +156,98 @@ auto compiler_2019_09_applicator_contains_conditional_annotate( // an annotation "true" } - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueRange{ - minimum, maximum, - // TODO: We only need to be exhaustive here if `unevaluatedItems` is - // in use on the schema. Can we pre-determine that and speed things up - // if not? - annotate}, + ValueRange{minimum, maximum, + // TODO: We only need to be exhaustive here if + // `unevaluatedItems` is in use on the schema. Can we + // pre-determine that and speed things up if not? + annotate}, std::move(children))}; } -auto compiler_2019_09_applicator_contains( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2019_09_applicator_contains(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { return compiler_2019_09_applicator_contains_conditional_annotate( context, schema_context, dynamic_context, false); } auto compiler_2019_09_applicator_additionalproperties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_additionalproperties_conditional_annotation( context, schema_context, dynamic_context, - context.uses_unevaluated_properties || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_properties || context.mode == Mode::Exhaustive); } -auto compiler_2019_09_applicator_items( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2019_09_applicator_items(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { return compiler_draft4_applicator_items_conditional_annotation( context, schema_context, dynamic_context, - context.uses_unevaluated_items || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_items || context.mode == Mode::Exhaustive); } auto compiler_2019_09_applicator_additionalitems( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_additionalitems_conditional_annotation( context, schema_context, dynamic_context, - context.uses_unevaluated_items || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_items || context.mode == Mode::Exhaustive); } auto compiler_2019_09_applicator_unevaluateditems( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "array") { return {}; } - SchemaCompilerTemplate children{ - compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; - children.push_back(make( + Template children{compile(context, schema_context, relative_dynamic_context, + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; + children.push_back(make( true, context, schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::JSON{true})); if (schema_context.vocabularies.contains( "https://json-schema.org/draft/2019-09/vocab/applicator")) { - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueItemsAnnotationKeywords{ + ValueItemsAnnotationKeywords{ "items", {}, {"items", "additionalItems", "unevaluatedItems"}}, std::move(children))}; } else if (schema_context.vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator")) { - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueItemsAnnotationKeywords{ + ValueItemsAnnotationKeywords{ "prefixItems", {"contains"}, {"prefixItems", "items", "contains", "unevaluatedItems"}}, std::move(children))}; } else { - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueString{"unevaluatedItems"}, std::move(children))}; + ValueString{"unevaluatedItems"}, std::move(children))}; } } auto compiler_2019_09_applicator_unevaluatedproperties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "object") { return {}; } - SchemaCompilerValueStrings dependencies{"unevaluatedProperties"}; + ValueStrings dependencies{"unevaluatedProperties"}; if (schema_context.vocabularies.contains( "https://json-schema.org/draft/2019-09/vocab/applicator")) { @@ -288,24 +263,21 @@ auto compiler_2019_09_applicator_unevaluatedproperties( dependencies.push_back("additionalProperties"); } - SchemaCompilerTemplate children{ - compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; - children.push_back(make( - true, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{})); + Template children{compile(context, schema_context, relative_dynamic_context, + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; + children.push_back(make( + true, context, schema_context, relative_dynamic_context, ValueNone{})); - return {make( + return {make( true, context, schema_context, dynamic_context, std::move(dependencies), std::move(children))}; } -auto compiler_2019_09_core_recursiveref( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2019_09_core_recursiveref(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { const auto current{ to_uri(schema_context.relative_pointer, schema_context.base).recompose()}; assert(context.frame.contains( @@ -318,30 +290,24 @@ auto compiler_2019_09_core_recursiveref( return compiler_draft4_core_ref(context, schema_context, dynamic_context); } - return {make( - true, context, schema_context, dynamic_context, "")}; + return {make(true, context, schema_context, + dynamic_context, "")}; } auto compiler_2019_09_applicator_properties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_properties_conditional_annotation( context, schema_context, dynamic_context, - context.uses_unevaluated_properties || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_properties || context.mode == Mode::Exhaustive); } auto compiler_2019_09_applicator_patternproperties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_patternproperties_conditional_annotation( context, schema_context, dynamic_context, - context.uses_unevaluated_properties || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_properties || context.mode == Mode::Exhaustive); } } // namespace internal diff --git a/src/compiler/default_compiler_2020_12.h b/src/compiler/default_compiler_2020_12.h index d72226b9..07810943 100644 --- a/src/compiler/default_compiler_2020_12.h +++ b/src/compiler/default_compiler_2020_12.h @@ -11,21 +11,17 @@ namespace internal { using namespace sourcemeta::blaze; auto compiler_2020_12_applicator_prefixitems( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_items_array( context, schema_context, dynamic_context, - context.uses_unevaluated_items || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_items || context.mode == Mode::Exhaustive); } -auto compiler_2020_12_applicator_items( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2020_12_applicator_items(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { const auto cursor{(schema_context.schema.defines("prefixItems") && schema_context.schema.at("prefixItems").is_array()) ? schema_context.schema.at("prefixItems").size() @@ -33,26 +29,22 @@ auto compiler_2020_12_applicator_items( return compiler_draft4_applicator_additionalitems_from_cursor( context, schema_context, dynamic_context, cursor, - context.uses_unevaluated_items || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_items || context.mode == Mode::Exhaustive); } -auto compiler_2020_12_applicator_contains( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2020_12_applicator_contains(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { return compiler_2019_09_applicator_contains_conditional_annotate( context, schema_context, dynamic_context, - context.uses_unevaluated_items || - context.mode == SchemaCompilerMode::Exhaustive); + context.uses_unevaluated_items || context.mode == Mode::Exhaustive); } -auto compiler_2020_12_core_dynamicref( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_2020_12_core_dynamicref(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { const auto current{ to_uri(schema_context.relative_pointer, schema_context.base).recompose()}; assert(context.frame.contains( @@ -76,7 +68,7 @@ auto compiler_2020_12_core_dynamicref( // Note we don't need to even care about the static part of the dynamic // reference (if any), as even if we jump first there, we will still // look for the oldest dynamic anchor in the schema resource chain. - return {make( + return {make( true, context, schema_context, dynamic_context, std::string{reference.fragment().value()})}; } diff --git a/src/compiler/default_compiler_draft4.h b/src/compiler/default_compiler_draft4.h index c9bf05cb..f0e7c62f 100644 --- a/src/compiler/default_compiler_draft4.h +++ b/src/compiler/default_compiler_draft4.h @@ -22,19 +22,18 @@ static auto parse_regex(const std::string &pattern, } catch (const std::regex_error &) { std::ostringstream message; message << "Invalid regular expression: " << pattern; - throw sourcemeta::blaze::SchemaCompilationError(base, schema_location, - message.str()); + throw sourcemeta::blaze::CompilerError(base, schema_location, + message.str()); } } namespace internal { using namespace sourcemeta::blaze; -auto compiler_draft4_core_ref( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_core_ref(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { // Determine the label const auto type{sourcemeta::jsontoolkit::ReferenceType::Static}; const auto current{ @@ -55,9 +54,8 @@ auto compiler_draft4_core_ref( // The label is already registered, so just jump to it if (schema_context.labels.contains(label)) { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueUnsignedInteger{label})}; + return {make(true, context, schema_context, dynamic_context, + ValueUnsignedInteger{label})}; } auto new_schema_context{schema_context}; @@ -85,7 +83,7 @@ auto compiler_draft4_core_ref( schema_context.references.contains(reference.destination)}; if (!is_recursive && direct_children_references <= 5) { - if (context.mode == SchemaCompilerMode::FastValidation && + if (context.mode == Mode::FastValidation && // Expanding references inline when dynamic scoping is required // may not work, as we might omit the instruction that introduces // one of the necessary schema resources to the evaluator @@ -95,9 +93,8 @@ auto compiler_draft4_core_ref( sourcemeta::jsontoolkit::empty_pointer, reference.destination); } else { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueNone{}, + return {make( + true, context, schema_context, dynamic_context, ValueNone{}, compile(context, new_schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer, @@ -112,28 +109,27 @@ auto compiler_draft4_core_ref( // handler, without having to add logic to every single keyword to check // whether something points to them and add the "checkpoint" themselves. new_schema_context.labels.insert(label); - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueUnsignedInteger{label}, + ValueUnsignedInteger{label}, compile(context, new_schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer, reference.destination))}; } -auto compiler_draft4_validation_type( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_validation_type(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { if (schema_context.schema.at(dynamic_context.keyword).is_string()) { const auto &type{ schema_context.schema.at(dynamic_context.keyword).to_string()}; if (type == "null") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Null)}; } else if (type == "boolean") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Boolean)}; } else if (type == "object") { @@ -141,14 +137,14 @@ auto compiler_draft4_validation_type( unsigned_integer_property(schema_context.schema, "minProperties", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxProperties")}; - if (context.mode == SchemaCompilerMode::FastValidation && + if (context.mode == Mode::FastValidation && (minimum > 0 || maximum.has_value())) { - return {make( - true, context, schema_context, dynamic_context, - {minimum, maximum, false})}; + return {make(true, context, schema_context, + dynamic_context, + {minimum, maximum, false})}; } - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Object)}; } else if (type == "array") { @@ -156,24 +152,24 @@ auto compiler_draft4_validation_type( unsigned_integer_property(schema_context.schema, "minItems", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxItems")}; - if (context.mode == SchemaCompilerMode::FastValidation && + if (context.mode == Mode::FastValidation && (minimum > 0 || maximum.has_value())) { - return {make( - true, context, schema_context, dynamic_context, - {minimum, maximum, false})}; + return {make(true, context, schema_context, + dynamic_context, + {minimum, maximum, false})}; } - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Array)}; } else if (type == "number") { - return {make( + return {make( true, context, schema_context, dynamic_context, std::vector{ sourcemeta::jsontoolkit::JSON::Type::Real, sourcemeta::jsontoolkit::JSON::Type::Integer})}; } else if (type == "integer") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Integer)}; } else if (type == "string") { @@ -181,14 +177,14 @@ auto compiler_draft4_validation_type( unsigned_integer_property(schema_context.schema, "minLength", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxLength")}; - if (context.mode == SchemaCompilerMode::FastValidation && + if (context.mode == Mode::FastValidation && (minimum > 0 || maximum.has_value())) { - return {make( - true, context, schema_context, dynamic_context, - {minimum, maximum, false})}; + return {make(true, context, schema_context, + dynamic_context, + {minimum, maximum, false})}; } - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::String)}; } else { @@ -202,33 +198,33 @@ auto compiler_draft4_validation_type( const auto &type{ schema_context.schema.at(dynamic_context.keyword).front().to_string()}; if (type == "null") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Null)}; } else if (type == "boolean") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Boolean)}; } else if (type == "object") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Object)}; } else if (type == "array") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Array)}; } else if (type == "number") { - return {make( + return {make( true, context, schema_context, dynamic_context, std::vector{ sourcemeta::jsontoolkit::JSON::Type::Real, sourcemeta::jsontoolkit::JSON::Type::Integer})}; } else if (type == "integer") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::Integer)}; } else if (type == "string") { - return {make( + return {make( true, context, schema_context, dynamic_context, sourcemeta::jsontoolkit::JSON::Type::String)}; } else { @@ -260,18 +256,17 @@ auto compiler_draft4_validation_type( assert(types.size() >= schema_context.schema.at(dynamic_context.keyword).size()); - return {make( - true, context, schema_context, dynamic_context, std::move(types))}; + return {make(true, context, schema_context, + dynamic_context, std::move(types))}; } return {}; } -auto compiler_draft4_validation_required( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_validation_required(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); if (schema_context.schema.defines("type") && @@ -291,35 +286,33 @@ auto compiler_draft4_validation_required( } if (properties.size() == 1) { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueString{*(properties.cbegin())})}; + return {make(true, context, schema_context, + dynamic_context, + ValueString{*(properties.cbegin())})}; } else { - return {make( - true, context, schema_context, dynamic_context, - std::move(properties))}; + return {make(true, context, schema_context, + dynamic_context, + std::move(properties))}; } } else { assert( schema_context.schema.at(dynamic_context.keyword).front().is_string()); - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueString{ - schema_context.schema.at(dynamic_context.keyword) - .front() - .to_string()})}; + ValueString{schema_context.schema.at(dynamic_context.keyword) + .front() + .to_string()})}; } } -auto compiler_draft4_applicator_allof( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_applicator_allof(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - SchemaCompilerTemplate children; + Template children; for (std::uint64_t index = 0; index < schema_context.schema.at(dynamic_context.keyword).size(); index++) { @@ -331,74 +324,67 @@ auto compiler_draft4_applicator_allof( } } - return {make( - true, context, schema_context, dynamic_context, SchemaCompilerValueNone{}, - std::move(children))}; + return {make(true, context, schema_context, dynamic_context, + ValueNone{}, std::move(children))}; } -auto compiler_draft4_applicator_anyof( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_applicator_anyof(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - SchemaCompilerTemplate disjunctors; + Template disjunctors; for (std::uint64_t index = 0; index < schema_context.schema.at(dynamic_context.keyword).size(); index++) { - disjunctors.push_back(make( - false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{}, + disjunctors.push_back(make( + false, context, schema_context, relative_dynamic_context, ValueNone{}, compile(context, schema_context, relative_dynamic_context, {static_cast( index)}))); } - const auto requires_exhaustive{ - context.mode == SchemaCompilerMode::Exhaustive || - context.uses_unevaluated_properties || context.uses_unevaluated_items}; + const auto requires_exhaustive{context.mode == Mode::Exhaustive || + context.uses_unevaluated_properties || + context.uses_unevaluated_items}; - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueBoolean{requires_exhaustive}, std::move(disjunctors))}; + return {make(true, context, schema_context, dynamic_context, + ValueBoolean{requires_exhaustive}, + std::move(disjunctors))}; } -auto compiler_draft4_applicator_oneof( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_applicator_oneof(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - SchemaCompilerTemplate disjunctors; + Template disjunctors; for (std::uint64_t index = 0; index < schema_context.schema.at(dynamic_context.keyword).size(); index++) { - disjunctors.push_back(make( - false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{}, + disjunctors.push_back(make( + false, context, schema_context, relative_dynamic_context, ValueNone{}, compile(context, schema_context, relative_dynamic_context, {static_cast( index)}))); } - const auto requires_exhaustive{ - context.mode == SchemaCompilerMode::Exhaustive || - context.uses_unevaluated_properties || context.uses_unevaluated_items}; + const auto requires_exhaustive{context.mode == Mode::Exhaustive || + context.uses_unevaluated_properties || + context.uses_unevaluated_items}; - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueBoolean{requires_exhaustive}, std::move(disjunctors))}; + return {make(true, context, schema_context, dynamic_context, + ValueBoolean{requires_exhaustive}, + std::move(disjunctors))}; } auto compiler_draft4_applicator_properties_conditional_annotation( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, const bool annotate) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const bool annotate) -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_object()); if (schema_context.schema.at(dynamic_context.keyword).empty()) { return {}; @@ -438,7 +424,7 @@ auto compiler_draft4_applicator_properties_conditional_annotation( } std::size_t is_required = 0; - std::vector> properties; + std::vector> properties; for (const auto &entry : schema_context.schema.at(dynamic_context.keyword).as_object()) { properties.push_back( @@ -488,35 +474,35 @@ auto compiler_draft4_applicator_properties_conditional_annotation( !is_directly_inside_oneof}; if (prefer_loop_over_instance) { - SchemaCompilerValueNamedIndexes indexes; - SchemaCompilerTemplate children; + ValueNamedIndexes indexes; + Template children; std::size_t cursor = 0; for (auto &&[name, substeps] : properties) { indexes.emplace(name, cursor); if (annotate) { - substeps.push_back(make( + substeps.push_back(make( true, context, schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::JSON{name})); } // Note that the evaluator completely ignores this wrapper anyway - children.push_back(make( - false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{}, std::move(substeps))); + children.push_back(make(false, context, schema_context, + relative_dynamic_context, ValueNone{}, + std::move(substeps))); cursor += 1; } - return {make( - true, context, schema_context, dynamic_context, std::move(indexes), - std::move(children))}; + return {make(true, context, schema_context, + dynamic_context, std::move(indexes), + std::move(children))}; } - SchemaCompilerTemplate children; + Template children; for (auto &&[name, substeps] : properties) { if (annotate) { - substeps.push_back(make( + substeps.push_back(make( true, context, schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::JSON{name})); } @@ -541,81 +527,65 @@ auto compiler_draft4_applicator_properties_conditional_annotation( // Optimize `properties` where its subschemas just include a type check, // as that's a very common pattern - } else if (context.mode == SchemaCompilerMode::FastValidation && - substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - const auto &type_step{ - std::get(substeps.front())}; - children.push_back(SchemaCompilerAssertionPropertyTypeStrict{ + } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && + std::holds_alternative(substeps.front())) { + const auto &type_step{std::get(substeps.front())}; + children.push_back(AssertionPropertyTypeStrict{ type_step.relative_schema_location, dynamic_context.base_instance_location.concat( type_step.relative_instance_location), type_step.keyword_location, type_step.schema_resource, type_step.dynamic, type_step.report, type_step.value}); - } else if (context.mode == SchemaCompilerMode::FastValidation && - substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - const auto &type_step{ - std::get(substeps.front())}; - children.push_back(SchemaCompilerAssertionPropertyType{ + } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && + std::holds_alternative(substeps.front())) { + const auto &type_step{std::get(substeps.front())}; + children.push_back(AssertionPropertyType{ type_step.relative_schema_location, dynamic_context.base_instance_location.concat( type_step.relative_instance_location), type_step.keyword_location, type_step.schema_resource, type_step.dynamic, type_step.report, type_step.value}); - } else if (context.mode == SchemaCompilerMode::FastValidation && - substeps.size() == 1 && - std::holds_alternative< - SchemaCompilerAssertionPropertyTypeStrict>( + } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && + std::holds_alternative( substeps.front())) { - children.push_back(unroll( + children.push_back(unroll( relative_dynamic_context, substeps.front(), dynamic_context.base_instance_location)); - } else if (context.mode == SchemaCompilerMode::FastValidation && - substeps.size() == 1 && - std::holds_alternative( + } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && + std::holds_alternative( substeps.front())) { - children.push_back(unroll( + children.push_back(unroll( relative_dynamic_context, substeps.front(), dynamic_context.base_instance_location)); } else { - children.push_back(make( + children.push_back(make( false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueString{name}, std::move(substeps))); + ValueString{name}, std::move(substeps))); } } // Optimize away the wrapper when emitting a single instruction - if (context.mode == SchemaCompilerMode::FastValidation && - children.size() == 1 && - std::holds_alternative( - children.front())) { - return {unroll( - dynamic_context, children.front())}; + if (context.mode == Mode::FastValidation && children.size() == 1 && + std::holds_alternative(children.front())) { + return { + unroll(dynamic_context, children.front())}; } - return {make( - true, context, schema_context, dynamic_context, SchemaCompilerValueNone{}, - std::move(children))}; + return {make(true, context, schema_context, dynamic_context, + ValueNone{}, std::move(children))}; } auto compiler_draft4_applicator_properties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_properties_conditional_annotation( context, schema_context, dynamic_context, false); } auto compiler_draft4_applicator_patternproperties_conditional_annotation( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, const bool annotate) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const bool annotate) -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_object()); if (schema_context.schema.at(dynamic_context.keyword).empty()) { return {}; @@ -627,7 +597,7 @@ auto compiler_draft4_applicator_patternproperties_conditional_annotation( return {}; } - SchemaCompilerTemplate children; + Template children; // To guarantee ordering std::vector patterns; @@ -647,21 +617,21 @@ auto compiler_draft4_applicator_patternproperties_conditional_annotation( // The evaluator will make sure the same annotation is not reported twice. // For example, if the same property matches more than one subschema in // `patternProperties` - substeps.push_back(make( + substeps.push_back(make( true, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{})); + ValueNone{})); } // If the `patternProperties` subschema for the given pattern does // nothing, then we can avoid generating an entire loop for it if (!substeps.empty()) { // Loop over the instance properties - children.push_back(make( + children.push_back(make( // Treat this as an internal step false, context, schema_context, relative_dynamic_context, - SchemaCompilerValueRegex{parse_regex(pattern, schema_context.base, - schema_context.relative_pointer), - pattern}, + ValueRegex{parse_regex(pattern, schema_context.base, + schema_context.relative_pointer), + pattern}, std::move(substeps))); } } @@ -671,43 +641,37 @@ auto compiler_draft4_applicator_patternproperties_conditional_annotation( } // If the instance is an object... - return {make( - true, context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Object, std::move(children))}; + return {make(true, context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Object, + std::move(children))}; } auto compiler_draft4_applicator_patternproperties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_patternproperties_conditional_annotation( context, schema_context, dynamic_context, false); } auto compiler_draft4_applicator_additionalproperties_conditional_annotation( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, const bool annotate) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const bool annotate) -> Template { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "object") { return {}; } - SchemaCompilerTemplate children{ - compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Template children{compile(context, schema_context, relative_dynamic_context, + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (annotate) { - children.push_back(make( - true, context, schema_context, relative_dynamic_context, - SchemaCompilerValueNone{})); + children.push_back(make( + true, context, schema_context, relative_dynamic_context, ValueNone{})); } - SchemaCompilerValuePropertyFilter filter; + ValuePropertyFilter filter; if (schema_context.schema.defines("properties") && schema_context.schema.at("properties").is_object()) { @@ -736,49 +700,40 @@ auto compiler_draft4_applicator_additionalproperties_conditional_annotation( } if (!filter.first.empty() || !filter.second.empty()) { - return {make( - true, context, schema_context, dynamic_context, std::move(filter), - std::move(children))}; + return {make(true, context, schema_context, + dynamic_context, std::move(filter), + std::move(children))}; } else { - if (context.mode == SchemaCompilerMode::FastValidation && - children.size() == 1) { + if (context.mode == Mode::FastValidation && children.size() == 1) { // Optimize `additionalProperties` set to just `type`, which is a // pretty common pattern - if (std::holds_alternative( - children.front())) { - const auto &type_step{ - std::get(children.front())}; - return {make( - true, context, schema_context, dynamic_context, type_step.value)}; - } else if (std::holds_alternative( - children.front())) { - const auto &type_step{ - std::get(children.front())}; - return {make( + if (std::holds_alternative(children.front())) { + const auto &type_step{std::get(children.front())}; + return {make( true, context, schema_context, dynamic_context, type_step.value)}; + } else if (std::holds_alternative(children.front())) { + const auto &type_step{std::get(children.front())}; + return {make(true, context, schema_context, + dynamic_context, type_step.value)}; } } - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueNone{}, std::move(children))}; + return {make(true, context, schema_context, dynamic_context, + ValueNone{}, std::move(children))}; } } auto compiler_draft4_applicator_additionalproperties( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context) -> Template { return compiler_draft4_applicator_additionalproperties_conditional_annotation( context, schema_context, dynamic_context, false); } -auto compiler_draft4_validation_pattern( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_validation_pattern(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_string()); if (schema_context.schema.defines("type") && @@ -789,18 +744,17 @@ auto compiler_draft4_validation_pattern( const auto ®ex_string{ schema_context.schema.at(dynamic_context.keyword).to_string()}; - return {make( + return {make( true, context, schema_context, dynamic_context, - SchemaCompilerValueRegex{parse_regex(regex_string, schema_context.base, - schema_context.relative_pointer), - regex_string})}; + ValueRegex{parse_regex(regex_string, schema_context.base, + schema_context.relative_pointer), + regex_string})}; } -auto compiler_draft4_validation_format( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_validation_format(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { if (!schema_context.schema.at(dynamic_context.keyword).is_string()) { return {}; } @@ -822,19 +776,17 @@ auto compiler_draft4_validation_format( schema_context.schema.at(dynamic_context.keyword).to_string()}; if (format == "uri") { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueStringType::URI)}; + return {make(true, context, schema_context, + dynamic_context, ValueStringType::URI)}; } #define COMPILE_FORMAT_REGEX(name, regular_expression) \ if (format == (name)) { \ - return {make( \ + return {make( \ true, context, schema_context, dynamic_context, \ - SchemaCompilerValueRegex{parse_regex(regular_expression, \ - schema_context.base, \ - schema_context.relative_pointer), \ - (regular_expression)})}; \ + ValueRegex{parse_regex(regular_expression, schema_context.base, \ + schema_context.relative_pointer), \ + (regular_expression)})}; \ } COMPILE_FORMAT_REGEX("ipv4", FORMAT_REGEX_IPV4) @@ -844,25 +796,22 @@ auto compiler_draft4_validation_format( return {}; } -auto compiler_draft4_applicator_not( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context) - -> SchemaCompilerTemplate { +auto compiler_draft4_applicator_not(const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context) + -> Template { // Only emit a `not` instruction that keeps track of // dropping annotations if we really need it - if (context.mode != SchemaCompilerMode::FastValidation || + if (context.mode != Mode::FastValidation || context.uses_unevaluated_properties || context.uses_unevaluated_items) { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueNone{}, + return {make( + true, context, schema_context, dynamic_context, ValueNone{}, compile(context, schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer))}; } else { - return {make( - true, context, schema_context, dynamic_context, - SchemaCompilerValueNone{}, + return {make( + true, context, schema_context, dynamic_context, ValueNone{}, compile(context, schema_context, relative_dynamic_context, sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer))}; @@ -870,10 +819,8 @@ auto compiler_draft4_applicator_not( } auto compiler_draft4_applicator_items_array( - const SchemaCompilerContext &context, - const SchemaCompilerSchemaContext &schema_context, - const SchemaCompilerDynamicContext &dynamic_context, const bool annotate) - -> SchemaCompilerTemplate { + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const bool annotate) -> Template { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); const auto items_size{ schema_context.schema.at(dynamic_context.keyword).size()}; @@ -896,7 +843,7 @@ auto compiler_draft4_applicator_items_array( // - [ {} ] if the instance array size is == 1 // Precompile subschemas - std::vector subschemas; + std::vector