-
Notifications
You must be signed in to change notification settings - Fork 3
/
jparse.y
981 lines (855 loc) · 31.4 KB
/
jparse.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
* JSON parser - JSON parser in bison grammar form
*
* "Because specs w/o version numbers are forced to commit to their original design flaws." :-)
*
* This JSON parser was co-developed in 2022 by:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* "Share and Enjoy!"
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
/* Section 0: Declarations */
/*
* We enable lookahead correction parser for improved error messages:
*
* %define parse.lac full
*/
%define parse.lac full
/*
* we want a re-entrant parser:
*
* %define api.pure full
*/
%define api.pure full
/*
* we need locations for better error reporting:
*
* %locations
*/
%locations
/*
* we want a prefix that's not yy
*
* %define api.prefix {jparse_}
*/
%define api.prefix {jparse_}
/*
* We use our struct json (see json_parse.h for its definition) instead of bison
* %union:
*
* %define api.value.type {struct json *}
*
*/
%define api.value.type {struct json *}
/*
* we need access to the tree in parse_json() so we tell bison that jparse_parse()
* takes a struct json **tree:
*
* %parse-param { struct json **tree }
*/
%parse-param { struct json **tree }
/*
* Re-entrancy.
*
* This is to make the scanner re-entrant:
*
* %param { yyscan_t scanner }
*/
%param { yyscan_t scanner }
%{
/*
* jparse - JSON parser
*/
#include "jparse.h"
/*
* version - JSON parser API and tool version
*/
#include "version.h"
/*
* for the re-entrant scanner.
*
* NOTE that we cannot #include "jparse.lex.h" in jparse.h so we do it here
* instead.
*/
#include "jparse.lex.h"
/*
* bison debug information for development
*/
int jparse_debug = 0; /* 0 ==> verbose bison debug off, 1 ==> verbose bison debug on */
/*
* JSON parser version strings (format: major.minor YYYY-MM-DD)
*/
const char *const jparse_library_version = JPARSE_LIBRARY_VERSION; /* jparse library version */
const char *const jparse_utf8_version = JPARSE_UTF8_VERSION; /* jparse utf8 version */
const char *const jparse_version = JPARSE_VERSION; /* jparse tool version */
%}
%code provides {
#ifndef YY_DECL
#define YY_DECL int jparse_lex(JPARSE_STYPE *yylval_param, JPARSE_LTYPE *yylloc_param, yyscan_t yyscanner)
YY_DECL;
#endif
}
%code requires {
#if !defined(JPARSE_LTYPE_IS_DECLARED)
struct JPARSE_LTYPE
{
int first_line;
int first_column;
int last_line;
int last_column;
char const *filename;
};
typedef struct JPARSE_LTYPE JPARSE_LTYPE;
#define JPARSE_LTYPE_IS_DECLARED 1
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
if (N) \
{ \
(Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
(Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
(Current).last_line = YYRHSLOC (Rhs, N).last_line; \
(Current).last_column = YYRHSLOC (Rhs, N).last_column; \
(Current).filename = YYRHSLOC (Rhs, 1).filename; \
} \
else \
{ /* empty RHS */ \
(Current).first_line = (Current).last_line = \
YYRHSLOC (Rhs, 0).last_line; \
(Current).first_column = (Current).last_column = \
YYRHSLOC (Rhs, 0).last_column; \
(Current).filename = NULL; \
} \
while (0)
#endif
typedef void * yyscan_t;
}
/*
* Terminal symbols (token kind)
*
* For most of the terminal symbols we use string literals to identify them as
* this makes it easier to read error messages. This feature is not POSIX Yacc
* compatible but we've decided that the benefit outweighs this fact. Thus we
* have:
*
* %token JSON_OPEN_BRACE "{"
* %token JSON_CLOSE_BRACE "}"
* %token JSON_OPEN_BRACKET "["
* %token JSON_CLOSE_BRACKET "]"
* %token JSON_COMMA ","
* %token JSON_COLON ":"
* %token JSON_NULL "null"
* %token JSON_TRUE "true"
* %token JSON_FALSE "false"
* %token JSON_STRING
* %token JSON_NUMBER
*
* Observe that these exist in jparse.l. In the case that t here is no value,
* you might check jparse.l for the regexp.
*/
%token JSON_OPEN_BRACE "{"
%token JSON_CLOSE_BRACE "}"
%token JSON_OPEN_BRACKET "["
%token JSON_CLOSE_BRACKET "]"
%token JSON_COMMA ","
%token JSON_COLON ":"
%token JSON_NULL "null"
%token JSON_TRUE "true"
%token JSON_FALSE "false"
%token JSON_STRING
%token JSON_NUMBER
/*
* The next token 'token' is a hack for better error messages with invalid
* tokens. Bison syntax error messages are in the form of:
*
* syntax error, unexpected <token name>
* syntax error, unexpected <token name>, expecting } or JSON_STRING
*
* etc. where <token name> is whatever is returned in the lexer actions (e.g.
* JSON_STRING) to the parser. But the problem is what do we call an invalid
* token without knowing what the token actually is? Thus we call it token
* so that it will read literally as 'unexpected token' which removes any
* ambiguity (otherwise it could be read as 'it's unexpected in this place but
* it is valid in other contexts' but it's actually never valid: it's a catch
* all for anything that's not valid).
*
* Then as a hack (or maybe kludge) in yyerror() we refer to yytext in a
* way that shows what the token is that caused the failure (whether it's a
* syntax error or something else).
*
* This hack is defined like:
*
* %token token
*/
%token token
/*
* Section 1: Rules
*
* See https://www.json.org/json-en.html for the JSON specification. We have
* tried to make the below grammar as close to the JSON specification as
* possible for those who are familiar with it but there might be some minor
* differences in order or naming. One that we do not have is whitespace but
* that's not needed and would actually cause many complications and parsing
* errors. There are some others we do not need to include as well.
*/
%%
json:
json_element
{
/*
* $$ = $json
* $1 = $json_element
*/
/* pre action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json: starting: "
"json: json_element");
json_dbg(JSON_DBG_HIGH, __func__, "under json: $json_element type: %s",
json_item_type_name($json_element));
json_dbg(JSON_DBG_HIGH, __func__, "under json: about to perform: "
"$json = $json_element;");
}
/* action */
$json = $json_element; /* magic: json_value becomes json_element's type */
/* post action debugging */
if (json_dbg_allowed(JSON_DBG_MED)) {
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json: returning $json_element type: %s",
json_item_type_name($json));
}
json_dbg_tree_print(JSON_DBG_MED, __func__, $json, JSON_DEFAULT_MAX_DEPTH);
}
if (tree == NULL) {
warn(__func__, "under json: tree == NULL");
} else {
/* return the JSON parse tree */
if ($json == NULL) {
/* Magic: See below magic note for how *tree will be set to NULL */
warn(__func__, "under json: $json == NULL: about to perform: "
"*tree = NULL;");
}
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json: about to also perform: "
"*tree = $json;");
}
/*
* Magic: If $json == NULL then *tree will be set to NULL. If it's
* not NULL, however, *tree will be set to the parse tree itself
* ($json).
*/
*tree = $json; /* more magic: set jparse_parse(tree) arg to point to JSON parse tree */
}
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json: ending: "
"json: json_element");
}
}
;
json_value:
json_object
{
/*
* $$ = $json_value
* $1 = $json_object
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: json_object");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: $json_object type: %s",
json_item_type_name($json_object));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = $json_object;");
}
/* action */
$json_value = $json_object; /* magic: json_value becomes JTYPE_OBJECT type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: json_object");
}
}
|
json_array
{
/*
* $$ = $json_value
* $1 = $json_array
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: json_array");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: $json_array type: %s",
json_item_type_name($json_array));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = $json_array;");
}
/* action */
$json_value = $json_array; /* magic: json_value becomes JTYPE_ARRAY type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: json_array");
}
}
|
json_string
{
/*
* $$ = $json_value
* $1 = $json_string
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: json_string");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: $json_string type: %s",
json_item_type_name($json_string));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = $json_string;");
}
/* action */
$json_value = $json_string; /* magic: json_value becomes JTYPE_STRING type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: json_string");
}
}
|
json_number
{
/*
* $$ = $json_value
* $1 = $json_number
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: json_number");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: $json_number type: %s",
json_item_type_name($json_number));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = $json_number;");
}
/* action */
$json_value = $json_number; /* magic: json_value becomes JTYPE_NUMBER type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: json_number");
}
}
|
JSON_TRUE
{
/*
* $$ = $json_value
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: JSON_TRUE");
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yytext: <%s>", jparse_get_text(scanner));
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yyleng: <%ju>", (intmax_t)jparse_get_leng(scanner));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = json_parse_bool(jparse_get_text(scanner));");
}
/* action */
$json_value = json_parse_bool(jparse_get_text(scanner)); /* magic: json_value becomes JTYPE_BOOL type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: JSON_TRUE");
}
}
|
JSON_FALSE
{
/*
* $$ = $json_value
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: JSON_FALSE");
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yytext: <%s>", jparse_get_text(scanner));
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yyleng: <%jd>", (intmax_t)jparse_get_leng(scanner));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = json_parse_bool(jparse_get_text(scanner))");
}
/* action */
$json_value = json_parse_bool(jparse_get_text(scanner)); /* magic: json_value becomes JTYPE_BOOL type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: JSON_FALSE");
}
}
|
JSON_NULL
{
/*
* $$ = $json_value
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: starting: "
"json_value: JSON_NULL");
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yytext: <%s>", jparse_get_text(scanner));
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_value: yyleng: <%jd>", (intmax_t)jparse_get_leng(scanner));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: about to perform: "
"$json_value = json_parse_null(jparse_get_text(scanner));");
}
/* action */
$json_value = json_parse_null(jparse_get_text(scanner)); /* magic: json_value becomes JTYPE_NULL type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_value: returning $json_value type: %s",
json_item_type_name($json_value));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_value, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_value: ending: "
"json_value: JSON_NULL");
}
}
;
json_object:
JSON_OPEN_BRACE json_members JSON_CLOSE_BRACE
{
/*
* $$ = $json_object
* $1 = $json_members
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: starting: "
"json_object: JSON_OPEN_BRACE json_members JSON_CLOSE_BRACE");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: $json_members type: %s",
json_item_type_name($json_members));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: about to perform: "
"$json_object = $json_members;");
}
/* action */
$json_object = $json_members; /* magic: json_value becomes JTYPE_OBJECT type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_object: returning $json_object type: %s",
json_item_type_name($json_object));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_object, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: ending: "
"json_object: JSON_OPEN_BRACE json_members JSON_CLOSE_BRACE");
}
}
|
JSON_OPEN_BRACE JSON_CLOSE_BRACE
{
/*
* $$ = $json_object
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: starting: "
"json_object: JSON_OPEN_BRACE JSON_CLOSE_BRACE");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: about to perform: "
"$json_object = json_create_object();");
}
/* action */
$json_object = json_create_object(); /* json_object becomes JTYPE_OBJECT */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_object: returning $json_object type: %s",
json_item_type_name($json_object));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_object, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_object: ending: "
"json_object: JSON_OPEN_BRACE JSON_CLOSE_BRACE");
}
}
;
json_members:
json_member
{
/*
* $$ = $json_members
* $1 = $json_member
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: starting: "
"json_members: json_member");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: $json_member type: %s",
json_item_type_name($json_member));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: about to perform: "
"$json_members = json_create_object();");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: about to also perform: "
"$json_members = json_object_add_member($json_members, $json_member);");
}
/* action */
$json_members = json_create_object();
$json_members = json_object_add_member($json_members, $json_member);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: returning $json_members type: %s",
json_item_type_name($json_members));
json_dbg_tree_print(JSON_DBG_VHIGH, __func__, $json_members, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_HIGH, __func__, "under json_members: ending: "
"json_members: json_member");
}
}
|
json_members JSON_COMMA json_member
{
/*
* $$ = $json_members
* $1 = $json_members
* $3 = $json_member
*
* NOTE: Cannot use $json_members due to ambiguity.
* But we can use $3 for $json_member.
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: starting: "
"json_members: json_members JSON_COMMA json_member");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: $1 ($json_members) type: %s",
json_item_type_name($1));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: $3 ($json_member) type: %s",
json_item_type_name($3));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: about to perform: "
"$$ = json_object_add_member($1, $json_member)");
}
/* action */
$$ = json_object_add_member($1, $json_member);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_members: returning $$ ($json_members) type: %s",
json_item_type_name($$));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $$, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_members: ending: "
"json_members: json_members JSON_COMMA json_member");
}
}
;
json_member:
json_string JSON_COLON json_element
{
/*
* $$ = $json_member
* $1 = $json_string
* $3 = $json_element
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_member: starting: "
"json_member: json_string JSON_COLON json_element");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_member: $json_string type: %s",
json_item_type_name($json_string));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_member: $json_element type: %s",
json_item_type_name($json_element));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_member: about to perform: "
"$json_member = json_parse_member($json_string, $json_element);");
}
/* action */
$json_member = json_parse_member($json_string, $json_element);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_member: returning $json_member type: %s",
json_item_type_name($json_member));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_member, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_member: ending: "
"json_member: json_string JSON_COLON json_element");
}
}
;
json_array:
JSON_OPEN_BRACKET json_elements JSON_CLOSE_BRACKET
{
/*
* $$ = $json_array
* $2 = $json_elements
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: starting: "
"json_array: JSON_OPEN_BRACKET json_elements JSON_CLOSE_BRACKET");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: $json_elements type: %s",
json_item_type_name($json_elements));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: about to perform: "
"$json_array = json_parse_array($json_elements);");
}
/* action */
$json_array = json_parse_array($json_elements);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_array: returning $json_array type: %s",
json_item_type_name($json_array));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_elements, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: ending: "
"json_array: JSON_OPEN_BRACKET json_elements JSON_CLOSE_BRACKET");
}
}
|
JSON_OPEN_BRACKET JSON_CLOSE_BRACKET
{
/*
* $$ = $json_array
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: starting: "
"json_array: JSON_OPEN_BRACKET JSON_CLOSE_BRACKET");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: about to perform: "
"$json_array = json_create_array();");
}
/* action */
$json_array = json_create_array();
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_array: returning $json_array type: %s",
json_item_type_name($json_array));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_array, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_array: ending: "
"json_array: JSON_OPEN_BRACKET JSON_CLOSE_BRACKET");
}
}
;
json_elements:
json_element
{
/*
* $$ = $json_elements
* $1 = $json_element
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: starting: "
"json_elements: json_element");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: $json_element type: %s",
json_item_type_name($json_element));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: about to perform: "
"$json_elements = json_create_elements();");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: about to also perform: "
"$json_elements = json_elements_add_value($json_elements, $json_element);");
}
/* action */
$json_elements = json_create_elements();
$json_elements = json_elements_add_value($json_elements, $json_element);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_elements: returning $json_elements type: %s",
json_item_type_name($json_elements));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_elements, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: ending: "
"json_elements: json_element");
}
}
|
json_elements JSON_COMMA json_element
{
/*
* $$ = $json_elements
* $3 = $json_element
*
* NOTE: Cannot use $json_elements due to ambiguity.
* But we can use $3 for $json_element.
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: starting: "
"json_elements: json_elements JSON_COMMA json_element");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: $1 ($json_elements) type: %s",
json_item_type_name($1));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: $3 ($json_element) type: %s",
json_item_type_name($3));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: about to perform: "
"$$ = json_elements_add_value($1, $json_element);");
}
/* action */
$$ = json_elements_add_value($1, $json_element);
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_elements: returning $$ ($json_elements) type: %s",
json_item_type_name($$));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $$, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_elements: ending: "
"json_elements: json_elements JSON_COMMA json_element");
}
}
;
json_element:
json_value
{
/*
* $$ = $json_element
* $1 = $json_value
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_element: starting: "
"json_element: json_value");
json_dbg(JSON_DBG_VHIGH, __func__, "under json_element: $json_value type: %s",
json_item_type_name($json_value));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_element: about to perform: "
"$json_element = $json_value;");
}
/* action */
$json_element = $json_value; /* magic: json_element becomes json_value's type */
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_element: returning $json_element type: %s",
json_item_type_name($json_element));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_element, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_element: ending: "
"json_element: json_value");
}
}
;
json_string:
JSON_STRING
{
/*
* $$ = $json_string
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_string: starting: "
"json_string: JSON_STRING");
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_string: yytext: <%s>", jparse_get_text(scanner));
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_string: yyleng: <%jd>", (intmax_t)jparse_get_leng(scanner));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_string: about to perform: "
"$json_string = json_parse_string(jparse_get_text(scanner), (size_t)jparse_get_leng(scanner);");
}
/* action */
$json_string = json_parse_string(jparse_get_text(scanner), (size_t)jparse_get_leng(scanner));
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_string: returning $json_string type: %s",
json_item_type_name($json_string));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_string, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_string: ending: "
"json_string: JSON_STRING");
}
}
;
json_number:
JSON_NUMBER
{
/*
* $$ = $json_number
*/
/* pre-action debugging */
if (json_dbg_allowed(JSON_DBG_VHIGH)) {
json_dbg(JSON_DBG_VHIGH, __func__, "under json_number: starting: "
"json_number: JSON_NUMBER");
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_number: yytext: <%s>", jparse_get_text(scanner));
json_dbg(JSON_DBG_VVHIGH, __func__, "under json_number: yyleng: <%jd>", (intmax_t)jparse_get_leng(scanner));
json_dbg(JSON_DBG_VHIGH, __func__, "under json_number: about to perform: "
"$json_number = json_parse_number(jparse_get_text(scanner));");
}
/* action */
$json_number = json_parse_number(jparse_get_text(scanner));
/* post-action debugging */
if (json_dbg_allowed(JSON_DBG_HIGH)) {
json_dbg(JSON_DBG_HIGH, __func__, "under json_number: returning $json_number type: %s",
json_item_type_name($json_number));
json_dbg_tree_print(JSON_DBG_HIGH, __func__, $json_number, JSON_DEFAULT_MAX_DEPTH);
json_dbg(JSON_DBG_VHIGH, __func__, "under json_number: ending: "
"json_number: JSON_NUMBER");
}
}
;
%%
/* Section 2: C code */
/*
* yyerror - generate an error message for the scanner/parser
*
* given:
*
* yyltype location type
* node pointer to struct json * or NULL
* scanner scanner instance
* format printf style format string
* ... optional parameters based on the format
*
*/
void
yyerror(JPARSE_LTYPE *yyltype, struct json **node, yyscan_t scanner, char const *format, ...)
{
va_list ap; /* variable argument list */
int ret; /* libc function return value */
/*
* firewall
*/
if (scanner == NULL) {
err(58, __func__, "NULL scanner");
not_reached();
}
/*
* stdarg variable argument list setup
*/
va_start(ap, format);
/*
* generate an error message for the JSON parser and scanner
*/
vfpr(stderr, __func__, format, ap);
if (node != NULL && *node != NULL) {
fprint(stderr, " node type %s", json_item_type_name(*node));
}
if (yyltype != NULL) {
if (yyltype->filename != NULL && *yyltype->filename != '\0') {
fprint(stderr, " in file %s", yyltype->filename);
}
fprint(stderr, " at line %d column %d: ", yyltype->first_line, yyltype->first_column);
}
if (jparse_get_text(scanner) != NULL && *jparse_get_text(scanner) != '\0') {
fprint(stderr, "%s\n", jparse_get_text(scanner));
} else if (jparse_get_text(scanner) == NULL) {
fprstr(stderr, "text == NULL\n");
} else {
fprstr(stderr, "empty text\n");
}
/*
* flush stderr
*/
clearerr(stderr); /* pre-clear ferror() status */
errno = 0; /* pre-clear errno for warnp() */
ret = fflush(stderr);
if (ret == EOF) {
if (ferror(stderr)) {
warnp(__func__, "called from %s: error flushing stream", __func__);
} else if (feof(stderr)) {
warnp(__func__, "called from %s: EOF while flushing stream", __func__);
} else {
warnp(__func__, "called from %s: unexpected fflush error while flushing stream", __func__);
}
}
/*
* stdarg variable argument list clean up
*/
va_end(ap);
}