1
0

test-json-schema-to-grammar.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include "json-schema-to-grammar.h"
  5. #include "../src/llama-grammar.h"
  6. #include <nlohmann/json.hpp>
  7. #include <cassert>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <regex>
  11. static std::string trim(const std::string & source) {
  12. std::string s(source);
  13. s.erase(0,s.find_first_not_of(" \n\r\t"));
  14. s.erase(s.find_last_not_of(" \n\r\t")+1);
  15. return std::regex_replace(s, std::regex("(^|\n)[ \t]+"), "$1");
  16. }
  17. enum TestCaseStatus {
  18. SUCCESS,
  19. FAILURE
  20. };
  21. struct TestCase {
  22. TestCaseStatus expected_status;
  23. std::string name;
  24. std::string schema;
  25. std::string expected_grammar;
  26. void _print_failure_header() const {
  27. fprintf(stderr, "#\n# Test '%s' failed.\n#\n%s\n", name.c_str(), schema.c_str());
  28. }
  29. void verify(const std::string & actual_grammar) const {
  30. if (trim(actual_grammar) != trim(expected_grammar)) {
  31. _print_failure_header();
  32. fprintf(stderr, "# EXPECTED:\n%s\n# ACTUAL:\n%s\n", expected_grammar.c_str(), actual_grammar.c_str());
  33. assert(false);
  34. }
  35. }
  36. void verify_expectation_parseable() const {
  37. try {
  38. llama_grammar_parser state;
  39. state.parse(expected_grammar.c_str());
  40. if (state.symbol_ids.find("root") == state.symbol_ids.end()) {
  41. throw std::runtime_error("Grammar failed to parse:\n" + expected_grammar);
  42. }
  43. } catch (const std::runtime_error & ex) {
  44. _print_failure_header();
  45. fprintf(stderr, "# GRAMMAR ERROR: %s\n", ex.what());
  46. assert(false);
  47. }
  48. }
  49. void verify_status(TestCaseStatus status) const {
  50. if (status != expected_status) {
  51. _print_failure_header();
  52. fprintf(stderr, "# EXPECTED STATUS: %s\n", expected_status == SUCCESS ? "SUCCESS" : "FAILURE");
  53. fprintf(stderr, "# ACTUAL STATUS: %s\n", status == SUCCESS ? "SUCCESS" : "FAILURE");
  54. assert(false);
  55. }
  56. }
  57. };
  58. static void write(const std::string & file, const std::string & content) {
  59. std::ofstream f;
  60. f.open(file.c_str());
  61. f << content.c_str();
  62. f.close();
  63. }
  64. static std::string read(const std::string & file) {
  65. std::ostringstream actuals;
  66. actuals << std::ifstream(file.c_str()).rdbuf();
  67. return actuals.str();
  68. }
  69. static void test_all(const std::string & lang, std::function<void(const TestCase &)> runner) {
  70. fprintf(stderr, "#\n# Testing JSON schema conversion (%s)\n#\n", lang.c_str());
  71. auto test = [&](const TestCase & tc) {
  72. fprintf(stderr, "- %s%s\n", tc.name.c_str(), tc.expected_status == FAILURE ? " (failure expected)" : "");
  73. runner(tc);
  74. };
  75. test({
  76. SUCCESS,
  77. "min 0",
  78. R"""({
  79. "type": "integer",
  80. "minimum": 0
  81. })""",
  82. R"""(
  83. root ::= ([0] | [1-9] [0-9]{0,15}) space
  84. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  85. )"""
  86. });
  87. test({
  88. SUCCESS,
  89. "min 1",
  90. R"""({
  91. "type": "integer",
  92. "minimum": 1
  93. })""",
  94. R"""(
  95. root ::= ([1-9] [0-9]{0,15}) space
  96. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  97. )"""
  98. });
  99. test({
  100. SUCCESS,
  101. "min 3",
  102. R"""({
  103. "type": "integer",
  104. "minimum": 3
  105. })""",
  106. R"""(
  107. root ::= ([1-2] [0-9]{1,15} | [3-9] [0-9]{0,15}) space
  108. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  109. )"""
  110. });
  111. test({
  112. SUCCESS,
  113. "min 9",
  114. R"""({
  115. "type": "integer",
  116. "minimum": 9
  117. })""",
  118. R"""(
  119. root ::= ([1-8] [0-9]{1,15} | [9] [0-9]{0,15}) space
  120. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  121. )"""
  122. });
  123. test({
  124. SUCCESS,
  125. "min 10",
  126. R"""({
  127. "type": "integer",
  128. "minimum": 10
  129. })""",
  130. R"""(
  131. root ::= ([1] ([0-9]{1,15}) | [2-9] [0-9]{1,15}) space
  132. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  133. )"""
  134. });
  135. test({
  136. SUCCESS,
  137. "min 25",
  138. R"""({
  139. "type": "integer",
  140. "minimum": 25
  141. })""",
  142. R"""(
  143. 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
  144. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  145. )"""
  146. });
  147. test({
  148. SUCCESS,
  149. "max 30",
  150. R"""({
  151. "type": "integer",
  152. "maximum": 30
  153. })""",
  154. R"""(
  155. root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-2] [0-9] | [3] "0")) space
  156. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  157. )"""
  158. });
  159. test({
  160. SUCCESS,
  161. "min -5",
  162. R"""({
  163. "type": "integer",
  164. "minimum": -5
  165. })""",
  166. R"""(
  167. root ::= ("-" ([0-5]) | [0] | [1-9] [0-9]{0,15}) space
  168. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  169. )"""
  170. });
  171. test({
  172. SUCCESS,
  173. "min -123",
  174. R"""({
  175. "type": "integer",
  176. "minimum": -123
  177. })""",
  178. R"""(
  179. 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
  180. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  181. )"""
  182. });
  183. test({
  184. SUCCESS,
  185. "max -5",
  186. R"""({
  187. "type": "integer",
  188. "maximum": -5
  189. })""",
  190. R"""(
  191. root ::= ("-" ([0-4] [0-9]{1,15} | [5-9] [0-9]{0,15})) space
  192. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  193. )"""
  194. });
  195. test({
  196. SUCCESS,
  197. "max 1",
  198. R"""({
  199. "type": "integer",
  200. "maximum": 1
  201. })""",
  202. R"""(
  203. root ::= ("-" [1-9] [0-9]{0,15} | [0-1]) space
  204. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  205. )"""
  206. });
  207. test({
  208. SUCCESS,
  209. "max 100",
  210. R"""({
  211. "type": "integer",
  212. "maximum": 100
  213. })""",
  214. R"""(
  215. root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-8] [0-9] | [9] [0-9]) | "100") space
  216. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  217. )"""
  218. });
  219. test({
  220. SUCCESS,
  221. "min 0 max 23",
  222. R"""({
  223. "type": "integer",
  224. "minimum": 0,
  225. "maximum": 23
  226. })""",
  227. R"""(
  228. root ::= ([0-9] | ([1] [0-9] | [2] [0-3])) space
  229. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  230. )"""
  231. });
  232. test({
  233. SUCCESS,
  234. "min 15 max 300",
  235. R"""({
  236. "type": "integer",
  237. "minimum": 15,
  238. "maximum": 300
  239. })""",
  240. R"""(
  241. root ::= (([1] ([5-9]) | [2-9] [0-9]) | ([1-2] [0-9]{2} | [3] "00")) space
  242. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  243. )"""
  244. });
  245. test({
  246. SUCCESS,
  247. "min 5 max 30",
  248. R"""({
  249. "type": "integer",
  250. "minimum": 5,
  251. "maximum": 30
  252. })""",
  253. R"""(
  254. root ::= ([5-9] | ([1-2] [0-9] | [3] "0")) space
  255. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  256. )"""
  257. });
  258. test({
  259. SUCCESS,
  260. "min -123 max 42",
  261. R"""({
  262. "type": "integer",
  263. "minimum": -123,
  264. "maximum": 42
  265. })""",
  266. R"""(
  267. 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
  268. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  269. )"""
  270. });
  271. test({
  272. SUCCESS,
  273. "min -10 max 10",
  274. R"""({
  275. "type": "integer",
  276. "minimum": -10,
  277. "maximum": 10
  278. })""",
  279. R"""(
  280. root ::= ("-" ([0-9] | "10") | [0-9] | "10") space
  281. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  282. )"""
  283. });
  284. test({
  285. FAILURE,
  286. "unknown type",
  287. R"""({
  288. "type": "kaboom"
  289. })""",
  290. ""
  291. });
  292. test({
  293. FAILURE,
  294. "invalid type",
  295. R"""({
  296. "type": 123
  297. })""",
  298. ""
  299. });
  300. test({
  301. SUCCESS,
  302. "empty schema (object)",
  303. "{}",
  304. R"""(
  305. array ::= "[" space ( value ("," space value)* )? "]" space
  306. boolean ::= ("true" | "false") space
  307. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  308. decimal-part ::= [0-9]{1,16}
  309. integral-part ::= [0] | [1-9] [0-9]{0,15}
  310. null ::= "null" space
  311. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  312. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  313. root ::= object
  314. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  315. string ::= "\"" char* "\"" space
  316. value ::= object | array | string | number | boolean | null
  317. )"""
  318. });
  319. test({
  320. SUCCESS,
  321. "exotic formats",
  322. R"""({
  323. "items": [
  324. { "format": "date" },
  325. { "format": "uuid" },
  326. { "format": "time" },
  327. { "format": "date-time" }
  328. ]
  329. })""",
  330. R"""(
  331. date ::= [0-9]{4} "-" ( "0" [1-9] | "1" [0-2] ) "-" ( "0" [1-9] | [1-2] [0-9] | "3" [0-1] )
  332. date-string ::= "\"" date "\"" space
  333. date-time ::= date "T" time
  334. date-time-string ::= "\"" date-time "\"" space
  335. root ::= "[" space tuple-0 "," space uuid "," space tuple-2 "," space tuple-3 "]" space
  336. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  337. 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] )
  338. time-string ::= "\"" time "\"" space
  339. tuple-0 ::= date-string
  340. tuple-2 ::= time-string
  341. tuple-3 ::= date-time-string
  342. 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
  343. )"""
  344. });
  345. test({
  346. SUCCESS,
  347. "string",
  348. R"""({
  349. "type": "string"
  350. })""",
  351. R"""(
  352. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  353. root ::= "\"" char* "\"" space
  354. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  355. )"""
  356. });
  357. test({
  358. SUCCESS,
  359. "string w/ min length 1",
  360. R"""({
  361. "type": "string",
  362. "minLength": 1
  363. })""",
  364. R"""(
  365. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  366. root ::= "\"" char+ "\"" space
  367. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  368. )"""
  369. });
  370. test({
  371. SUCCESS,
  372. "string w/ min length 3",
  373. R"""({
  374. "type": "string",
  375. "minLength": 3
  376. })""",
  377. R"""(
  378. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  379. root ::= "\"" char{3,} "\"" space
  380. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  381. )"""
  382. });
  383. test({
  384. SUCCESS,
  385. "string w/ max length",
  386. R"""({
  387. "type": "string",
  388. "maxLength": 3
  389. })""",
  390. R"""(
  391. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  392. root ::= "\"" char{0,3} "\"" space
  393. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  394. )"""
  395. });
  396. test({
  397. SUCCESS,
  398. "string w/ min & max length",
  399. R"""({
  400. "type": "string",
  401. "minLength": 1,
  402. "maxLength": 4
  403. })""",
  404. R"""(
  405. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  406. root ::= "\"" char{1,4} "\"" space
  407. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  408. )"""
  409. });
  410. test({
  411. SUCCESS,
  412. "boolean",
  413. R"""({
  414. "type": "boolean"
  415. })""",
  416. R"""(
  417. root ::= ("true" | "false") space
  418. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  419. )"""
  420. });
  421. test({
  422. SUCCESS,
  423. "integer",
  424. R"""({
  425. "type": "integer"
  426. })""",
  427. R"""(
  428. integral-part ::= [0] | [1-9] [0-9]{0,15}
  429. root ::= ("-"? integral-part) space
  430. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  431. )"""
  432. });
  433. test({
  434. SUCCESS,
  435. "string const",
  436. R"""({
  437. "const": "foo"
  438. })""",
  439. R"""(
  440. root ::= "\"foo\"" space
  441. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  442. )"""
  443. });
  444. test({
  445. SUCCESS,
  446. "non-string const",
  447. R"""({
  448. "const": 123
  449. })""",
  450. R"""(
  451. root ::= "123" space
  452. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  453. )"""
  454. });
  455. test({
  456. SUCCESS,
  457. "non-string enum",
  458. R"""({
  459. "enum": ["red", "amber", "green", null, 42, ["foo"]]
  460. })""",
  461. R"""(
  462. root ::= ("\"red\"" | "\"amber\"" | "\"green\"" | "null" | "42" | "[\"foo\"]") space
  463. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  464. )"""
  465. });
  466. test({
  467. SUCCESS,
  468. "string array",
  469. R"""({
  470. "type": "array",
  471. "prefixItems": { "type": "string" }
  472. })""",
  473. R"""(
  474. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  475. root ::= "[" space (string ("," space string)*)? "]" space
  476. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  477. string ::= "\"" char* "\"" space
  478. )"""
  479. });
  480. test({
  481. SUCCESS,
  482. "nullable string array",
  483. R"""({
  484. "type": ["array", "null"],
  485. "prefixItems": { "type": "string" }
  486. })""",
  487. R"""(
  488. alternative-0 ::= "[" space (string ("," space string)*)? "]" space
  489. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  490. null ::= "null" space
  491. root ::= alternative-0 | null
  492. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  493. string ::= "\"" char* "\"" space
  494. )"""
  495. });
  496. test({
  497. SUCCESS,
  498. "tuple1",
  499. R"""({
  500. "prefixItems": [{ "type": "string" }]
  501. })""",
  502. R"""(
  503. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  504. root ::= "[" space string "]" space
  505. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  506. string ::= "\"" char* "\"" space
  507. )"""
  508. });
  509. test({
  510. SUCCESS,
  511. "tuple2",
  512. R"""({
  513. "prefixItems": [{ "type": "string" }, { "type": "number" }]
  514. })""",
  515. R"""(
  516. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  517. decimal-part ::= [0-9]{1,16}
  518. integral-part ::= [0] | [1-9] [0-9]{0,15}
  519. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  520. root ::= "[" space string "," space number "]" space
  521. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  522. string ::= "\"" char* "\"" space
  523. )"""
  524. });
  525. test({
  526. SUCCESS,
  527. "number",
  528. R"""({
  529. "type": "number"
  530. })""",
  531. R"""(
  532. decimal-part ::= [0-9]{1,16}
  533. integral-part ::= [0] | [1-9] [0-9]{0,15}
  534. root ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  535. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  536. )"""
  537. });
  538. test({
  539. SUCCESS,
  540. "minItems",
  541. R"""({
  542. "items": {
  543. "type": "boolean"
  544. },
  545. "minItems": 2
  546. })""",
  547. R"""(
  548. boolean ::= ("true" | "false") space
  549. root ::= "[" space boolean ("," space boolean)+ "]" space
  550. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  551. )"""
  552. });
  553. test({
  554. SUCCESS,
  555. "maxItems 0",
  556. R"""({
  557. "items": {
  558. "type": "boolean"
  559. },
  560. "maxItems": 0
  561. })""",
  562. R"""(
  563. boolean ::= ("true" | "false") space
  564. root ::= "[" space "]" space
  565. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  566. )"""
  567. });
  568. test({
  569. SUCCESS,
  570. "maxItems 1",
  571. R"""({
  572. "items": {
  573. "type": "boolean"
  574. },
  575. "maxItems": 1
  576. })""",
  577. R"""(
  578. boolean ::= ("true" | "false") space
  579. root ::= "[" space boolean? "]" space
  580. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  581. )"""
  582. });
  583. test({
  584. SUCCESS,
  585. "maxItems 2",
  586. R"""({
  587. "items": {
  588. "type": "boolean"
  589. },
  590. "maxItems": 2
  591. })""",
  592. R"""(
  593. boolean ::= ("true" | "false") space
  594. root ::= "[" space (boolean ("," space boolean)?)? "]" space
  595. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  596. )"""
  597. });
  598. test({
  599. SUCCESS,
  600. "min + maxItems",
  601. R"""({
  602. "items": {
  603. "type": ["number", "integer"]
  604. },
  605. "minItems": 3,
  606. "maxItems": 5
  607. })""",
  608. R"""(
  609. decimal-part ::= [0-9]{1,16}
  610. integer ::= ("-"? integral-part) space
  611. integral-part ::= [0] | [1-9] [0-9]{0,15}
  612. item ::= number | integer
  613. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  614. root ::= "[" space item ("," space item){2,4} "]" space
  615. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  616. )"""
  617. });
  618. test({
  619. SUCCESS,
  620. "min + max items with min + max values across zero",
  621. R"""({
  622. "items": {
  623. "type": "integer",
  624. "minimum": -12,
  625. "maximum": 207
  626. },
  627. "minItems": 3,
  628. "maxItems": 5
  629. })""",
  630. R"""(
  631. item ::= ("-" ([0-9] | "1" [0-2]) | [0-9] | ([1-8] [0-9] | [9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
  632. root ::= "[" space item ("," space item){2,4} "]" space
  633. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  634. )"""
  635. });
  636. test({
  637. SUCCESS,
  638. "min + max items with min + max values",
  639. R"""({
  640. "items": {
  641. "type": "integer",
  642. "minimum": 12,
  643. "maximum": 207
  644. },
  645. "minItems": 3,
  646. "maxItems": 5
  647. })""",
  648. R"""(
  649. item ::= (([1] ([2-9]) | [2-9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
  650. root ::= "[" space item ("," space item){2,4} "]" space
  651. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  652. )"""
  653. });
  654. test({
  655. SUCCESS,
  656. "simple regexp",
  657. R"""({
  658. "type": "string",
  659. "pattern": "^abc?d*efg+(hij)?kl$"
  660. })""",
  661. R"""(
  662. root ::= "\"" ("ab" "c"? "d"* "ef" "g"+ ("hij")? "kl") "\"" space
  663. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  664. )"""
  665. });
  666. test({
  667. SUCCESS,
  668. "regexp escapes",
  669. R"""({
  670. "type": "string",
  671. "pattern": "^\\[\\]\\{\\}\\(\\)\\|\\+\\*\\?$"
  672. })""",
  673. R"""(
  674. root ::= "\"" ("[]{}()|+*?") "\"" space
  675. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  676. )"""
  677. });
  678. test({
  679. SUCCESS,
  680. "regexp quote",
  681. R"""({
  682. "type": "string",
  683. "pattern": "^\"$"
  684. })""",
  685. R"""(
  686. root ::= "\"" ("\"") "\"" space
  687. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  688. )"""
  689. });
  690. test({
  691. SUCCESS,
  692. "regexp with top-level alternation",
  693. R"""({
  694. "type": "string",
  695. "pattern": "^A|B|C|D$"
  696. })""",
  697. R"""(
  698. root ::= "\"" ("A" | "B" | "C" | "D") "\"" space
  699. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  700. )"""
  701. });
  702. test({
  703. SUCCESS,
  704. "regexp",
  705. R"""({
  706. "type": "string",
  707. "pattern": "^(\\([0-9]{1,3}\\))?[0-9]{3}-[0-9]{4} a{3,5}nd...$"
  708. })""",
  709. R"""(
  710. dot ::= [^\x0A\x0D]
  711. root ::= "\"" (("(" root-1{1,3} ")")? root-1{3,3} "-" root-1{4,4} " " "a"{3,5} "nd" dot dot dot) "\"" space
  712. root-1 ::= [0-9]
  713. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  714. )"""
  715. });
  716. test({
  717. SUCCESS,
  718. "required props in original order",
  719. R"""({
  720. "type": "object",
  721. "properties": {
  722. "b": {"type": "string"},
  723. "c": {"type": "string"},
  724. "a": {"type": "string"}
  725. },
  726. "required": [
  727. "a",
  728. "b",
  729. "c"
  730. ],
  731. "additionalProperties": false,
  732. "definitions": {}
  733. })""",
  734. R"""(
  735. a-kv ::= "\"a\"" space ":" space string
  736. b-kv ::= "\"b\"" space ":" space string
  737. c-kv ::= "\"c\"" space ":" space string
  738. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  739. root ::= "{" space b-kv "," space c-kv "," space a-kv "}" space
  740. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  741. string ::= "\"" char* "\"" space
  742. )"""
  743. });
  744. test({
  745. SUCCESS,
  746. "1 optional prop",
  747. R"""({
  748. "properties": {
  749. "a": {
  750. "type": "string"
  751. }
  752. },
  753. "additionalProperties": false
  754. })""",
  755. R"""(
  756. a-kv ::= "\"a\"" space ":" space string
  757. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  758. root ::= "{" space (a-kv )? "}" space
  759. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  760. string ::= "\"" char* "\"" space
  761. )"""
  762. });
  763. test({
  764. SUCCESS,
  765. "N optional props",
  766. R"""({
  767. "properties": {
  768. "a": {"type": "string"},
  769. "b": {"type": "string"},
  770. "c": {"type": "string"}
  771. },
  772. "additionalProperties": false
  773. })""",
  774. R"""(
  775. a-kv ::= "\"a\"" space ":" space string
  776. a-rest ::= ( "," space b-kv )? b-rest
  777. b-kv ::= "\"b\"" space ":" space string
  778. b-rest ::= ( "," space c-kv )?
  779. c-kv ::= "\"c\"" space ":" space string
  780. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  781. root ::= "{" space (a-kv a-rest | b-kv b-rest | c-kv )? "}" space
  782. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  783. string ::= "\"" char* "\"" space
  784. )"""
  785. });
  786. test({
  787. SUCCESS,
  788. "required + optional props each in original order",
  789. R"""({
  790. "properties": {
  791. "b": {"type": "string"},
  792. "a": {"type": "string"},
  793. "d": {"type": "string"},
  794. "c": {"type": "string"}
  795. },
  796. "required": ["a", "b"],
  797. "additionalProperties": false
  798. })""",
  799. R"""(
  800. a-kv ::= "\"a\"" space ":" space string
  801. b-kv ::= "\"b\"" space ":" space string
  802. c-kv ::= "\"c\"" space ":" space string
  803. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  804. d-kv ::= "\"d\"" space ":" space string
  805. d-rest ::= ( "," space c-kv )?
  806. root ::= "{" space b-kv "," space a-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
  807. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  808. string ::= "\"" char* "\"" space
  809. )"""
  810. });
  811. test({
  812. SUCCESS,
  813. "additional props",
  814. R"""({
  815. "type": "object",
  816. "additionalProperties": {"type": "array", "items": {"type": "number"}}
  817. })""",
  818. R"""(
  819. additional-kv ::= string ":" space additional-value
  820. additional-value ::= "[" space (number ("," space number)*)? "]" space
  821. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  822. decimal-part ::= [0-9]{1,16}
  823. integral-part ::= [0] | [1-9] [0-9]{0,15}
  824. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  825. root ::= "{" space (additional-kv ( "," space additional-kv )* )? "}" space
  826. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  827. string ::= "\"" char* "\"" space
  828. )"""
  829. });
  830. test({
  831. SUCCESS,
  832. "additional props (true)",
  833. R"""({
  834. "type": "object",
  835. "additionalProperties": true
  836. })""",
  837. R"""(
  838. array ::= "[" space ( value ("," space value)* )? "]" space
  839. boolean ::= ("true" | "false") space
  840. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  841. decimal-part ::= [0-9]{1,16}
  842. integral-part ::= [0] | [1-9] [0-9]{0,15}
  843. null ::= "null" space
  844. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  845. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  846. root ::= object
  847. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  848. string ::= "\"" char* "\"" space
  849. value ::= object | array | string | number | boolean | null
  850. )"""
  851. });
  852. test({
  853. SUCCESS,
  854. "additional props (implicit)",
  855. R"""({
  856. "type": "object"
  857. })""",
  858. R"""(
  859. array ::= "[" space ( value ("," space value)* )? "]" space
  860. boolean ::= ("true" | "false") space
  861. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  862. decimal-part ::= [0-9]{1,16}
  863. integral-part ::= [0] | [1-9] [0-9]{0,15}
  864. null ::= "null" space
  865. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  866. object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? "}" space
  867. root ::= object
  868. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  869. string ::= "\"" char* "\"" space
  870. value ::= object | array | string | number | boolean | null
  871. )"""
  872. });
  873. test({
  874. SUCCESS,
  875. "empty w/o additional props",
  876. R"""({
  877. "type": "object",
  878. "additionalProperties": false
  879. })""",
  880. R"""(
  881. root ::= "{" space "}" space
  882. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  883. )"""
  884. });
  885. test({
  886. SUCCESS,
  887. "required + additional props",
  888. R"""({
  889. "type": "object",
  890. "properties": {
  891. "a": {"type": "number"}
  892. },
  893. "required": ["a"],
  894. "additionalProperties": {"type": "string"}
  895. })""",
  896. R"""(
  897. a-kv ::= "\"a\"" space ":" space number
  898. additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
  899. additional-kv ::= additional-k ":" space string
  900. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  901. decimal-part ::= [0-9]{1,16}
  902. integral-part ::= [0] | [1-9] [0-9]{0,15}
  903. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  904. root ::= "{" space a-kv ( "," space ( additional-kv ( "," space additional-kv )* ) )? "}" space
  905. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  906. string ::= "\"" char* "\"" space
  907. )"""
  908. });
  909. test({
  910. SUCCESS,
  911. "optional + additional props",
  912. R"""({
  913. "type": "object",
  914. "properties": {
  915. "a": {"type": "number"}
  916. },
  917. "additionalProperties": {"type": "number"}
  918. })""",
  919. R"""(
  920. a-kv ::= "\"a\"" space ":" space number
  921. a-rest ::= ( "," space additional-kv )*
  922. additional-k ::= ["] ( [a] char+ | [^"a] char* )? ["] space
  923. additional-kv ::= additional-k ":" space number
  924. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  925. decimal-part ::= [0-9]{1,16}
  926. integral-part ::= [0] | [1-9] [0-9]{0,15}
  927. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  928. root ::= "{" space (a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
  929. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  930. )"""
  931. });
  932. test({
  933. SUCCESS,
  934. "required + optional + additional props",
  935. R"""({
  936. "type": "object",
  937. "properties": {
  938. "and": {"type": "number"},
  939. "also": {"type": "number"}
  940. },
  941. "required": ["and"],
  942. "additionalProperties": {"type": "number"}
  943. })""",
  944. R"""(
  945. additional-k ::= ["] ( [a] ([l] ([s] ([o] char+ | [^"o] char*) | [^"s] char*) | [n] ([d] char+ | [^"d] char*) | [^"ln] char*) | [^"a] char* )? ["] space
  946. additional-kv ::= additional-k ":" space number
  947. also-kv ::= "\"also\"" space ":" space number
  948. also-rest ::= ( "," space additional-kv )*
  949. and-kv ::= "\"and\"" space ":" space number
  950. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  951. decimal-part ::= [0-9]{1,16}
  952. integral-part ::= [0] | [1-9] [0-9]{0,15}
  953. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  954. root ::= "{" space and-kv ( "," space ( also-kv also-rest | additional-kv ( "," space additional-kv )* ) )? "}" space
  955. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  956. )"""
  957. });
  958. test({
  959. SUCCESS,
  960. "optional props with empty name",
  961. R"""({
  962. "properties": {
  963. "": {"type": "integer"},
  964. "a": {"type": "integer"}
  965. },
  966. "additionalProperties": {"type": "integer"}
  967. })""",
  968. R"""(
  969. -kv ::= "\"\"" space ":" space root
  970. -rest ::= ( "," space a-kv )? a-rest
  971. a-kv ::= "\"a\"" space ":" space integer
  972. a-rest ::= ( "," space additional-kv )*
  973. additional-k ::= ["] ( [a] char+ | [^"a] char* ) ["] space
  974. additional-kv ::= additional-k ":" space integer
  975. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  976. integer ::= ("-"? integral-part) space
  977. integral-part ::= [0] | [1-9] [0-9]{0,15}
  978. root ::= ("-"? integral-part) space
  979. root0 ::= "{" space (-kv -rest | a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
  980. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  981. )"""
  982. });
  983. test({
  984. SUCCESS,
  985. "optional props with nested names",
  986. R"""({
  987. "properties": {
  988. "a": {"type": "integer"},
  989. "aa": {"type": "integer"}
  990. },
  991. "additionalProperties": {"type": "integer"}
  992. })""",
  993. R"""(
  994. a-kv ::= "\"a\"" space ":" space integer
  995. a-rest ::= ( "," space aa-kv )? aa-rest
  996. aa-kv ::= "\"aa\"" space ":" space integer
  997. aa-rest ::= ( "," space additional-kv )*
  998. additional-k ::= ["] ( [a] ([a] char+ | [^"a] char*) | [^"a] char* )? ["] space
  999. additional-kv ::= additional-k ":" space integer
  1000. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  1001. integer ::= ("-"? integral-part) space
  1002. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1003. root ::= "{" space (a-kv a-rest | aa-kv aa-rest | additional-kv ( "," space additional-kv )* )? "}" space
  1004. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1005. )"""
  1006. });
  1007. test({
  1008. SUCCESS,
  1009. "optional props with common prefix",
  1010. R"""({
  1011. "properties": {
  1012. "ab": {"type": "integer"},
  1013. "ac": {"type": "integer"}
  1014. },
  1015. "additionalProperties": {"type": "integer"}
  1016. })""",
  1017. R"""(
  1018. ab-kv ::= "\"ab\"" space ":" space integer
  1019. ab-rest ::= ( "," space ac-kv )? ac-rest
  1020. ac-kv ::= "\"ac\"" space ":" space integer
  1021. ac-rest ::= ( "," space additional-kv )*
  1022. additional-k ::= ["] ( [a] ([b] char+ | [c] char+ | [^"bc] char*) | [^"a] char* )? ["] space
  1023. additional-kv ::= additional-k ":" space integer
  1024. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  1025. integer ::= ("-"? integral-part) space
  1026. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1027. root ::= "{" space (ab-kv ab-rest | ac-kv ac-rest | additional-kv ( "," space additional-kv )* )? "}" space
  1028. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1029. )"""
  1030. });
  1031. test({
  1032. SUCCESS,
  1033. "top-level $ref",
  1034. R"""({
  1035. "$ref": "#/definitions/foo",
  1036. "definitions": {
  1037. "foo": {
  1038. "type": "object",
  1039. "properties": {
  1040. "a": {
  1041. "type": "string"
  1042. }
  1043. },
  1044. "required": [
  1045. "a"
  1046. ],
  1047. "additionalProperties": false
  1048. }
  1049. }
  1050. })""",
  1051. R"""(
  1052. char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
  1053. foo ::= "{" space foo-a-kv "}" space
  1054. foo-a-kv ::= "\"a\"" space ":" space string
  1055. root ::= foo
  1056. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1057. string ::= "\"" char* "\"" space
  1058. )"""
  1059. });
  1060. test({
  1061. SUCCESS,
  1062. "anyOf",
  1063. R"""({
  1064. "anyOf": [
  1065. {"$ref": "#/definitions/foo"},
  1066. {"$ref": "#/definitions/bar"}
  1067. ],
  1068. "definitions": {
  1069. "foo": {
  1070. "properties": {"a": {"type": "number"}}
  1071. },
  1072. "bar": {
  1073. "properties": {"b": {"type": "number"}}
  1074. }
  1075. },
  1076. "type": "object"
  1077. })""",
  1078. R"""(
  1079. alternative-0 ::= foo
  1080. alternative-1 ::= bar
  1081. bar ::= "{" space (bar-b-kv )? "}" space
  1082. bar-b-kv ::= "\"b\"" space ":" space number
  1083. decimal-part ::= [0-9]{1,16}
  1084. foo ::= "{" space (foo-a-kv )? "}" space
  1085. foo-a-kv ::= "\"a\"" space ":" space number
  1086. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1087. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1088. root ::= alternative-0 | alternative-1
  1089. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1090. )"""
  1091. });
  1092. test({
  1093. SUCCESS,
  1094. "mix of allOf, anyOf and $ref (similar to https://json.schemastore.org/tsconfig.json)",
  1095. R"""({
  1096. "allOf": [
  1097. {"$ref": "#/definitions/foo"},
  1098. {"$ref": "#/definitions/bar"},
  1099. {
  1100. "anyOf": [
  1101. {"$ref": "#/definitions/baz"},
  1102. {"$ref": "#/definitions/bam"}
  1103. ]
  1104. }
  1105. ],
  1106. "definitions": {
  1107. "foo": {
  1108. "properties": {"a": {"type": "number"}}
  1109. },
  1110. "bar": {
  1111. "properties": {"b": {"type": "number"}}
  1112. },
  1113. "bam": {
  1114. "properties": {"c": {"type": "number"}}
  1115. },
  1116. "baz": {
  1117. "properties": {"d": {"type": "number"}}
  1118. }
  1119. },
  1120. "type": "object"
  1121. })""",
  1122. R"""(
  1123. a-kv ::= "\"a\"" space ":" space number
  1124. b-kv ::= "\"b\"" space ":" space number
  1125. c-kv ::= "\"c\"" space ":" space number
  1126. d-kv ::= "\"d\"" space ":" space number
  1127. d-rest ::= ( "," space c-kv )?
  1128. decimal-part ::= [0-9]{1,16}
  1129. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1130. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1131. root ::= "{" space a-kv "," space b-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
  1132. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1133. )"""
  1134. });
  1135. test({
  1136. SUCCESS,
  1137. "allOf with enum schema",
  1138. R"""({
  1139. "allOf": [
  1140. {"$ref": "#/definitions/foo"}
  1141. ],
  1142. "definitions": {
  1143. "foo": {
  1144. "type": "string",
  1145. "enum": ["a", "b"]
  1146. }
  1147. }
  1148. })""",
  1149. R"""(
  1150. root ::= ("\"a\"" | "\"b\"") space
  1151. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1152. )"""
  1153. });
  1154. test({
  1155. SUCCESS,
  1156. "allOf with multiple enum schemas",
  1157. R"""({
  1158. "allOf": [
  1159. {"$ref": "#/definitions/foo"},
  1160. {"$ref": "#/definitions/bar"}
  1161. ],
  1162. "definitions": {
  1163. "foo": {
  1164. "type": "string",
  1165. "enum": ["a", "b", "c"]
  1166. },
  1167. "bar": {
  1168. "type": "string",
  1169. "enum": ["b", "c", "d"]
  1170. }
  1171. }
  1172. })""",
  1173. R"""(
  1174. root ::= ("\"b\"" | "\"c\"") space
  1175. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1176. )"""
  1177. });
  1178. test({
  1179. SUCCESS,
  1180. "conflicting names",
  1181. R"""({
  1182. "type": "object",
  1183. "properties": {
  1184. "number": {
  1185. "type": "object",
  1186. "properties": {
  1187. "number": {
  1188. "type": "object",
  1189. "properties": {
  1190. "root": {
  1191. "type": "number"
  1192. }
  1193. },
  1194. "required": [
  1195. "root"
  1196. ],
  1197. "additionalProperties": false
  1198. }
  1199. },
  1200. "required": [
  1201. "number"
  1202. ],
  1203. "additionalProperties": false
  1204. }
  1205. },
  1206. "required": [
  1207. "number"
  1208. ],
  1209. "additionalProperties": false,
  1210. "definitions": {}
  1211. })""",
  1212. R"""(
  1213. decimal-part ::= [0-9]{1,16}
  1214. integral-part ::= [0] | [1-9] [0-9]{0,15}
  1215. number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
  1216. number- ::= "{" space number-number-kv "}" space
  1217. number-kv ::= "\"number\"" space ":" space number-
  1218. number-number ::= "{" space number-number-root-kv "}" space
  1219. number-number-kv ::= "\"number\"" space ":" space number-number
  1220. number-number-root-kv ::= "\"root\"" space ":" space number
  1221. root ::= "{" space number-kv "}" space
  1222. space ::= | " " | "\n"{1,2} [ \t]{0,20}
  1223. )"""
  1224. });
  1225. }
  1226. int main() {
  1227. fprintf(stderr, "LLAMA_NODE_AVAILABLE = %s\n", getenv("LLAMA_NODE_AVAILABLE") ? "true" : "false");
  1228. fprintf(stderr, "LLAMA_PYTHON_AVAILABLE = %s\n", getenv("LLAMA_PYTHON_AVAILABLE") ? "true" : "false");
  1229. test_all("C++", [](const TestCase & tc) {
  1230. try {
  1231. tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema), true));
  1232. tc.verify_status(SUCCESS);
  1233. } catch (const std::runtime_error & ex) {
  1234. fprintf(stderr, "Error: %s\n", ex.what());
  1235. tc.verify_status(FAILURE);
  1236. }
  1237. });
  1238. if (getenv("LLAMA_SKIP_TESTS_SLOW_ON_EMULATOR")) {
  1239. fprintf(stderr, "\033[33mWARNING: Skipping slow tests on emulator.\n\033[0m");
  1240. } else {
  1241. 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)) {
  1242. test_all("Python", [](const TestCase & tc) {
  1243. write("test-json-schema-input.tmp", tc.schema);
  1244. tc.verify_status(std::system(
  1245. "python ./examples/json_schema_to_grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
  1246. tc.verify(read("test-grammar-output.tmp"));
  1247. });
  1248. } else {
  1249. fprintf(stderr, "\033[33mWARNING: Python not found (min version required is 3.8), skipping Python JSON schema -> grammar tests.\n\033[0m");
  1250. }
  1251. if (getenv("LLAMA_NODE_AVAILABLE") || (std::system("node --version") == 0)) {
  1252. test_all("JavaScript", [](const TestCase & tc) {
  1253. write("test-json-schema-input.tmp", tc.schema);
  1254. tc.verify_status(std::system(
  1255. "node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0 ? SUCCESS : FAILURE);
  1256. tc.verify(read("test-grammar-output.tmp"));
  1257. });
  1258. } else {
  1259. fprintf(stderr, "\033[33mWARNING: Node not found, skipping JavaScript JSON schema -> grammar tests.\n\033[0m");
  1260. }
  1261. }
  1262. test_all("Check Expectations Validity", [](const TestCase & tc) {
  1263. if (tc.expected_status == SUCCESS) {
  1264. tc.verify_expectation_parseable();
  1265. }
  1266. });
  1267. }