Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCDConfigParser_parse.y « generated - github.com/ambrop72/badvpn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17e86fd15e0aa5f4ed0d68b55a6784ffbbd0fd43 (plain)
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
/**
 * @file NCDConfigParser.y
 * @author Ambroz Bizjak <ambrop7@gmail.com>
 * 
 * @section LICENSE
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

%include {

#include <string.h>
#include <stddef.h>

#include <misc/debug.h>
#include <misc/concat_strings.h>
#include <ncd/NCDAst.h>

struct parser_out {
    int out_of_memory;
    int syntax_error;
    int have_ast;
    NCDProgram ast;
};

struct token {
    char *str;
    size_t len;
};

struct program {
    int have;
    NCDProgram v;
};

struct block {
    int have;
    NCDBlock v;
};

struct statement {
    int have;
    NCDStatement v;
};

struct ifblock {
    int have;
    NCDIfBlock v;
};

struct value {
    int have;
    NCDValue v;
};

static void free_token (struct token o) { free(o.str); }
static void free_program (struct program o) { if (o.have) NCDProgram_Free(&o.v); }
static void free_block (struct block o) { if (o.have) NCDBlock_Free(&o.v); }
static void free_statement (struct statement o) { if (o.have) NCDStatement_Free(&o.v); }
static void free_ifblock (struct ifblock o) { if (o.have) NCDIfBlock_Free(&o.v); }
static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }

}

%extra_argument { struct parser_out *parser_out }

%token_type { struct token }

%token_destructor { free_token($$); }

%type processes { struct program }
%type statement { struct statement }
%type elif_maybe { struct ifblock }
%type elif { struct ifblock }
%type else_maybe { struct block }
%type statements { struct block }
%type dotted_name { char * }
%type list_contents_maybe { struct value }
%type list_contents { struct value }
%type list { struct value }
%type map_contents { struct value }
%type map  { struct value }
%type invoc { struct value }
%type value  { struct value }
%type name_maybe { char * }
%type process_or_template { int }
%type name_list { struct value }
%type interrupt_maybe { struct block }

// mention parser_out in some destructor to avoid an unused-variable warning
%destructor processes { (void)parser_out; free_program($$); }
%destructor statement { free_statement($$); }
%destructor elif_maybe { free_ifblock($$); }
%destructor elif { free_ifblock($$); }
%destructor else_maybe { free_block($$); }
%destructor statements { free_block($$); }
%destructor dotted_name { free($$); }
%destructor list_contents_maybe { free_value($$); }
%destructor list_contents { free_value($$); }
%destructor list { free_value($$); }
%destructor map_contents { free_value($$); }
%destructor map { free_value($$); }
%destructor invoc { free_value($$); }
%destructor value { free_value($$); }
%destructor name_maybe { free($$); }
%destructor name_list { free_value($$); }
%destructor interrupt_maybe { free_block($$); }

%stack_size 0

%syntax_error {
    parser_out->syntax_error = 1;
}

// workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
%stack_overflow {
    if (yypMinor) {
        free_token(yypMinor->yy0);
    }
}

input ::= processes(A). {
    ASSERT(!parser_out->have_ast)

    if (A.have) {
        parser_out->have_ast = 1;
        parser_out->ast = A.v;
    }
}

processes(R) ::= . {
    NCDProgram prog;
    NCDProgram_Init(&prog);
    
    R.have = 1;
    R.v = prog;
}

processes(R) ::= INCLUDE STRING(A) processes(N). {
    ASSERT(A.str)
    if (!N.have) {
        goto failA0;
    }
    
    NCDProgramElem elem;
    if (!NCDProgramElem_InitInclude(&elem, A.str, A.len)) {
        goto failA0;
    }
    
    if (!NCDProgram_PrependElem(&N.v, elem)) {
        goto failA1;
    }
    
    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneA;

failA1:
    NCDProgramElem_Free(&elem);
failA0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneA:
    free_token(A);
    free_program(N);
}

processes(R) ::= INCLUDE_GUARD STRING(A) processes(N). {
    ASSERT(A.str)
    if (!N.have) {
        goto failZ0;
    }
    
    NCDProgramElem elem;
    if (!NCDProgramElem_InitIncludeGuard(&elem, A.str, A.len)) {
        goto failZ0;
    }
    
    if (!NCDProgram_PrependElem(&N.v, elem)) {
        goto failZ1;
    }
    
    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneZ;

failZ1:
    NCDProgramElem_Free(&elem);
failZ0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneZ:
    free_token(A);
    free_program(N);
}

processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
    ASSERT(A.str)
    if (!B.have || !N.have) {
        goto failB0;
    }

    NCDProcess proc;
    if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
        goto failB0;
    }
    B.have = 0;
    
    NCDProgramElem elem;
    NCDProgramElem_InitProcess(&elem, proc);

    if (!NCDProgram_PrependElem(&N.v, elem)) {
        goto failB1;
    }

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneB;

failB1:
    NCDProgramElem_Free(&elem);
failB0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneB:
    free_token(A);
    free_block(B);
    free_program(N);
}

statement(R) ::= dotted_name(A) ROUND_OPEN list_contents_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
    if (!A || !B.have) {
        goto failC0;
    }

    if (!NCDStatement_InitReg(&R.v, C, NULL, A, B.v)) {
        goto failC0;
    }
    B.have = 0;

    R.have = 1;
    goto doneC;

failC0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneC:
    free(A);
    free_value(B);
    free(C);
}

statement(R) ::= dotted_name(M) ARROW dotted_name(A) ROUND_OPEN list_contents_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
    if (!M || !A || !B.have) {
        goto failD0;
    }

    if (!NCDStatement_InitReg(&R.v, C, M, A, B.v)) {
        goto failD0;
    }
    B.have = 0;

    R.have = 1;
    goto doneD;

failD0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneD:
    free(M);
    free(A);
    free_value(B);
    free(C);
}

statement(R) ::= IF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif_maybe(I) else_maybe(E) name_maybe(C) SEMICOLON. {
    if (!A.have || !B.have || !I.have) {
        goto failE0;
    }

    NCDIf ifc;
    NCDIf_Init(&ifc, A.v, B.v);
    A.have = 0;
    B.have = 0;

    if (!NCDIfBlock_PrependIf(&I.v, ifc)) {
        NCDIf_Free(&ifc);
        goto failE0;
    }

    if (!NCDStatement_InitIf(&R.v, C, I.v, NCDIFTYPE_IF)) {
        goto failE0;
    }
    I.have = 0;

    if (E.have) {
        NCDStatement_IfAddElse(&R.v, E.v);
        E.have = 0;
    }

    R.have = 1;
    goto doneE;

failE0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneE:
    free_value(A);
    free_block(B);
    free_ifblock(I);
    free_block(E);
    free(C);
}

statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
    if (!A.have || !B.str || !S.have) {
        goto failEA0;
    }
    
    if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, NULL, S.v)) {
        goto failEA0;
    }
    A.have = 0;
    S.have = 0;
    
    R.have = 1;
    goto doneEA0;
    
failEA0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneEA0:
    free_value(A);
    free_token(B);
    free_block(S);
    free(N);
}

statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) COLON NAME(C) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
    if (!A.have || !B.str || !C.str || !S.have) {
        goto failEB0;
    }
    
    if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, C.str, S.v)) {
        goto failEB0;
    }
    A.have = 0;
    S.have = 0;
    
    R.have = 1;
    goto doneEB0;
    
failEB0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneEB0:
    free_value(A);
    free_token(B);
    free_token(C);
    free_block(S);
    free(N);
}

elif_maybe(R) ::= . {
    NCDIfBlock_Init(&R.v);
    R.have = 1;
}

elif_maybe(R) ::= elif(A). {
    R = A;
}

elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE. {
    if (!A.have || !B.have) {
        goto failF0;
    }

    NCDIfBlock_Init(&R.v);

    NCDIf ifc;
    NCDIf_Init(&ifc, A.v, B.v);
    A.have = 0;
    B.have = 0;

    if (!NCDIfBlock_PrependIf(&R.v, ifc)) {
        goto failF1;
    }

    R.have = 1;
    goto doneF0;

failF1:
    NCDIf_Free(&ifc);
    NCDIfBlock_Free(&R.v);
failF0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneF0:
    free_value(A);
    free_block(B);
}

elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif(N). {
    if (!A.have || !B.have || !N.have) {
        goto failG0;
    }

    NCDIf ifc;
    NCDIf_Init(&ifc, A.v, B.v);
    A.have = 0;
    B.have = 0;

    if (!NCDIfBlock_PrependIf(&N.v, ifc)) {
        goto failG1;
    }

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneG0;

failG1:
    NCDIf_Free(&ifc);
failG0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneG0:
    free_value(A);
    free_block(B);
    free_ifblock(N);
}

else_maybe(R) ::= . {
    R.have = 0;
}

else_maybe(R) ::= ELSE CURLY_OPEN statements(B) CURLY_CLOSE. {
    R = B;
}

statement(R) ::= BLOCK CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
    if (!S.have) {
        goto failGA0;
    }
    
    if (!NCDStatement_InitBlock(&R.v, N, S.v)) {
        goto failGA0;
    }
    S.have = 0;
    
    R.have = 1;
    goto doneGA0;
    
failGA0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneGA0:
    free_block(S);
    free(N);
}

interrupt_maybe(R) ::= . {
    R.have = 0;
}

interrupt_maybe(R) ::= TOKEN_INTERRUPT CURLY_OPEN statements(S) CURLY_CLOSE. {
    R = S;
}

statement(R) ::= TOKEN_DO CURLY_OPEN statements(S) CURLY_CLOSE interrupt_maybe(I) name_maybe(N) SEMICOLON. {
    if (!S.have) {
        goto failGB0;
    }
    
    NCDIfBlock if_block;
    NCDIfBlock_Init(&if_block);
    
    if (I.have) {
        NCDIf int_if;
        NCDIf_InitBlock(&int_if, I.v);
        I.have = 0;
        
        if (!NCDIfBlock_PrependIf(&if_block, int_if)) {
            NCDIf_Free(&int_if);
            goto failGB1;
        }
    }
    
    NCDIf the_if;
    NCDIf_InitBlock(&the_if, S.v);
    S.have = 0;
    
    if (!NCDIfBlock_PrependIf(&if_block, the_if)) {
        NCDIf_Free(&the_if);
        goto failGB1;
    }
    
    if (!NCDStatement_InitIf(&R.v, N, if_block, NCDIFTYPE_DO)) {
        goto failGB1;
    }
    
    R.have = 1;
    goto doneGB0;
    
failGB1:
    NCDIfBlock_Free(&if_block);
failGB0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneGB0:
    free_block(S);
    free_block(I);
    free(N);
}

statements(R) ::= statement(A). {
    if (!A.have) {
        goto failH0;
    }

    NCDBlock_Init(&R.v);

    if (!NCDBlock_PrependStatement(&R.v, A.v)) {
        goto failH1;
    }
    A.have = 0;

    R.have = 1;
    goto doneH;

failH1:
    NCDBlock_Free(&R.v);
failH0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneH:
    free_statement(A);
}

statements(R) ::= statement(A) statements(N). {
    if (!A.have || !N.have) {
        goto failI0;
    }

    if (!NCDBlock_PrependStatement(&N.v, A.v)) {
        goto failI1;
    }
    A.have = 0;

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneI;

failI1:
    NCDBlock_Free(&R.v);
failI0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneI:
    free_statement(A);
    free_block(N);
}

dotted_name(R) ::= NAME(A). {
    ASSERT(A.str)

    R = A.str;
}

dotted_name(R) ::= NAME(A) DOT dotted_name(N). {
    ASSERT(A.str)
    if (!N) {
        goto failJ0;
    }

    if (!(R = concat_strings(3, A.str, ".", N))) {
        goto failJ0;
    }

    goto doneJ;

failJ0:
    R = NULL;
    parser_out->out_of_memory = 1;
doneJ:
    free_token(A);
    free(N);
}

name_list(R) ::= NAME(A). {
    if (!A.str) {
        goto failK0;
    }

    NCDValue_InitList(&R.v);
    
    NCDValue this_string;
    if (!NCDValue_InitString(&this_string, A.str)) {
        goto failK1;
    }
    
    if (!NCDValue_ListPrepend(&R.v, this_string)) {
        goto failK2;
    }

    R.have = 1;
    goto doneK;

failK2:
    NCDValue_Free(&this_string);
failK1:
    NCDValue_Free(&R.v);
failK0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneK:
    free_token(A);
}

name_list(R) ::= NAME(A) DOT name_list(N). {
    if (!A.str || !N.have) {
        goto failKA0;
    }
    
    NCDValue this_string;
    if (!NCDValue_InitString(&this_string, A.str)) {
        goto failKA0;
    }

    if (!NCDValue_ListPrepend(&N.v, this_string)) {
        goto failKA1;
    }

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneKA;

failKA1:
    NCDValue_Free(&this_string);
failKA0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneKA:
    free_token(A);
    free_value(N);
}

list_contents_maybe(R) ::= . {
    R.have = 1;
    NCDValue_InitList(&R.v);
}

list_contents_maybe(R) ::= list_contents(A). {
    R = A;
}

list_contents(R) ::= value(A). {
    if (!A.have) {
        goto failL0;
    }

    NCDValue_InitList(&R.v);

    if (!NCDValue_ListPrepend(&R.v, A.v)) {
        goto failL1;
    }
    A.have = 0;

    R.have = 1;
    goto doneL;

failL1:
    NCDValue_Free(&R.v);
failL0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneL:
    free_value(A);
}

list_contents(R) ::= value(A) COMMA list_contents(N). {
    if (!A.have || !N.have) {
        goto failM0;
    }

    if (!NCDValue_ListPrepend(&N.v, A.v)) {
        goto failM0;
    }
    A.have = 0;

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneM;

failM0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneM:
    free_value(A);
    free_value(N);
}

list(R) ::= CURLY_OPEN CURLY_CLOSE. {
    R.have = 1;
    NCDValue_InitList(&R.v);
}

list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
    R = A;
}

map_contents(R) ::= value(A) COLON value(B). {
    if (!A.have || !B.have) {
        goto failS0;
    }

    NCDValue_InitMap(&R.v);

    if (!NCDValue_MapPrepend(&R.v, A.v, B.v)) {
        goto failS1;
    }
    A.have = 0;
    B.have = 0;

    R.have = 1;
    goto doneS;

failS1:
    NCDValue_Free(&R.v);
failS0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneS:
    free_value(A);
    free_value(B);
}

map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
    if (!A.have || !B.have || !N.have) {
        goto failT0;
    }

    if (!NCDValue_MapPrepend(&N.v, A.v, B.v)) {
        goto failT0;
    }
    A.have = 0;
    B.have = 0;

    R.have = 1;
    R.v = N.v;
    N.have = 0;
    goto doneT;

failT0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneT:
    free_value(A);
    free_value(B);
    free_value(N);
}

map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
    R.have = 1;
    NCDValue_InitMap(&R.v);
}

map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
    R = A;
}

invoc(R) ::= value(F) ROUND_OPEN list_contents_maybe(A) ROUND_CLOSE. {
    if (!F.have || !A.have) {
        goto failQ0;
    }
    
    if (!NCDValue_InitInvoc(&R.v, F.v, A.v)) {
        goto failQ0;
    }
    F.have = 0;
    A.have = 0;
    R.have = 1;
    goto doneQ;
    
failQ0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneQ:
    free_value(F);
    free_value(A);
}

value(R) ::= STRING(A). {
    ASSERT(A.str)

    if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
        goto failU0;
    }

    R.have = 1;
    goto doneU;

failU0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneU:
    free_token(A);
}

value(R) ::= AT_SIGN dotted_name(A). {
    if (!A) {
        goto failUA0;
    }
    
    if (!NCDValue_InitString(&R.v, A)) {
        goto failUA0;
    }
    
    R.have = 1;
    goto doneUA0;
    
failUA0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneUA0:
    free(A);
}

value(R) ::= CARET name_list(A). {
    R = A;
}

value(R) ::= dotted_name(A). {
    if (!A) {
        goto failV0;
    }

    if (!NCDValue_InitVar(&R.v, A)) {
        goto failV0;
    }

    R.have = 1;
    goto doneV;

failV0:
    R.have = 0;
    parser_out->out_of_memory = 1;
doneV:
    free(A);
}

value(R) ::= list(A). {
    R = A;
}

value(R) ::= map(A). {
    R = A;
}

value(R) ::= ROUND_OPEN value(A) ROUND_CLOSE. {
    R = A;
}

value(R) ::= invoc(A). {
    R = A;
}

name_maybe(R) ::= . {
    R = NULL;
}

name_maybe(R) ::= NAME(A). {
    ASSERT(A.str)

    R = A.str;
}

process_or_template(R) ::= PROCESS. {
    R = 0;
}

process_or_template(R) ::= TEMPLATE. {
    R = 1;
}