fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6. /* public */
  7. enum jn_json_type
  8. {
  9. JN_STRING=1,
  10. JN_NUMBER,
  11. JN_TRUE,
  12. JN_FALSE,
  13. JN_NULL,
  14. JN_ARRAY,
  15. JN_OBJECT,
  16. JN_KEYVALUE
  17. };
  18.  
  19. /* public */
  20. typedef struct jn_tok_st
  21. {
  22. char* s;
  23. int len;
  24. }
  25. jn_tok_t;
  26.  
  27. /* public */
  28. typedef struct jn_json_st jn_json_t;
  29.  
  30. /* private */
  31. typedef struct jn_json_st
  32. {
  33. int type;
  34. jn_tok_t k;
  35. struct jn_json_st* v;
  36. struct jn_json_st* r; /* right */
  37. #ifdef JN_AVLTREE
  38. struct jn_json_st* l;
  39. struct jn_json_st* p;
  40. #endif
  41. }
  42. jn_json_t;
  43.  
  44.  
  45. #define JN_ALLOCATOR
  46. #undef JN_ALLOCATOR
  47. #define JN_ALLOCATOR
  48.  
  49. /* public */
  50. typedef struct jn_alloc_st
  51. {
  52. jn_json_t* (*alloc) (int, struct jn_alloc_st*);
  53. void (*free) (jn_json_t* ,struct jn_alloc_st*);
  54. #ifdef JN_ALLOCATOR
  55. jn_json_t* nodes;
  56. int cnt;
  57. #endif
  58. }
  59. jn_alloc_t;
  60.  
  61.  
  62. #define JN_MAX_LIMIT (0x100000)
  63.  
  64.  
  65.  
  66.  
  67. /* public */
  68. void jn_init_alloc(jn_alloc_t* a);
  69. int jn_parse(jn_alloc_t* a, char* in_buf, jn_json_t** out_json);
  70. void jn_print_json(jn_json_t* json);
  71.  
  72.  
  73. /* private */
  74. jn_json_t* jn_alloc_json (int size, jn_alloc_t* a);
  75. void jn_free_json (jn_json_t* n , jn_alloc_t* a);
  76. int jn_scan(char* in_buf, jn_tok_t* out_toks, int* out_len);
  77. void _jn_print_json(jn_json_t* json, int indent, int kv);
  78.  
  79.  
  80. /* optimized tokenizer */
  81. int jn_scan(char* in_buf, jn_tok_t* out_toks, int* out_len)
  82. {
  83. static int eqcls[256] = {
  84. 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  85. 0x0, 0x2, 0x2, 0x0, 0x0, 0x2, 0x0, 0x0,
  86. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  87. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  88. 0x2, 0x0, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
  89. 0x0, 0x0, 0x0, 0x1000, 0x1, 0x1104, 0x400, 0x20,
  90. 0x2e84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84,
  91. 0x2f84, 0x2f84, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
  92. 0x0, 0x80, 0x80, 0x80, 0x80, 0x880, 0x80, 0x0,
  93. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  94. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  95. 0x0, 0x0, 0x0, 0x1, 0x38, 0x1, 0x0, 0x0,
  96. 0x0, 0x20080, 0xa0, 0x80, 0x80, 0x110880, 0xa6, 0x0,
  97. 0x0, 0x0, 0x0, 0x0, 0x440000, 0x0, 0x27, 0x0,
  98. 0x0, 0x0, 0x4020, 0x80000, 0x25, 0x208040, 0x0, 0x0,
  99. 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0,
  100. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  101. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  102. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  103. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  104. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  105. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  106. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  107. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  108. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  109. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  110. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  111. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  112. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  113. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  114. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  115. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  116. };
  117.  
  118. static int tbl[] = {
  119. 0x200, 0xa00, 0x7000d01, 0x7000c01, 0x3030e09, 0x3080a18, 0x10e0e36, 0x1110e3c,
  120. 0x1150e44, 0x3031609, 0x1200, 0x7001501, 0x305160d, 0x1a00, 0x3031e09, 0x1071e10,
  121. 0x2200, 0x1072612, 0x2a00, 0x1072e14, 0x3200, 0x1073616, 0x3a00, 0x3033e09,
  122. 0x4200, 0x308461c, 0x3084620, 0x3084624, 0x4a00, 0x4a00, 0x3084e20, 0x3084e24,
  123. 0x30a5228, 0x5200, 0x5200, 0x5200, 0x30a5a28, 0x5a00, 0x3085e24, 0x3085e24,
  124. 0x7006101, 0x30a662c, 0x30c6630, 0x6200, 0x7006901, 0x6a00, 0x30c6e30, 0x30a6e2c,
  125. 0x7200, 0x30c7633, 0x30c7633, 0x7007901, 0x7a00, 0x30c7e33, 0x8200, 0x10f8638,
  126. 0x8a00, 0x1108e3a, 0x9200, 0x7009501, 0x9a00, 0x1129e3e, 0xa200, 0x113a640,
  127. 0xaa00, 0x114ae42, 0xb200, 0x700b501, 0xba00, 0x116be46, 0xc200, 0x116c648,
  128. 0xca00, 0x700cd01 };
  129.  
  130. //start = 0x7000201
  131.  
  132. char* p = in_buf;
  133. char* s = in_buf;
  134. jn_tok_t* t = out_toks;
  135. int state = 0x7000201;
  136. unsigned int cls = 0;
  137. do
  138. {
  139. p += (state>>10) & 0x1;
  140. t->s = s;
  141. t->len = p - s;
  142. t += (state>>8) & 0x1;
  143. s += (p - s) & (UINT_MAX + ((state>>9) & 0x1));
  144. cls = eqcls[(unsigned char)(*p)];
  145. cls = (cls>>((state & 0x00FF0000)>>16))&(state>>24);
  146. state = tbl[ (state & 0x000000FF) + cls ];
  147. }
  148. while( (state & 0x000000FF) > 0);
  149.  
  150. *out_len = t - out_toks;
  151. if(*p == 0)
  152. {
  153. return 1; /* SUCCESS */
  154. }
  155. cls = ((state & 0xf800) >> 11); /* fail state idx */
  156. if(cls < 1) /* fail state idx */
  157. {
  158. /* fail msg */
  159. }
  160. return 0;
  161. }
  162.  
  163.  
  164. /* simple memory allocator */
  165. void jn_init_alloc(jn_alloc_t* a)
  166. {
  167. #ifdef JN_ALLOCATOR
  168. static jn_json_t json_nodes[JN_MAX_LIMIT];
  169. memset(json_nodes, 0x00 ,sizeof(json_nodes));
  170. a->nodes = json_nodes;
  171. a->cnt = 0;
  172. #endif
  173. a->alloc = jn_alloc_json;
  174. a->free = jn_free_json;
  175. }
  176.  
  177. jn_json_t* jn_alloc_json (int size, jn_alloc_t* a)
  178. {
  179. jn_json_t* ret = NULL;
  180. #ifdef JN_ALLOCATOR
  181. ret = &(a->nodes[a->cnt]);
  182. a->cnt += size;
  183. #else
  184. ret = (jn_json_t*)malloc( size * sizeof(jn_json_t));
  185. #endif
  186. memset(ret, 0x00, size * sizeof(sizeof(jn_json_t)));
  187. return ret;
  188. }
  189. void jn_free_json (jn_json_t* n , jn_alloc_t* a)
  190. {
  191. #ifdef JN_ALLOCATOR
  192. /* pass */
  193. #else
  194. free(n);
  195. #endif
  196. }
  197.  
  198. typedef struct jn_stack_st
  199. {
  200. int state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, 0, 0, 0, 0,
  241. 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 41, 0, 0, 0, 0, 0, 0, 0,
  242. 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  243. };
  244. static jn_stack_t stack[JN_MAX_LIMIT];
  245.  
  246. /* */
  247. static jn_tok_t toks[JN_MAX_LIMIT];
  248. int tok_len = 0;
  249. int tok_idx = 0;
  250. int ret = 0;
  251. /* */
  252. int state =0;
  253. int top = 0; /* stack top */
  254. int top_state = 0;
  255. int top_cls = 0;
  256. jn_tok_t* top_tok = NULL;
  257. jn_json_t* top_json = NULL;
  258.  
  259. /* tokenizing */
  260. ret = jn_scan(in_buf, toks, &tok_len);
  261. if(ret == 0)
  262. {
  263. return 0; /* fail */
  264. }
  265. toks[tok_len].s = "";
  266. toks[tok_len].len = 0;
  267. tok_idx = 0;
  268.  
  269. /* parsing */
  270. do
  271. {
  272. jn_tok_t* t = &(toks[tok_idx]);
  273. int cls = eqcls[(unsigned char)(*toks[tok_idx].s)];
  274. state = tbl[stack[top].state * 18 + cls];
  275.  
  276. switch(state)
  277. {
  278. case 0:
  279. break;
  280. /* value: STR | NUM | TRUE | FALSE | NULL */
  281. case 26: case 27: case 28: case 29: case 30:
  282. top_json = a->alloc(1, a); /* alloc */
  283. top_json->type = state - 25;
  284. top_json->k = *(stack[top].token);
  285.  
  286. top -= 1;
  287. top_cls = 12; /* value */
  288. top_state = tbl[stack[top].state * 18 + top_cls];
  289. top_tok = NULL;
  290. break;
  291. /* value: array | object */
  292. case 31: case 32:
  293. top_json = stack[top].json; /* pass */
  294.  
  295. top -= 1;
  296. top_cls = 12; /* value */
  297. top_state = tbl[stack[top].state * 18 + top_cls];
  298. top_tok = NULL;
  299. break;
  300. /* array: [] */
  301. case 33:
  302. top_json = a->alloc(1, a); /* alloc */
  303. top_json->type = JN_ARRAY;
  304.  
  305. top -= 2;
  306. top_cls = 13; /* array */
  307. top_state = tbl[stack[top].state * 18 + top_cls];
  308. top_tok = NULL;
  309. break;
  310.  
  311. /* array: [ elements ] */
  312. case 34:
  313. top_json = a->alloc(1, a); /* alloc */
  314. top_json->type = JN_ARRAY;
  315. top_json->v = stack[top-1].json;
  316.  
  317. top -= 3;
  318. top_cls = 13; /* array */
  319. top_state = tbl[stack[top].state * 18 + top_cls];
  320. top_tok = NULL;
  321. break;
  322.  
  323. /* elements: value */
  324. case 35:
  325. top_json = stack[top].json; /* pass */
  326.  
  327. top -= 1;
  328. top_cls = 14; /* elements */
  329. top_state = tbl[stack[top].state * 18 + top_cls];
  330. top_tok = NULL;
  331. break;
  332. /* elements: value , elements */
  333. case 36:
  334. top_json = stack[top-2].json;
  335. top_json->r = stack[top].json;
  336.  
  337. top -= 3;
  338. top_cls = 14; /* elements */
  339. top_state = tbl[stack[top].state * 18 + top_cls];
  340. top_tok = NULL;
  341. break;
  342.  
  343. /* object: {} */
  344. case 37:
  345. top_json = a->alloc(1, a); /* alloc */
  346. top_json->type = JN_OBJECT;
  347.  
  348. top -= 2;
  349. top_cls = 15; /* object */
  350. top_state = tbl[stack[top].state * 18 + top_cls];
  351. top_tok = NULL;
  352. break;
  353.  
  354. /* object: { members } */
  355. case 38:
  356. top_json = a->alloc(1, a); /* alloc */
  357. top_json->type = JN_OBJECT;
  358. top_json->v = stack[top-1].json;
  359.  
  360. top -= 3;
  361. top_cls = 15; /* object */
  362. top_state = tbl[stack[top].state * 18 + top_cls];
  363. top_tok = NULL;
  364. break;
  365.  
  366. /* members: member */
  367. case 39:
  368. top_json = stack[top].json; /* pass */
  369.  
  370. top -= 1;
  371. top_cls = 16; /* members */
  372. top_state = tbl[stack[top].state * 18 + top_cls];
  373. top_tok = NULL;
  374. break;
  375. /* members: member , members */
  376. case 40:
  377. top_json = stack[top-2].json;
  378. top_json->r = stack[top].json;
  379.  
  380. top -= 3;
  381. top_cls = 16; /* members */
  382. top_state = tbl[stack[top].state * 18 + top_cls];
  383. top_tok = NULL;
  384. break;
  385.  
  386. /* member : str : value */
  387. case 41:
  388. top_json = a->alloc(1, a); /* alloc */
  389. top_json->type = JN_KEYVALUE;
  390. top_json->k = *(stack[top-2].token);
  391. top_json->v = stack[top].json;
  392.  
  393. top -= 3;
  394. top_cls = 17; /* members */
  395. top_state = tbl[stack[top].state * 18 + top_cls];
  396. top_tok = NULL;
  397. break;
  398.  
  399. default:
  400. /* shift */
  401. top_cls = cls;
  402. top_state = state;
  403. top_tok = t;
  404. top_json = NULL;
  405. tok_idx++;
  406. break;
  407. }
  408. if(state > 0)
  409. {
  410. top++;
  411. stack[top].cls = top_cls;
  412. stack[top].state = top_state;
  413. stack[top].token = top_tok;
  414. stack[top].json = top_json;
  415. }
  416.  
  417. }while(state > 0 && tok_idx <= tok_len);
  418.  
  419.  
  420. if(state == -1) /* accept */
  421. {
  422. *out_json = stack[top].json;
  423. return 1; /* success */
  424. }
  425. else
  426. {
  427. /* TODO: free all json memory */
  428. *out_json = NULL;
  429. }
  430.  
  431.  
  432. return 0;
  433. }
  434.  
  435.  
  436. void jn_print_json(jn_json_t* json)
  437. {
  438. _jn_print_json(json, 0, 0);
  439. }
  440. void _jn_print_json(jn_json_t* json, int indent, int kv)
  441. {
  442. if(json != NULL)
  443. {
  444. int i=0;
  445. for(i=0; i < indent && kv == 0; i++)
  446. {
  447. printf(" ");
  448. }
  449. switch(json->type)
  450. {
  451. case JN_STRING:
  452. case JN_NUMBER:
  453. case JN_TRUE:
  454. case JN_FALSE:
  455. case JN_NULL:
  456. printf("%.*s", json->k.len, json->k.s);
  457. break;
  458. case JN_ARRAY:
  459. printf("[\n");
  460. _jn_print_json(json->v, indent+1, 0);
  461. printf("\n");
  462. for(i=0; i < indent; i++)
  463. {
  464. printf(" ");
  465. }
  466. printf("]");
  467. break;
  468. case JN_OBJECT:
  469. printf("{\n");
  470. _jn_print_json(json->v, indent+1, 0);
  471. printf("\n");
  472. for(i=0; i < indent; i++)
  473. {
  474. printf(" ");
  475. }
  476. printf("}");
  477. break;
  478. case JN_KEYVALUE:
  479. printf("%.*s: ", json->k.len, json->k.s);
  480. _jn_print_json(json->v, indent, 1);
  481. break;
  482. }
  483. if(json->r != NULL)
  484. {
  485. printf(",\n");
  486. _jn_print_json(json->r, indent, 0);
  487. }
  488. else
  489. {
  490. printf("");
  491. }
  492. }
  493. }
  494.  
  495.  
  496. /* LALR table
  497. -------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-
  498. [ ] | $ | str | num | true | false | null | [ | ] | , | { | } | = | value | array | elements | object | members | member |
  499. -------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-
  500. [ 0] | | s 4 | s 6 | s 7 | s 8 | s 9 | s 10 | | | s 5 | | | 1 | 2 | | 3 | | |
  501. [ 1] | ACC | | | | | | | | | | | | | | | | | |
  502. [ 2] | r 6 | | | | | | | r 6 | r 6 | | r 6 | | | | | | | |
  503. [ 3] | r 7 | | | | | | | r 7 | r 7 | | r 7 | | | | | | | |
  504. [ 4] | r 1 | | | | | | | r 1 | r 1 | | r 1 | | | | | | | |
  505. [ 5] | | s 14 | | | | | | | | | s 13 | | | | | | 11 | 12 |
  506. [ 6] | r 2 | | | | | | | r 2 | r 2 | | r 2 | | | | | | | |
  507. [ 7] | r 3 | | | | | | | r 3 | r 3 | | r 3 | | | | | | | |
  508. [ 8] | r 4 | | | | | | | r 4 | r 4 | | r 4 | | | | | | | |
  509. [ 9] | r 5 | | | | | | | r 5 | r 5 | | r 5 | | | | | | | |
  510. [ 10] | | s 4 | s 6 | s 7 | s 8 | s 9 | s 10 | s 17 | | s 5 | | | 15 | 2 | 16 | 3 | | |
  511. [ 11] | | | | | | | | | | | s 18 | | | | | | | |
  512. [ 12] | | | | | | | | | s 19 | | r 14 | | | | | | | |
  513. [ 13] | r 12 | | | | | | | r 12 | r 12 | | r 12 | | | | | | | |
  514. [ 14] | | | | | | | | | | | | s 20 | | | | | | |
  515. [ 15] | | | | | | | | r 10 | s 21 | | | | | | | | | |
  516. [ 16] | | | | | | | | s 22 | | | | | | | | | | |
  517. [ 17] | r 8 | | | | | | | r 8 | r 8 | | r 8 | | | | | | | |
  518. [ 18] | r 13 | | | | | | | r 13 | r 13 | | r 13 | | | | | | | |
  519. [ 19] | | s 14 | | | | | | | | | | | | | | | 23 | 12 |
  520. [ 20] | | s 4 | s 6 | s 7 | s 8 | s 9 | s 10 | | | s 5 | | | 24 | 2 | | 3 | | |
  521. [ 21] | | s 4 | s 6 | s 7 | s 8 | s 9 | s 10 | | | s 5 | | | 15 | 2 | 25 | 3 | | |
  522. [ 22] | r 9 | | | | | | | r 9 | r 9 | | r 9 | | | | | | | |
  523. [ 23] | | | | | | | | | | | r 15 | | | | | | | |
  524. [ 24] | | | | | | | | | r 16 | | r 16 | | | | | | | |
  525. [ 25] | | | | | | | | r 11 | | | | | | | | | | |
  526. -------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-
  527. value: str | num | true | false | null | array | object ;
  528. array: [ ] | [ elements ] ;
  529. elements: value | value , elements ;
  530. object: { } | { members } ;
  531. members: member | member , members ;
  532. member: str = value ;
  533. */
  534.  
  535.  
  536.  
  537. int main(void)
  538. {
  539. jn_alloc_t a;
  540.  
  541. char* succ_json1 = "{\"key1\": \"this is test json\", \"key2\": [0, 1, 12, 0.123, 12.234, -293, -394.294E-23]}";
  542. char* succ_json2 = "{\"gram\":[{\"head\":\"value\",\"body\":[\"str\"],\"op\":\"str\"},{\"head\":\"value\",\"body\":[\"num\"],\"op\":\"num\"},{\"head\":\"value\",\"body\":[\"true\"],\"op\":\"true\"},{\"head\":\"value\",\"body\":[\"false\"],\"op\":\"false\"},{\"head\":\"value\",\"body\":[\"null\"],\"op\":\"null\"},{\"head\":\"value\",\"body\":[\"array\"],\"op\":null},{\"head\":\"value\",\"body\":[\"object\"],\"op\":null},{\"head\":\"array\",\"body\":[\"[\",\"]\"],\"op\":\"]\"},{\"head\":\"array\",\"body\":[\"[\",\"elements\",\"]\"],\"op\":\"]\"},{\"head\":\"elements\",\"body\":[\"value\"],\"op\":null},{\"head\":\"elements\",\"body\":[\"value\",\",\",\"elements\"],\"op\":\",\"},{\"head\":\"object\",\"body\":[\"{\",\"}\"],\"op\":\"}\"},{\"head\":\"object\",\"body\":[\"{\",\"members\",\"}\"],\"op\":\"}\"},{\"head\":\"members\",\"body\":[\"member\"],\"op\":null},{\"head\":\"members\",\"body\":[\"member\",\",\",\"members\"],\"op\":\",\"},{\"head\":\"member\",\"body\":[\"str\",\"=\",\"value\"],\"op\":\"=\"}],\"non\":{\"value\":0,\"array\":1,\"elements\":2,\"object\":3,\"members\":4,\"member\":5},\"ter\":{\"str\":0,\"num\":1,\"true\":2,\"false\":3,\"null\":4,\"[\":5,\"]\":6,\",\":7,\"{\":8,\"}\":9,\"=\":10}}";
  543. jn_json_t* out = NULL;
  544.  
  545. printf("------------\n");
  546. out = NULL;
  547. jn_init_alloc(&a);
  548. if(jn_parse(&a, succ_json1, &out))
  549. jn_print_json(out);
  550.  
  551. printf("------------\n");
  552. out = NULL;
  553. jn_init_alloc(&a);
  554. if(jn_parse(&a, succ_json2, &out))
  555. jn_print_json(out);
  556.  
  557. return 0;
  558. }
Success #stdin #stdout 0.02s 42400KB
stdin
Standard input is empty
stdout
------------
{
  "key1": "this is test json",
  "key2": [
    0,
    1,
    12,
    0.123,
    12.234,
    -293,
    -394.294E-23
  ]
}------------
{
  "gram": [
    {
      "head": "value",
      "body": [
        "str"
      ],
      "op": "str"
    },
    {
      "head": "value",
      "body": [
        "num"
      ],
      "op": "num"
    },
    {
      "head": "value",
      "body": [
        "true"
      ],
      "op": "true"
    },
    {
      "head": "value",
      "body": [
        "false"
      ],
      "op": "false"
    },
    {
      "head": "value",
      "body": [
        "null"
      ],
      "op": "null"
    },
    {
      "head": "value",
      "body": [
        "array"
      ],
      "op": null
    },
    {
      "head": "value",
      "body": [
        "object"
      ],
      "op": null
    },
    {
      "head": "array",
      "body": [
        "[",
        "]"
      ],
      "op": "]"
    },
    {
      "head": "array",
      "body": [
        "[",
        "elements",
        "]"
      ],
      "op": "]"
    },
    {
      "head": "elements",
      "body": [
        "value"
      ],
      "op": null
    },
    {
      "head": "elements",
      "body": [
        "value",
        ",",
        "elements"
      ],
      "op": ","
    },
    {
      "head": "object",
      "body": [
        "{",
        "}"
      ],
      "op": "}"
    },
    {
      "head": "object",
      "body": [
        "{",
        "members",
        "}"
      ],
      "op": "}"
    },
    {
      "head": "members",
      "body": [
        "member"
      ],
      "op": null
    },
    {
      "head": "members",
      "body": [
        "member",
        ",",
        "members"
      ],
      "op": ","
    },
    {
      "head": "member",
      "body": [
        "str",
        "=",
        "value"
      ],
      "op": "="
    }
  ],
  "non": {
    "value": 0,
    "array": 1,
    "elements": 2,
    "object": 3,
    "members": 4,
    "member": 5
  },
  "ter": {
    "str": 0,
    "num": 1,
    "true": 2,
    "false": 3,
    "null": 4,
    "[": 5,
    "]": 6,
    ",": 7,
    "{": 8,
    "}": 9,
    "=": 10
  }
}