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

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