diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 322f91b6e0..e2c5e7c038 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -998,7 +998,7 @@ std::string json_schema_to_grammar(const common_json & schema, bool force_gbnf) (void)force_gbnf; #endif // LLAMA_USE_LLGUIDANCE try { - return json_schema_to_grammar(common_schema_parse(schema)); + return json_schema_to_grammar(common_schema_from_json(schema)); } catch (const std::runtime_error & e) { throw std::invalid_argument(std::string("JSON schema conversion failed:\n") + e.what()); } diff --git a/common/json-schema.cpp b/common/json-schema.cpp index d0c42592f8..a9a55fddc5 100644 --- a/common/json-schema.cpp +++ b/common/json-schema.cpp @@ -9,14 +9,14 @@ #include #include -class common_schema_parser { +class common_schema_builder { const common_json & root_; common_schema_document & doc_; - // the targets parsed here, moved into doc_ once the whole schema parsed + // the targets built here, moved into doc_ once the whole schema is built std::map refs_; - // ref nodes get their target once every $ref is parsed, a cycle would otherwise need it too early + // ref nodes get their target once every $ref is built, a cycle would otherwise need it too early std::vector pending_; [[noreturn]] static void fail(const std::string & path, const std::string & msg) { @@ -323,9 +323,9 @@ class common_schema_parser { } public: - common_schema_parser(const common_json & root, common_schema_document & doc) : root_(root), doc_(doc) {} + common_schema_builder(const common_json & root, common_schema_document & doc) : root_(root), doc_(doc) {} - common_schema_ptr parse() { + common_schema_ptr build() { auto node = parse_schema(root_, "#"); for (auto & entry : refs_) { doc_.refs[entry.first] = std::move(entry.second); @@ -337,14 +337,14 @@ class common_schema_parser { } }; -common_schema_document common_schema_parse(const common_json & schema) { +common_schema_document common_schema_from_json(const common_json & schema) { common_schema_document doc; - doc.root = common_schema_parser(schema, doc).parse(); + doc.root = common_schema_builder(schema, doc).build(); return doc; } -common_schema_ptr common_schema_parse(const common_json & schema, common_schema_document & doc) { - return common_schema_parser(schema, doc).parse(); +common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc) { + return common_schema_builder(schema, doc).build(); } static common_schema::value_type json_type(const common_json & value) { diff --git a/common/json-schema.h b/common/json-schema.h index 60db8afd0e..a5029081a2 100644 --- a/common/json-schema.h +++ b/common/json-schema.h @@ -191,13 +191,13 @@ struct common_schema_document { std::map refs; }; -// A document shared by the parsers built from its nodes, which it keeps alive +// A document shared by the PEG parsers built from its nodes, which it keeps alive using common_schema_document_ptr = std::shared_ptr; // Throws std::runtime_error when the schema falls outside the supported subset. -common_schema_document common_schema_parse(const common_json & schema); +common_schema_document common_schema_from_json(const common_json & schema); -// Parses a schema that belongs to a document parsed earlier, e.g. one property of it. +// Builds a schema that belongs to a document built earlier, e.g. one property of it. // A $ref it cannot resolve on its own is looked up in doc.refs, the targets it resolves itself are added there. // doc is unchanged when the schema is rejected. -common_schema_ptr common_schema_parse(const common_json & schema, common_schema_document & doc); +common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc); diff --git a/common/parsers/minimax-m3.cpp b/common/parsers/minimax-m3.cpp index 1b5d37f621..7e123a1dd4 100644 --- a/common/parsers/minimax-m3.cpp +++ b/common/parsers/minimax-m3.cpp @@ -89,7 +89,7 @@ common_chat_params common_chat_params_init_minimax_m3(const common_chat_template const auto & function = tool.at("function"); std::string name = function.at("name"); auto params = function.contains("parameters") ? function.at("parameters") : json::object(); - auto doc = std::make_shared(common_schema_parse(params)); + auto doc = std::make_shared(common_schema_from_json(params)); // The template expands argument values recursively in XML (see the to_xml() macro) std::function value_of; diff --git a/common/parsers/parsers.cpp b/common/parsers/parsers.cpp index d3e752099b..28778af091 100644 --- a/common/parsers/parsers.cpp +++ b/common/parsers/parsers.cpp @@ -14,7 +14,7 @@ void foreach_function(const json & tools, const std::function & fn) { auto params = function.contains("parameters") ? function.at("parameters") : json::object(); - auto doc = std::make_shared(common_schema_parse(params)); + auto doc = std::make_shared(common_schema_from_json(params)); const auto * object = dynamic_cast(doc->root.get()); if (!object) { return; diff --git a/common/peg-parser.cpp b/common/peg-parser.cpp index f346728986..6fc793220d 100644 --- a/common/peg-parser.cpp +++ b/common/peg-parser.cpp @@ -1124,7 +1124,7 @@ common_peg_parser common_peg_parser_builder::schema(const common_peg_parser & p, } common_peg_parser common_peg_parser_builder::schema(const common_peg_parser & p, const std::string & name, const common_json & schema, bool raw) { - auto doc = std::make_shared(common_schema_parse(schema)); + auto doc = std::make_shared(common_schema_from_json(schema)); return this->schema(p, name, doc, *doc->root, raw); } diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index 826081a360..b3f29a3c5a 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -1534,7 +1534,7 @@ int main() { } } })"""); - assert(json_schema_to_grammar(common_schema_parse(schema)) == json_schema_to_grammar(schema, true)); + assert(json_schema_to_grammar(common_schema_from_json(schema)) == json_schema_to_grammar(schema, true)); } // a property node carries its $ref target, so its grammar names the ref rule @@ -1565,7 +1565,7 @@ int main() { string ::= "\"" char* "\"" )""", }; - auto doc = common_schema_parse(parameters); + auto doc = common_schema_from_json(parameters); tc.verify(build_grammar([&](const common_grammar_builder & builder) { const auto & item = static_cast(*doc.root).properties.at(0); builder.add_schema("root", *item.schema); diff --git a/tests/test-json-schema.cpp b/tests/test-json-schema.cpp index df99f6d2bb..1c5dcbb78a 100644 --- a/tests/test-json-schema.cpp +++ b/tests/test-json-schema.cpp @@ -9,7 +9,7 @@ #include static common_schema_document parse(const std::string & schema) { - return common_schema_parse(common_json::parse(schema)); + return common_schema_from_json(common_json::parse(schema)); } // the node as T, aborting the current test when it is some other kind @@ -370,13 +370,13 @@ static void test_ref(testing & t) { t.test("a schema parsed into a document shares its refs", [](testing & t) { auto doc = parse(R"({"properties": {"a": {"$ref": "#/$defs/t"}}, "$defs": {"t": {"type": "boolean"}}})"); - auto node = common_schema_parse(common_json::parse(R"({"items": {"$ref": "#/$defs/t"}})"), doc); + auto node = common_schema_from_json(common_json::parse(R"({"items": {"$ref": "#/$defs/t"}})"), doc); const auto & a = as(t, node.get(), "node"); const auto & r = as(t, a.items.get(), "items"); t.assert_true("shared target", r.target == doc.refs.at("#/$defs/t").get()); t.assert_equal("refs", (size_t) 1, doc.refs.size()); - auto added = common_schema_parse(common_json::parse(R"({"$ref": "#/$defs/u", "$defs": {"u": {"type": "null"}}})"), doc); + auto added = common_schema_from_json(common_json::parse(R"({"$ref": "#/$defs/u", "$defs": {"u": {"type": "null"}}})"), doc); as(t, as(t, added.get(), "added").target, "target"); t.assert_equal("refs", (size_t) 2, doc.refs.size()); }); @@ -384,7 +384,7 @@ static void test_ref(testing & t) { t.test("a rejected schema leaves the document unchanged", [](testing & t) { common_schema_document doc; try { - common_schema_parse(common_json::parse(R"({"allOf": [{"$ref": "#/$defs/t"}, {"type": "x"}], "$defs": {"t": {"type": "null"}}})"), doc); + common_schema_from_json(common_json::parse(R"({"allOf": [{"$ref": "#/$defs/t"}, {"type": "x"}], "$defs": {"t": {"type": "null"}}})"), doc); t.assert_true("rejected", false); } catch (const std::runtime_error &) { t.assert_true("no refs", doc.refs.empty());