diff --git a/DEPENDENCIES b/DEPENDENCIES index 1bcd6625..b549ad58 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -1,7 +1,7 @@ vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4 -noa https://github.com/sourcemeta/noa caad2e1ceedf9fd1a18686a6a6d1e2b9757ead75 -jsontoolkit https://github.com/sourcemeta/jsontoolkit 7a398224cc2e76ea9ae8541a872b131c7c025267 -hydra https://github.com/sourcemeta/hydra a4a74f3cabd32f2f829f449d67339dac33f9910e -alterschema https://github.com/sourcemeta/alterschema 92e370ce9c1f0582014b54d43e388ee012dfe13d -jsonbinpack https://github.com/sourcemeta/jsonbinpack d777179441d3c703e1fda1187742541aa26836b5 -blaze https://github.com/sourcemeta/blaze 4990da88241847b561cf43efba2c0c7d2bfc9642 +noa https://github.com/sourcemeta/noa 924f5cc8549af7f12227869dcbab4259029ac650 +jsontoolkit https://github.com/sourcemeta/jsontoolkit 4b37450b287695fd0fa63f50c1fe6a43d2384c51 +hydra https://github.com/sourcemeta/hydra c31b5b612beb3e6a870deb2581ae437fc5183f47 +alterschema https://github.com/sourcemeta/alterschema 0fcd3b3b921bd47dc0867477619383f6519939fa +jsonbinpack https://github.com/sourcemeta/jsonbinpack ea02b415f266c7529be13b9e2b26bc8d74b5e4a8 +blaze https://github.com/sourcemeta/blaze a906d6601bc5d7afd4ddb6cbf9f64326aeee1b0c diff --git a/README.markdown b/README.markdown index b0db1993..40ce2e20 100644 --- a/README.markdown +++ b/README.markdown @@ -27,17 +27,17 @@ Version support We aim to fully support _every_ version of JSON Schema and combinations between them. -| Dialect | Support | -|---------------------|------------------------------------------------------------------| -| JSON Schema 2020-12 | **Full** | -| JSON Schema 2019-09 | **Full** | -| JSON Schema Draft 7 | **Full** | -| JSON Schema Draft 6 | **Full** | -| JSON Schema Draft 4 | **Full** | -| JSON Schema Draft 3 | Partial (except `validate`, `test`, `metaschema`, and `compile`) | -| JSON Schema Draft 2 | Partial (except `validate`, `test`, `metaschema`, and `compile`) | -| JSON Schema Draft 1 | Partial (except `validate`, `test`, `metaschema`, and `compile`) | -| JSON Schema Draft 0 | Partial (except `validate`, `test`, `metaschema`, and `compile`) | +| Dialect | Support | +|---------------------|-------------------------------------------------------| +| JSON Schema 2020-12 | **Full** | +| JSON Schema 2019-09 | **Full** | +| JSON Schema Draft 7 | **Full** | +| JSON Schema Draft 6 | **Full** | +| JSON Schema Draft 4 | **Full** | +| JSON Schema Draft 3 | Partial (except `validate`, `test`, and `metaschema`) | +| JSON Schema Draft 2 | Partial (except `validate`, `test`, and `metaschema`) | +| JSON Schema Draft 1 | Partial (except `validate`, `test`, and `metaschema`) | +| JSON Schema Draft 0 | Partial (except `validate`, `test`, and `metaschema`) | What our users are saying ------------------------- @@ -63,7 +63,6 @@ documentation: - [`jsonschema lint`](./docs/lint.markdown) - [`jsonschema bundle`](./docs/bundle.markdown) (for inlining remote references in a schema) - [`jsonschema frame`](./docs/frame.markdown) (for debugging references) -- [`jsonschema compile`](./docs/compile.markdown) (for internal debugging) - [`jsonschema identify`](./docs/identify.markdown) - [`jsonschema canonicalize`](./docs/canonicalize.markdown) (for static analysis) - [`jsonschema encode`](./docs/encode.markdown) (for binary compression) diff --git a/docs/compile.markdown b/docs/compile.markdown deleted file mode 100644 index daec635b..00000000 --- a/docs/compile.markdown +++ /dev/null @@ -1,151 +0,0 @@ -Compile -======= - -> [!WARNING] -> JSON Schema Draft 3 and older are not supported at this point in time. - -```sh -jsonschema compile -``` - -Evaluating a JSON Schema is a complex process with a non-trivial associated -performance overhead. For faster evaluation, we internally pre-process a given -JSON Schema into a compiled low-level form. This command allows you to obtain -the schema compilation result as a separate step. - -**At least for now, this command is mainly for internal debugging purposes. As -an end-user, you are better served by the [`validate`](./validate.markdown) -command, which internally compiles schemas.** - -Examples --------- - -For example, consider the following simple schema: - -```json -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "properties": { - "foo": { "type": "string" } - } -} -``` - -The compilation process will result in something like this: - -``` -[ - { - "category": "logical", - "type": "and", - "value": null, - "absoluteKeywordLocation": "#/properties", - "relativeSchemaLocation": "/properties", - "relativeInstanceLocation": "", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [ - { - "category": "assertion", - "type": "type-strict", - "value": { - "category": "value", - "type": "type", - "value": "object" - }, - "absoluteKeywordLocation": "#/properties", - "relativeSchemaLocation": "", - "relativeInstanceLocation": "", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [] - } - ], - "children": [ - { - "category": "internal", - "type": "container", - "value": null, - "absoluteKeywordLocation": "#/properties", - "relativeSchemaLocation": "", - "relativeInstanceLocation": "", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [ - { - "category": "assertion", - "type": "defines", - "value": { - "category": "value", - "type": "string", - "value": "foo" - }, - "absoluteKeywordLocation": "#/properties", - "relativeSchemaLocation": "", - "relativeInstanceLocation": "", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [] - } - ], - "children": [ - { - "category": "assertion", - "type": "type-strict", - "value": { - "category": "value", - "type": "type", - "value": "string" - }, - "absoluteKeywordLocation": "#/properties/foo/type", - "relativeSchemaLocation": "/foo/type", - "relativeInstanceLocation": "/foo", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [] - }, - { - "category": "annotation", - "type": "public", - "value": { - "category": "value", - "type": "json", - "value": "foo" - }, - "absoluteKeywordLocation": "#/properties", - "relativeSchemaLocation": "", - "relativeInstanceLocation": "", - "target": { - "category": "target", - "type": "instance", - "location": "" - }, - "condition": [] - } - ] - } - ] - } -] -``` - -### Compile a JSON Schema - -```sh -jsonschema compile path/to/my/schema.json -``` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40550a2f..afa14806 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,6 @@ add_executable(jsonschema_cli command_fmt.cc command_frame.cc command_bundle.cc - command_compile.cc command_test.cc command_lint.cc command_metaschema.cc diff --git a/src/command.h b/src/command.h index 62074f6a..56a20e39 100644 --- a/src/command.h +++ b/src/command.h @@ -8,7 +8,6 @@ namespace sourcemeta::jsonschema::cli { auto fmt(const std::span &arguments) -> int; auto frame(const std::span &arguments) -> int; auto bundle(const std::span &arguments) -> int; -auto compile(const std::span &arguments) -> int; auto test(const std::span &arguments) -> int; auto lint(const std::span &arguments) -> int; auto validate(const std::span &arguments) -> int; diff --git a/src/command_bundle.cc b/src/command_bundle.cc index 95575083..e593ec42 100644 --- a/src/command_bundle.cc +++ b/src/command_bundle.cc @@ -21,17 +21,15 @@ auto sourcemeta::jsonschema::cli::bundle( auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())}; + sourcemeta::jsontoolkit::bundle( + schema, sourcemeta::jsontoolkit::default_schema_walker, + resolver(options, options.contains("h") || options.contains("http"))); + if (options.contains("w") || options.contains("without-id")) { - log_verbose(options) << "Bundling without using identifiers\n"; - sourcemeta::jsontoolkit::bundle( - schema, sourcemeta::jsontoolkit::default_schema_walker, - resolver(options, options.contains("h") || options.contains("http")), - sourcemeta::jsontoolkit::BundleOptions::WithoutIdentifiers); - } else { - sourcemeta::jsontoolkit::bundle( + log_verbose(options) << "Removing schema identifiers\n"; + sourcemeta::jsontoolkit::unidentify( schema, sourcemeta::jsontoolkit::default_schema_walker, - resolver(options, options.contains("h") || options.contains("http")), - sourcemeta::jsontoolkit::BundleOptions::Default); + resolver(options, options.contains("h") || options.contains("http"))); } sourcemeta::jsontoolkit::prettify( diff --git a/src/command_compile.cc b/src/command_compile.cc deleted file mode 100644 index e7dc59a7..00000000 --- a/src/command_compile.cc +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include - -#include -#include - -#include // EXIT_SUCCESS -#include // std::cout, std::endl - -#include "command.h" -#include "utils.h" - -auto sourcemeta::jsonschema::cli::compile( - const std::span &arguments) -> int { - const auto options{parse_options(arguments, {})}; - - if (options.at("").size() < 1) { - std::cerr - << "error: This command expects a path to a schema. For example:\n\n" - << " jsonschema compile path/to/schema.json\n"; - return EXIT_FAILURE; - } - - const auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())}; - - const auto compiled_schema{sourcemeta::blaze::compile( - schema, sourcemeta::jsontoolkit::default_schema_walker, - resolver(options, options.contains("h") || options.contains("http")), - sourcemeta::blaze::default_schema_compiler)}; - - const sourcemeta::jsontoolkit::JSON result{ - sourcemeta::blaze::to_json(compiled_schema)}; - sourcemeta::jsontoolkit::prettify(result, std::cout, - sourcemeta::blaze::template_format_compare); - std::cout << std::endl; - return EXIT_SUCCESS; -} diff --git a/src/command_frame.cc b/src/command_frame.cc index 43b6fd29..c1f5bd7f 100644 --- a/src/command_frame.cc +++ b/src/command_frame.cc @@ -9,15 +9,17 @@ #include "utils.h" static auto -enum_to_string(const sourcemeta::jsontoolkit::ReferenceEntryType type) +enum_to_string(const sourcemeta::jsontoolkit::Frame::LocationType type) -> std::string { switch (type) { - case sourcemeta::jsontoolkit::ReferenceEntryType::Resource: + case sourcemeta::jsontoolkit::Frame::LocationType::Resource: return "resource"; - case sourcemeta::jsontoolkit::ReferenceEntryType::Anchor: + case sourcemeta::jsontoolkit::Frame::LocationType::Anchor: return "anchor"; - case sourcemeta::jsontoolkit::ReferenceEntryType::Pointer: + case sourcemeta::jsontoolkit::Frame::LocationType::Pointer: return "pointer"; + case sourcemeta::jsontoolkit::Frame::LocationType::Subschema: + return "subschema"; default: return "unknown"; } @@ -36,11 +38,9 @@ auto sourcemeta::jsonschema::cli::frame( const sourcemeta::jsontoolkit::JSON schema{ sourcemeta::jsontoolkit::from_file(options.at("").front())}; - sourcemeta::jsontoolkit::ReferenceFrame frame; - sourcemeta::jsontoolkit::ReferenceMap references; - sourcemeta::jsontoolkit::frame(schema, frame, references, - sourcemeta::jsontoolkit::default_schema_walker, - resolver(options)); + sourcemeta::jsontoolkit::Frame frame; + frame.analyse(schema, sourcemeta::jsontoolkit::default_schema_walker, + resolver(options)); const auto output_json = options.contains("json") || options.contains("j"); if (output_json) { @@ -48,7 +48,7 @@ auto sourcemeta::jsonschema::cli::frame( auto frame_json = sourcemeta::jsontoolkit::JSON::make_object(); auto references_json = sourcemeta::jsontoolkit::JSON::make_object(); - for (const auto &[key, entry] : frame) { + for (const auto &[key, entry] : frame.locations()) { auto frame_entry = sourcemeta::jsontoolkit::JSON::make_object(); if (entry.root.has_value()) { frame_entry.assign("root", @@ -70,12 +70,14 @@ auto sourcemeta::jsonschema::cli::frame( sourcemeta::jsontoolkit::JSON{reference_stream.str()}); frame_entry.assign("dialect", sourcemeta::jsontoolkit::JSON{entry.dialect}); + frame_entry.assign("baseDialect", + sourcemeta::jsontoolkit::JSON{entry.base_dialect}); frame_json.assign(key.second, sourcemeta::jsontoolkit::JSON{frame_entry}); } output_json_object.assign("frames", sourcemeta::jsontoolkit::JSON{frame_json}); - for (const auto &[pointer, entry] : references) { + for (const auto &[pointer, entry] : frame.references()) { auto ref_entry = sourcemeta::jsontoolkit::JSON::make_object(); ref_entry.assign( "type", @@ -109,17 +111,20 @@ auto sourcemeta::jsonschema::cli::frame( sourcemeta::jsontoolkit::prettify(output_json_object, print_stream); std::cout << print_stream.str() << std::endl; } else { - for (const auto &[key, entry] : frame) { + for (const auto &[key, entry] : frame.locations()) { switch (entry.type) { - case sourcemeta::jsontoolkit::ReferenceEntryType::Resource: + case sourcemeta::jsontoolkit::Frame::LocationType::Resource: std::cout << "(LOCATION)"; break; - case sourcemeta::jsontoolkit::ReferenceEntryType::Anchor: + case sourcemeta::jsontoolkit::Frame::LocationType::Anchor: std::cout << "(ANCHOR)"; break; - case sourcemeta::jsontoolkit::ReferenceEntryType::Pointer: + case sourcemeta::jsontoolkit::Frame::LocationType::Pointer: std::cout << "(POINTER)"; break; + case sourcemeta::jsontoolkit::Frame::LocationType::Subschema: + std::cout << "(SUBSCHEMA)"; + break; default: // We should never get here assert(false); @@ -154,9 +159,10 @@ auto sourcemeta::jsonschema::cli::frame( sourcemeta::jsontoolkit::stringify(entry.relative_pointer, std::cout); std::cout << "\n"; std::cout << " Dialect : " << entry.dialect << "\n"; + std::cout << " Base Dialect : " << entry.base_dialect << "\n"; } - for (const auto &[pointer, entry] : references) { + for (const auto &[pointer, entry] : frame.references()) { std::cout << "(REFERENCE) URI: "; sourcemeta::jsontoolkit::stringify(pointer.second, std::cout); std::cout << "\n"; diff --git a/src/command_metaschema.cc b/src/command_metaschema.cc index 2c149662..6e9bc2e6 100644 --- a/src/command_metaschema.cc +++ b/src/command_metaschema.cc @@ -21,6 +21,7 @@ auto sourcemeta::jsonschema::cli::metaschema( const auto custom_resolver{ resolver(options, options.contains("h") || options.contains("http"))}; bool result{true}; + sourcemeta::blaze::Evaluator evaluator; std::map cache; @@ -47,13 +48,13 @@ auto sourcemeta::jsonschema::cli::metaschema( if (trace) { sourcemeta::blaze::TraceOutput output; - result = sourcemeta::blaze::evaluate(cache.at(dialect.value()), - entry.second, std::ref(output)); + result = evaluator.validate(cache.at(dialect.value()), entry.second, + std::ref(output)); print(output, std::cout); } else { sourcemeta::blaze::ErrorOutput output{entry.second}; - if (sourcemeta::blaze::evaluate(cache.at(dialect.value()), entry.second, - std::ref(output))) { + if (evaluator.validate(cache.at(dialect.value()), entry.second, + std::ref(output))) { log_verbose(options) << "ok: " << std::filesystem::weakly_canonical(entry.first).string() << "\n matches " << dialect.value() << "\n"; diff --git a/src/command_test.cc b/src/command_test.cc index 7f74e7ea..86b69dbd 100644 --- a/src/command_test.cc +++ b/src/command_test.cc @@ -71,6 +71,7 @@ auto sourcemeta::jsonschema::cli::test( const auto test_resolver{ resolver(options, options.contains("h") || options.contains("http"))}; const auto verbose{options.contains("verbose") || options.contains("v")}; + sourcemeta::blaze::Evaluator evaluator; for (const auto &entry : for_each_json(options.at(""), parse_ignore(options), parse_extensions(options))) { @@ -245,8 +246,8 @@ auto sourcemeta::jsonschema::cli::test( get_data(test_case, entry.first.parent_path(), verbose)}; const std::string ref{"$ref"}; sourcemeta::blaze::ErrorOutput output{instance, {std::cref(ref)}}; - const auto case_result{sourcemeta::blaze::evaluate( - schema_template, instance, std::ref(output))}; + const auto case_result{ + evaluator.validate(schema_template, instance, std::ref(output))}; std::ostringstream test_case_description; if (test_case.defines("description")) { diff --git a/src/command_validate.cc b/src/command_validate.cc index c77f045a..09614a69 100644 --- a/src/command_validate.cc +++ b/src/command_validate.cc @@ -56,6 +56,7 @@ auto sourcemeta::jsonschema::cli::validate( const auto schema_template{sourcemeta::blaze::compile( schema, sourcemeta::jsontoolkit::default_schema_walker, custom_resolver, sourcemeta::blaze::default_schema_compiler)}; + sourcemeta::blaze::Evaluator evaluator; bool result{true}; @@ -68,7 +69,6 @@ auto sourcemeta::jsonschema::cli::validate( << "Interpreting input as JSONL: " << std::filesystem::weakly_canonical(instance_path).string() << "\n"; std::size_t index{0}; - auto stream{sourcemeta::jsontoolkit::read_file(instance_path)}; try { for (const auto &instance : sourcemeta::jsontoolkit::JSONL{stream}) { @@ -80,7 +80,7 @@ auto sourcemeta::jsonschema::cli::validate( if (benchmark) { const auto timestamp_start{ std::chrono::high_resolution_clock::now()}; - subresult = sourcemeta::blaze::evaluate(schema_template, instance); + subresult = evaluator.validate(schema_template, instance); const auto timestamp_end{std::chrono::high_resolution_clock::now()}; const auto duration_us{ std::chrono::duration_cast( @@ -91,11 +91,11 @@ auto sourcemeta::jsonschema::cli::validate( error << "error: Schema validation failure\n"; } } else if (trace) { - subresult = sourcemeta::blaze::evaluate(schema_template, instance, - std::ref(trace_output)); + subresult = evaluator.validate(schema_template, instance, + std::ref(trace_output)); } else { - subresult = sourcemeta::blaze::evaluate(schema_template, instance, - std::ref(output)); + subresult = + evaluator.validate(schema_template, instance, std::ref(output)); } if (trace) { @@ -138,7 +138,7 @@ auto sourcemeta::jsonschema::cli::validate( bool subresult{true}; if (benchmark) { const auto timestamp_start{std::chrono::high_resolution_clock::now()}; - subresult = sourcemeta::blaze::evaluate(schema_template, instance); + subresult = evaluator.validate(schema_template, instance); const auto timestamp_end{std::chrono::high_resolution_clock::now()}; const auto duration_us{ std::chrono::duration_cast( @@ -150,11 +150,11 @@ auto sourcemeta::jsonschema::cli::validate( result = false; } } else if (trace) { - subresult = sourcemeta::blaze::evaluate(schema_template, instance, - std::ref(trace_output)); + subresult = evaluator.validate(schema_template, instance, + std::ref(trace_output)); } else { - subresult = sourcemeta::blaze::evaluate(schema_template, instance, - std::ref(output)); + subresult = + evaluator.validate(schema_template, instance, std::ref(output)); } if (trace) { diff --git a/src/main.cc b/src/main.cc index e29d9c53..55a5e715 100644 --- a/src/main.cc +++ b/src/main.cc @@ -60,11 +60,6 @@ Global Options: Statically analyse a schema to display schema locations and references in a human-readable manner. - compile - - Pre-process a JSON Schema into JSON Toolkit's low-level JSON-based - compiled form for faster evaluation. - identify [--relative-to/-t ] Print the URI of the given schema to standard output, optionally @@ -94,8 +89,6 @@ auto jsonschema_main(const std::string &program, const std::string &command, return sourcemeta::jsonschema::cli::frame(arguments); } else if (command == "bundle") { return sourcemeta::jsonschema::cli::bundle(arguments); - } else if (command == "compile") { - return sourcemeta::jsonschema::cli::compile(arguments); } else if (command == "lint") { return sourcemeta::jsonschema::cli::lint(arguments); } else if (command == "validate") { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 018d88c6..a2459c38 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -144,14 +144,6 @@ add_jsonschema_test_unix(frame/fail_no_schema) add_jsonschema_test_unix(frame/fail_schema_invalid_json) add_jsonschema_test_unix(frame/fail_unknown_metaschema) -# Compile -add_jsonschema_test_unix(compile/pass_1) -add_jsonschema_test_unix(compile/pass_2) -add_jsonschema_test_unix(compile/fail_no_schema) -add_jsonschema_test_unix(compile/fail_schema_invalid_json) -add_jsonschema_test_unix(compile/fail_unknown_metaschema) -add_jsonschema_test_unix(compile/fail_ref_from_unknown_keyword) - # Identify add_jsonschema_test_unix(identify/fail_resolve_from_no_match) add_jsonschema_test_unix(identify/fail_resolve_from_equal) diff --git a/test/bundle/pass_without_id_verbose.sh b/test/bundle/pass_without_id_verbose.sh index 4a59c75e..e6979004 100755 --- a/test/bundle/pass_without_id_verbose.sh +++ b/test/bundle/pass_without_id_verbose.sh @@ -27,7 +27,8 @@ EOF "$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" --without-id --verbose 1> "$TMP/result.json" 2>&1 cat << EOF > "$TMP/expected.json" -Bundling without using identifiers +Importing schema into the resolution context: $(realpath "$TMP")/schemas/remote.json +Removing schema identifiers Importing schema into the resolution context: $(realpath "$TMP")/schemas/remote.json { "\$schema": "https://json-schema.org/draft/2020-12/schema", @@ -41,4 +42,6 @@ Importing schema into the resolution context: $(realpath "$TMP")/schemas/remote. } EOF +cat "$TMP/result.json" + diff "$TMP/result.json" "$TMP/expected.json" diff --git a/test/compile/fail_no_schema.sh b/test/compile/fail_no_schema.sh deleted file mode 100755 index 2fb8aea6..00000000 --- a/test/compile/fail_no_schema.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -"$1" compile 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" -test "$CODE" = "1" || exit 1 - -cat << 'EOF' > "$TMP/expected.txt" -error: This command expects a path to a schema. For example: - - jsonschema compile path/to/schema.json -EOF - -diff "$TMP/stderr.txt" "$TMP/expected.txt" diff --git a/test/compile/fail_ref_from_unknown_keyword.sh b/test/compile/fail_ref_from_unknown_keyword.sh deleted file mode 100755 index 9442b1bd..00000000 --- a/test/compile/fail_ref_from_unknown_keyword.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -cat << 'EOF' > "$TMP/schema.json" -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "properties": { - "foo": { "$ref": "#/$defs/bar" }, - "baz": { "type": "string" } - }, - "$defs": { - "bar": { - "$ref": "#/properties/baz" - } - } -} -EOF - -"$1" compile "$TMP/schema.json" 2>"$TMP/stderr.txt" \ - && CODE="$?" || CODE="$?" -test "$CODE" = "1" || exit 1 - -cat << 'EOF' > "$TMP/expected.txt" -error: The schema location is inside of an unknown keyword - #/properties/baz - at schema location "/$defs/bar/$ref" -EOF - -diff "$TMP/stderr.txt" "$TMP/expected.txt" diff --git a/test/compile/fail_schema_invalid_json.sh b/test/compile/fail_schema_invalid_json.sh deleted file mode 100755 index 516bccec..00000000 --- a/test/compile/fail_schema_invalid_json.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -cat << 'EOF' > "$TMP/schema.json" -{ - "type" string -} -EOF - -"$1" compile "$TMP/schema.json" 2>"$TMP/stderr.txt" \ - && CODE="$?" || CODE="$?" -test "$CODE" = "1" || exit 1 - -cat << EOF > "$TMP/expected.txt" -error: Failed to parse the JSON document at line 2 and column 10 - $(realpath "$TMP")/schema.json -EOF - -diff "$TMP/stderr.txt" "$TMP/expected.txt" diff --git a/test/compile/fail_unknown_metaschema.sh b/test/compile/fail_unknown_metaschema.sh deleted file mode 100755 index 43278538..00000000 --- a/test/compile/fail_unknown_metaschema.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -cat << 'EOF' > "$TMP/schema.json" -{ - "$schema": "https://example.com/unknown", - "$id": "https://example.com", - "$ref": "nested" -} -EOF - -"$1" compile "$TMP/schema.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" -test "$CODE" = "1" || exit 1 - -cat << EOF > "$TMP/expected.txt" -error: Could not resolve the requested schema - at https://example.com/unknown -EOF - -diff "$TMP/stderr.txt" "$TMP/expected.txt" diff --git a/test/compile/pass_1.sh b/test/compile/pass_1.sh deleted file mode 100755 index 1c4cd938..00000000 --- a/test/compile/pass_1.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -cat << 'EOF' > "$TMP/schema.json" -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "properties": { - "foo": { "type": "string" } - } -} -EOF - -"$1" compile "$TMP/schema.json" > "$TMP/result.json" - -cat << 'EOF' > "$TMP/expected.json" -[ - { - "category": "assertion", - "type": "property-type-strict", - "value": { - "category": "value", - "type": "type", - "value": "string" - }, - "schemaResource": 0, - "absoluteKeywordLocation": "#/properties/foo/type", - "relativeSchemaLocation": "/properties/foo/type", - "relativeInstanceLocation": "/foo", - "dynamic": false, - "track": false - } -] -EOF - -diff "$TMP/result.json" "$TMP/expected.json" diff --git a/test/compile/pass_2.sh b/test/compile/pass_2.sh deleted file mode 100755 index 207f3d85..00000000 --- a/test/compile/pass_2.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -TMP="$(mktemp -d)" -clean() { rm -rf "$TMP"; } -trap clean EXIT - -cat << 'EOF' > "$TMP/schema.json" -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "allOf": [ {"$ref": "#/definitions/job" } ], - "definitions": { "job": {} } -} -EOF - -# We cannot assert on the result as the reference generates a random label -"$1" compile "$TMP/schema.json" diff --git a/test/frame/pass.sh b/test/frame/pass.sh index a584f0fa..a1043610 100755 --- a/test/frame/pass.sh +++ b/test/frame/pass.sh @@ -28,6 +28,7 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (POINTER) URI: https://example.com#/$defs Type : Static Schema : https://example.com @@ -35,13 +36,15 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : /$defs Dialect : https://json-schema.org/draft/2020-12/schema -(POINTER) URI: https://example.com#/$defs/string + Base Dialect : https://json-schema.org/draft/2020-12/schema +(SUBSCHEMA) URI: https://example.com#/$defs/string Type : Static Schema : https://example.com Pointer : /$defs/string Base URI : https://example.com Relative Pointer : /$defs/string Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (POINTER) URI: https://example.com#/$defs/string/type Type : Static Schema : https://example.com @@ -49,6 +52,7 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : /$defs/string/type Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (POINTER) URI: https://example.com#/$id Type : Static Schema : https://example.com @@ -56,6 +60,7 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : /$id Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (POINTER) URI: https://example.com#/$ref Type : Static Schema : https://example.com @@ -63,6 +68,7 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : /$ref Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (POINTER) URI: https://example.com#/$schema Type : Static Schema : https://example.com @@ -70,6 +76,7 @@ cat << 'EOF' > "$TMP/expected.txt" Base URI : https://example.com Relative Pointer : /$schema Dialect : https://json-schema.org/draft/2020-12/schema + Base Dialect : https://json-schema.org/draft/2020-12/schema (REFERENCE) URI: /$ref Type : Static Destination : https://example.com#/$defs/string diff --git a/vendor/alterschema/LICENSE b/vendor/alterschema/LICENSE index 0be6f020..9348973b 100644 --- a/vendor/alterschema/LICENSE +++ b/vendor/alterschema/LICENSE @@ -1,661 +1,12 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU Affero General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - JSON Toolkit - The swiss army knife for JSON applications - Copyright (C) 2022 Juan Cruz Viotti - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see -. +This software is dual-licensed: you can redistribute it and/or modify it under +the terms of the GNU Affero General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. For the terms of this license, see +. + +You are free to use this software under the terms of the GNU Affero General +Public License WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +Alternatively, you can use this software under a commercial license, as set out +in . diff --git a/vendor/alterschema/LICENSE-COMMERCIAL b/vendor/alterschema/LICENSE-COMMERCIAL deleted file mode 100644 index 2b3c48a3..00000000 --- a/vendor/alterschema/LICENSE-COMMERCIAL +++ /dev/null @@ -1,2 +0,0 @@ -Refer to https://www.sourcemeta.com/licensing/ for learning more about -obtaining a commercial license. diff --git a/vendor/alterschema/src/linter/simplify/single_type_array.h b/vendor/alterschema/src/linter/simplify/single_type_array.h index 0e81bd49..b673435a 100644 --- a/vendor/alterschema/src/linter/simplify/single_type_array.h +++ b/vendor/alterschema/src/linter/simplify/single_type_array.h @@ -27,6 +27,7 @@ class SingleTypeArray final : public Rule { } auto transform(Transformer &transformer) const -> void override { - transformer.replace({"type"}, transformer.schema().at("type").front()); + auto type{transformer.schema().at("type").front()}; + transformer.replace({"type"}, std::move(type)); } }; diff --git a/vendor/blaze/cmake/FindGoogleBenchmark.cmake b/vendor/blaze/cmake/FindGoogleBenchmark.cmake index da442f63..6d0ad9ed 100644 --- a/vendor/blaze/cmake/FindGoogleBenchmark.cmake +++ b/vendor/blaze/cmake/FindGoogleBenchmark.cmake @@ -1,5 +1,5 @@ -if(NOT Benchmark_FOUND) +if(NOT GoogleBenchmark_FOUND) set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Enable testing of the benchmark library.") add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/googlebenchmark") - set(Benchmark_FOUND ON) + set(GoogleBenchmark_FOUND ON) endif() diff --git a/vendor/blaze/config.cmake.in b/vendor/blaze/config.cmake.in index 7a5a0bb8..047a05ac 100644 --- a/vendor/blaze/config.cmake.in +++ b/vendor/blaze/config.cmake.in @@ -9,7 +9,7 @@ if(NOT BLAZE_COMPONENTS) endif() include(CMakeFindDependencyMacro) -find_dependency(JSONToolkit COMPONENTS uri json jsonpointer jsonschema) +find_dependency(JSONToolkit COMPONENTS uri json regex jsonpointer jsonschema) foreach(component ${BLAZE_COMPONENTS}) if(component STREQUAL "compiler") diff --git a/vendor/blaze/src/compiler/CMakeLists.txt b/vendor/blaze/src/compiler/CMakeLists.txt index 9e6537a3..8005827d 100644 --- a/vendor/blaze/src/compiler/CMakeLists.txt +++ b/vendor/blaze/src/compiler/CMakeLists.txt @@ -2,7 +2,7 @@ noa_library(NAMESPACE sourcemeta PROJECT blaze NAME compiler FOLDER "Blaze/Compiler" PRIVATE_HEADERS error.h output.h SOURCES - compile.cc compile_json.cc compile_describe.cc + compile.cc compile_describe.cc compile_output_error.cc compile_output_trace.cc compile_helpers.h default_compiler.cc default_compiler_2020_12.h @@ -17,6 +17,8 @@ endif() target_link_libraries(sourcemeta_blaze_compiler PUBLIC sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_blaze_compiler PRIVATE + sourcemeta::jsontoolkit::regex) target_link_libraries(sourcemeta_blaze_compiler PUBLIC sourcemeta::jsontoolkit::jsonpointer) target_link_libraries(sourcemeta_blaze_compiler PRIVATE diff --git a/vendor/blaze/src/compiler/compile.cc b/vendor/blaze/src/compiler/compile.cc index 6d6a00c5..a3708b58 100644 --- a/vendor/blaze/src/compiler/compile.cc +++ b/vendor/blaze/src/compiler/compile.cc @@ -1,5 +1,5 @@ #include -#include +#include #include @@ -16,7 +16,7 @@ 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 { + -> sourcemeta::blaze::Instructions { using namespace sourcemeta::blaze; assert(is_schema(schema_context.schema)); @@ -26,12 +26,12 @@ auto compile_subschema(const sourcemeta::blaze::Context &context, if (schema_context.schema.to_boolean()) { return {}; } else { - return {make(context, schema_context, dynamic_context, - ValueNone{})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionFail, context, + schema_context, dynamic_context, ValueNone{})}; } } - Template steps; + Instructions steps; for (const auto &entry : sourcemeta::jsontoolkit::SchemaKeywordIterator{ schema_context.schema, context.walker, context.resolver, default_dialect}) { @@ -46,12 +46,13 @@ auto compile_subschema(const sourcemeta::blaze::Context &context, // TODO: This represents a copy schema_context.labels, schema_context.references}, {keyword, dynamic_context.base_schema_location, - dynamic_context.base_instance_location})) { + dynamic_context.base_instance_location, + dynamic_context.property_as_target}, + steps)) { // Just a sanity check to ensure every keyword location is indeed valid - assert(context.frame.contains( + assert(context.frame.locations().contains( {sourcemeta::jsontoolkit::ReferenceType::Static, - std::visit([](const auto &value) { return value.keyword_location; }, - step)})); + step.keyword_location})); steps.push_back(std::move(step)); } } @@ -63,10 +64,10 @@ auto precompile( const sourcemeta::blaze::Context &context, sourcemeta::blaze::SchemaContext &schema_context, const sourcemeta::blaze::DynamicContext &dynamic_context, - const sourcemeta::jsontoolkit::ReferenceFrame::value_type &entry) - -> sourcemeta::blaze::Template { + const sourcemeta::jsontoolkit::Frame::Locations::value_type &entry) + -> sourcemeta::blaze::Instructions { const sourcemeta::jsontoolkit::URI anchor_uri{entry.first.second}; - const auto label{sourcemeta::blaze::EvaluationContext{}.hash( + const auto label{sourcemeta::blaze::Evaluator{}.hash( schema_resource_id(context, anchor_uri.recompose_without_fragment().value_or("")), std::string{anchor_uri.fragment().value_or("")})}; @@ -86,11 +87,12 @@ auto precompile( {}, {}}; - return {make( - context, nested_schema_context, dynamic_context, + return {make( + sourcemeta::blaze::InstructionIndex::ControlMark, context, + nested_schema_context, dynamic_context, sourcemeta::blaze::ValueUnsignedInteger{label}, sourcemeta::blaze::compile(context, nested_schema_context, - sourcemeta::blaze::relative_dynamic_context, + sourcemeta::blaze::relative_dynamic_context(), sourcemeta::jsontoolkit::empty_pointer, sourcemeta::jsontoolkit::empty_pointer, entry.first.second))}; @@ -110,14 +112,11 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, // Make sure the input schema is bundled, otherwise we won't be able to // resolve remote references here const sourcemeta::jsontoolkit::JSON result{sourcemeta::jsontoolkit::bundle( - schema, walker, resolver, sourcemeta::jsontoolkit::BundleOptions::Default, - default_dialect)}; + schema, walker, resolver, default_dialect)}; // Perform framing to resolve references later on - sourcemeta::jsontoolkit::ReferenceFrame frame; - sourcemeta::jsontoolkit::ReferenceMap references; - sourcemeta::jsontoolkit::frame(result, frame, references, walker, resolver, - default_dialect); + sourcemeta::jsontoolkit::Frame frame; + frame.analyse(result, walker, resolver, default_dialect); const std::string base{sourcemeta::jsontoolkit::URI{ sourcemeta::jsontoolkit::identify( @@ -128,15 +127,15 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, .canonicalize() .recompose()}; - assert( - frame.contains({sourcemeta::jsontoolkit::ReferenceType::Static, base})); - const auto root_frame_entry{ - frame.at({sourcemeta::jsontoolkit::ReferenceType::Static, base})}; + assert(frame.locations().contains( + {sourcemeta::jsontoolkit::ReferenceType::Static, base})); + const auto root_frame_entry{frame.locations().at( + {sourcemeta::jsontoolkit::ReferenceType::Static, base})}; // Check whether dynamic referencing takes places in this schema. If not, // we can avoid the overhead of keeping track of dynamics scopes, etc bool uses_dynamic_scopes{false}; - for (const auto &reference : references) { + for (const auto &reference : frame.references()) { if (reference.first.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic) { uses_dynamic_scopes = true; @@ -154,84 +153,10 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, {}, {}}; - std::set unevaluated_properties_schemas; - std::set unevaluated_items_schemas; - - if (schema_context.vocabularies.contains( - "https://json-schema.org/draft/2019-09/vocab/core") || - schema_context.vocabularies.contains( - "https://json-schema.org/draft/2020-12/vocab/core")) { - for (const auto &entry : sourcemeta::jsontoolkit::SchemaIterator{ - result, walker, resolver, default_dialect}) { - if (!entry.vocabularies.contains( - "https://json-schema.org/draft/2019-09/vocab/applicator") && - !entry.vocabularies.contains( - "https://json-schema.org/draft/2020-12/vocab/unevaluated")) { - continue; - } - - const auto &subschema{ - sourcemeta::jsontoolkit::get(result, entry.pointer)}; - if (!subschema.is_object()) { - continue; - } - - if (subschema.defines("unevaluatedProperties") && - sourcemeta::jsontoolkit::is_schema( - subschema.at("unevaluatedProperties"))) { - - // No need to consider `unevaluatedProperties` if it has a sibling - // `additionalProperties`. By definition, nothing could remain - // unevaluated. - - if (entry.vocabularies.contains( - "https://json-schema.org/draft/2019-09/vocab/applicator") && - subschema.defines("additionalProperties")) { - continue; - } - - if (entry.vocabularies.contains( - "https://json-schema.org/draft/2020-12/vocab/applicator") && - subschema.defines("additionalProperties")) { - continue; - } - - unevaluated_properties_schemas.insert(entry.pointer); - } - - if (subschema.defines("unevaluatedItems") && - sourcemeta::jsontoolkit::is_schema( - subschema.at("unevaluatedItems"))) { - - // No need to consider `unevaluatedItems` if it has a - // sibling `items` schema (or its older equivalents). - // By definition, nothing could remain unevaluated. - - if (entry.vocabularies.contains( - "https://json-schema.org/draft/2020-12/vocab/applicator") && - subschema.defines("items")) { - continue; - } else if (entry.vocabularies.contains("https://json-schema.org/draft/" - "2019-09/vocab/applicator")) { - if (subschema.defines("items") && - sourcemeta::jsontoolkit::is_schema(subschema.at("items"))) { - continue; - } else if (subschema.defines("items") && - subschema.at("items").is_array() && - subschema.defines("additionalItems")) { - continue; - } - } - - unevaluated_items_schemas.insert(entry.pointer); - } - } - } - std::vector resources; - for (const auto &entry : frame) { + for (const auto &entry : frame.locations()) { if (entry.second.type == - sourcemeta::jsontoolkit::ReferenceEntryType::Resource) { + sourcemeta::jsontoolkit::Frame::LocationType::Resource) { resources.push_back(entry.first.second); } } @@ -245,18 +170,21 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, std::set(resources.cbegin(), resources.cend()).size()); // Calculate the top static reference destinations for precompilation purposes + // TODO: Replace this logic with `.frame()` `destination_of` information std::map static_references_count; - for (const auto &reference : references) { + for (const auto &reference : frame.references()) { if (reference.first.first != sourcemeta::jsontoolkit::ReferenceType::Static || - !frame.contains({sourcemeta::jsontoolkit::ReferenceType::Static, - reference.second.destination})) { + !frame.locations().contains( + {sourcemeta::jsontoolkit::ReferenceType::Static, + reference.second.destination})) { continue; } - const auto &entry{frame.at({sourcemeta::jsontoolkit::ReferenceType::Static, - reference.second.destination})}; - for (const auto &subreference : references) { + const auto &entry{ + frame.locations().at({sourcemeta::jsontoolkit::ReferenceType::Static, + reference.second.destination})}; + for (const auto &subreference : frame.references()) { if (subreference.first.second.starts_with(entry.pointer)) { static_references_count[reference.second.destination] += 1; } @@ -281,25 +209,26 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, } } + auto unevaluated{ + sourcemeta::jsontoolkit::unevaluated(result, frame, walker, resolver)}; + const Context context{result, - frame, - references, + std::move(frame), std::move(resources), walker, resolver, compiler, mode, uses_dynamic_scopes, - unevaluated_properties_schemas, - unevaluated_items_schemas, + std::move(unevaluated), std::move(precompiled_static_schemas)}; - const DynamicContext dynamic_context{relative_dynamic_context}; - Template compiler_template; + const DynamicContext dynamic_context{relative_dynamic_context()}; + Instructions compiler_template; for (const auto &destination : context.precompiled_static_schemas) { - assert(frame.contains( + assert(context.frame.locations().contains( {sourcemeta::jsontoolkit::ReferenceType::Static, destination})); - const auto match{frame.find( + const auto match{context.frame.locations().find( {sourcemeta::jsontoolkit::ReferenceType::Static, destination})}; for (auto &&substep : precompile(context, schema_context, dynamic_context, *match)) { @@ -312,10 +241,10 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, "https://json-schema.org/draft/2019-09/vocab/core") || schema_context.vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/core"))) { - for (const auto &entry : frame) { + for (const auto &entry : context.frame.locations()) { // We are only trying to find dynamic anchors if (entry.second.type != - sourcemeta::jsontoolkit::ReferenceEntryType::Anchor || + sourcemeta::jsontoolkit::Frame::LocationType::Anchor || entry.first.first != sourcemeta::jsontoolkit::ReferenceType::Dynamic) { continue; @@ -330,13 +259,23 @@ auto compile(const sourcemeta::jsontoolkit::JSON &schema, auto children{compile_subschema(context, schema_context, dynamic_context, root_frame_entry.dialect)}; + + const bool track{ + context.mode != Mode::FastValidation || + requires_evaluation(context, schema_context) || + // TODO: This expression should go away if we start properly compiling + // `unevaluatedItems` like we compile `unevaluatedProperties` + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; if (compiler_template.empty()) { - return children; + return {std::move(children), uses_dynamic_scopes, track}; } else { compiler_template.reserve(compiler_template.size() + children.size()); std::move(children.begin(), children.end(), std::back_inserter(compiler_template)); - return compiler_template; + return {std::move(compiler_template), uses_dynamic_scopes, track}; } } @@ -344,7 +283,7 @@ 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) -> Template { + const std::optional &uri) -> Instructions { // Determine URI of the destination after recursion const std::string destination{ uri.has_value() @@ -355,14 +294,14 @@ auto compile(const Context &context, const SchemaContext &schema_context, .recompose()}; // Otherwise the recursion attempt is non-sense - if (!context.frame.contains( + if (!context.frame.locations().contains( {sourcemeta::jsontoolkit::ReferenceType::Static, destination})) { throw sourcemeta::jsontoolkit::SchemaReferenceError( destination, schema_context.relative_pointer, "The target of the reference does not exist in the schema"); } - const auto &entry{context.frame.at( + const auto &entry{context.frame.locations().at( {sourcemeta::jsontoolkit::ReferenceType::Static, destination})}; const auto &new_schema{get(context.root, entry.pointer)}; @@ -389,7 +328,8 @@ auto compile(const Context &context, const SchemaContext &schema_context, // TODO: This represents a copy schema_context.labels, schema_context.references}, {dynamic_context.keyword, destination_pointer, - dynamic_context.base_instance_location.concat(instance_suffix)}, + dynamic_context.base_instance_location.concat(instance_suffix), + dynamic_context.property_as_target}, entry.dialect); } diff --git a/vendor/blaze/src/compiler/compile_describe.cc b/vendor/blaze/src/compiler/compile_describe.cc index 592eb4c7..6ade413e 100644 --- a/vendor/blaze/src/compiler/compile_describe.cc +++ b/vendor/blaze/src/compiler/compile_describe.cc @@ -8,9 +8,10 @@ namespace { using namespace sourcemeta::blaze; -template auto step_value(const T &step) -> decltype(auto) { +template +auto instruction_value(const T &step) -> decltype(auto) { if constexpr (requires { step.value; }) { - return step.value; + return std::get(step.value); } else { return step.id; } @@ -126,45 +127,53 @@ auto unknown() -> std::string { return ""; } -struct DescribeVisitor { - const bool valid; - const sourcemeta::jsontoolkit::WeakPointer &evaluate_path; - const std::string &keyword; - const sourcemeta::jsontoolkit::WeakPointer &instance_location; - const sourcemeta::jsontoolkit::JSON ⌖ - const sourcemeta::jsontoolkit::JSON &annotation; +} // namespace + +namespace sourcemeta::blaze { - auto operator()(const AssertionFail &) const -> std::string { - if (this->keyword == "contains") { +// 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 Instruction &step, + const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, + const sourcemeta::jsontoolkit::WeakPointer &instance_location, + const sourcemeta::jsontoolkit::JSON &instance, + const sourcemeta::jsontoolkit::JSON &annotation) -> std::string { + assert(evaluate_path.empty() || evaluate_path.back().is_property()); + const std::string keyword{ + evaluate_path.empty() ? "" : evaluate_path.back().to_property()}; + const sourcemeta::jsontoolkit::JSON &target{get(instance, instance_location)}; + + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionFail) { + if (keyword == "contains") { return "The constraints declared for this keyword were not satisfiable"; } - if (this->keyword == "additionalProperties" || - this->keyword == "unevaluatedProperties") { + if (keyword == "additionalProperties" || + keyword == "unevaluatedProperties") { std::ostringstream message; - assert(!this->instance_location.empty()); - assert(this->instance_location.back().is_property()); + assert(!instance_location.empty()); + assert(instance_location.back().is_property()); message << "The object value was not expected to define the property " - << escape_string(this->instance_location.back().to_property()); + << escape_string(instance_location.back().to_property()); return message.str(); } - if (this->keyword == "unevaluatedItems") { + if (keyword == "unevaluatedItems") { std::ostringstream message; - assert(!this->instance_location.empty()); - assert(this->instance_location.back().is_index()); + assert(!instance_location.empty()); + assert(instance_location.back().is_index()); message << "The array value was not expected to define the item at index " - << this->instance_location.back().to_index(); + << instance_location.back().to_index(); return message.str(); } return "No instance is expected to succeed against the false schema"; } - auto operator()(const LogicalOr &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalOr) { assert(!step.children.empty()); std::ostringstream message; - message << "The " << to_string(this->target.type()) + message << "The " << to_string(target.type()) << " value was expected to validate against "; if (step.children.size() > 1) { message << "at least one of the " << step.children.size() @@ -176,11 +185,11 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LogicalAnd &step) const -> std::string { - if (this->keyword == "allOf") { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalAnd) { + if (keyword == "allOf") { assert(!step.children.empty()); std::ostringstream message; - message << "The " << to_string(this->target.type()) + message << "The " << to_string(target.type()) << " value was expected to validate against the "; if (step.children.size() > 1) { message << step.children.size() << " given subschemas"; @@ -191,11 +200,11 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "properties") { + if (keyword == "properties") { assert(!step.children.empty()); - if (!this->target.is_object()) { + if (!target.is_object()) { std::ostringstream message; - describe_type_check(this->valid, this->target.type(), + describe_type_check(valid, target.type(), sourcemeta::jsontoolkit::JSON::Type::Object, message); return message.str(); @@ -215,17 +224,17 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "$ref") { - return describe_reference(this->target); + if (keyword == "$ref") { + return describe_reference(target); } return unknown(); } - auto operator()(const LogicalXor &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalXor) { assert(!step.children.empty()); std::ostringstream message; - message << "The " << to_string(this->target.type()) + message << "The " << to_string(target.type()) << " value was expected to validate against "; if (step.children.size() > 1) { message << "one and only one of the " << step.children.size() @@ -237,56 +246,57 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LogicalCondition &) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalCondition) { std::ostringstream message; - message << "The " << to_string(this->target.type()) + message << "The " << to_string(target.type()) << " value was expected to validate against the given conditional"; return message.str(); } - auto operator()(const LogicalNot &) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalNot) { std::ostringstream message; message - << "The " << to_string(this->target.type()) + << "The " << to_string(target.type()) << " value was expected to not validate against the given subschema"; - if (!this->valid) { + if (!valid) { message << ", but it did"; } return message.str(); } - auto operator()(const LogicalNotEvaluate &) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalNotEvaluate) { std::ostringstream message; message - << "The " << to_string(this->target.type()) + << "The " << to_string(target.type()) << " value was expected to not validate against the given subschema"; - if (!this->valid) { + if (!valid) { message << ", but it did"; } return message.str(); } - auto operator()(const ControlLabel &) const -> std::string { - return describe_reference(this->target); + if (step.type == sourcemeta::blaze::InstructionIndex::ControlLabel) { + return describe_reference(target); } - auto operator()(const ControlMark &) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::ControlMark) { return "The schema location was marked for future use"; } - auto operator()(const ControlEvaluate &) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::ControlEvaluate) { return "The instance location was marked as evaluated"; } - auto operator()(const ControlJump &) const -> std::string { - return describe_reference(this->target); + if (step.type == sourcemeta::blaze::InstructionIndex::ControlJump) { + return describe_reference(target); } - auto operator()(const ControlDynamicAnchorJump &step) const -> std::string { - if (this->keyword == "$dynamicRef") { - const auto &value{step_value(step)}; + if (step.type == + sourcemeta::blaze::InstructionIndex::ControlDynamicAnchorJump) { + if (keyword == "$dynamicRef") { + const auto &value{instruction_value(step)}; std::ostringstream message; message << "The " << to_string(target.type()) << " value was expected to validate against the first subschema " @@ -295,7 +305,7 @@ struct DescribeVisitor { return message.str(); } - assert(this->keyword == "$recursiveRef"); + assert(keyword == "$recursiveRef"); std::ostringstream message; message << "The " << to_string(target.type()) << " value was expected to validate against the first subschema " @@ -303,34 +313,33 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AnnotationEmit &) const -> std::string { - if (this->keyword == "properties") { - assert(this->annotation.is_string()); + if (step.type == sourcemeta::blaze::InstructionIndex::AnnotationEmit) { + if (keyword == "properties") { + assert(annotation.is_string()); std::ostringstream message; - message << "The object property " - << escape_string(this->annotation.to_string()) + message << "The object property " << escape_string(annotation.to_string()) << " successfully validated against its property " "subschema"; return message.str(); } - if ((this->keyword == "items" || this->keyword == "additionalItems") && - this->annotation.is_boolean() && this->annotation.to_boolean()) { - assert(this->target.is_array()); + if ((keyword == "items" || keyword == "additionalItems") && + annotation.is_boolean() && annotation.to_boolean()) { + assert(target.is_array()); return "Every item in the array value was successfully validated"; } - if ((this->keyword == "prefixItems" || this->keyword == "items") && - this->annotation.is_integer()) { - assert(this->target.is_array()); - assert(this->annotation.is_positive()); + if ((keyword == "prefixItems" || keyword == "items") && + annotation.is_integer()) { + assert(target.is_array()); + assert(annotation.is_positive()); std::ostringstream message; - if (this->annotation.to_integer() == 0) { + if (annotation.to_integer() == 0) { message << "The first item of the array value successfully validated " "against the first " "positional subschema"; } else { - message << "The first " << this->annotation.to_integer() + 1 + message << "The first " << annotation.to_integer() + 1 << " items of the array value successfully validated against " "the given " "positional subschemas"; @@ -339,58 +348,58 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "prefixItems" && this->annotation.is_boolean() && - this->annotation.to_boolean()) { - assert(this->target.is_array()); + if (keyword == "prefixItems" && annotation.is_boolean() && + annotation.to_boolean()) { + assert(target.is_array()); std::ostringstream message; message << "Every item of the array value validated against the given " "positional subschemas"; return message.str(); } - if (this->keyword == "title" || this->keyword == "description") { - assert(this->annotation.is_string()); + if (keyword == "title" || keyword == "description") { + assert(annotation.is_string()); std::ostringstream message; - message << "The " << this->keyword << " of the"; - if (this->instance_location.empty()) { + message << "The " << keyword << " of the"; + if (instance_location.empty()) { message << " instance"; } else { message << " instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - message << " was " << escape_string(this->annotation.to_string()); + message << " was " << escape_string(annotation.to_string()); return message.str(); } - if (this->keyword == "default") { + if (keyword == "default") { std::ostringstream message; message << "The default value of the"; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << " instance"; } else { message << " instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } message << " was "; - stringify(this->annotation, message); + stringify(annotation, message); return message.str(); } - if (this->keyword == "deprecated" && this->annotation.is_boolean()) { + if (keyword == "deprecated" && annotation.is_boolean()) { std::ostringstream message; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << "The instance"; } else { message << "The instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - if (this->annotation.to_boolean()) { + if (annotation.to_boolean()) { message << " was considered deprecated"; } else { message << " was not considered deprecated"; @@ -399,17 +408,17 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "readOnly" && this->annotation.is_boolean()) { + if (keyword == "readOnly" && annotation.is_boolean()) { std::ostringstream message; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << "The instance"; } else { message << "The instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - if (this->annotation.to_boolean()) { + if (annotation.to_boolean()) { message << " was considered read-only"; } else { message << " was not considered read-only"; @@ -418,17 +427,17 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "writeOnly" && this->annotation.is_boolean()) { + if (keyword == "writeOnly" && annotation.is_boolean()) { std::ostringstream message; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << "The instance"; } else { message << "The instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - if (this->annotation.to_boolean()) { + if (annotation.to_boolean()) { message << " was considered write-only"; } else { message << " was not considered write-only"; @@ -437,21 +446,21 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "examples") { - assert(this->annotation.is_array()); + if (keyword == "examples") { + assert(annotation.is_array()); std::ostringstream message; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << "Examples of the instance"; } else { message << "Examples of the instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } message << " were "; - for (auto iterator = this->annotation.as_array().cbegin(); - iterator != this->annotation.as_array().cend(); ++iterator) { - if (std::next(iterator) == this->annotation.as_array().cend()) { + for (auto iterator = annotation.as_array().cbegin(); + iterator != annotation.as_array().cend(); ++iterator) { + if (std::next(iterator) == annotation.as_array().cend()) { message << "and "; stringify(*iterator, message); } else { @@ -463,65 +472,65 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "contentEncoding") { - assert(this->annotation.is_string()); + if (keyword == "contentEncoding") { + assert(annotation.is_string()); std::ostringstream message; message << "The content encoding of the"; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << " instance"; } else { message << " instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - message << " was " << escape_string(this->annotation.to_string()); + message << " was " << escape_string(annotation.to_string()); return message.str(); } - if (this->keyword == "contentMediaType") { - assert(this->annotation.is_string()); + if (keyword == "contentMediaType") { + assert(annotation.is_string()); std::ostringstream message; message << "The content media type of the"; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << " instance"; } else { message << " instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } - message << " was " << escape_string(this->annotation.to_string()); + message << " was " << escape_string(annotation.to_string()); return message.str(); } - if (this->keyword == "contentSchema") { + if (keyword == "contentSchema") { std::ostringstream message; message << "When decoded, the"; - if (this->instance_location.empty()) { + if (instance_location.empty()) { message << " instance"; } else { message << " instance location \""; - stringify(this->instance_location, message); + stringify(instance_location, message); message << "\""; } message << " was expected to validate against the schema "; - stringify(this->annotation, message); + stringify(annotation, message); return message.str(); } std::ostringstream message; - message << "The unrecognized keyword " << escape_string(this->keyword) + message << "The unrecognized keyword " << escape_string(keyword) << " was collected as the annotation "; - stringify(this->annotation, message); + stringify(annotation, message); return message.str(); } - auto operator()(const AnnotationToParent &) const -> std::string { - if (this->keyword == "unevaluatedItems" && this->annotation.is_boolean() && - this->annotation.to_boolean()) { - assert(this->target.is_array()); + if (step.type == sourcemeta::blaze::InstructionIndex::AnnotationToParent) { + if (keyword == "unevaluatedItems" && annotation.is_boolean() && + annotation.to_boolean()) { + assert(target.is_array()); std::ostringstream message; message << "At least one item of the array value successfully validated " "against the subschema for unevaluated items"; @@ -531,42 +540,40 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AnnotationBasenameToParent &) const -> std::string { - if (this->keyword == "patternProperties") { - assert(this->annotation.is_string()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent) { + if (keyword == "patternProperties") { + assert(annotation.is_string()); std::ostringstream message; - message << "The object property " - << escape_string(this->annotation.to_string()) + message << "The object property " << escape_string(annotation.to_string()) << " successfully validated against its pattern property " "subschema"; return message.str(); } - if (this->keyword == "additionalProperties") { - assert(this->annotation.is_string()); + if (keyword == "additionalProperties") { + assert(annotation.is_string()); std::ostringstream message; - message << "The object property " - << escape_string(this->annotation.to_string()) + message << "The object property " << escape_string(annotation.to_string()) << " successfully validated against the additional properties " "subschema"; return message.str(); } - if (this->keyword == "unevaluatedProperties") { - assert(this->annotation.is_string()); + if (keyword == "unevaluatedProperties") { + assert(annotation.is_string()); std::ostringstream message; - message << "The object property " - << escape_string(this->annotation.to_string()) + message << "The object property " << escape_string(annotation.to_string()) << " successfully validated against the subschema for " "unevaluated properties"; return message.str(); } - if (this->keyword == "contains" && this->annotation.is_integer()) { - assert(this->target.is_array()); - assert(this->annotation.is_positive()); + if (keyword == "contains" && annotation.is_integer()) { + assert(target.is_array()); + assert(annotation.is_positive()); std::ostringstream message; - message << "The item at index " << this->annotation.to_integer() + message << "The item at index " << annotation.to_integer() << " of the array value successfully validated against the " "containment check subschema"; return message.str(); @@ -575,13 +582,22 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const LoopProperties &step) const -> std::string { - assert(this->keyword == "additionalProperties"); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopProperties) { + assert(keyword == "additionalProperties" || + keyword == "unevaluatedProperties"); std::ostringstream message; - if (step.children.size() == 1 && - std::holds_alternative(step.children.front())) { - message << "The object value was not expected to define additional " - "properties"; + if (!step.children.empty() && + step.children.front().type == InstructionIndex::AssertionFail) { + if (keyword == "unevaluatedProperties") { + message << "The object value was not expected to define unevaluated " + "properties"; + } else { + message << "The object value was not expected to define additional " + "properties"; + } + } else if (keyword == "unevaluatedProperties") { + message << "The object properties not covered by other object " + "keywords were expected to validate against this subschema"; } else { message << "The object properties not covered by other adjacent object " "keywords were expected to validate against this subschema"; @@ -590,11 +606,12 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopPropertiesEvaluate &step) const -> std::string { - assert(this->keyword == "additionalProperties"); + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesEvaluate) { + assert(keyword == "additionalProperties"); std::ostringstream message; if (step.children.size() == 1 && - std::holds_alternative(step.children.front())) { + step.children.front().type == InstructionIndex::AssertionFail) { message << "The object value was not expected to define additional " "properties"; } else { @@ -605,11 +622,12 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopPropertiesUnevaluated &step) const -> std::string { - if (this->keyword == "unevaluatedProperties") { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesUnevaluated) { + if (keyword == "unevaluatedProperties") { std::ostringstream message; if (!step.children.empty() && - std::holds_alternative(step.children.front())) { + step.children.front().type == InstructionIndex::AssertionFail) { message << "The object value was not expected to define unevaluated " "properties"; } else { @@ -623,12 +641,12 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const LoopPropertiesUnevaluatedExcept &step) const - -> std::string { - if (this->keyword == "unevaluatedProperties") { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesUnevaluatedExcept) { + if (keyword == "unevaluatedProperties") { std::ostringstream message; if (!step.children.empty() && - std::holds_alternative(step.children.front())) { + step.children.front().type == InstructionIndex::AssertionFail) { message << "The object value was not expected to define unevaluated " "properties"; } else { @@ -642,13 +660,22 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const LoopPropertiesExcept &step) const -> std::string { - assert(this->keyword == "additionalProperties"); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopPropertiesExcept) { + assert(keyword == "additionalProperties" || + keyword == "unevaluatedProperties"); std::ostringstream message; - if (step.children.size() == 1 && - std::holds_alternative(step.children.front())) { - message << "The object value was not expected to define additional " - "properties"; + if (!step.children.empty() && + step.children.front().type == InstructionIndex::AssertionFail) { + if (keyword == "unevaluatedProperties") { + message << "The object value was not expected to define unevaluated " + "properties"; + } else { + message << "The object value was not expected to define additional " + "properties"; + } + } else if (keyword == "unevaluatedProperties") { + message << "The object properties not covered by other object " + "keywords were expected to validate against this subschema"; } else { message << "The object properties not covered by other adjacent object " "keywords were expected to validate against this subschema"; @@ -657,45 +684,73 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopPropertiesWhitelist &) const -> std::string { - assert(this->keyword == "additionalProperties"); + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesWhitelist) { + assert(keyword == "additionalProperties"); return "The object value was not expected to define additional properties"; } - auto operator()(const LoopPropertiesType &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesExactlyTypeStrict) { + std::ostringstream message; + message << "The required object properties were expected to be of type " + << to_string(instruction_value(step).first); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex:: + LoopPropertiesExactlyTypeStrictHash) { + std::ostringstream message; + message << "The required object properties were expected to be of type " + << to_string(instruction_value(step).first); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex:: + LoopItemsPropertiesExactlyTypeStrictHash) { + std::ostringstream message; + message << "Every item in the array was expected to be an object whose " + "required properties were of type " + << to_string(instruction_value(step).first); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::LoopPropertiesType) { std::ostringstream message; message << "The object properties were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopPropertiesTypeEvaluate &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeEvaluate) { std::ostringstream message; message << "The object properties were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopPropertiesTypeStrict &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrict) { std::ostringstream message; message << "The object properties were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopPropertiesTypeStrictEvaluate &step) const - -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrictEvaluate) { std::ostringstream message; message << "The object properties were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopPropertiesTypeStrictAny &step) const - -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrictAny) { std::ostringstream message; message << "The object properties were expected to be of type "; - const auto &types{step_value(step)}; + const auto &types{instruction_value(step)}; for (auto iterator = types.cbegin(); iterator != types.cend(); ++iterator) { if (std::next(iterator) == types.cend()) { message << "or " << to_string(*iterator); @@ -707,11 +762,11 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopPropertiesTypeStrictAnyEvaluate &step) const - -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex:: + LoopPropertiesTypeStrictAnyEvaluate) { std::ostringstream message; message << "The object properties were expected to be of type "; - const auto &types{step_value(step)}; + const auto &types{instruction_value(step)}; for (auto iterator = types.cbegin(); iterator != types.cend(); ++iterator) { if (std::next(iterator) == types.cend()) { message << "or " << to_string(*iterator); @@ -723,24 +778,24 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopKeys &) const -> std::string { - assert(this->keyword == "propertyNames"); - assert(this->target.is_object()); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopKeys) { + assert(keyword == "propertyNames"); + assert(target.is_object()); std::ostringstream message; - if (this->target.size() == 0) { - assert(this->valid); + if (target.size() == 0) { + assert(valid); message << "The object is empty and no properties were expected to " "validate against the given subschema"; - } else if (this->target.size() == 1) { + } else if (target.size() == 1) { message << "The object property "; - message << escape_string(this->target.as_object().cbegin()->first); + message << escape_string(target.as_object().cbegin()->first); message << " was expected to validate against the given subschema"; } else { message << "The object properties "; - for (auto iterator = this->target.as_object().cbegin(); - iterator != this->target.as_object().cend(); ++iterator) { - if (std::next(iterator) == this->target.as_object().cend()) { + for (auto iterator = target.as_object().cbegin(); + iterator != target.as_object().cend(); ++iterator) { + if (std::next(iterator) == target.as_object().cend()) { message << "and " << escape_string(iterator->first); } else { message << escape_string(iterator->first) << ", "; @@ -753,9 +808,15 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopItems &step) const -> std::string { - assert(this->target.is_array()); - const auto &value{step_value(step)}; + if (step.type == sourcemeta::blaze::InstructionIndex::LoopItems) { + assert(target.is_array()); + return "Every item in the array value was expected to validate against the " + "given subschema"; + } + + if (step.type == sourcemeta::blaze::InstructionIndex::LoopItemsFrom) { + assert(target.is_array()); + const auto &value{instruction_value(step)}; std::ostringstream message; message << "Every item in the array value"; if (value == 1) { @@ -768,32 +829,33 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopItemsUnevaluated &) const -> std::string { - assert(this->keyword == "unevaluatedItems"); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopItemsUnevaluated) { + assert(keyword == "unevaluatedItems"); std::ostringstream message; message << "The array items not covered by other array keywords, if any, " "were expected to validate against this subschema"; return message.str(); } - auto operator()(const LoopItemsType &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LoopItemsType) { std::ostringstream message; message << "The array items were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopItemsTypeStrict &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LoopItemsTypeStrict) { std::ostringstream message; message << "The array items were expected to be of type " - << to_string(step.value); + << to_string(instruction_value(step)); return message.str(); } - auto operator()(const LoopItemsTypeStrictAny &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopItemsTypeStrictAny) { std::ostringstream message; message << "The array items were expected to be of type "; - const auto &types{step_value(step)}; + const auto &types{instruction_value(step)}; for (auto iterator = types.cbegin(); iterator != types.cend(); ++iterator) { if (std::next(iterator) == types.cend()) { message << "or " << to_string(*iterator); @@ -805,10 +867,10 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopContains &step) const -> std::string { - assert(this->target.is_array()); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopContains) { + assert(target.is_array()); std::ostringstream message; - const auto &value{step_value(step)}; + const auto &value{instruction_value(step)}; const auto minimum{std::get<0>(value)}; const auto maximum{std::get<1>(value)}; bool plural{true}; @@ -849,35 +911,51 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionDefines &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionDefines) { std::ostringstream message; message << "The object value was expected to define the property " - << escape_string(step_value(step)); + << escape_string(instruction_value(step).first); return message.str(); } - auto operator()(const AssertionDefinesAll &step) const -> std::string { - const auto &value{step_value(step)}; + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionDefinesStrict) { + std::ostringstream message; + message + << "The value was expected to be an object that defines the property " + << escape_string(instruction_value(step).first); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionDefinesAll) { + const auto &value{instruction_value(step)}; assert(value.size() > 1); + + std::vector value_vector; + for (const auto &entry : value) { + value_vector.push_back(entry.first); + } + std::ostringstream message; message << "The object value was expected to define properties "; - for (auto iterator = value.cbegin(); iterator != value.cend(); ++iterator) { - if (std::next(iterator) == value.cend()) { + for (auto iterator = value_vector.cbegin(); iterator != value_vector.cend(); + ++iterator) { + if (std::next(iterator) == value_vector.cend()) { message << "and " << escape_string(*iterator); } else { message << escape_string(*iterator) << ", "; } } - if (this->valid) { + if (valid) { return message.str(); } - assert(this->target.is_object()); + assert(target.is_object()); std::set missing; for (const auto &property : value) { - if (!this->target.defines(property)) { - missing.insert(property); + if (!target.defines(property.first, property.second)) { + missing.insert(property.first); } } @@ -900,52 +978,127 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionType &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionDefinesAllStrict) { + const auto &value{instruction_value(step)}; + assert(value.size() > 1); + + std::vector value_vector; + for (const auto &entry : value) { + value_vector.push_back(entry.first); + } + std::ostringstream message; - describe_type_check(this->valid, this->target.type(), step_value(step), - message); + message + << "The value was expected to be an object that defines properties "; + for (auto iterator = value_vector.cbegin(); iterator != value_vector.cend(); + ++iterator) { + if (std::next(iterator) == value_vector.cend()) { + message << "and " << escape_string(*iterator); + } else { + message << escape_string(*iterator) << ", "; + } + } + return message.str(); } - auto operator()(const AssertionTypeStrict &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionDefinesExactly) { + const auto &value{instruction_value(step)}; + assert(value.size() > 1); + std::vector value_vector; + for (const auto &entry : value) { + value_vector.push_back(entry.first); + } + + std::sort(value_vector.begin(), value_vector.end()); std::ostringstream message; - const auto &value{step_value(step)}; - if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && - this->target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { + message << "The object value was expected to only define properties "; + for (auto iterator = value_vector.cbegin(); iterator != value_vector.cend(); + ++iterator) { + if (std::next(iterator) == value_vector.cend()) { + message << "and " << escape_string(*iterator); + } else { + message << escape_string(*iterator) << ", "; + } + } + + return message.str(); + } + + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionDefinesExactlyStrict) { + const auto &value{instruction_value(step)}; + assert(value.size() > 1); + std::vector value_vector; + for (const auto &entry : value) { + value_vector.push_back(entry.first); + } + + std::sort(value_vector.begin(), value_vector.end()); + std::ostringstream message; + message << "The value was expected to be an object that only defines " + "properties "; + for (auto iterator = value_vector.cbegin(); iterator != value_vector.cend(); + ++iterator) { + if (std::next(iterator) == value_vector.cend()) { + message << "and " << escape_string(*iterator); + } else { + message << escape_string(*iterator) << ", "; + } + } + + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionType) { + std::ostringstream message; + describe_type_check(valid, target.type(), + instruction_value(step), message); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionTypeStrict) { + std::ostringstream message; + const auto &value{instruction_value(step)}; + if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && + target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { message << "The value was expected to be a real number but it was an integer"; - } else if (!this->valid && + } else if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Integer && - this->target.type() == - sourcemeta::jsontoolkit::JSON::Type::Real) { + target.type() == sourcemeta::jsontoolkit::JSON::Type::Real) { message << "The value was expected to be an integer but it was a real number"; } else { - describe_type_check(this->valid, this->target.type(), value, message); + describe_type_check(valid, target.type(), value, message); } return message.str(); } - auto operator()(const AssertionTypeAny &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionTypeAny) { std::ostringstream message; - describe_types_check(this->valid, this->target.type(), step_value(step), - message); + describe_types_check(valid, target.type(), + instruction_value(step), message); return message.str(); } - auto operator()(const AssertionTypeStrictAny &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny) { std::ostringstream message; - describe_types_check(this->valid, this->target.type(), step_value(step), - message); + describe_types_check(valid, target.type(), + instruction_value(step), message); return message.str(); } - auto operator()(const AssertionTypeStringBounded &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeStringBounded) { std::ostringstream message; - const auto minimum{std::get<0>(step.value)}; - const auto maximum{std::get<1>(step.value)}; + const auto minimum{std::get<0>(instruction_value(step))}; + const auto maximum{std::get<1>(instruction_value(step))}; if (minimum == 0 && maximum.has_value()) { message << "The value was expected to consist of a string of at most " << maximum.value() @@ -962,11 +1115,23 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionTypeArrayBounded &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeStringUpper) { std::ostringstream message; + message << "The value was expected to consist of a string of at most " + << instruction_value(step) + << (instruction_value(step) == 1 + ? " character" + : " characters"); + return message.str(); + } - const auto minimum{std::get<0>(step.value)}; - const auto maximum{std::get<1>(step.value)}; + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeArrayBounded) { + std::ostringstream message; + + const auto minimum{std::get<0>(instruction_value(step))}; + const auto maximum{std::get<1>(instruction_value(step))}; if (minimum == 0 && maximum.has_value()) { message << "The value was expected to consist of an array of at most " << maximum.value() << (maximum.value() == 1 ? " item" : " items"); @@ -982,11 +1147,22 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionTypeObjectBounded &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeArrayUpper) { + std::ostringstream message; + message << "The value was expected to consist of an array of at most " + << instruction_value(step) + << (instruction_value(step) == 1 ? " item" + : " items"); + return message.str(); + } + + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeObjectBounded) { std::ostringstream message; - const auto minimum{std::get<0>(step.value)}; - const auto maximum{std::get<1>(step.value)}; + const auto minimum{std::get<0>(instruction_value(step))}; + const auto maximum{std::get<1>(instruction_value(step))}; if (minimum == 0 && maximum.has_value()) { message << "The value was expected to consist of an object of at most " << maximum.value() @@ -1003,48 +1179,59 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionRegex &step) const -> std::string { - if (std::any_of(this->evaluate_path.cbegin(), this->evaluate_path.cend(), + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionTypeObjectUpper) { + std::ostringstream message; + message << "The value was expected to consist of an object of at most " + << instruction_value(step) + << (instruction_value(step) == 1 + ? " property" + : " properties"); + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionRegex) { + if (std::any_of(evaluate_path.cbegin(), evaluate_path.cend(), [](const auto &token) { return token.is_property() && token.to_property() == "propertyNames"; }) && - !this->instance_location.empty() && - this->instance_location.back().is_property()) { + !instance_location.empty() && instance_location.back().is_property()) { std::ostringstream message; message << "The property name " - << escape_string(this->instance_location.back().to_property()) + << escape_string(instance_location.back().to_property()) << " was expected to match the regular expression " - << escape_string(step_value(step).second); + << escape_string(instruction_value(step).second); return message.str(); } - assert(this->target.is_string()); + assert(target.is_string()); std::ostringstream message; - message << "The string value " << escape_string(this->target.to_string()) + message << "The string value " << escape_string(target.to_string()) << " was expected to match the regular expression " - << escape_string(step_value(step).second); + << escape_string(instruction_value(step).second); return message.str(); } - auto operator()(const AssertionStringSizeLess &step) const -> std::string { - if (this->keyword == "maxLength") { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionStringSizeLess) { + if (keyword == "maxLength") { std::ostringstream message; - const auto maximum{step_value(step) - 1}; + const auto maximum{instruction_value(step) - 1}; - if (is_within_keyword(this->evaluate_path, "propertyNames")) { - assert(this->instance_location.back().is_property()); + if (is_within_keyword(evaluate_path, "propertyNames")) { + assert(instance_location.back().is_property()); message << "The object property name " - << escape_string(this->instance_location.back().to_property()); + << escape_string(instance_location.back().to_property()); } else { message << "The string value "; - stringify(this->target, message); + stringify(target, message); } message << " was expected to consist of at most " << maximum << (maximum == 1 ? " character" : " characters"); - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; @@ -1052,14 +1239,14 @@ struct DescribeVisitor { message << " it consisted of "; - if (is_within_keyword(this->evaluate_path, "propertyNames")) { - message << this->instance_location.back().to_property().size(); - message << (this->instance_location.back().to_property().size() == 1 + if (is_within_keyword(evaluate_path, "propertyNames")) { + message << instance_location.back().to_property().size(); + message << (instance_location.back().to_property().size() == 1 ? " character" : " characters"); } else { - message << this->target.size(); - message << (this->target.size() == 1 ? " character" : " characters"); + message << target.size(); + message << (target.size() == 1 ? " character" : " characters"); } return message.str(); @@ -1068,24 +1255,25 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionStringSizeGreater &step) const -> std::string { - if (this->keyword == "minLength") { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionStringSizeGreater) { + if (keyword == "minLength") { std::ostringstream message; - const auto minimum{step_value(step) + 1}; + const auto minimum{instruction_value(step) + 1}; - if (is_within_keyword(this->evaluate_path, "propertyNames")) { - assert(this->instance_location.back().is_property()); + if (is_within_keyword(evaluate_path, "propertyNames")) { + assert(instance_location.back().is_property()); message << "The object property name " - << escape_string(this->instance_location.back().to_property()); + << escape_string(instance_location.back().to_property()); } else { message << "The string value "; - stringify(this->target, message); + stringify(target, message); } message << " was expected to consist of at least " << minimum << (minimum == 1 ? " character" : " characters"); - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; @@ -1093,14 +1281,14 @@ struct DescribeVisitor { message << " it consisted of "; - if (is_within_keyword(this->evaluate_path, "propertyNames")) { - message << this->instance_location.back().to_property().size(); - message << (this->instance_location.back().to_property().size() == 1 + if (is_within_keyword(evaluate_path, "propertyNames")) { + message << instance_location.back().to_property().size(); + message << (instance_location.back().to_property().size() == 1 ? " character" : " characters"); } else { - message << this->target.size(); - message << (this->target.size() == 1 ? " character" : " characters"); + message << target.size(); + message << (target.size() == 1 ? " character" : " characters"); } return message.str(); @@ -1109,11 +1297,12 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionArraySizeLess &step) const -> std::string { - if (this->keyword == "maxItems") { - assert(this->target.is_array()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionArraySizeLess) { + if (keyword == "maxItems") { + assert(target.is_array()); std::ostringstream message; - const auto maximum{step_value(step) - 1}; + const auto maximum{instruction_value(step) - 1}; message << "The array value was expected to contain at most " << maximum; assert(maximum > 0); if (maximum == 1) { @@ -1122,14 +1311,14 @@ struct DescribeVisitor { message << " items"; } - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; } - message << " it contained " << this->target.size(); - if (this->target.size() == 1) { + message << " it contained " << target.size(); + if (target.size() == 1) { message << " item"; } else { message << " items"; @@ -1141,10 +1330,11 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionArraySizeGreater &step) const -> std::string { - assert(this->target.is_array()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionArraySizeGreater) { + assert(target.is_array()); std::ostringstream message; - const auto minimum{step_value(step) + 1}; + const auto minimum{instruction_value(step) + 1}; message << "The array value was expected to contain at least " << minimum; assert(minimum > 0); if (minimum == 1) { @@ -1153,14 +1343,14 @@ struct DescribeVisitor { message << " items"; } - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; } - message << " it contained " << this->target.size(); - if (this->target.size() == 1) { + message << " it contained " << target.size(); + if (target.size() == 1) { message << " item"; } else { message << " items"; @@ -1169,11 +1359,17 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionObjectSizeLess &step) const -> std::string { - if (this->keyword == "maxProperties") { - assert(this->target.is_object()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionObjectSizeLess) { + if (keyword == "additionalProperties") { + return "The object value was not expected to define additional " + "properties"; + } + + if (keyword == "maxProperties") { + assert(target.is_object()); std::ostringstream message; - const auto maximum{step_value(step) - 1}; + const auto maximum{instruction_value(step) - 1}; message << "The object value was expected to contain at most " << maximum; assert(maximum > 0); if (maximum == 1) { @@ -1182,21 +1378,21 @@ struct DescribeVisitor { message << " properties"; } - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; } - message << " it contained " << this->target.size(); - if (this->target.size() == 1) { + message << " it contained " << target.size(); + if (target.size() == 1) { message << " property: "; - message << escape_string(this->target.as_object().cbegin()->first); + message << escape_string(target.as_object().cbegin()->first); } else { message << " properties: "; std::vector properties; - for (const auto &entry : this->target.as_object()) { + for (const auto &entry : target.as_object()) { properties.push_back(entry.first); } std::sort(properties.begin(), properties.end()); @@ -1217,11 +1413,12 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionObjectSizeGreater &step) const -> std::string { - if (this->keyword == "minProperties") { - assert(this->target.is_object()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionObjectSizeGreater) { + if (keyword == "minProperties") { + assert(target.is_object()); std::ostringstream message; - const auto minimum{step_value(step) + 1}; + const auto minimum{instruction_value(step) + 1}; message << "The object value was expected to contain at least " << minimum; assert(minimum > 0); @@ -1231,20 +1428,20 @@ struct DescribeVisitor { message << " properties"; } - if (this->valid) { + if (valid) { message << " and"; } else { message << " but"; } - message << " it contained " << this->target.size(); - if (this->target.size() == 1) { + message << " it contained " << target.size(); + if (target.size() == 1) { message << " property: "; - message << escape_string(this->target.as_object().cbegin()->first); + message << escape_string(target.as_object().cbegin()->first); } else { message << " properties: "; std::vector properties; - for (const auto &entry : this->target.as_object()) { + for (const auto &entry : target.as_object()) { properties.push_back(entry.first); } std::sort(properties.begin(), properties.end()); @@ -1265,74 +1462,74 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionEqual &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionEqual) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to equal the " << to_string(value.type()) << " constant "; stringify(value, message); return message.str(); } - auto operator()(const AssertionGreaterEqual &step) const { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionGreaterEqual) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to be greater than or equal to the " << to_string(value.type()) << " "; stringify(value, message); return message.str(); } - auto operator()(const AssertionLessEqual &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionLessEqual) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to be less than or equal to the " << to_string(value.type()) << " "; stringify(value, message); return message.str(); } - auto operator()(const AssertionGreater &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionGreater) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to be greater than the " << to_string(value.type()) << " "; stringify(value, message); - if (!this->valid && value == this->target) { + if (!valid && value == target) { message << ", but they were equal"; } return message.str(); } - auto operator()(const AssertionLess &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionLess) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to be less than the " << to_string(value.type()) << " "; stringify(value, message); - if (!this->valid && value == this->target) { + if (!valid && value == target) { message << ", but they were equal"; } return message.str(); } - auto operator()(const AssertionUnique &) const -> std::string { - assert(this->target.is_array()); - auto array{this->target.as_array()}; + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionUnique) { + assert(target.is_array()); + auto array{target.as_array()}; std::ostringstream message; - if (this->valid) { + if (valid) { message << "The array value was expected to not contain duplicate items"; } else { std::set duplicates; @@ -1369,22 +1566,22 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionDivisible &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionDivisible) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); message << " was expected to be divisible by the " << to_string(value.type()) << " "; stringify(value, message); return message.str(); } - auto operator()(const AssertionEqualsAny &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionEqualsAny) { std::ostringstream message; - const auto &value{step_value(step)}; - message << "The " << to_string(this->target.type()) << " value "; - stringify(this->target, message); + const auto &value{instruction_value(step)}; + message << "The " << to_string(target.type()) << " value "; + stringify(target, message); assert(!value.empty()); if (value.size() == 1) { @@ -1392,14 +1589,17 @@ struct DescribeVisitor { << to_string(value.cbegin()->type()) << " constant "; stringify(*(value.cbegin()), message); } else { - if (this->valid) { + if (valid) { message << " was expected to equal one of the " << value.size() << " declared values"; } else { message << " was expected to equal one of the following values: "; - for (auto iterator = value.cbegin(); iterator != value.cend(); + std::vector copy{value.cbegin(), + value.cend()}; + std::sort(copy.begin(), copy.end()); + for (auto iterator = copy.cbegin(); iterator != copy.cend(); ++iterator) { - if (std::next(iterator) == value.cend()) { + if (std::next(iterator) == copy.cend()) { message << "and "; stringify(*iterator, message); } else { @@ -1413,12 +1613,12 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionStringType &step) const -> std::string { - assert(this->target.is_string()); + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionStringType) { + assert(target.is_string()); std::ostringstream message; - message << "The string value " << escape_string(this->target.to_string()) + message << "The string value " << escape_string(target.to_string()) << " was expected to represent a valid"; - switch (step_value(step)) { + switch (instruction_value(step)) { case ValueStringType::URI: message << " URI"; break; @@ -1429,109 +1629,105 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionPropertyType &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionPropertyType) { std::ostringstream message; - const auto &value{step_value(step)}; - if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && - this->target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { + const auto &value{instruction_value(step)}; + if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && + target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { message << "The value was expected to be a real number but it was an integer"; - } else if (!this->valid && + } else if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Integer && - this->target.type() == - sourcemeta::jsontoolkit::JSON::Type::Real) { + target.type() == sourcemeta::jsontoolkit::JSON::Type::Real) { message << "The value was expected to be an integer but it was a real number"; } else { - describe_type_check(this->valid, this->target.type(), value, message); + describe_type_check(valid, target.type(), value, message); } return message.str(); } - auto operator()(const AssertionPropertyTypeEvaluate &step) const - -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeEvaluate) { std::ostringstream message; - const auto &value{step_value(step)}; - if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && - this->target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { + const auto &value{instruction_value(step)}; + if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && + target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { message << "The value was expected to be a real number but it was an integer"; - } else if (!this->valid && + } else if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Integer && - this->target.type() == - sourcemeta::jsontoolkit::JSON::Type::Real) { + target.type() == sourcemeta::jsontoolkit::JSON::Type::Real) { message << "The value was expected to be an integer but it was a real number"; } else { - describe_type_check(this->valid, this->target.type(), value, message); + describe_type_check(valid, target.type(), value, message); } return message.str(); } - auto operator()(const AssertionPropertyTypeStrict &step) const - -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeStrict) { std::ostringstream message; - const auto &value{step_value(step)}; - if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && - this->target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { + const auto &value{instruction_value(step)}; + if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && + target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { message << "The value was expected to be a real number but it was an integer"; - } else if (!this->valid && + } else if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Integer && - this->target.type() == - sourcemeta::jsontoolkit::JSON::Type::Real) { + target.type() == sourcemeta::jsontoolkit::JSON::Type::Real) { message << "The value was expected to be an integer but it was a real number"; } else { - describe_type_check(this->valid, this->target.type(), value, message); + describe_type_check(valid, target.type(), value, message); } return message.str(); } - auto operator()(const AssertionPropertyTypeStrictEvaluate &step) const - -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex:: + AssertionPropertyTypeStrictEvaluate) { std::ostringstream message; - const auto &value{step_value(step)}; - if (!this->valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && - this->target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { + const auto &value{instruction_value(step)}; + if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Real && + target.type() == sourcemeta::jsontoolkit::JSON::Type::Integer) { message << "The value was expected to be a real number but it was an integer"; - } else if (!this->valid && + } else if (!valid && value == sourcemeta::jsontoolkit::JSON::Type::Integer && - this->target.type() == - sourcemeta::jsontoolkit::JSON::Type::Real) { + target.type() == sourcemeta::jsontoolkit::JSON::Type::Real) { message << "The value was expected to be an integer but it was a real number"; } else { - describe_type_check(this->valid, this->target.type(), value, message); + describe_type_check(valid, target.type(), value, message); } return message.str(); } - auto operator()(const AssertionPropertyTypeStrictAny &step) const - -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeStrictAny) { std::ostringstream message; - describe_types_check(this->valid, this->target.type(), step_value(step), - message); + describe_types_check(valid, target.type(), + instruction_value(step), message); return message.str(); } - auto operator()(const AssertionPropertyTypeStrictAnyEvaluate &step) const - -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex:: + AssertionPropertyTypeStrictAnyEvaluate) { std::ostringstream message; - describe_types_check(this->valid, this->target.type(), step_value(step), - message); + describe_types_check(valid, target.type(), + instruction_value(step), message); return message.str(); } - auto operator()(const AssertionArrayPrefix &step) const -> std::string { - assert(this->keyword == "items" || this->keyword == "prefixItems"); + if (step.type == sourcemeta::blaze::InstructionIndex::AssertionArrayPrefix) { + assert(keyword == "items" || keyword == "prefixItems"); assert(!step.children.empty()); - assert(this->target.is_array()); + assert(target.is_array()); std::ostringstream message; message << "The first "; @@ -1545,11 +1741,11 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const AssertionArrayPrefixEvaluate &step) const - -> std::string { - assert(this->keyword == "items" || this->keyword == "prefixItems"); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionArrayPrefixEvaluate) { + assert(keyword == "items" || keyword == "prefixItems"); assert(!step.children.empty()); - assert(this->target.is_array()); + assert(target.is_array()); std::ostringstream message; message << "The first "; @@ -1563,9 +1759,9 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LoopPropertiesMatch &step) const -> std::string { + if (step.type == sourcemeta::blaze::InstructionIndex::LoopPropertiesMatch) { assert(!step.children.empty()); - assert(this->target.is_object()); + assert(target.is_object()); std::ostringstream message; message << "The object value was expected to validate against the "; if (step.children.size() == 1) { @@ -1577,43 +1773,73 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LogicalWhenDefines &step) const -> std::string { + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesMatchClosed) { + assert(!step.children.empty()); + assert(target.is_object()); std::ostringstream message; - message << "The object value defined the property \"" << step_value(step) - << "\""; + if (step.children.size() == 1) { + message << "The object value was expected to validate against the "; + message << "single defined property subschema"; + } else { + message + << "Every object value was expected to validate against one of the "; + message << step.children.size() << " defined properties subschemas"; + } + + return message.str(); + } + + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalWhenDefines) { + std::ostringstream message; + message << "The object value defined the property \"" + << instruction_value(step).first << "\""; return message.str(); } - auto operator()(const LoopPropertiesRegex &step) const -> std::string { - assert(this->target.is_object()); + if (step.type == sourcemeta::blaze::InstructionIndex::LoopPropertiesRegex) { + assert(target.is_object()); std::ostringstream message; message << "The object properties that match the regular expression \"" - << step_value(step).second + << instruction_value(step).second << "\" were expected to validate against the defined pattern " "property subschema"; return message.str(); } - auto operator()(const LoopPropertiesStartsWith &step) const -> std::string { - assert(this->target.is_object()); + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesRegexClosed) { + assert(target.is_object()); + std::ostringstream message; + message << "The object properties were expected to match the regular " + "expression \"" + << instruction_value(step).second + << "\" and validate against the defined pattern " + "property subschema"; + return message.str(); + } + + if (step.type == + sourcemeta::blaze::InstructionIndex::LoopPropertiesStartsWith) { + assert(target.is_object()); std::ostringstream message; message << "The object properties that start with the string \"" - << step_value(step) + << instruction_value(step) << "\" were expected to validate against the defined pattern " "property subschema"; return message.str(); } - auto operator()(const LogicalWhenType &step) const -> std::string { - if (this->keyword == "items") { + if (step.type == sourcemeta::blaze::InstructionIndex::LogicalWhenType) { + if (keyword == "items") { std::ostringstream message; - describe_type_check(this->valid, this->target.type(), step_value(step), - message); + describe_type_check(valid, target.type(), + instruction_value(step), message); return message.str(); } - if (this->keyword == "dependencies") { - assert(this->target.is_object()); + if (keyword == "dependencies") { + assert(target.is_object()); assert(!step.children.empty()); std::set present; @@ -1624,11 +1850,11 @@ struct DescribeVisitor { for (const auto &child : step.children) { // Schema - if (std::holds_alternative(child)) { - const auto &substep{std::get(child)}; - const auto &property{step_value(substep)}; + if (child.type == InstructionIndex::LogicalWhenDefines) { + const auto &substep{child}; + const auto &property{instruction_value(substep).first}; all_dependencies.insert(property); - if (!this->target.defines(property)) { + if (!target.defines(property)) { continue; } @@ -1637,16 +1863,16 @@ struct DescribeVisitor { // Properties } else { - assert(std::holds_alternative(child)); - const auto &substep{std::get(child)}; - - for (const auto &[property, dependencies] : substep.value) { - all_dependencies.insert(property); - if (this->target.defines(property)) { - present.insert(property); - present_with_properties.insert(property); - for (const auto &dependency : dependencies) { - if (this->valid || !this->target.defines(dependency)) { + assert(child.type == InstructionIndex::AssertionPropertyDependencies); + const auto &substep{child}; + + for (const auto &entry : std::get(substep.value)) { + all_dependencies.insert(entry.first); + if (target.defines(entry.first)) { + present.insert(entry.first); + present_with_properties.insert(entry.first); + for (const auto &dependency : entry.second) { + if (valid || !target.defines(dependency)) { required_properties.insert(dependency); } } @@ -1740,17 +1966,17 @@ struct DescribeVisitor { return message.str(); } - if (this->keyword == "dependentSchemas") { - assert(this->target.is_object()); + if (keyword == "dependentSchemas") { + assert(target.is_object()); assert(!step.children.empty()); std::set present; std::set all_dependencies; for (const auto &child : step.children) { - assert(std::holds_alternative(child)); - const auto &substep{std::get(child)}; - const auto &property{step_value(substep)}; + assert(child.type == InstructionIndex::LogicalWhenDefines); + const auto &substep{child}; + const auto &property{instruction_value(substep).first}; all_dependencies.insert(property); - if (!this->target.defines(property)) { + if (!target.defines(property)) { continue; } @@ -1805,19 +2031,19 @@ struct DescribeVisitor { return unknown(); } - auto operator()(const AssertionPropertyDependencies &step) const - -> std::string { - assert(this->target.is_object()); + if (step.type == + sourcemeta::blaze::InstructionIndex::AssertionPropertyDependencies) { + assert(target.is_object()); std::set present; std::set all_dependencies; std::set required; - for (const auto &[property, dependencies] : step.value) { - all_dependencies.insert(property); - if (this->target.defines(property)) { - present.insert(property); - for (const auto &dependency : dependencies) { - if (this->valid || !this->target.defines(dependency)) { + for (const auto &entry : instruction_value(step)) { + all_dependencies.insert(entry.first); + if (target.defines(entry.first)) { + present.insert(entry.first); + for (const auto &dependency : entry.second) { + if (valid || !target.defines(dependency)) { required.insert(dependency); } } @@ -1879,14 +2105,15 @@ struct DescribeVisitor { return message.str(); } - auto operator()(const LogicalWhenArraySizeGreater &step) const - -> std::string { - if (this->keyword == "additionalItems" || this->keyword == "items") { - assert(this->target.is_array()); + if (step.type == + sourcemeta::blaze::InstructionIndex::LogicalWhenArraySizeGreater) { + if (keyword == "additionalItems" || keyword == "items") { + assert(target.is_array()); std::ostringstream message; - if (this->target.size() > step_value(step)) { - const auto rest{this->target.size() - step_value(step)}; + if (target.size() > instruction_value(step)) { + const auto rest{target.size() - + instruction_value(step)}; message << "The array value contains " << rest << " additional" << (rest == 1 ? " item" : " items") << " not described by related keywords"; @@ -1901,35 +2128,7 @@ struct DescribeVisitor { return unknown(); } - // These steps are never described, at least not right now - - auto operator()(const ControlGroup &) const -> std::string { - return unknown(); - } - - auto operator()(const ControlGroupWhenDefines &) const -> std::string { - return unknown(); - } -}; - -} // namespace - -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 Template::value_type &step, - const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, - const sourcemeta::jsontoolkit::WeakPointer &instance_location, - const sourcemeta::jsontoolkit::JSON &instance, - const sourcemeta::jsontoolkit::JSON &annotation) -> std::string { - assert(evaluate_path.empty() || evaluate_path.back().is_property()); - return std::visit( - DescribeVisitor{ - valid, evaluate_path, - evaluate_path.empty() ? "" : evaluate_path.back().to_property(), - instance_location, get(instance, instance_location), annotation}, - step); + return unknown(); } } // namespace sourcemeta::blaze diff --git a/vendor/blaze/src/compiler/compile_helpers.h b/vendor/blaze/src/compiler/compile_helpers.h index 2f0459dc..8aef4651 100644 --- a/vendor/blaze/src/compiler/compile_helpers.h +++ b/vendor/blaze/src/compiler/compile_helpers.h @@ -13,9 +13,22 @@ namespace sourcemeta::blaze { -static const DynamicContext relative_dynamic_context{ - "", sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer}; +inline auto relative_dynamic_context() -> DynamicContext { + return {"", sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, false}; +} + +inline auto relative_dynamic_context(const DynamicContext &dynamic_context) + -> DynamicContext { + return {"", sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, + dynamic_context.property_as_target}; +} + +inline auto property_relative_dynamic_context() -> DynamicContext { + return {"", sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, true}; +} inline auto schema_resource_id(const Context &context, const std::string &resource) -> std::size_t { @@ -32,12 +45,12 @@ inline auto schema_resource_id(const Context &context, } // Instantiate a value-oriented step -template -auto make(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 { +inline auto make(const InstructionIndex type, const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const Value &value) + -> Instruction { return { + type, dynamic_context.keyword.empty() ? dynamic_context.base_schema_location : dynamic_context.base_schema_location.concat( @@ -45,21 +58,17 @@ auto make(const Context &context, const SchemaContext &schema_context, dynamic_context.base_instance_location, to_uri(schema_context.relative_pointer, schema_context.base).recompose(), schema_resource_id(context, schema_context.base.recompose()), - context.uses_dynamic_scopes, - context.mode != Mode::FastValidation || - !context.unevaluated_properties_schemas.empty() || - !context.unevaluated_items_schemas.empty(), - value}; + value, + {}}; } // Instantiate an applicator step -template -auto make(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, Template &&children) - -> Step { +inline auto make(const InstructionIndex type, const Context &context, + const SchemaContext &schema_context, + const DynamicContext &dynamic_context, Value &&value, + Instructions &&children) -> Instruction { return { + type, dynamic_context.keyword.empty() ? dynamic_context.base_schema_location : dynamic_context.base_schema_location.concat( @@ -67,38 +76,32 @@ auto make(const Context &context, const SchemaContext &schema_context, dynamic_context.base_instance_location, to_uri(schema_context.relative_pointer, schema_context.base).recompose(), schema_resource_id(context, schema_context.base.recompose()), - context.uses_dynamic_scopes, - context.mode != Mode::FastValidation || - !context.unevaluated_properties_schemas.empty() || - !context.unevaluated_items_schemas.empty(), std::move(value), std::move(children)}; } -template -auto unroll(const Step &step, - const sourcemeta::jsontoolkit::Pointer &base_instance_location = - sourcemeta::jsontoolkit::empty_pointer) -> Type { - assert(std::holds_alternative(step)); - return {std::get(step).relative_schema_location, - base_instance_location.concat( - std::get(step).relative_instance_location), - std::get(step).keyword_location, - std::get(step).schema_resource, - std::get(step).dynamic, - std::get(step).track, - std::get(step).value}; -} - -template -auto rephrase(const Step &step) -> Type { - return {step.relative_schema_location, +inline auto +unroll(const Instruction &step, + const sourcemeta::jsontoolkit::Pointer &base_instance_location = + sourcemeta::jsontoolkit::empty_pointer) -> Instruction { + return {step.type, + step.relative_schema_location, + base_instance_location.concat(step.relative_instance_location), + step.keyword_location, + step.schema_resource, + step.value, + {}}; +} + +inline auto rephrase(const InstructionIndex type, const Instruction &step) + -> Instruction { + return {type, + step.relative_schema_location, step.relative_instance_location, step.keyword_location, step.schema_resource, - step.dynamic, - step.track, - step.value}; + step.value, + {}}; } inline auto @@ -123,12 +126,12 @@ unsigned_integer_property(const sourcemeta::jsontoolkit::JSON &document, inline auto static_frame_entry(const Context &context, const SchemaContext &schema_context) - -> const sourcemeta::jsontoolkit::ReferenceFrameEntry & { - const auto type{sourcemeta::jsontoolkit::ReferenceType::Static}; + -> const sourcemeta::jsontoolkit::Frame::LocationsEntry & { const auto current{ to_uri(schema_context.relative_pointer, schema_context.base).recompose()}; - assert(context.frame.contains({type, current})); - return context.frame.at({type, current}); + const auto type{sourcemeta::jsontoolkit::ReferenceType::Static}; + assert(context.frame.locations().contains({type, current})); + return context.frame.locations().at({type, current}); } inline auto walk_subschemas(const Context &context, @@ -140,6 +143,7 @@ inline auto walk_subschemas(const Context &context, context.resolver, entry.dialect}; } +// TODO: Get rid of this given the new JSON Toolkit regex optimisations inline auto pattern_as_prefix(const std::string &pattern) -> std::optional { static const std::regex starts_with_regex{R"(^\^([a-zA-Z0-9-_/]+)$)"}; @@ -172,12 +176,14 @@ inline auto find_adjacent(const Context &context, to_uri(schema_context.relative_pointer.initial().concat({"$ref"}), schema_context.base) .recompose()}; - assert(context.frame.contains({reference_type, destination_uri})); + assert( + context.frame.locations().contains({reference_type, destination_uri})); const auto &destination{ - context.frame.at({reference_type, destination_uri})}; - assert(context.references.contains({reference_type, destination.pointer})); + context.frame.locations().at({reference_type, destination_uri})}; + assert(context.frame.references().contains( + {reference_type, destination.pointer})); const auto &reference{ - context.references.at({reference_type, destination.pointer})}; + context.frame.references().at({reference_type, destination.pointer})}; const auto keyword_uri{ sourcemeta::jsontoolkit::to_uri( sourcemeta::jsontoolkit::to_pointer(reference.fragment.value_or("")) @@ -195,14 +201,15 @@ inline auto find_adjacent(const Context &context, result; for (const auto &possible_keyword_uri : possible_keyword_uris) { - if (!context.frame.contains({sourcemeta::jsontoolkit::ReferenceType::Static, - possible_keyword_uri})) { + if (!context.frame.locations().contains( + {sourcemeta::jsontoolkit::ReferenceType::Static, + possible_keyword_uri})) { continue; } - const auto &frame_entry{ - context.frame.at({sourcemeta::jsontoolkit::ReferenceType::Static, - possible_keyword_uri})}; + const auto &frame_entry{context.frame.locations().at( + {sourcemeta::jsontoolkit::ReferenceType::Static, + possible_keyword_uri})}; const auto &subschema{ sourcemeta::jsontoolkit::get(context.root, frame_entry.pointer)}; const auto &subschema_vocabularies{sourcemeta::jsontoolkit::vocabularies( @@ -220,21 +227,39 @@ inline auto find_adjacent(const Context &context, return result; } -inline auto recursive_template_size(const Template &steps) -> std::size_t { +inline auto recursive_template_size(const Instructions &steps) -> std::size_t { std::size_t result{steps.size()}; for (const auto &variant : steps) { - std::visit( - [&result](const auto &step) { - if constexpr (requires { step.children; }) { - result += recursive_template_size(step.children); - } - }, - variant); + result += recursive_template_size(variant.children); } return result; } +inline auto make_property(const ValueString &property) -> ValueProperty { + static const sourcemeta::jsontoolkit::Hash hasher; + return {property, hasher(property)}; +} + +inline auto requires_evaluation(const Context &context, + const SchemaContext &schema_context) -> bool { + const auto &entry{static_frame_entry(context, schema_context)}; + for (const auto &unevaluated : context.unevaluated) { + if (unevaluated.second.unresolved || + unevaluated.second.dynamic_dependencies.contains(entry.pointer)) { + return true; + } + + for (const auto &dependency : unevaluated.second.dynamic_dependencies) { + if (dependency.starts_with(entry.pointer)) { + return true; + } + } + } + + return false; +} + } // namespace sourcemeta::blaze #endif diff --git a/vendor/blaze/src/compiler/compile_json.cc b/vendor/blaze/src/compiler/compile_json.cc deleted file mode 100644 index 3bc9ee35..00000000 --- a/vendor/blaze/src/compiler/compile_json.cc +++ /dev/null @@ -1,344 +0,0 @@ -#include - -#include // assert -#include // std::less -#include // std::map -#include // std::ostringstream -#include // std::string_view -#include // std::is_same_v -#include // std::move - -namespace { - -template -auto value_to_json(const T &value) -> sourcemeta::jsontoolkit::JSON { - using namespace sourcemeta::blaze; - sourcemeta::jsontoolkit::JSON result{ - sourcemeta::jsontoolkit::JSON::make_object()}; - result.assign("category", sourcemeta::jsontoolkit::JSON{"value"}); - 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) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"boolean"}); - result.assign("value", sourcemeta::jsontoolkit::JSON{value}); - return result; - } 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) { - 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) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"types"}); - sourcemeta::jsontoolkit::JSON types{ - sourcemeta::jsontoolkit::JSON::make_array()}; - for (const auto type : value) { - std::ostringstream type_string; - type_string << type; - types.push_back(sourcemeta::jsontoolkit::JSON{type_string.str()}); - } - - result.assign("value", std::move(types)); - return result; - } 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) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"strings"}); - sourcemeta::jsontoolkit::JSON items{ - sourcemeta::jsontoolkit::JSON::make_array()}; - for (const auto &item : value) { - items.push_back(sourcemeta::jsontoolkit::JSON{item}); - } - - result.assign("value", std::move(items)); - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"array"}); - sourcemeta::jsontoolkit::JSON items{ - sourcemeta::jsontoolkit::JSON::make_array()}; - for (const auto &item : value) { - items.push_back(item); - } - - result.assign("value", std::move(items)); - return result; - } 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) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"range"}); - sourcemeta::jsontoolkit::JSON values{ - sourcemeta::jsontoolkit::JSON::make_array()}; - const auto &range{value}; - values.push_back(sourcemeta::jsontoolkit::JSON{std::get<0>(range)}); - values.push_back( - std::get<1>(range).has_value() - ? sourcemeta::jsontoolkit::JSON{std::get<1>(range).value()} - : sourcemeta::jsontoolkit::JSON{nullptr}); - 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) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"named-indexes"}); - sourcemeta::jsontoolkit::JSON values{ - sourcemeta::jsontoolkit::JSON::make_object()}; - for (const auto &[name, index] : value) { - values.assign(name, sourcemeta::jsontoolkit::JSON{index}); - } - - result.assign("value", std::move(values)); - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"string-map"}); - sourcemeta::jsontoolkit::JSON map{ - sourcemeta::jsontoolkit::JSON::make_object()}; - for (const auto &[string, strings] : value) { - sourcemeta::jsontoolkit::JSON dependencies{ - sourcemeta::jsontoolkit::JSON::make_array()}; - for (const auto &substring : strings) { - dependencies.push_back(sourcemeta::jsontoolkit::JSON{substring}); - } - - map.assign(string, std::move(dependencies)); - } - - result.assign("value", std::move(map)); - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"property-filter"}); - sourcemeta::jsontoolkit::JSON data{ - sourcemeta::jsontoolkit::JSON::make_object()}; - data.assign("names", sourcemeta::jsontoolkit::JSON::make_array()); - data.assign("prefixes", sourcemeta::jsontoolkit::JSON::make_array()); - data.assign("patterns", sourcemeta::jsontoolkit::JSON::make_array()); - - for (const auto &name : std::get<0>(value)) { - data.at("names").push_back(sourcemeta::jsontoolkit::JSON{name}); - } - - for (const auto &prefix : std::get<1>(value)) { - data.at("prefixes").push_back(sourcemeta::jsontoolkit::JSON{prefix}); - } - - for (const auto &pattern : std::get<2>(value)) { - data.at("patterns") - .push_back(sourcemeta::jsontoolkit::JSON{pattern.second}); - } - - result.assign("value", std::move(data)); - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"string-type"}); - switch (value) { - case ValueStringType::URI: - result.assign("value", sourcemeta::jsontoolkit::JSON{"uri"}); - break; - default: - // We should never get here - assert(false); - } - - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"index-pair"}); - sourcemeta::jsontoolkit::JSON data{ - sourcemeta::jsontoolkit::JSON::make_array()}; - data.push_back(sourcemeta::jsontoolkit::JSON{value.first}); - data.push_back(sourcemeta::jsontoolkit::JSON{value.second}); - result.assign("value", std::move(data)); - return result; - } else if constexpr (std::is_same_v) { - result.assign("type", sourcemeta::jsontoolkit::JSON{"pointer"}); - result.assign("value", sourcemeta::jsontoolkit::JSON{ - sourcemeta::jsontoolkit::to_string(value)}); - return result; - } else { - static_assert(std::is_same_v); - return sourcemeta::jsontoolkit::JSON{nullptr}; - } -} - -template -auto step_to_json(const sourcemeta::blaze::Template::value_type &step) - -> sourcemeta::jsontoolkit::JSON { - static V visitor; - return std::visit(visitor, step); -} - -template -auto encode_step(const std::string_view category, const std::string_view type, - const T &step) -> sourcemeta::jsontoolkit::JSON { - sourcemeta::jsontoolkit::JSON result{ - sourcemeta::jsontoolkit::JSON::make_object()}; - result.assign("category", sourcemeta::jsontoolkit::JSON{category}); - result.assign("type", sourcemeta::jsontoolkit::JSON{type}); - result.assign( - "relativeSchemaLocation", - sourcemeta::jsontoolkit::JSON{to_string(step.relative_schema_location)}); - result.assign("relativeInstanceLocation", - sourcemeta::jsontoolkit::JSON{ - to_string(step.relative_instance_location)}); - result.assign("absoluteKeywordLocation", - sourcemeta::jsontoolkit::JSON{step.keyword_location}); - result.assign("schemaResource", - sourcemeta::jsontoolkit::JSON{step.schema_resource}); - result.assign("dynamic", sourcemeta::jsontoolkit::JSON{step.dynamic}); - result.assign("track", sourcemeta::jsontoolkit::JSON{step.track}); - result.assign("value", value_to_json(step.value)); - - if constexpr (requires { step.children; }) { - result.assign("children", sourcemeta::jsontoolkit::JSON::make_array()); - for (const auto &child : step.children) { - result.at("children").push_back(step_to_json(child)); - } - } - - return result; -} - -struct StepVisitor { -#define HANDLE_STEP(category, type, name) \ - auto operator()(const sourcemeta::blaze::name &step) \ - const->sourcemeta::jsontoolkit::JSON { \ - return encode_step(category, type, step); \ - } - - HANDLE_STEP("assertion", "fail", AssertionFail) - HANDLE_STEP("assertion", "defines", AssertionDefines) - HANDLE_STEP("assertion", "defines-all", AssertionDefinesAll) - HANDLE_STEP("assertion", "property-dependencies", - 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-evaluate", - AssertionPropertyTypeEvaluate) - HANDLE_STEP("assertion", "property-type-strict", AssertionPropertyTypeStrict) - HANDLE_STEP("assertion", "property-type-strict-evaluate", - AssertionPropertyTypeStrictEvaluate) - HANDLE_STEP("assertion", "property-type-strict-any", - AssertionPropertyTypeStrictAny) - HANDLE_STEP("assertion", "property-type-strict-any-evaluate", - AssertionPropertyTypeStrictAnyEvaluate) - HANDLE_STEP("assertion", "array-prefix", AssertionArrayPrefix) - HANDLE_STEP("assertion", "array-prefix-evaluate", - AssertionArrayPrefixEvaluate) - HANDLE_STEP("assertion", "equals-any", AssertionEqualsAny) - HANDLE_STEP("annotation", "emit", AnnotationEmit) - HANDLE_STEP("annotation", "to-parent", AnnotationToParent) - HANDLE_STEP("annotation", "basename-to-parent", AnnotationBasenameToParent) - HANDLE_STEP("logical", "not", LogicalNot) - HANDLE_STEP("logical", "not-evaluate", LogicalNotEvaluate) - 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("loop", "properties-unevaluated", LoopPropertiesUnevaluated) - HANDLE_STEP("loop", "properties-unevaluated-except", - LoopPropertiesUnevaluatedExcept) - HANDLE_STEP("loop", "properties-match", LoopPropertiesMatch) - HANDLE_STEP("loop", "properties", LoopProperties) - HANDLE_STEP("loop", "properties-evaluate", LoopPropertiesEvaluate) - HANDLE_STEP("loop", "properties-regex", LoopPropertiesRegex) - HANDLE_STEP("loop", "properties-starts-with", LoopPropertiesStartsWith) - HANDLE_STEP("loop", "properties-except", LoopPropertiesExcept) - HANDLE_STEP("loop", "properties-whitelist", LoopPropertiesWhitelist) - HANDLE_STEP("loop", "properties-type", LoopPropertiesType) - HANDLE_STEP("loop", "properties-type-evaluate", LoopPropertiesTypeEvaluate) - HANDLE_STEP("loop", "properties-type-strict", LoopPropertiesTypeStrict) - HANDLE_STEP("loop", "properties-type-strict-evaluate", - LoopPropertiesTypeStrictEvaluate) - HANDLE_STEP("loop", "properties-type-strict-any", LoopPropertiesTypeStrictAny) - HANDLE_STEP("loop", "properties-type-strict-evaluate-any", - LoopPropertiesTypeStrictAnyEvaluate) - HANDLE_STEP("loop", "keys", LoopKeys) - HANDLE_STEP("loop", "items", LoopItems) - HANDLE_STEP("loop", "items-unevaluated", LoopItemsUnevaluated) - HANDLE_STEP("loop", "items-type", LoopItemsType) - HANDLE_STEP("loop", "items-type-strict", LoopItemsTypeStrict) - HANDLE_STEP("loop", "items-type-strict-any", LoopItemsTypeStrictAny) - HANDLE_STEP("loop", "contains", LoopContains) - HANDLE_STEP("control", "group", ControlGroup) - HANDLE_STEP("control", "group-when-defines", ControlGroupWhenDefines) - HANDLE_STEP("control", "label", ControlLabel) - HANDLE_STEP("control", "mark", ControlMark) - HANDLE_STEP("control", "evaluate", ControlEvaluate) - HANDLE_STEP("control", "jump", ControlJump) - HANDLE_STEP("control", "dynamic-anchor-jump", ControlDynamicAnchorJump) - -#undef HANDLE_STEP -}; - -} // namespace - -namespace sourcemeta::blaze { - -auto to_json(const Template &steps) -> sourcemeta::jsontoolkit::JSON { - sourcemeta::jsontoolkit::JSON result{ - sourcemeta::jsontoolkit::JSON::make_array()}; - for (const auto &step : steps) { - result.push_back(step_to_json(step)); - } - - return result; -} - -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, - sourcemeta::jsontoolkit::JSON::Allocator>>; - static Rank rank{{"category", 0}, - {"type", 1}, - {"value", 2}, - {"schemaResource", 3}, - {"absoluteKeywordLocation", 4}, - {"relativeSchemaLocation", 5}, - {"relativeInstanceLocation", 6}, - {"evaluatePath", 7}, - {"dynamic", 8}, - {"track", 9}, - {"children", 10}}; - - constexpr std::uint64_t DEFAULT_RANK{999}; - const auto left_rank{rank.contains(left) ? rank.at(left) : DEFAULT_RANK}; - const auto right_rank{rank.contains(right) ? rank.at(right) : DEFAULT_RANK}; - return left_rank < right_rank; -} - -} // namespace sourcemeta::blaze diff --git a/vendor/blaze/src/compiler/compile_output_error.cc b/vendor/blaze/src/compiler/compile_output_error.cc index bc09221d..64eab550 100644 --- a/vendor/blaze/src/compiler/compile_output_error.cc +++ b/vendor/blaze/src/compiler/compile_output_error.cc @@ -26,8 +26,7 @@ auto ErrorOutput::cbegin() const -> const_iterator { auto ErrorOutput::cend() const -> const_iterator { return this->output.cend(); } auto ErrorOutput::operator()( - const EvaluationType type, const bool result, - const Template::value_type &step, + const EvaluationType type, const bool result, const Instruction &step, const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, const sourcemeta::jsontoolkit::WeakPointer &instance_location, const sourcemeta::jsontoolkit::JSON &annotation) -> void { diff --git a/vendor/blaze/src/compiler/compile_output_trace.cc b/vendor/blaze/src/compiler/compile_output_trace.cc index 435b81a1..e10815df 100644 --- a/vendor/blaze/src/compiler/compile_output_trace.cc +++ b/vendor/blaze/src/compiler/compile_output_trace.cc @@ -5,40 +5,12 @@ #include // std::move #include // std::visit -#ifdef __clang__ -#include // abi::__cxa_demangle -#include // std::free -static auto step_name(const sourcemeta::blaze::Template::value_type &step) - -> std::string { - return std::visit( - [](const auto &value) { - int status; - std::string name{typeid(value).name()}; - char *demangled = - abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status); - if (demangled) { - name = demangled; - std::free(demangled); - } - - return name; - }, - step); -} -#elif defined(_MSC_VER) -static auto step_name(const sourcemeta::blaze::Template::value_type &step) - -> std::string { - return std::visit( - [](const auto &value) { return std::string{typeid(value).name()}; }, - step); -} -#else -static auto step_name(const sourcemeta::blaze::Template::value_type &) - -> std::string { - // TODO: Properly implement for GCC - return "????"; +static auto step_name(const sourcemeta::blaze::Instruction &instruction) + -> std::string_view { + return sourcemeta::blaze::InstructionNames + [static_cast>( + instruction.type)]; } -#endif namespace sourcemeta::blaze { @@ -58,40 +30,26 @@ auto TraceOutput::cbegin() const -> const_iterator { auto TraceOutput::cend() const -> const_iterator { return this->output.cend(); } auto TraceOutput::operator()( - const EvaluationType type, const bool result, - const Template::value_type &step, + const EvaluationType type, const bool result, const Instruction &step, const sourcemeta::jsontoolkit::WeakPointer &evaluate_path, const sourcemeta::jsontoolkit::WeakPointer &instance_location, const sourcemeta::jsontoolkit::JSON &) -> void { -#if defined(_MSC_VER) - const std::string step_prefix{"struct sourcemeta::blaze::"}; -#else - const std::string step_prefix{"sourcemeta::blaze::"}; -#endif - - const auto full_step_name{step_name(step)}; - const auto short_step_name{full_step_name.starts_with(step_prefix) - ? full_step_name.substr(step_prefix.size()) - : full_step_name}; - + const auto short_step_name{step_name(step)}; auto effective_evaluate_path{evaluate_path.resolve_from(this->base_)}; - auto keyword_location{std::visit( - [](const auto &value) { return value.keyword_location; }, step)}; - if (type == EvaluationType::Pre) { this->output.push_back({EntryType::Push, short_step_name, instance_location, std::move(effective_evaluate_path), - std::move(keyword_location)}); + step.keyword_location}); } else if (result) { this->output.push_back({EntryType::Pass, short_step_name, instance_location, std::move(effective_evaluate_path), - std::move(keyword_location)}); + step.keyword_location}); } else { this->output.push_back({EntryType::Fail, short_step_name, instance_location, std::move(effective_evaluate_path), - std::move(keyword_location)}); + step.keyword_location}); } } diff --git a/vendor/blaze/src/compiler/default_compiler.cc b/vendor/blaze/src/compiler/default_compiler.cc index 1c77ece5..0d5befd8 100644 --- a/vendor/blaze/src/compiler/default_compiler.cc +++ b/vendor/blaze/src/compiler/default_compiler.cc @@ -13,10 +13,12 @@ auto sourcemeta::blaze::default_schema_compiler( const sourcemeta::blaze::Context &context, const sourcemeta::blaze::SchemaContext &schema_context, - const sourcemeta::blaze::DynamicContext &dynamic_context) - -> sourcemeta::blaze::Template { + const sourcemeta::blaze::DynamicContext &dynamic_context, + const sourcemeta::blaze::Instructions ¤t) + -> sourcemeta::blaze::Instructions { assert(!dynamic_context.keyword.empty()); + // TODO: Support HyperSchema in Draft 7 and earlier static std::set SUPPORTED_VOCABULARIES{ "https://json-schema.org/draft/2020-12/vocab/core", "https://json-schema.org/draft/2020-12/vocab/applicator", @@ -31,6 +33,7 @@ auto sourcemeta::blaze::default_schema_compiler( "https://json-schema.org/draft/2019-09/vocab/meta-data", "https://json-schema.org/draft/2019-09/vocab/format", "https://json-schema.org/draft/2019-09/vocab/content", + "https://json-schema.org/draft/2019-09/vocab/hyper-schema", "http://json-schema.org/draft-07/schema#", "http://json-schema.org/draft-06/schema#", "http://json-schema.org/draft-04/schema#"}; @@ -47,7 +50,8 @@ auto sourcemeta::blaze::default_schema_compiler( #define COMPILE(vocabulary, _keyword, handler) \ if (schema_context.vocabularies.contains(vocabulary) && \ dynamic_context.keyword == (_keyword)) { \ - return internal::handler(context, schema_context, dynamic_context); \ + return internal::handler(context, schema_context, dynamic_context, \ + current); \ } #define STOP_IF_SIBLING_KEYWORD(vocabulary, _keyword) \ @@ -515,7 +519,7 @@ auto sourcemeta::blaze::default_schema_compiler( } return internal::compiler_2019_09_core_annotation(context, schema_context, - dynamic_context); + dynamic_context, current); } return {}; diff --git a/vendor/blaze/src/compiler/default_compiler_2019_09.h b/vendor/blaze/src/compiler/default_compiler_2019_09.h index 734c87e6..17c526d7 100644 --- a/vendor/blaze/src/compiler/default_compiler_2019_09.h +++ b/vendor/blaze/src/compiler/default_compiler_2019_09.h @@ -11,7 +11,8 @@ using namespace sourcemeta::blaze; auto compiler_2019_09_applicator_dependentschemas( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_object()); if (schema_context.schema.defines("type") && @@ -20,7 +21,7 @@ auto compiler_2019_09_applicator_dependentschemas( return {}; } - Template children; + Instructions children; // To guarantee order std::vector dependents; @@ -38,23 +39,27 @@ auto compiler_2019_09_applicator_dependentschemas( } if (!dependency.is_boolean() || !dependency.to_boolean()) { - children.push_back(make( - context, schema_context, relative_dynamic_context, - ValueString{dependent}, - compile(context, schema_context, relative_dynamic_context, - {dependent}, sourcemeta::jsontoolkit::empty_pointer))); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::LogicalWhenDefines, context, + schema_context, relative_dynamic_context(dynamic_context), + make_property(dependent), + compile(context, schema_context, + relative_dynamic_context(dynamic_context), {dependent}, + sourcemeta::jsontoolkit::empty_pointer))); } } // TODO: Is this wrapper really necessary? - return {make(context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Object, - std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LogicalWhenType, context, + schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Object, + std::move(children))}; } auto compiler_2019_09_validation_dependentrequired( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { if (!schema_context.schema.at(dynamic_context.keyword).is_object()) { return {}; } @@ -79,7 +84,7 @@ auto compiler_2019_09_validation_dependentrequired( } if (!properties.empty()) { - dependencies.emplace(entry.first, std::move(properties)); + dependencies.assign(entry.first, std::move(properties)); } } @@ -87,24 +92,25 @@ auto compiler_2019_09_validation_dependentrequired( return {}; } - return {make( - context, schema_context, dynamic_context, std::move(dependencies))}; + return { + make(sourcemeta::blaze::InstructionIndex::AssertionPropertyDependencies, + context, schema_context, dynamic_context, std::move(dependencies))}; } auto compiler_2019_09_core_annotation(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON{ - schema_context.schema.at(dynamic_context.keyword)})}; + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { + return {make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context, + schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON{ + schema_context.schema.at(dynamic_context.keyword)})}; } auto compiler_2019_09_applicator_contains_with_options( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context, const bool annotate, - const bool track_evaluation) -> Template { + const DynamicContext &dynamic_context, const Instructions &, + const bool annotate, const bool track_evaluation) -> Instructions { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "array") { @@ -136,21 +142,24 @@ auto compiler_2019_09_applicator_contains_with_options( } if (maximum.has_value() && minimum > maximum.value()) { - return {make(context, schema_context, dynamic_context, - ValueNone{})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionFail, context, + schema_context, dynamic_context, ValueNone{})}; } if (minimum == 0 && !maximum.has_value()) { return {}; } - Template children{compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Instructions children{compile(context, schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (annotate) { - children.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent, + context, schema_context, relative_dynamic_context(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 @@ -159,219 +168,282 @@ auto compiler_2019_09_applicator_contains_with_options( } if (track_evaluation) { - children.push_back(make( - context, schema_context, relative_dynamic_context, ValuePointer{})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, relative_dynamic_context(dynamic_context), + ValuePointer{})); } if (children.empty()) { // We still need to check the instance is not empty - return {make( - context, schema_context, dynamic_context, ValueUnsignedInteger{0})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionArraySizeGreater, + context, schema_context, dynamic_context, + ValueUnsignedInteger{0})}; } - return {make( - context, schema_context, dynamic_context, - ValueRange{minimum, maximum, annotate || track_evaluation}, - std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopContains, context, + schema_context, dynamic_context, + ValueRange{minimum, maximum, annotate || track_evaluation}, + std::move(children))}; } auto compiler_2019_09_applicator_contains(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions ¤t) + -> Instructions { return compiler_2019_09_applicator_contains_with_options( - context, schema_context, dynamic_context, false, false); + context, schema_context, dynamic_context, current, false, false); } auto compiler_2019_09_applicator_additionalproperties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + + -> Instructions { return compiler_draft4_applicator_additionalproperties_with_options( context, schema_context, dynamic_context, context.mode == Mode::Exhaustive, - !context.unevaluated_properties_schemas.empty()); + requires_evaluation(context, schema_context)); } auto compiler_2019_09_applicator_items(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; + + if (schema_context.schema.at(dynamic_context.keyword).is_array()) { + return compiler_draft4_applicator_items_with_options( + context, schema_context, dynamic_context, + context.mode == Mode::Exhaustive, track); + } + return compiler_draft4_applicator_items_with_options( context, schema_context, dynamic_context, context.mode == Mode::Exhaustive, - !context.unevaluated_items_schemas.empty()); + track && !schema_context.schema.defines("unevaluatedItems")); } auto compiler_2019_09_applicator_additionalitems( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; + return compiler_draft4_applicator_additionalitems_with_options( context, schema_context, dynamic_context, context.mode == Mode::Exhaustive, - !context.unevaluated_items_schemas.empty()); + track && !schema_context.schema.defines("unevaluatedItems")); } auto compiler_2019_09_applicator_unevaluateditems( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "array") { return {}; } - if (!context.unevaluated_items_schemas.contains( - static_frame_entry(context, schema_context).pointer.initial())) { - return {}; + const auto current_uri{ + to_uri(schema_context.relative_pointer, schema_context.base).recompose()}; + assert(context.unevaluated.contains(current_uri)); + const auto &dependencies{context.unevaluated.at(current_uri)}; + + for (const auto &dependency : dependencies.static_dependencies) { + assert(!dependency.empty()); + assert(dependency.back().is_property()); + const auto &keyword{dependency.back().to_property()}; + const auto &subschema{ + sourcemeta::jsontoolkit::get(context.root, dependency)}; + if (keyword == "items" && sourcemeta::jsontoolkit::is_schema(subschema)) { + return {}; + } else if (keyword == "additionalItems" || keyword == "unevaluatedItems") { + return {}; + } } - Template children{compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Instructions children{compile(context, schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (context.mode == Mode::Exhaustive) { - children.push_back(make( - context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::JSON{true})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationToParent, context, + schema_context, relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::JSON{true})); } if (children.empty()) { - return {make(context, schema_context, dynamic_context, - ValuePointer{})}; + if (dependencies.dynamic_dependencies.empty() && !dependencies.unresolved && + !requires_evaluation(context, schema_context)) { + return {}; + } + + return {make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, dynamic_context, ValuePointer{})}; } // TODO: Attempt to short-circuit evaluation tracking by looking at sibling // and adjacent keywords like we do for `unevaluatedProperties` - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopItemsUnevaluated, + context, schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } auto compiler_2019_09_applicator_unevaluatedproperties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "object") { return {}; } - if (!context.unevaluated_properties_schemas.contains( - static_frame_entry(context, schema_context).pointer.initial())) { - return {}; - } - - Template children{compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Instructions children{compile(context, schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (context.mode == Mode::Exhaustive) { - children.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent, + context, schema_context, relative_dynamic_context(dynamic_context), + ValueNone{})); } - ValueStrings filter_strings; + ValueStringSet filter_strings; ValueStrings filter_prefixes; std::vector filter_regexes; - for (const auto &entry : find_adjacent( - context, schema_context, - {"https://json-schema.org/draft/2019-09/vocab/applicator", - "https://json-schema.org/draft/2020-12/vocab/applicator"}, - "properties", sourcemeta::jsontoolkit::JSON::Type::Object)) { - for (const auto &property : entry.get().as_object()) { - filter_strings.push_back(property.first); + const auto current_uri{ + to_uri(schema_context.relative_pointer, schema_context.base).recompose()}; + assert(context.unevaluated.contains(current_uri)); + const auto &dependencies{context.unevaluated.at(current_uri)}; + + for (const auto &dependency : dependencies.static_dependencies) { + assert(!dependency.empty()); + assert(dependency.back().is_property()); + const auto &keyword{dependency.back().to_property()}; + const auto &subschema{ + sourcemeta::jsontoolkit::get(context.root, dependency)}; + if (keyword == "properties") { + if (subschema.is_object()) { + for (const auto &property : subschema.as_object()) { + filter_strings.insert(property.first); + } + } + } else if (keyword == "patternProperties") { + if (subschema.is_object()) { + for (const auto &property : subschema.as_object()) { + const auto maybe_prefix{pattern_as_prefix(property.first)}; + if (maybe_prefix.has_value()) { + filter_prefixes.push_back(maybe_prefix.value()); + } else { + filter_regexes.push_back( + {parse_regex(property.first, schema_context.base, + schema_context.relative_pointer.initial().concat( + {"patternProperties"})), + property.first}); + } + } + } + } else if (keyword == "additionalProperties" || + keyword == "unevaluatedProperties") { + return {}; } } - for (const auto &entry : find_adjacent( - context, schema_context, - {"https://json-schema.org/draft/2019-09/vocab/applicator", - "https://json-schema.org/draft/2020-12/vocab/applicator"}, - "patternProperties", sourcemeta::jsontoolkit::JSON::Type::Object)) { - for (const auto &property : entry.get().as_object()) { - const auto maybe_prefix{pattern_as_prefix(property.first)}; - if (maybe_prefix.has_value()) { - filter_prefixes.push_back(maybe_prefix.value()); - } else { - filter_regexes.push_back( - {parse_regex(property.first, schema_context.base, - schema_context.relative_pointer.initial().concat( - {"patternProperties"})), - property.first}); - } + if (dependencies.dynamic_dependencies.empty() && !dependencies.unresolved && + !requires_evaluation(context, schema_context)) { + if (children.empty()) { + return {}; + } else if (!filter_strings.empty() || !filter_prefixes.empty() || + !filter_regexes.empty()) { + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesExcept, + context, schema_context, dynamic_context, + ValuePropertyFilter{std::move(filter_strings), + std::move(filter_prefixes), + std::move(filter_regexes)}, + std::move(children))}; + } else { + return {make(sourcemeta::blaze::InstructionIndex::LoopProperties, context, + schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } } if (children.empty()) { - return {make(context, schema_context, dynamic_context, - ValuePointer{})}; + return {make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, dynamic_context, ValuePointer{})}; } else if (!filter_strings.empty() || !filter_prefixes.empty() || !filter_regexes.empty()) { - return {make( + return {make( + sourcemeta::blaze::InstructionIndex::LoopPropertiesUnevaluatedExcept, context, schema_context, dynamic_context, ValuePropertyFilter{std::move(filter_strings), std::move(filter_prefixes), std::move(filter_regexes)}, std::move(children))}; } else { - return {make(context, schema_context, - dynamic_context, ValueNone{}, - std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesUnevaluated, + context, schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } } auto compiler_2019_09_core_recursiveref(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions ¤t) + -> Instructions { const auto &entry{static_frame_entry(context, schema_context)}; // In this case, just behave as a normal static reference - if (!context.references.contains( + if (!context.frame.references().contains( {sourcemeta::jsontoolkit::ReferenceType::Dynamic, entry.pointer})) { - return compiler_draft4_core_ref(context, schema_context, dynamic_context); + return compiler_draft4_core_ref(context, schema_context, dynamic_context, + current); } - return {make(context, schema_context, - dynamic_context, "")}; + return {make(sourcemeta::blaze::InstructionIndex::ControlDynamicAnchorJump, + context, schema_context, dynamic_context, "")}; } auto compiler_2019_09_applicator_properties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { - // If there is a sibling `unevaluatedProperties`, then no need - // to track evaluation, as that keyword will statically consider - // these properties through `ValuePropertyFilter` - if (context.unevaluated_properties_schemas.contains( - static_frame_entry(context, schema_context).pointer.initial())) { - return compiler_draft4_applicator_properties_with_options( - context, schema_context, dynamic_context, - context.mode == Mode::Exhaustive, false); - } - + const DynamicContext &dynamic_context, const Instructions ¤t) + -> Instructions { return compiler_draft4_applicator_properties_with_options( - context, schema_context, dynamic_context, + context, schema_context, dynamic_context, current, context.mode == Mode::Exhaustive, - !context.unevaluated_properties_schemas.empty()); + requires_evaluation(context, schema_context)); } auto compiler_2019_09_applicator_patternproperties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { - // If there is a sibling `unevaluatedProperties`, then no need - // to track evaluation, as that keyword will statically consider - // these properties through `ValuePropertyFilter` - if (context.unevaluated_properties_schemas.contains( - static_frame_entry(context, schema_context).pointer.initial())) { - return compiler_draft4_applicator_patternproperties_with_options( - context, schema_context, dynamic_context, - context.mode == Mode::Exhaustive, false); - } - + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { return compiler_draft4_applicator_patternproperties_with_options( context, schema_context, dynamic_context, context.mode == Mode::Exhaustive, - !context.unevaluated_properties_schemas.empty()); + requires_evaluation(context, schema_context)); } } // namespace internal diff --git a/vendor/blaze/src/compiler/default_compiler_2020_12.h b/vendor/blaze/src/compiler/default_compiler_2020_12.h index ec6402b1..79100ec0 100644 --- a/vendor/blaze/src/compiler/default_compiler_2020_12.h +++ b/vendor/blaze/src/compiler/default_compiler_2020_12.h @@ -12,47 +12,73 @@ using namespace sourcemeta::blaze; auto compiler_2020_12_applicator_prefixitems( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; + return compiler_draft4_applicator_items_array( context, schema_context, dynamic_context, - context.mode == Mode::Exhaustive, - !context.unevaluated_items_schemas.empty()); + context.mode == Mode::Exhaustive, track); } auto compiler_2020_12_applicator_items(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { const auto cursor{(schema_context.schema.defines("prefixItems") && schema_context.schema.at("prefixItems").is_array()) ? schema_context.schema.at("prefixItems").size() : 0}; + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; + return compiler_draft4_applicator_additionalitems_from_cursor( context, schema_context, dynamic_context, cursor, context.mode == Mode::Exhaustive, - !context.unevaluated_items_schemas.empty()); + track && !schema_context.schema.defines("unevaluatedItems")); } auto compiler_2020_12_applicator_contains(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions ¤t) + -> Instructions { + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; + return compiler_2019_09_applicator_contains_with_options( - context, schema_context, dynamic_context, - context.mode == Mode::Exhaustive, - !context.unevaluated_items_schemas.empty()); + context, schema_context, dynamic_context, current, + context.mode == Mode::Exhaustive, track); } auto compiler_2020_12_core_dynamicref(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions ¤t) + -> Instructions { const auto &entry{static_frame_entry(context, schema_context)}; // In this case, just behave as a normal static reference - if (!context.references.contains( + if (!context.frame.references().contains( {sourcemeta::jsontoolkit::ReferenceType::Dynamic, entry.pointer})) { - return compiler_draft4_core_ref(context, schema_context, dynamic_context); + return compiler_draft4_core_ref(context, schema_context, dynamic_context, + current); } assert(schema_context.schema.at(dynamic_context.keyword).is_string()); @@ -71,9 +97,9 @@ auto compiler_2020_12_core_dynamicref(const Context &context, // 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( - context, schema_context, dynamic_context, - std::string{reference.fragment().value()})}; + return {make(sourcemeta::blaze::InstructionIndex::ControlDynamicAnchorJump, + context, schema_context, dynamic_context, + std::string{reference.fragment().value()})}; } } // namespace internal diff --git a/vendor/blaze/src/compiler/default_compiler_draft4.h b/vendor/blaze/src/compiler/default_compiler_draft4.h index 85182fd1..834589f1 100644 --- a/vendor/blaze/src/compiler/default_compiler_draft4.h +++ b/vendor/blaze/src/compiler/default_compiler_draft4.h @@ -2,11 +2,11 @@ #define SOURCEMETA_BLAZE_COMPILER_DEFAULT_COMPILER_DRAFT4_H_ #include -#include +#include +#include #include // std::sort, std::any_of, std::all_of, std::find_if, std::none_of #include // assert -#include // std::regex, std::regex_error #include // std::set #include // std::ostringstream #include // std::move @@ -16,48 +16,45 @@ static auto parse_regex(const std::string &pattern, const sourcemeta::jsontoolkit::URI &base, const sourcemeta::jsontoolkit::Pointer &schema_location) - -> std::regex { - try { - return std::regex{pattern, std::regex::ECMAScript | std::regex::nosubs}; - } catch (const std::regex_error &) { + -> sourcemeta::jsontoolkit::Regex { + const auto result{sourcemeta::jsontoolkit::to_regex(pattern)}; + if (!result.has_value()) { std::ostringstream message; message << "Invalid regular expression: " << pattern; throw sourcemeta::blaze::CompilerError(base, schema_location, message.str()); } + + return result.value(); } -static auto collect_jump_labels(const sourcemeta::blaze::Template &steps, +static auto collect_jump_labels(const sourcemeta::blaze::Instructions &steps, std::set &output) -> void { for (const auto &variant : steps) { - std::visit( - [&output](const auto &step) { - using T = std::decay_t; - if constexpr (std::is_same_v) { - output.emplace(step.value); - } else if constexpr (requires { step.children; }) { - collect_jump_labels(step.children, output); - } - }, - variant); + if (variant.type == sourcemeta::blaze::InstructionIndex::ControlJump) { + output.emplace( + std::get(variant.value)); + } else { + collect_jump_labels(variant.children, output); + } } } -static auto relative_schema_location_size( - const sourcemeta::blaze::Template::value_type &variant) -> std::size_t { - return std::visit( - [](const auto &step) { return step.relative_schema_location.size(); }, - variant); +static auto +relative_schema_location_size(const sourcemeta::blaze::Instruction &step) + -> std::size_t { + return step.relative_schema_location.size(); } -static auto defines_direct_enumeration(const sourcemeta::blaze::Template &steps) +static auto +defines_direct_enumeration(const sourcemeta::blaze::Instructions &steps) -> std::optional { const auto iterator{ std::find_if(steps.cbegin(), steps.cend(), [](const auto &step) { - return std::holds_alternative( - step) || - std::holds_alternative( - step); + return step.type == + sourcemeta::blaze::InstructionIndex::AssertionEqual || + step.type == + sourcemeta::blaze::InstructionIndex::AssertionEqualsAny; })}; if (iterator == steps.cend()) { @@ -75,43 +72,126 @@ is_inside_disjunctor(const sourcemeta::jsontoolkit::Pointer &pointer) -> bool { pointer.at(pointer.size() - 3).to_property() == "anyOf"); } +static auto +json_array_to_string_set(const sourcemeta::jsontoolkit::JSON &document) + -> sourcemeta::blaze::ValueStringSet { + sourcemeta::blaze::ValueStringSet result; + for (const auto &value : document.as_array()) { + assert(value.is_string()); + result.insert(value.to_string()); + } + + return result; +} + +static auto +is_closed_properties_required(const sourcemeta::jsontoolkit::JSON &schema, + const sourcemeta::blaze::ValueStringSet &required) + -> bool { + return schema.defines("additionalProperties") && + schema.at("additionalProperties").is_boolean() && + !schema.at("additionalProperties").to_boolean() && + schema.defines("properties") && schema.at("properties").is_object() && + schema.at("properties").size() == required.size() && + std::all_of(required.begin(), required.end(), + [&schema](const auto &property) { + return schema.at("properties") + .defines(property.first, property.second); + }); +} + +static auto +compile_properties(const sourcemeta::blaze::Context &context, + const sourcemeta::blaze::SchemaContext &schema_context, + const sourcemeta::blaze::DynamicContext &dynamic_context, + const sourcemeta::blaze::Instructions &) + -> std::vector> { + std::vector> + properties; + for (const auto &entry : schema_context.schema.at("properties").as_object()) { + properties.push_back( + {entry.first, compile(context, schema_context, dynamic_context, + {entry.first}, {entry.first})}); + } + + // In many cases, `properties` have some subschemas that are small + // and some subschemas that are large. To attempt to improve performance, + // we prefer to evaluate smaller subschemas first, in the hope of failing + // earlier without spending a lot of time on other subschemas + std::sort(properties.begin(), properties.end(), + [](const auto &left, const auto &right) { + const auto left_size{recursive_template_size(left.second)}; + const auto right_size{recursive_template_size(right.second)}; + if (left_size == right_size) { + const auto left_direct_enumeration{ + defines_direct_enumeration(left.second)}; + const auto right_direct_enumeration{ + defines_direct_enumeration(right.second)}; + + // Enumerations always take precedence + if (left_direct_enumeration.has_value() && + right_direct_enumeration.has_value()) { + // If both options have a direct enumeration, we choose + // the one with the shorter relative schema location + return relative_schema_location_size( + left.second.at(left_direct_enumeration.value())) < + relative_schema_location_size( + right.second.at(right_direct_enumeration.value())); + } else if (left_direct_enumeration.has_value()) { + return true; + } else if (right_direct_enumeration.has_value()) { + return false; + } + + return left.first < right.first; + } else { + return left_size < right_size; + } + }); + + return properties; +} + namespace internal { using namespace sourcemeta::blaze; auto compiler_draft4_core_ref(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { // Determine the label const auto &entry{static_frame_entry(context, schema_context)}; const auto type{sourcemeta::jsontoolkit::ReferenceType::Static}; - if (!context.references.contains({type, entry.pointer})) { + if (!context.frame.references().contains({type, entry.pointer})) { assert(schema_context.schema.at(dynamic_context.keyword).is_string()); throw sourcemeta::jsontoolkit::SchemaReferenceError( schema_context.schema.at(dynamic_context.keyword).to_string(), entry.pointer, "The schema location is inside of an unknown keyword"); } - const auto &reference{context.references.at({type, entry.pointer})}; - const auto label{EvaluationContext{}.hash( - schema_resource_id(context, reference.base.value_or("")), - reference.fragment.value_or(""))}; + const auto &reference{context.frame.references().at({type, entry.pointer})}; + const auto label{ + Evaluator{}.hash(schema_resource_id(context, reference.base.value_or("")), + reference.fragment.value_or(""))}; // The label is already registered, so just jump to it if (schema_context.labels.contains(label) || context.precompiled_static_schemas.contains(reference.destination)) { - return {make(context, schema_context, dynamic_context, - ValueUnsignedInteger{label})}; + return {make(sourcemeta::blaze::InstructionIndex::ControlJump, context, + schema_context, dynamic_context, ValueUnsignedInteger{label})}; } auto new_schema_context{schema_context}; new_schema_context.references.insert(reference.destination); + // TODO: Replace this logic with `.frame()` `destination_of` information std::size_t direct_children_references{0}; - if (context.frame.contains({type, reference.destination})) { - for (const auto &reference_entry : context.references) { + if (context.frame.locations().contains({type, reference.destination})) { + for (const auto &reference_entry : context.frame.references()) { if (reference_entry.first.second.starts_with( - context.frame.at({type, reference.destination}).pointer)) { + context.frame.locations() + .at({type, reference.destination}) + .pointer)) { direct_children_references += 1; } } @@ -120,12 +200,16 @@ auto compiler_draft4_core_ref(const Context &context, // If the reference is not a recursive one, we can avoid the extra // overhead of marking the location for future jumps, and pretty much // just expand the reference destination in place. + // TODO: Elevate the calculation required to detect recursive references + // to JSON Toolkit's `.frame()` + // See: https://github.com/sourcemeta/jsontoolkit/issues/1394 const bool is_recursive{ // This means the reference is directly recursive, by jumping to // a parent of the reference itself. - (context.frame.contains({type, reference.destination}) && - entry.pointer.starts_with( - context.frame.at({type, reference.destination}).pointer)) || + (context.frame.locations().contains({type, reference.destination}) && + entry.pointer.starts_with(context.frame.locations() + .at({type, reference.destination}) + .pointer)) || schema_context.references.contains(reference.destination)}; if (!is_recursive && direct_children_references <= 5) { @@ -139,20 +223,21 @@ auto compiler_draft4_core_ref(const Context &context, sourcemeta::jsontoolkit::empty_pointer, reference.destination); } else { - return {make( - context, schema_context, dynamic_context, ValueNone{}, - compile(context, new_schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer, - reference.destination))}; + return {make(sourcemeta::blaze::InstructionIndex::LogicalAnd, context, + schema_context, dynamic_context, ValueNone{}, + compile(context, new_schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, + reference.destination))}; } } new_schema_context.labels.insert(label); - Template children{ - compile(context, new_schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer, reference.destination)}; + Instructions children{compile( + context, new_schema_context, relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, reference.destination)}; // If we ended up not using the label after all, then we can ignore the // wrapper, at the expense of compiling the reference instructions once more @@ -171,14 +256,15 @@ auto compiler_draft4_core_ref(const Context &context, // feel weird, we do it so we can handle references purely in this keyword // handler, without having to add logic to every single keyword to check // whether something points to them and add the "checkpoint" themselves. - return {make(context, schema_context, dynamic_context, - ValueUnsignedInteger{label}, std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::ControlLabel, context, + schema_context, dynamic_context, ValueUnsignedInteger{label}, + std::move(children))}; } auto compiler_draft4_validation_type(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { if (schema_context.schema.at(dynamic_context.keyword).is_string()) { const auto &type{ schema_context.schema.at(dynamic_context.keyword).to_string()}; @@ -192,9 +278,9 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return { - make(context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Null)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Null)}; } else if (type == "boolean") { if (context.mode == Mode::FastValidation && schema_context.schema.defines("enum") && @@ -205,19 +291,27 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Boolean)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Boolean)}; } else if (type == "object") { const auto minimum{ unsigned_integer_property(schema_context.schema, "minProperties", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxProperties")}; - if (context.mode == Mode::FastValidation && - (minimum > 0 || maximum.has_value())) { - return {make(context, schema_context, - dynamic_context, - {minimum, maximum, false})}; + + if (context.mode == Mode::FastValidation) { + if (maximum.has_value() && minimum == 0) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionTypeObjectUpper, + context, schema_context, dynamic_context, + ValueUnsignedInteger{maximum.value()})}; + } else if (minimum > 0 || maximum.has_value()) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionTypeObjectBounded, + context, schema_context, dynamic_context, + ValueRange{minimum, maximum, false})}; + } } if (context.mode == Mode::FastValidation && @@ -229,19 +323,32 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Object)}; + if (context.mode == Mode::FastValidation && + schema_context.schema.defines("required")) { + return {}; + } + + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Object)}; } else if (type == "array") { const auto minimum{ unsigned_integer_property(schema_context.schema, "minItems", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxItems")}; - if (context.mode == Mode::FastValidation && - (minimum > 0 || maximum.has_value())) { - return {make(context, schema_context, - dynamic_context, - {minimum, maximum, false})}; + + if (context.mode == Mode::FastValidation) { + if (maximum.has_value() && minimum == 0) { + return { + make(sourcemeta::blaze::InstructionIndex::AssertionTypeArrayUpper, + context, schema_context, dynamic_context, + ValueUnsignedInteger{maximum.value()})}; + } else if (minimum > 0 || maximum.has_value()) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionTypeArrayBounded, + context, schema_context, dynamic_context, + ValueRange{minimum, maximum, false})}; + } } if (context.mode == Mode::FastValidation && @@ -253,9 +360,9 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Array)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Array)}; } else if (type == "number") { if (context.mode == Mode::FastValidation && schema_context.schema.defines("enum") && @@ -266,11 +373,11 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - std::vector{ - sourcemeta::jsontoolkit::JSON::Type::Real, - sourcemeta::jsontoolkit::JSON::Type::Integer})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny, + context, schema_context, dynamic_context, + std::vector{ + sourcemeta::jsontoolkit::JSON::Type::Real, + sourcemeta::jsontoolkit::JSON::Type::Integer})}; } else if (type == "integer") { if (context.mode == Mode::FastValidation && schema_context.schema.defines("enum") && @@ -281,19 +388,27 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Integer)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Integer)}; } else if (type == "string") { const auto minimum{ unsigned_integer_property(schema_context.schema, "minLength", 0)}; const auto maximum{ unsigned_integer_property(schema_context.schema, "maxLength")}; - if (context.mode == Mode::FastValidation && - (minimum > 0 || maximum.has_value())) { - return {make(context, schema_context, - dynamic_context, - {minimum, maximum, false})}; + + if (context.mode == Mode::FastValidation) { + if (maximum.has_value() && minimum == 0) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionTypeStringUpper, + context, schema_context, dynamic_context, + ValueUnsignedInteger{maximum.value()})}; + } else if (minimum > 0 || maximum.has_value()) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionTypeStringBounded, + context, schema_context, dynamic_context, + ValueRange{minimum, maximum, false})}; + } } if (context.mode == Mode::FastValidation && @@ -305,9 +420,9 @@ auto compiler_draft4_validation_type(const Context &context, return {}; } - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::String)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::String)}; } else { return {}; } @@ -319,35 +434,35 @@ auto compiler_draft4_validation_type(const Context &context, const auto &type{ schema_context.schema.at(dynamic_context.keyword).front().to_string()}; if (type == "null") { - return { - make(context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Null)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Null)}; } else if (type == "boolean") { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Boolean)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Boolean)}; } else if (type == "object") { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Object)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Object)}; } else if (type == "array") { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Array)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Array)}; } else if (type == "number") { - return {make( - context, schema_context, dynamic_context, - std::vector{ - sourcemeta::jsontoolkit::JSON::Type::Real, - sourcemeta::jsontoolkit::JSON::Type::Integer})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny, + context, schema_context, dynamic_context, + std::vector{ + sourcemeta::jsontoolkit::JSON::Type::Real, + sourcemeta::jsontoolkit::JSON::Type::Integer})}; } else if (type == "integer") { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::Integer)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::Integer)}; } else if (type == "string") { - return {make( - context, schema_context, dynamic_context, - sourcemeta::jsontoolkit::JSON::Type::String)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + sourcemeta::jsontoolkit::JSON::Type::String)}; } else { return {}; } @@ -377,8 +492,8 @@ auto compiler_draft4_validation_type(const Context &context, assert(types.size() >= schema_context.schema.at(dynamic_context.keyword).size()); - return {make(context, schema_context, - dynamic_context, std::move(types))}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny, + context, schema_context, dynamic_context, std::move(types))}; } return {}; @@ -386,8 +501,9 @@ auto compiler_draft4_validation_type(const Context &context, auto compiler_draft4_validation_required(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions ¤t) + -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); if (schema_context.schema.defines("type") && @@ -396,178 +512,246 @@ auto compiler_draft4_validation_required(const Context &context, return {}; } + const auto assume_object{schema_context.schema.defines("type") && + schema_context.schema.at("type").is_string() && + schema_context.schema.at("type").to_string() == + "object"}; + if (schema_context.schema.at(dynamic_context.keyword).empty()) { return {}; } else if (schema_context.schema.at(dynamic_context.keyword).size() > 1) { - std::vector properties; - for (const auto &property : - schema_context.schema.at(dynamic_context.keyword).as_array()) { - assert(property.is_string()); - properties.push_back(property.to_string()); - } + ValueStringSet properties_set{json_array_to_string_set( + schema_context.schema.at(dynamic_context.keyword))}; + if (properties_set.size() == 1) { + if (context.mode == Mode::FastValidation && assume_object) { + return { + make(sourcemeta::blaze::InstructionIndex::AssertionDefinesStrict, + context, schema_context, dynamic_context, + make_property(properties_set.begin()->first))}; + } else { + return {make(sourcemeta::blaze::InstructionIndex::AssertionDefines, + context, schema_context, dynamic_context, + make_property(properties_set.begin()->first))}; + } + } else if (is_closed_properties_required(schema_context.schema, + properties_set)) { + if (context.mode == Mode::FastValidation && assume_object) { + const SchemaContext new_schema_context{ + schema_context.relative_pointer.initial().concat({"properties"}), + schema_context.schema, + schema_context.vocabularies, + schema_context.base, + schema_context.labels, + schema_context.references}; + const DynamicContext new_dynamic_context{ + "properties", sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer, false}; + auto properties{compile_properties(context, new_schema_context, + new_dynamic_context, current)}; + if (std::all_of(properties.cbegin(), properties.cend(), + [](const auto &property) { + return property.second.size() == 1 && + property.second.front().type == + InstructionIndex::AssertionTypeStrict; + })) { + std::set types; + for (const auto &property : properties) { + types.insert(std::get(property.second.front().value)); + } + + if (types.size() == 1) { + // Handled in `properties` + return {}; + } + } - if (properties.size() == 1) { - return {make(context, schema_context, dynamic_context, - ValueString{*(properties.cbegin())})}; + return {make( + sourcemeta::blaze::InstructionIndex::AssertionDefinesExactlyStrict, + context, schema_context, dynamic_context, + std::move(properties_set))}; + } else { + return { + make(sourcemeta::blaze::InstructionIndex::AssertionDefinesExactly, + context, schema_context, dynamic_context, + std::move(properties_set))}; + } + } else if (context.mode == Mode::FastValidation && assume_object) { + return {make( + sourcemeta::blaze::InstructionIndex::AssertionDefinesAllStrict, + context, schema_context, dynamic_context, std::move(properties_set))}; } else { - return {make( - context, schema_context, dynamic_context, std::move(properties))}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionDefinesAll, + context, schema_context, dynamic_context, + std::move(properties_set))}; } + } else if (context.mode == Mode::FastValidation && assume_object) { + assert( + schema_context.schema.at(dynamic_context.keyword).front().is_string()); + return {make(sourcemeta::blaze::InstructionIndex::AssertionDefinesStrict, + context, schema_context, dynamic_context, + make_property(schema_context.schema.at(dynamic_context.keyword) + .front() + .to_string()))}; } else { assert( schema_context.schema.at(dynamic_context.keyword).front().is_string()); - return {make( - context, schema_context, dynamic_context, - ValueString{schema_context.schema.at(dynamic_context.keyword) - .front() - .to_string()})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionDefines, context, + schema_context, dynamic_context, + make_property(schema_context.schema.at(dynamic_context.keyword) + .front() + .to_string()))}; } } auto compiler_draft4_applicator_allof(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - Template children; - for (std::uint64_t index = 0; - index < schema_context.schema.at(dynamic_context.keyword).size(); - index++) { - for (auto &&step : - compile(context, schema_context, relative_dynamic_context, - {static_cast( - index)})) { - children.push_back(std::move(step)); + Instructions children; + + if (context.mode == Mode::FastValidation && + // TODO: Make this work with `$dynamicRef` + !context.uses_dynamic_scopes) { + for (std::uint64_t index = 0; + index < schema_context.schema.at(dynamic_context.keyword).size(); + index++) { + for (auto &&step : + compile(context, schema_context, dynamic_context, + {static_cast( + index)})) { + children.push_back(std::move(step)); + } } - } - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + return children; + } else { + for (std::uint64_t index = 0; + index < schema_context.schema.at(dynamic_context.keyword).size(); + index++) { + for (auto &&step : + compile(context, schema_context, + relative_dynamic_context(dynamic_context), + {static_cast( + index)})) { + children.push_back(std::move(step)); + } + } + + return {make(sourcemeta::blaze::InstructionIndex::LogicalAnd, context, + schema_context, dynamic_context, ValueNone{}, + std::move(children))}; + } } auto compiler_draft4_applicator_anyof(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - Template disjunctors; + Instructions disjunctors; for (std::uint64_t index = 0; index < schema_context.schema.at(dynamic_context.keyword).size(); index++) { - disjunctors.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{}, - compile(context, schema_context, relative_dynamic_context, + disjunctors.push_back(make( + sourcemeta::blaze::InstructionIndex::ControlGroup, context, + schema_context, relative_dynamic_context(dynamic_context), ValueNone{}, + compile(context, schema_context, + relative_dynamic_context(dynamic_context), {static_cast( index)}))); } - const auto requires_exhaustive{ - context.mode == Mode::Exhaustive || - !context.unevaluated_properties_schemas.empty() || - !context.unevaluated_items_schemas.empty()}; + if (context.mode == Mode::FastValidation && + std::all_of(disjunctors.cbegin(), disjunctors.cend(), + [](const auto &instruction) { + return instruction.children.size() == 1 && + (instruction.children.front().type == + sourcemeta::blaze::InstructionIndex:: + AssertionTypeStrict || + instruction.children.front().type == + sourcemeta::blaze::InstructionIndex:: + AssertionTypeStrictAny); + })) { + ValueTypes types; + for (const auto &instruction : disjunctors) { + if (instruction.children.front().type == + sourcemeta::blaze::InstructionIndex::AssertionTypeStrict) { + const auto &value{ + *std::get_if(&instruction.children.front().value)}; + types.push_back(value); + } + + if (instruction.children.front().type == + sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny) { + const auto &value{ + *std::get_if(&instruction.children.front().value)}; + for (const auto type : value) { + types.push_back(type); + } + } + } + + assert(!types.empty()); + if (types.size() > 1) { + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrictAny, + context, schema_context, dynamic_context, std::move(types))}; + } else { + return {make(sourcemeta::blaze::InstructionIndex::AssertionTypeStrict, + context, schema_context, dynamic_context, + ValueType{*types.cbegin()})}; + } + } + + const auto requires_exhaustive{context.mode == Mode::Exhaustive || + requires_evaluation(context, schema_context)}; - return {make(context, schema_context, dynamic_context, - ValueBoolean{requires_exhaustive}, - std::move(disjunctors))}; + return {make(sourcemeta::blaze::InstructionIndex::LogicalOr, context, + schema_context, dynamic_context, + ValueBoolean{requires_exhaustive}, std::move(disjunctors))}; } auto compiler_draft4_applicator_oneof(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); assert(!schema_context.schema.at(dynamic_context.keyword).empty()); - Template disjunctors; + Instructions disjunctors; for (std::uint64_t index = 0; index < schema_context.schema.at(dynamic_context.keyword).size(); index++) { - disjunctors.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{}, - compile(context, schema_context, relative_dynamic_context, + disjunctors.push_back(make( + sourcemeta::blaze::InstructionIndex::ControlGroup, context, + schema_context, relative_dynamic_context(dynamic_context), ValueNone{}, + compile(context, schema_context, + relative_dynamic_context(dynamic_context), {static_cast( index)}))); } - const auto requires_exhaustive{ - context.mode == Mode::Exhaustive || - !context.unevaluated_properties_schemas.empty() || - !context.unevaluated_items_schemas.empty()}; - - return {make(context, schema_context, dynamic_context, - ValueBoolean{requires_exhaustive}, - std::move(disjunctors))}; -} - -static auto compile_properties(const Context &context, - const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> std::vector> { - std::vector> properties; - for (const auto &entry : schema_context.schema.at("properties").as_object()) { - properties.push_back( - {entry.first, compile(context, schema_context, dynamic_context, - {entry.first}, {entry.first})}); - } - - // In many cases, `properties` have some subschemas that are small - // and some subschemas that are large. To attempt to improve performance, - // we prefer to evaluate smaller subschemas first, in the hope of failing - // earlier without spending a lot of time on other subschemas - std::sort(properties.begin(), properties.end(), - [](const auto &left, const auto &right) { - const auto left_size{recursive_template_size(left.second)}; - const auto right_size{recursive_template_size(right.second)}; - if (left_size == right_size) { - const auto left_direct_enumeration{ - defines_direct_enumeration(left.second)}; - const auto right_direct_enumeration{ - defines_direct_enumeration(right.second)}; + const auto requires_exhaustive{context.mode == Mode::Exhaustive || + requires_evaluation(context, schema_context)}; - // Enumerations always take precedence - if (left_direct_enumeration.has_value() && - right_direct_enumeration.has_value()) { - // If both options have a direct enumeration, we choose - // the one with the shorter relative schema location - return relative_schema_location_size( - left.second.at(left_direct_enumeration.value())) < - relative_schema_location_size( - right.second.at(right_direct_enumeration.value())); - } else if (left_direct_enumeration.has_value()) { - return true; - } else if (right_direct_enumeration.has_value()) { - return false; - } - - return left.first < right.first; - } else { - return left_size < right_size; - } - }); - - return properties; + return {make(sourcemeta::blaze::InstructionIndex::LogicalXor, context, + schema_context, dynamic_context, + ValueBoolean{requires_exhaustive}, std::move(disjunctors))}; } -auto compiler_draft4_applicator_properties_with_options( - const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context, const bool annotate, - const bool track_evaluation) -> Template { - assert(schema_context.schema.at(dynamic_context.keyword).is_object()); - if (schema_context.schema.at(dynamic_context.keyword).empty()) { - return {}; - } - - if (schema_context.schema.defines("type") && - schema_context.schema.at("type").is_string() && - schema_context.schema.at("type").to_string() != "object") { - return {}; - } - - const auto size{schema_context.schema.at(dynamic_context.keyword).size()}; +// There are two ways to compile `properties` depending on whether +// most of the properties are marked as required using `required` +// or whether most of the properties are optional. Each shines +// in the corresponding case. +auto properties_as_loop(const Context &context, + const SchemaContext &schema_context, + const sourcemeta::jsontoolkit::JSON &properties) + -> bool { + const auto size{properties.size()}; const auto imports_validation_vocabulary = schema_context.vocabularies.contains( "http://json-schema.org/draft-04/schema#") || @@ -596,8 +780,7 @@ auto compiler_draft4_applicator_properties_with_options( schema_context.schema.at("required").as_array()) { if (property.is_string() && // Only count the required property if its indeed in "properties" - schema_context.schema.at(dynamic_context.keyword) - .defines(property.to_string())) { + properties.defines(property.to_string())) { required.insert(property.to_string()); } } @@ -607,16 +790,17 @@ auto compiler_draft4_applicator_properties_with_options( const auto inside_disjunctor{ is_inside_disjunctor(schema_context.relative_pointer) || // Check if any reference from `anyOf` or `oneOf` points to us - std::any_of(context.references.cbegin(), context.references.cend(), + std::any_of(context.frame.references().cbegin(), + context.frame.references().cend(), [&context, ¤t_entry](const auto &reference) { - if (!context.frame.contains( + if (!context.frame.locations().contains( {sourcemeta::jsontoolkit::ReferenceType::Static, reference.second.destination})) { return false; } const auto &target{ - context.frame + context.frame.locations() .at({sourcemeta::jsontoolkit::ReferenceType::Static, reference.second.destination}) .pointer}; @@ -624,11 +808,16 @@ auto compiler_draft4_applicator_properties_with_options( current_entry.pointer.initial() == target; })}; - // There are two ways to compile `properties` depending on whether - // most of the properties are marked as required using `required` - // or whether most of the properties are optional. Each shines - // in the corresponding case. - const auto prefer_loop_over_instance{ + if (!inside_disjunctor && + schema_context.schema.defines("additionalProperties") && + schema_context.schema.at("additionalProperties").is_boolean() && + !schema_context.schema.at("additionalProperties").to_boolean() && + // If all properties are required, we should still unroll + required.size() < size) { + return true; + } + + return // This strategy only makes sense if most of the properties are "optional" required.size() <= (size / 4) && // If `properties` only defines a relatively small amount of properties, @@ -637,131 +826,284 @@ auto compiler_draft4_applicator_properties_with_options( // Always unroll inside `oneOf` or `anyOf`, to have a // better chance at quickly short-circuiting (!inside_disjunctor || - std::none_of( - schema_context.schema.at(dynamic_context.keyword) - .as_object() - .cbegin(), - schema_context.schema.at(dynamic_context.keyword).as_object().cend(), - [&](const auto &pair) { - return pair.second.is_object() && - ((imports_validation_vocabulary && - pair.second.defines("enum")) || - (imports_const && pair.second.defines("const"))); - }))}; - - if (prefer_loop_over_instance) { + std::none_of(properties.as_object().cbegin(), + properties.as_object().cend(), [&](const auto &pair) { + return pair.second.is_object() && + ((imports_validation_vocabulary && + pair.second.defines("enum")) || + (imports_const && pair.second.defines("const"))); + })); +} + +auto compiler_draft4_applicator_properties_with_options( + const Context &context, const SchemaContext &schema_context, + const DynamicContext &dynamic_context, const Instructions ¤t, + const bool annotate, const bool track_evaluation) -> Instructions { + assert(schema_context.schema.at(dynamic_context.keyword).is_object()); + if (schema_context.schema.at(dynamic_context.keyword).empty()) { + return {}; + } + + if (schema_context.schema.defines("type") && + schema_context.schema.at("type").is_string() && + schema_context.schema.at("type").to_string() != "object") { + return {}; + } + + if (properties_as_loop(context, schema_context, + schema_context.schema.at(dynamic_context.keyword))) { ValueNamedIndexes indexes; - Template children; + Instructions children; std::size_t cursor = 0; for (auto &&[name, substeps] : compile_properties( - context, schema_context, relative_dynamic_context)) { - indexes.emplace(name, cursor); + context, schema_context, relative_dynamic_context(dynamic_context), + current)) { + indexes.assign(name, cursor); if (track_evaluation) { - substeps.push_back(make(context, schema_context, - relative_dynamic_context, - ValuePointer{name})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, relative_dynamic_context(dynamic_context), + ValuePointer{name})); } if (annotate) { - substeps.push_back(make( - context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::JSON{name})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context, + schema_context, relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::JSON{name})); } // Note that the evaluator completely ignores this wrapper anyway - children.push_back(make(context, schema_context, - relative_dynamic_context, - ValueNone{}, std::move(substeps))); + children.push_back(make(sourcemeta::blaze::InstructionIndex::ControlGroup, + context, schema_context, + relative_dynamic_context(dynamic_context), + ValueNone{}, std::move(substeps))); cursor += 1; } - return {make(context, schema_context, dynamic_context, - std::move(indexes), std::move(children))}; + if (context.mode == Mode::FastValidation && !track_evaluation && + !schema_context.schema.defines("patternProperties") && + schema_context.schema.defines("additionalProperties") && + schema_context.schema.at("additionalProperties").is_boolean() && + !schema_context.schema.at("additionalProperties").to_boolean()) { + return { + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesMatchClosed, + context, schema_context, dynamic_context, std::move(indexes), + std::move(children))}; + } + + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesMatch, + context, schema_context, dynamic_context, std::move(indexes), + std::move(children))}; } - Template children; + Instructions children; + + const auto effective_dynamic_context{ + context.mode == Mode::FastValidation + ? dynamic_context + : relative_dynamic_context(dynamic_context)}; + + const auto assume_object{schema_context.schema.defines("type") && + schema_context.schema.at("type").is_string() && + schema_context.schema.at("type").to_string() == + "object"}; + + auto properties{compile_properties(context, schema_context, + effective_dynamic_context, current)}; + + if (context.mode == Mode::FastValidation && + schema_context.schema.defines("additionalProperties") && + schema_context.schema.at("additionalProperties").is_boolean() && + !schema_context.schema.at("additionalProperties").to_boolean() && + // TODO: Check that the validation vocabulary is present + schema_context.schema.defines("required") && + schema_context.schema.at("required").is_array() && + schema_context.schema.at("required").size() == + schema_context.schema.at(dynamic_context.keyword).size() && + std::all_of(properties.cbegin(), properties.cend(), + [&schema_context](const auto &property) { + return schema_context.schema.at("required") + .contains( + sourcemeta::jsontoolkit::JSON{property.first}); + })) { + if (std::all_of(properties.cbegin(), properties.cend(), + [](const auto &property) { + return property.second.size() == 1 && + property.second.front().type == + InstructionIndex::AssertionTypeStrict; + })) { + std::set types; + for (const auto &property : properties) { + types.insert(std::get(property.second.front().value)); + } + + if (types.size() == 1) { + if (schema_context.schema.defines("required") && + !schema_context.schema.defines("patternProperties") && + assume_object) { + ValueStringSet required{ + json_array_to_string_set(schema_context.schema.at("required"))}; + if (is_closed_properties_required(schema_context.schema, required)) { + std::vector + perfect_hashes; + for (const auto &entry : required) { + assert(required.contains(entry.first, entry.second)); + if (entry.second.is_perfect()) { + perfect_hashes.push_back(entry.second); + } + } + + if (perfect_hashes.size() == required.size()) { + return {make(sourcemeta::blaze::InstructionIndex:: + LoopPropertiesExactlyTypeStrictHash, + context, schema_context, dynamic_context, + ValueTypedHashes{*types.cbegin(), + std::move(perfect_hashes)})}; + } + + return {make( + sourcemeta::blaze::InstructionIndex:: + LoopPropertiesExactlyTypeStrict, + context, schema_context, dynamic_context, + ValueTypedProperties{*types.cbegin(), std::move(required)})}; + } + } - const auto effective_dynamic_context{context.mode == Mode::FastValidation - ? dynamic_context - : relative_dynamic_context}; + return { + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrict, + context, schema_context, dynamic_context, *types.cbegin())}; + } + } - for (auto &&[name, substeps] : - compile_properties(context, schema_context, effective_dynamic_context)) { + if (std::all_of(properties.cbegin(), properties.cend(), + [](const auto &property) { + return property.second.size() == 1 && + property.second.front().type == + InstructionIndex::AssertionType; + })) { + std::set types; + for (const auto &property : properties) { + types.insert(std::get(property.second.front().value)); + } + + if (types.size() == 1) { + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesType, + context, schema_context, dynamic_context, + *types.cbegin())}; + } + } + } + + for (auto &&[name, substeps] : properties) { if (annotate) { - substeps.push_back(make( - context, schema_context, effective_dynamic_context, - sourcemeta::jsontoolkit::JSON{name})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context, + schema_context, effective_dynamic_context, + sourcemeta::jsontoolkit::JSON{name})); } // Optimize `properties` where its subschemas just include a type check, // as that's a very common pattern if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative(substeps.front())) { - const auto &type_step{std::get(substeps.front())}; + substeps.front().type == InstructionIndex::AssertionTypeStrict) { + const auto &type_step{substeps.front()}; if (track_evaluation) { - children.push_back( - rephrase(type_step)); + children.push_back(rephrase(sourcemeta::blaze::InstructionIndex:: + AssertionPropertyTypeStrictEvaluate, + type_step)); } else { - children.push_back(rephrase(type_step)); + children.push_back(rephrase( + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeStrict, + type_step)); } } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative(substeps.front())) { - const auto &type_step{std::get(substeps.front())}; + substeps.front().type == InstructionIndex::AssertionType) { + const auto &type_step{substeps.front()}; if (track_evaluation) { - children.push_back(rephrase(type_step)); + children.push_back(rephrase( + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeEvaluate, + type_step)); } else { - children.push_back(rephrase(type_step)); + children.push_back( + rephrase(sourcemeta::blaze::InstructionIndex::AssertionPropertyType, + type_step)); } } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - const auto &type_step{std::get(substeps.front())}; + substeps.front().type == + InstructionIndex::AssertionTypeStrictAny) { + const auto &type_step{substeps.front()}; if (track_evaluation) { - children.push_back( - rephrase(type_step)); + children.push_back(rephrase(sourcemeta::blaze::InstructionIndex:: + AssertionPropertyTypeStrictAnyEvaluate, + type_step)); } else { - children.push_back(rephrase(type_step)); + children.push_back(rephrase( + sourcemeta::blaze::InstructionIndex::AssertionPropertyTypeStrictAny, + type_step)); } } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - children.push_back(unroll( + substeps.front().type == + InstructionIndex::AssertionPropertyTypeStrict) { + children.push_back(unroll( substeps.front(), effective_dynamic_context.base_instance_location)); } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - children.push_back(unroll( + substeps.front().type == + InstructionIndex::AssertionPropertyType) { + children.push_back(unroll( substeps.front(), effective_dynamic_context.base_instance_location)); } else if (context.mode == Mode::FastValidation && substeps.size() == 1 && - std::holds_alternative( - substeps.front())) { - children.push_back(unroll( + substeps.front().type == + InstructionIndex::AssertionPropertyTypeStrictAny) { + children.push_back(unroll( substeps.front(), effective_dynamic_context.base_instance_location)); } else { if (track_evaluation) { if (context.mode == Mode::FastValidation) { // We need this wrapper as `ControlEvaluate` doesn't push to the stack - substeps.push_back(make( - context, schema_context, effective_dynamic_context, ValueNone{}, - {make(context, schema_context, - effective_dynamic_context, - ValuePointer{name})})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::LogicalAnd, context, + schema_context, effective_dynamic_context, ValueNone{}, + {make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, + context, schema_context, effective_dynamic_context, + ValuePointer{name})})); } else { - substeps.push_back(make(context, schema_context, - effective_dynamic_context, - ValuePointer{name})); + substeps.push_back(make( + sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, effective_dynamic_context, ValuePointer{name})); } } if (!substeps.empty()) { - children.push_back(make( - context, schema_context, effective_dynamic_context, - ValueString{name}, std::move(substeps))); + // As a performance shortcut + if (effective_dynamic_context.base_instance_location.empty()) { + if (assume_object && + // TODO: Check that the validation vocabulary is present + schema_context.schema.defines("required") && + schema_context.schema.at("required").is_array() && + schema_context.schema.at("required") + .contains(sourcemeta::jsontoolkit::JSON{name})) { + for (auto &&step : substeps) { + children.push_back(std::move(step)); + } + } else { + children.push_back(make(sourcemeta::blaze::InstructionIndex:: + ControlGroupWhenDefinesDirect, + context, schema_context, + effective_dynamic_context, + make_property(name), std::move(substeps))); + } + } else { + children.push_back( + make(sourcemeta::blaze::InstructionIndex::ControlGroupWhenDefines, + context, schema_context, effective_dynamic_context, + make_property(name), std::move(substeps))); + } } } } @@ -771,22 +1113,24 @@ auto compiler_draft4_applicator_properties_with_options( } else if (children.empty()) { return {}; } else { - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LogicalAnd, context, + schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } } auto compiler_draft4_applicator_properties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions ¤t) + -> Instructions { return compiler_draft4_applicator_properties_with_options( - context, schema_context, dynamic_context, false, false); + context, schema_context, dynamic_context, current, false, false); } auto compiler_draft4_applicator_patternproperties_with_options( const Context &context, const SchemaContext &schema_context, const DynamicContext &dynamic_context, const bool annotate, - const bool track_evaluation) -> Template { + const bool track_evaluation) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_object()); if (schema_context.schema.at(dynamic_context.keyword).empty()) { return {}; @@ -798,7 +1142,7 @@ auto compiler_draft4_applicator_patternproperties_with_options( return {}; } - Template children; + Instructions children; // To guarantee ordering std::vector patterns; @@ -811,34 +1155,54 @@ auto compiler_draft4_applicator_patternproperties_with_options( // For each regular expression and corresponding subschema in the object for (const auto &pattern : patterns) { - auto substeps{compile(context, schema_context, relative_dynamic_context, - {pattern}, {})}; + auto substeps{compile(context, schema_context, + relative_dynamic_context(dynamic_context), {pattern}, + {})}; if (annotate) { - substeps.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent, + context, schema_context, + relative_dynamic_context(dynamic_context), ValueNone{})); } if (track_evaluation) { - substeps.push_back(make( - context, schema_context, relative_dynamic_context, ValuePointer{})); + substeps.push_back( + make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, relative_dynamic_context(dynamic_context), + ValuePointer{})); } - // If the `patternProperties` subschema for the given pattern does - // nothing, then we can avoid generating an entire loop for it - if (!substeps.empty()) { + if (context.mode == Mode::FastValidation && !track_evaluation && + patterns.size() == 1 && !schema_context.schema.defines("properties") && + schema_context.schema.defines("additionalProperties") && + schema_context.schema.at("additionalProperties").is_boolean() && + !schema_context.schema.at("additionalProperties").to_boolean()) { + children.push_back( + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesRegexClosed, + context, schema_context, dynamic_context, + ValueRegex{parse_regex(pattern, schema_context.base, + schema_context.relative_pointer), + pattern}, + std::move(substeps))); + + // If the `patternProperties` subschema for the given pattern does + // nothing, then we can avoid generating an entire loop for it + } else if (!substeps.empty()) { const auto maybe_prefix{pattern_as_prefix(pattern)}; if (maybe_prefix.has_value()) { - children.push_back(make( - context, schema_context, dynamic_context, - ValueString{maybe_prefix.value()}, std::move(substeps))); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesStartsWith, + context, schema_context, dynamic_context, + ValueString{maybe_prefix.value()}, std::move(substeps))); } else { - children.push_back(make( - context, schema_context, dynamic_context, - ValueRegex{parse_regex(pattern, schema_context.base, - schema_context.relative_pointer), - pattern}, - std::move(substeps))); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesRegex, + context, schema_context, dynamic_context, + ValueRegex{parse_regex(pattern, schema_context.base, + schema_context.relative_pointer), + pattern}, + std::move(substeps))); } } } @@ -848,7 +1212,8 @@ auto compiler_draft4_applicator_patternproperties_with_options( auto compiler_draft4_applicator_patternproperties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { return compiler_draft4_applicator_patternproperties_with_options( context, schema_context, dynamic_context, false, false); } @@ -856,23 +1221,26 @@ auto compiler_draft4_applicator_patternproperties( auto compiler_draft4_applicator_additionalproperties_with_options( const Context &context, const SchemaContext &schema_context, const DynamicContext &dynamic_context, const bool annotate, - const bool track_evaluation) -> Template { + const bool track_evaluation) -> Instructions { if (schema_context.schema.defines("type") && schema_context.schema.at("type").is_string() && schema_context.schema.at("type").to_string() != "object") { return {}; } - Template children{compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Instructions children{compile(context, schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; if (annotate) { - children.push_back(make( - context, schema_context, relative_dynamic_context, ValueNone{})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent, + context, schema_context, relative_dynamic_context(dynamic_context), + ValueNone{})); } - ValueStrings filter_strings; + ValueStringSet filter_strings; ValueStrings filter_prefixes; std::vector filter_regexes; @@ -880,7 +1248,7 @@ auto compiler_draft4_applicator_additionalproperties_with_options( schema_context.schema.at("properties").is_object()) { for (const auto &entry : schema_context.schema.at("properties").as_object()) { - filter_strings.push_back(entry.first); + filter_strings.insert(entry.first); } } @@ -908,86 +1276,115 @@ auto compiler_draft4_applicator_additionalproperties_with_options( } if (context.mode == Mode::FastValidation && children.size() == 1 && - std::holds_alternative(children.front()) && + children.front().type == InstructionIndex::AssertionFail && !filter_strings.empty() && filter_prefixes.empty() && filter_regexes.empty()) { - return {make( - context, schema_context, dynamic_context, std::move(filter_strings))}; + if (properties_as_loop(context, schema_context, + schema_context.schema.at("properties"))) { + return {}; + } else if (!children.empty() && + children.front().type == InstructionIndex::AssertionFail) { + return {}; + } else { + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesWhitelist, + context, schema_context, dynamic_context, + std::move(filter_strings))}; + } + } else if (context.mode == Mode::FastValidation && filter_strings.empty() && + filter_prefixes.empty() && filter_regexes.size() == 1 && + !track_evaluation && !children.empty() && + children.front().type == InstructionIndex::AssertionFail) { + return {}; } if (!filter_strings.empty() || !filter_prefixes.empty() || !filter_regexes.empty()) { if (track_evaluation) { - children.push_back(make( - context, schema_context, relative_dynamic_context, ValuePointer{})); + children.push_back( + make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, context, + schema_context, relative_dynamic_context(dynamic_context), + ValuePointer{})); } - return {make( - context, schema_context, dynamic_context, - ValuePropertyFilter{std::move(filter_strings), - std::move(filter_prefixes), - std::move(filter_regexes)}, - std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesExcept, + context, schema_context, dynamic_context, + ValuePropertyFilter{std::move(filter_strings), + std::move(filter_prefixes), + std::move(filter_regexes)}, + std::move(children))}; // Optimize `additionalProperties` set to just `type`, which is a // pretty common pattern } else if (context.mode == Mode::FastValidation && children.size() == 1 && - std::holds_alternative(children.front())) { - const auto &type_step{std::get(children.front())}; + children.front().type == InstructionIndex::AssertionTypeStrict) { + const auto &type_step{children.front()}; if (track_evaluation) { - return {make( + return {make( + sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrictEvaluate, context, schema_context, dynamic_context, type_step.value)}; } else { - return {make(context, schema_context, - dynamic_context, type_step.value)}; + return { + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrict, + context, schema_context, dynamic_context, type_step.value)}; } } else if (context.mode == Mode::FastValidation && children.size() == 1 && - std::holds_alternative(children.front())) { - const auto &type_step{std::get(children.front())}; + children.front().type == InstructionIndex::AssertionType) { + const auto &type_step{children.front()}; if (track_evaluation) { - return {make( - context, schema_context, dynamic_context, type_step.value)}; + return { + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeEvaluate, + context, schema_context, dynamic_context, type_step.value)}; } else { - return {make(context, schema_context, dynamic_context, - type_step.value)}; + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesType, + context, schema_context, dynamic_context, type_step.value)}; } } else if (context.mode == Mode::FastValidation && children.size() == 1 && - std::holds_alternative(children.front())) { - const auto &type_step{std::get(children.front())}; + children.front().type == + InstructionIndex::AssertionTypeStrictAny) { + const auto &type_step{children.front()}; if (track_evaluation) { - return {make( - context, schema_context, dynamic_context, type_step.value)}; + return {make(sourcemeta::blaze::InstructionIndex:: + LoopPropertiesTypeStrictAnyEvaluate, + context, schema_context, dynamic_context, type_step.value)}; } else { - return {make( - context, schema_context, dynamic_context, type_step.value)}; + return { + make(sourcemeta::blaze::InstructionIndex::LoopPropertiesTypeStrictAny, + context, schema_context, dynamic_context, type_step.value)}; } } else if (track_evaluation) { if (children.empty()) { - return {make(context, schema_context, dynamic_context, - ValuePointer{})}; + return {make(sourcemeta::blaze::InstructionIndex::ControlEvaluate, + context, schema_context, dynamic_context, ValuePointer{})}; } - return {make(context, schema_context, - dynamic_context, ValueNone{}, - std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopPropertiesEvaluate, + context, schema_context, dynamic_context, ValueNone{}, + std::move(children))}; + } else if (children.size() == 1 && + children.front().type == InstructionIndex::AssertionFail) { + return {make(sourcemeta::blaze::InstructionIndex::AssertionObjectSizeLess, + context, schema_context, dynamic_context, + ValueUnsignedInteger{1})}; } else { - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LoopProperties, context, + schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } } auto compiler_draft4_applicator_additionalproperties( const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) -> Template { + const DynamicContext &dynamic_context, const Instructions &) + -> Instructions { return compiler_draft4_applicator_additionalproperties_with_options( context, schema_context, dynamic_context, false, false); } auto compiler_draft4_validation_pattern(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_string()); if (schema_context.schema.defines("type") && @@ -998,17 +1395,17 @@ auto compiler_draft4_validation_pattern(const Context &context, const auto ®ex_string{ schema_context.schema.at(dynamic_context.keyword).to_string()}; - return {make( - context, schema_context, dynamic_context, - ValueRegex{parse_regex(regex_string, schema_context.base, - schema_context.relative_pointer), - regex_string})}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionRegex, context, + schema_context, dynamic_context, + ValueRegex{parse_regex(regex_string, schema_context.base, + schema_context.relative_pointer), + regex_string})}; } auto compiler_draft4_validation_format(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { if (!schema_context.schema.at(dynamic_context.keyword).is_string()) { return {}; } @@ -1030,17 +1427,19 @@ auto compiler_draft4_validation_format(const Context &context, schema_context.schema.at(dynamic_context.keyword).to_string()}; if (format == "uri") { - return {make(context, schema_context, dynamic_context, - ValueStringType::URI)}; + return {make(sourcemeta::blaze::InstructionIndex::AssertionStringType, + context, schema_context, dynamic_context, + ValueStringType::URI)}; } #define COMPILE_FORMAT_REGEX(name, regular_expression) \ if (format == (name)) { \ - return {make( \ - context, schema_context, dynamic_context, \ - ValueRegex{parse_regex(regular_expression, schema_context.base, \ - schema_context.relative_pointer), \ - (regular_expression)})}; \ + return { \ + make(sourcemeta::blaze::InstructionIndex::AssertionRegex, context, \ + schema_context, dynamic_context, \ + ValueRegex{parse_regex(regular_expression, schema_context.base, \ + schema_context.relative_pointer), \ + (regular_expression)})}; \ } COMPILE_FORMAT_REGEX("ipv4", FORMAT_REGEX_IPV4) @@ -1052,8 +1451,8 @@ auto compiler_draft4_validation_format(const Context &context, auto compiler_draft4_applicator_not(const Context &context, const SchemaContext &schema_context, - const DynamicContext &dynamic_context) - -> Template { + const DynamicContext &dynamic_context, + const Instructions &) -> Instructions { std::size_t subschemas{0}; for (const auto &subschema : walk_subschemas(context, schema_context, dynamic_context)) { @@ -1064,28 +1463,39 @@ auto compiler_draft4_applicator_not(const Context &context, subschemas += 1; } - Template children{compile(context, schema_context, relative_dynamic_context, - sourcemeta::jsontoolkit::empty_pointer, - sourcemeta::jsontoolkit::empty_pointer)}; + Instructions children{compile(context, schema_context, + relative_dynamic_context(dynamic_context), + sourcemeta::jsontoolkit::empty_pointer, + sourcemeta::jsontoolkit::empty_pointer)}; + + // TODO: Be smarter about how we treat `unevaluatedItems` like how we do for + // `unevaluatedProperties` + const bool track_items{ + std::any_of(context.unevaluated.cbegin(), context.unevaluated.cend(), + [](const auto &dependency) { + return dependency.first.ends_with("unevaluatedItems"); + })}; // Only emit a `not` instruction that keeps track of // evaluation if we really need it. If the "not" subschema // does not define applicators, then that's an easy case // we can skip - if (subschemas > 0 && (!context.unevaluated_properties_schemas.empty() || - !context.unevaluated_items_schemas.empty())) { - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + if (subschemas > 0 && + (requires_evaluation(context, schema_context) || track_items)) { + return {make(sourcemeta::blaze::InstructionIndex::LogicalNotEvaluate, + context, schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } else { - return {make(context, schema_context, dynamic_context, - ValueNone{}, std::move(children))}; + return {make(sourcemeta::blaze::InstructionIndex::LogicalNot, context, + schema_context, dynamic_context, ValueNone{}, + std::move(children))}; } } auto compiler_draft4_applicator_items_array( const Context &context, const SchemaContext &schema_context, const DynamicContext &dynamic_context, const bool annotate, - const bool track_evaluation) -> Template { + const bool track_evaluation) -> Instructions { assert(schema_context.schema.at(dynamic_context.keyword).is_array()); const auto items_size{ schema_context.schema.at(dynamic_context.keyword).size()}; @@ -1100,19 +1510,19 @@ auto compiler_draft4_applicator_items_array( } // Precompile subschemas - std::vector