| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304 |
- #ifdef NDEBUG
- #undef NDEBUG
- #endif
- #include "json-schema-to-grammar.h"
- #include "../src/llama-grammar.h"
- #include <cassert>
- #include <fstream>
- #include <sstream>
- #include <regex>
- static std::string trim(const std::string & source) {
- std::string s(source);
- s.erase(0,s.find_first_not_of(" \n\r\t"));
- s.erase(s.find_last_not_of(" \n\r\t")+1);
- return std::regex_replace(s, std::regex("(^|\n)[ \t]+"), "$1");
- }
- enum TestCaseStatus {
- SUCCESS,
- FAILURE
- };
- struct TestCase {
- TestCaseStatus expected_status;
- std::string name;
- std::string schema;
- std::string expected_grammar;
- void _print_failure_header() const {
- fprintf(stderr, "#\n# Test '%s' failed.\n#\n%s\n", name.c_str(), schema.c_str());
- }
- void verify(const std::string & actual_grammar) const {
- if (trim(actual_grammar) != trim(expected_grammar)) {
- _print_failure_header();
- fprintf(stderr, "# EXPECTED:\n%s\n# ACTUAL:\n%s\n", expected_grammar.c_str(), actual_grammar.c_str());
- assert(false);
- }
- }
- void verify_expectation_parseable() const {
- try {
- llama_grammar_parser state;
- state.parse(expected_grammar.c_str());
- if (state.symbol_ids.find("root") == state.symbol_ids.end()) {
- throw std::runtime_error("Grammar failed to parse:\n" + expected_grammar);
- }
- } catch (const std::runtime_error & ex) {
- _print_failure_header();
- fprintf(stderr, "# GRAMMAR ERROR: %s\n", ex.what());
- assert(false);
- }
- }
- void verify_status(TestCaseStatus status) const {
- if (status != expected_status) {
- _print_failure_header();
- fprintf(stderr, "# EXPECTED STATUS: %s\n", expected_status == SUCCESS ? "SUCCESS" : "FAILURE");
- fprintf(stderr, "# ACTUAL STATUS: %s\n", status == SUCCESS ? "SUCCESS" : "FAILURE");
- assert(false);
- }
- }
- };
- static void write(const std::string & file, const std::string & content) {
- std::ofstream f;
- f.open(file.c_str());
- f << content.c_str();
- f.close();
- }
- static std::string read(const std::string & file) {
- std::ostringstream actuals;
- actuals << std::ifstream(file.c_str()).rdbuf();
- return actuals.str();
- }
- static void test_all(const std::string & lang, std::function<void(const TestCase &)> runner) {
- fprintf(stderr, "#\n# Testing JSON schema conversion (%s)\n#\n", lang.c_str());
- auto test = [&](const TestCase & tc) {
- fprintf(stderr, "- %s%s\n", tc.name.c_str(), tc.expected_status == FAILURE ? " (failure expected)" : "");
- runner(tc);
- };
- test({
- SUCCESS,
- "min 0",
- R"""({
- "type": "integer",
- "minimum": 0
- })""",
- R"""(
- root ::= ([0] | [1-9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 1",
- R"""({
- "type": "integer",
- "minimum": 1
- })""",
- R"""(
- root ::= ([1-9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 3",
- R"""({
- "type": "integer",
- "minimum": 3
- })""",
- R"""(
- root ::= ([1-2] [0-9]{1,15} | [3-9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 9",
- R"""({
- "type": "integer",
- "minimum": 9
- })""",
- R"""(
- root ::= ([1-8] [0-9]{1,15} | [9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 10",
- R"""({
- "type": "integer",
- "minimum": 10
- })""",
- R"""(
- root ::= ([1] ([0-9]{1,15}) | [2-9] [0-9]{1,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 25",
- R"""({
- "type": "integer",
- "minimum": 25
- })""",
- R"""(
- root ::= ([1] [0-9]{2,15} | [2] ([0-4] [0-9]{1,14} | [5-9] [0-9]{0,14}) | [3-9] [0-9]{1,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "max 30",
- R"""({
- "type": "integer",
- "maximum": 30
- })""",
- R"""(
- root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-2] [0-9] | [3] "0")) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min -5",
- R"""({
- "type": "integer",
- "minimum": -5
- })""",
- R"""(
- root ::= ("-" ([0-5]) | [0] | [1-9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min -123",
- R"""({
- "type": "integer",
- "minimum": -123
- })""",
- R"""(
- root ::= ("-" ([0-9] | ([1-8] [0-9] | [9] [0-9]) | "1" ([0-1] [0-9] | [2] [0-3])) | [0] | [1-9] [0-9]{0,15}) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "max -5",
- R"""({
- "type": "integer",
- "maximum": -5
- })""",
- R"""(
- root ::= ("-" ([0-4] [0-9]{1,15} | [5-9] [0-9]{0,15})) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "max 1",
- R"""({
- "type": "integer",
- "maximum": 1
- })""",
- R"""(
- root ::= ("-" [1-9] [0-9]{0,15} | [0-1]) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "max 100",
- R"""({
- "type": "integer",
- "maximum": 100
- })""",
- R"""(
- root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-8] [0-9] | [9] [0-9]) | "100") space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 0 max 23",
- R"""({
- "type": "integer",
- "minimum": 0,
- "maximum": 23
- })""",
- R"""(
- root ::= ([0-9] | ([1] [0-9] | [2] [0-3])) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 15 max 300",
- R"""({
- "type": "integer",
- "minimum": 15,
- "maximum": 300
- })""",
- R"""(
- root ::= (([1] ([5-9]) | [2-9] [0-9]) | ([1-2] [0-9]{2} | [3] "00")) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min 5 max 30",
- R"""({
- "type": "integer",
- "minimum": 5,
- "maximum": 30
- })""",
- R"""(
- root ::= ([5-9] | ([1-2] [0-9] | [3] "0")) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min -123 max 42",
- R"""({
- "type": "integer",
- "minimum": -123,
- "maximum": 42
- })""",
- R"""(
- root ::= ("-" ([0-9] | ([1-8] [0-9] | [9] [0-9]) | "1" ([0-1] [0-9] | [2] [0-3])) | [0-9] | ([1-3] [0-9] | [4] [0-2])) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min -10 max 10",
- R"""({
- "type": "integer",
- "minimum": -10,
- "maximum": 10
- })""",
- R"""(
- root ::= ("-" ([0-9] | "10") | [0-9] | "10") space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- FAILURE,
- "unknown type",
- R"""({
- "type": "kaboom"
- })""",
- ""
- });
- test({
- FAILURE,
- "invalid type",
- R"""({
- "type": 123
- })""",
- ""
- });
- test({
- SUCCESS,
- "empty schema (object)",
- "{}",
- R"""(
- array ::= "[" space ( value ("," space value)* )? "]" space
- boolean ::= ("true" | "false") space
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- null ::= "null" space
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
- root ::= object
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- value ::= object | array | string | number | boolean | null
- )"""
- });
- test({
- SUCCESS,
- "exotic formats",
- R"""({
- "items": [
- { "format": "date" },
- { "format": "uuid" },
- { "format": "time" },
- { "format": "date-time" }
- ]
- })""",
- R"""(
- date ::= [0-9]{4} "-" ( "0" [1-9] | "1" [0-2] ) "-" ( "0" [1-9] | [1-2] [0-9] | "3" [0-1] )
- date-string ::= "\"" date "\"" space
- date-time ::= date "T" time
- date-time-string ::= "\"" date-time "\"" space
- root ::= "[" space tuple-0 "," space uuid "," space tuple-2 "," space tuple-3 "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- time ::= ([01] [0-9] | "2" [0-3]) ":" [0-5] [0-9] ":" [0-5] [0-9] ( "." [0-9]{3} )? ( "Z" | ( "+" | "-" ) ( [01] [0-9] | "2" [0-3] ) ":" [0-5] [0-9] )
- time-string ::= "\"" time "\"" space
- tuple-0 ::= date-string
- tuple-2 ::= time-string
- tuple-3 ::= date-time-string
- uuid ::= "\"" [0-9a-fA-F]{8} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{4} "-" [0-9a-fA-F]{12} "\"" space
- )"""
- });
- test({
- SUCCESS,
- "string",
- R"""({
- "type": "string"
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "\"" char* "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string w/ min length 1",
- R"""({
- "type": "string",
- "minLength": 1
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "\"" char+ "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string w/ min length 3",
- R"""({
- "type": "string",
- "minLength": 3
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "\"" char{3,} "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string w/ max length",
- R"""({
- "type": "string",
- "maxLength": 3
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "\"" char{0,3} "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string w/ min & max length",
- R"""({
- "type": "string",
- "minLength": 1,
- "maxLength": 4
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "\"" char{1,4} "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "boolean",
- R"""({
- "type": "boolean"
- })""",
- R"""(
- root ::= ("true" | "false") space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "integer",
- R"""({
- "type": "integer"
- })""",
- R"""(
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- root ::= ("-"? integral-part) space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string const",
- R"""({
- "const": "foo"
- })""",
- R"""(
- root ::= "\"foo\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "non-string const",
- R"""({
- "const": 123
- })""",
- R"""(
- root ::= "123" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "non-string enum",
- R"""({
- "enum": ["red", "amber", "green", null, 42, ["foo"]]
- })""",
- R"""(
- root ::= ("\"red\"" | "\"amber\"" | "\"green\"" | "null" | "42" | "[\"foo\"]") space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "string array",
- R"""({
- "type": "array",
- "prefixItems": { "type": "string" }
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "[" space (string ("," space string)*)? "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "nullable string array",
- R"""({
- "type": ["array", "null"],
- "prefixItems": { "type": "string" }
- })""",
- R"""(
- alternative-0 ::= "[" space (string ("," space string)*)? "]" space
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- null ::= "null" space
- root ::= alternative-0 | null
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "tuple1",
- R"""({
- "prefixItems": [{ "type": "string" }]
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "[" space string "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "tuple2",
- R"""({
- "prefixItems": [{ "type": "string" }, { "type": "number" }]
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "[" space string "," space number "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "number",
- R"""({
- "type": "number"
- })""",
- R"""(
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- root ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "minItems",
- R"""({
- "items": {
- "type": "boolean"
- },
- "minItems": 2
- })""",
- R"""(
- boolean ::= ("true" | "false") space
- root ::= "[" space boolean ("," space boolean)+ "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "maxItems 0",
- R"""({
- "items": {
- "type": "boolean"
- },
- "maxItems": 0
- })""",
- R"""(
- boolean ::= ("true" | "false") space
- root ::= "[" space "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "maxItems 1",
- R"""({
- "items": {
- "type": "boolean"
- },
- "maxItems": 1
- })""",
- R"""(
- boolean ::= ("true" | "false") space
- root ::= "[" space boolean? "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "maxItems 2",
- R"""({
- "items": {
- "type": "boolean"
- },
- "maxItems": 2
- })""",
- R"""(
- boolean ::= ("true" | "false") space
- root ::= "[" space (boolean ("," space boolean)?)? "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min + maxItems",
- R"""({
- "items": {
- "type": ["number", "integer"]
- },
- "minItems": 3,
- "maxItems": 5
- })""",
- R"""(
- decimal-part ::= [0-9]{1,16}
- integer ::= ("-"? integral-part) space
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- item ::= number | integer
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "[" space item ("," space item){2,4} "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min + max items with min + max values across zero",
- R"""({
- "items": {
- "type": "integer",
- "minimum": -12,
- "maximum": 207
- },
- "minItems": 3,
- "maxItems": 5
- })""",
- R"""(
- item ::= ("-" ([0-9] | "1" [0-2]) | [0-9] | ([1-8] [0-9] | [9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
- root ::= "[" space item ("," space item){2,4} "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "min + max items with min + max values",
- R"""({
- "items": {
- "type": "integer",
- "minimum": 12,
- "maximum": 207
- },
- "minItems": 3,
- "maxItems": 5
- })""",
- R"""(
- item ::= (([1] ([2-9]) | [2-9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
- root ::= "[" space item ("," space item){2,4} "]" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "simple regexp",
- R"""({
- "type": "string",
- "pattern": "^abc?d*efg+(hij)?kl$"
- })""",
- R"""(
- root ::= "\"" ("ab" "c"? "d"* "ef" "g"+ ("hij")? "kl") "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "regexp escapes",
- R"""({
- "type": "string",
- "pattern": "^\\[\\]\\{\\}\\(\\)\\|\\+\\*\\?$"
- })""",
- R"""(
- root ::= "\"" ("[]{}()|+*?") "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "regexp quote",
- R"""({
- "type": "string",
- "pattern": "^\"$"
- })""",
- R"""(
- root ::= "\"" ("\"") "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "regexp with top-level alternation",
- R"""({
- "type": "string",
- "pattern": "^A|B|C|D$"
- })""",
- R"""(
- root ::= "\"" ("A" | "B" | "C" | "D") "\"" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "regexp",
- R"""({
- "type": "string",
- "pattern": "^(\\([0-9]{1,3}\\))?[0-9]{3}-[0-9]{4} a{3,5}nd...$"
- })""",
- R"""(
- dot ::= [^\x0A\x0D]
- root ::= "\"" (("(" root-1{1,3} ")")? root-1{3,3} "-" root-1{4,4} " " "a"{3,5} "nd" dot dot dot) "\"" space
- root-1 ::= [0-9]
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "required props in original order",
- R"""({
- "type": "object",
- "properties": {
- "b": {"type": "string"},
- "c": {"type": "string"},
- "a": {"type": "string"}
- },
- "required": [
- "a",
- "b",
- "c"
- ],
- "additionalProperties": false,
- "definitions": {}
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space string
- b-kv ::= "\"b\"" space ":" space string
- c-kv ::= "\"c\"" space ":" space string
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "{" space b-kv "," space c-kv "," space a-kv "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "1 optional prop",
- R"""({
- "properties": {
- "a": {
- "type": "string"
- }
- },
- "additionalProperties": false
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space string
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "{" space (a-kv )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "N optional props",
- R"""({
- "properties": {
- "a": {"type": "string"},
- "b": {"type": "string"},
- "c": {"type": "string"}
- },
- "additionalProperties": false
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space string
- a-rest ::= ( "," space b-kv )? b-rest
- b-kv ::= "\"b\"" space ":" space string
- b-rest ::= ( "," space c-kv )?
- c-kv ::= "\"c\"" space ":" space string
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- root ::= "{" space (a-kv a-rest | b-kv b-rest | c-kv )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "required + optional props each in original order",
- R"""({
- "properties": {
- "b": {"type": "string"},
- "a": {"type": "string"},
- "d": {"type": "string"},
- "c": {"type": "string"}
- },
- "required": ["a", "b"],
- "additionalProperties": false
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space string
- b-kv ::= "\"b\"" space ":" space string
- c-kv ::= "\"c\"" space ":" space string
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- d-kv ::= "\"d\"" space ":" space string
- d-rest ::= ( "," space c-kv )?
- root ::= "{" space b-kv "," space a-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "additional props",
- R"""({
- "type": "object",
- "additionalProperties": {"type": "array", "items": {"type": "number"}}
- })""",
- R"""(
- additional-kv ::= string ":" space additional-value
- additional-value ::= "[" space (number ("," space number)*)? "]" space
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "{" space (additional-kv ( "," space additional-kv )* )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "additional props (true)",
- R"""({
- "type": "object",
- "additionalProperties": true
- })""",
- R"""(
- array ::= "[" space ( value ("," space value)* )? "]" space
- boolean ::= ("true" | "false") space
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- null ::= "null" space
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
- root ::= object
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- value ::= object | array | string | number | boolean | null
- )"""
- });
- test({
- SUCCESS,
- "additional props (implicit)",
- R"""({
- "type": "object"
- })""",
- R"""(
- array ::= "[" space ( value ("," space value)* )? "]" space
- boolean ::= ("true" | "false") space
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- null ::= "null" space
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
- root ::= object
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- value ::= object | array | string | number | boolean | null
- )"""
- });
- test({
- SUCCESS,
- "empty w/o additional props",
- R"""({
- "type": "object",
- "additionalProperties": false
- })""",
- R"""(
- root ::= "{" space "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "required + additional props",
- R"""({
- "type": "object",
- "properties": {
- "a": {"type": "number"}
- },
- "required": ["a"],
- "additionalProperties": {"type": "string"}
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space number
- additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
- additional-kv ::= additional-k ":" space string
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "{" space a-kv ( "," space ( additional-kv ( "," space additional-kv )* ) )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "optional + additional props",
- R"""({
- "type": "object",
- "properties": {
- "a": {"type": "number"}
- },
- "additionalProperties": {"type": "number"}
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space number
- a-rest ::= ( "," space additional-kv )*
- additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
- additional-kv ::= additional-k ":" space number
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "{" space (a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "required + optional + additional props",
- R"""({
- "type": "object",
- "properties": {
- "and": {"type": "number"},
- "also": {"type": "number"}
- },
- "required": ["and"],
- "additionalProperties": {"type": "number"}
- })""",
- R"""(
- additional-k ::= ["] ( [a] ([l] ([s] ([o] char+ | [^"o] char*) | [^"s] char*) | [n] ([d] char+ | [^"d] char*) | [^"ln] char*) | [^"a] char* )? ["] space
- additional-kv ::= additional-k ":" space number
- also-kv ::= "\"also\"" space ":" space number
- also-rest ::= ( "," space additional-kv )*
- and-kv ::= "\"and\"" space ":" space number
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "{" space and-kv ( "," space ( also-kv also-rest | additional-kv ( "," space additional-kv )* ) )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "optional props with empty name",
- R"""({
- "properties": {
- "": {"type": "integer"},
- "a": {"type": "integer"}
- },
- "additionalProperties": {"type": "integer"}
- })""",
- R"""(
- -kv ::= "\"\"" space ":" space root
- -rest ::= ( "," space a-kv )? a-rest
- a-kv ::= "\"a\"" space ":" space integer
- a-rest ::= ( "," space additional-kv )*
- additional-k ::= ["] ( [a] char+ | [^"a] char* ) ["] space
- additional-kv ::= additional-k ":" space integer
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- integer ::= ("-"? integral-part) space
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- root ::= ("-"? integral-part) space
- root0 ::= "{" space (-kv -rest | a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "optional props with nested names",
- R"""({
- "properties": {
- "a": {"type": "integer"},
- "aa": {"type": "integer"}
- },
- "additionalProperties": {"type": "integer"}
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space integer
- a-rest ::= ( "," space aa-kv )? aa-rest
- aa-kv ::= "\"aa\"" space ":" space integer
- aa-rest ::= ( "," space additional-kv )*
- additional-k ::= ["] ( [a] ([a] char+ | [^"a] char*) | [^"a] char* )? ["] space
- additional-kv ::= additional-k ":" space integer
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- integer ::= ("-"? integral-part) space
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- root ::= "{" space (a-kv a-rest | aa-kv aa-rest | additional-kv ( "," space additional-kv )* )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "optional props with common prefix",
- R"""({
- "properties": {
- "ab": {"type": "integer"},
- "ac": {"type": "integer"}
- },
- "additionalProperties": {"type": "integer"}
- })""",
- R"""(
- ab-kv ::= "\"ab\"" space ":" space integer
- ab-rest ::= ( "," space ac-kv )? ac-rest
- ac-kv ::= "\"ac\"" space ":" space integer
- ac-rest ::= ( "," space additional-kv )*
- additional-k ::= ["] ( [a] ([b] char+ | [c] char+ | [^"bc] char*) | [^"a] char* )? ["] space
- additional-kv ::= additional-k ":" space integer
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- integer ::= ("-"? integral-part) space
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- root ::= "{" space (ab-kv ab-rest | ac-kv ac-rest | additional-kv ( "," space additional-kv )* )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "top-level $ref",
- R"""({
- "$ref": "#/definitions/foo",
- "definitions": {
- "foo": {
- "type": "object",
- "properties": {
- "a": {
- "type": "string"
- }
- },
- "required": [
- "a"
- ],
- "additionalProperties": false
- }
- }
- })""",
- R"""(
- char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
- foo ::= "{" space foo-a-kv "}" space
- foo-a-kv ::= "\"a\"" space ":" space string
- root ::= foo
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- string ::= "\"" char* "\"" space
- )"""
- });
- test({
- SUCCESS,
- "anyOf",
- R"""({
- "anyOf": [
- {"$ref": "#/definitions/foo"},
- {"$ref": "#/definitions/bar"}
- ],
- "definitions": {
- "foo": {
- "properties": {"a": {"type": "number"}}
- },
- "bar": {
- "properties": {"b": {"type": "number"}}
- }
- },
- "type": "object"
- })""",
- R"""(
- alternative-0 ::= foo
- alternative-1 ::= bar
- bar ::= "{" space (bar-b-kv )? "}" space
- bar-b-kv ::= "\"b\"" space ":" space number
- decimal-part ::= [0-9]{1,16}
- foo ::= "{" space (foo-a-kv )? "}" space
- foo-a-kv ::= "\"a\"" space ":" space number
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= alternative-0 | alternative-1
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "mix of allOf, anyOf and $ref (similar to https://json.schemastore.org/tsconfig.json)",
- R"""({
- "allOf": [
- {"$ref": "#/definitions/foo"},
- {"$ref": "#/definitions/bar"},
- {
- "anyOf": [
- {"$ref": "#/definitions/baz"},
- {"$ref": "#/definitions/bam"}
- ]
- }
- ],
- "definitions": {
- "foo": {
- "properties": {"a": {"type": "number"}}
- },
- "bar": {
- "properties": {"b": {"type": "number"}}
- },
- "bam": {
- "properties": {"c": {"type": "number"}}
- },
- "baz": {
- "properties": {"d": {"type": "number"}}
- }
- },
- "type": "object"
- })""",
- R"""(
- a-kv ::= "\"a\"" space ":" space number
- b-kv ::= "\"b\"" space ":" space number
- c-kv ::= "\"c\"" space ":" space number
- d-kv ::= "\"d\"" space ":" space number
- d-rest ::= ( "," space c-kv )?
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- root ::= "{" space a-kv "," space b-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- test({
- SUCCESS,
- "conflicting names",
- R"""({
- "type": "object",
- "properties": {
- "number": {
- "type": "object",
- "properties": {
- "number": {
- "type": "object",
- "properties": {
- "root": {
- "type": "number"
- }
- },
- "required": [
- "root"
- ],
- "additionalProperties": false
- }
- },
- "required": [
- "number"
- ],
- "additionalProperties": false
- }
- },
- "required": [
- "number"
- ],
- "additionalProperties": false,
- "definitions": {}
- })""",
- R"""(
- decimal-part ::= [0-9]{1,16}
- integral-part ::= [0] | [1-9] [0-9]{0,15}
- number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
- number- ::= "{" space number-number-kv "}" space
- number-kv ::= "\"number\"" space ":" space number-
- number-number ::= "{" space number-number-root-kv "}" space
- number-number-kv ::= "\"number\"" space ":" space number-number
- number-number-root-kv ::= "\"root\"" space ":" space number
- root ::= "{" space number-kv "}" space
- space ::= | " " | "\n"{1,2} [ \t]{0,20}
- )"""
- });
- }
- int main() {
- fprintf(stderr, "LLAMA_NODE_AVAILABLE = %s\n", getenv("LLAMA_NODE_AVAILABLE") ? "true" : "false");
- fprintf(stderr, "LLAMA_PYTHON_AVAILABLE = %s\n", getenv("LLAMA_PYTHON_AVAILABLE") ? "true" : "false");
- test_all("C++", [](const TestCase & tc) {
- try {
- tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema), true));
- tc.verify_status(SUCCESS);
- } catch (const std::runtime_error & ex) {
- fprintf(stderr, "Error: %s\n", ex.what());
- tc.verify_status(FAILURE);
- }
- });
- if (getenv("LLAMA_SKIP_TESTS_SLOW_ON_EMULATOR")) {
- fprintf(stderr, "\033[33mWARNING: Skipping slow tests on emulator.\n\033[0m");
- } else {
- if (getenv("LLAMA_PYTHON_AVAILABLE") || (std::system("python -c \"import sys; exit(1) if sys.version_info < (3, 8) else print('Python version is sufficient')\"") == 0)) {
- test_all("Python", [](const TestCase & tc) {
- write("test-json-schema-input.tmp", tc.schema);
- tc.verify_status(std::system(
- "python ./examples/json_schema_to_grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
- tc.verify(read("test-grammar-output.tmp"));
- });
- } else {
- fprintf(stderr, "\033[33mWARNING: Python not found (min version required is 3.8), skipping Python JSON schema -> grammar tests.\n\033[0m");
- }
- if (getenv("LLAMA_NODE_AVAILABLE") || (std::system("node --version") == 0)) {
- test_all("JavaScript", [](const TestCase & tc) {
- write("test-json-schema-input.tmp", tc.schema);
- tc.verify_status(std::system(
- "node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
- tc.verify(read("test-grammar-output.tmp"));
- });
- } else {
- fprintf(stderr, "\033[33mWARNING: Node not found, skipping JavaScript JSON schema -> grammar tests.\n\033[0m");
- }
- }
- test_all("Check Expectations Validity", [](const TestCase & tc) {
- if (tc.expected_status == SUCCESS) {
- tc.verify_expectation_parseable();
- }
- });
- }
|