summaryrefslogtreecommitdiff
path: root/src/packrat-parsers.ads
blob: 7edddb7b3903347bbbc169656aeeeea7379aceb8 (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


--  This source is licensed under the Sunset License v1.0


with

    Packrat.Traits,
    Packrat.Parse_Graphs;

private with

    Ada.Strings.Unbounded,
    Ada.Containers.Vectors,
    Ada.Containers.Ordered_Maps,
    Ada.Containers.Ordered_Sets,
    Ada.Containers.Indefinite_Holders;


generic

    with package Traits is new Packrat.Traits (<>);
    with package Graphs is new Packrat.Parse_Graphs (Traits);

package Packrat.Parsers is


    type Parser_Context is private;




    type Combinator_Result is private;

    type Combinator is access function
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    type Combinator_Array is array (Positive range <>) of Combinator;




    type With_Input is access function
        return Traits.Element_Array;




    generic
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    package Parse_Parts is

        procedure Parse
               (Input  : in     Traits.Element_Array;
                Result :    out Graphs.Parse_Graph);

        procedure Reset;

    end Parse_Parts;


    generic
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    package Parse_Once is

        function Parse
               (Input : in Traits.Element_Array)
            return Graphs.Parse_Graph;

        procedure Reset;

    end Parse_Once;


    generic
        with function Root
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    package Parse_With is

        function Parse
               (Input : in With_Input)
            return Graphs.Parse_Graph;

        procedure Reset;

    end Parse_With;




    generic
        Label : in Traits.Label_Enum;
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Stamp
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Label : in Traits.Label_Enum;
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Discard
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Ignore
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    generic
    package Redirect is

        procedure Set
               (Target : in Combinator);

        function Call
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;

    end Redirect;




    generic
        Params : in Combinator_Array;
    function Sequence
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Part_One
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Part_Two
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Sequence_2
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Params : in Combinator_Array;
    function Choice
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Choice_One
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Choice_Two
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Choice_2
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Number : in Positive;
    function Count
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Minimum : in Natural := 0;
    function Many
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Followed_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Not_Followed_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Test
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Minimum : in Natural := 0;
    function Many_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Param
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Optional
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Item
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Separator
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Minimum : in Natural := 0;
    function Separate_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Item
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Separator
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Ender
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        Minimum : in Natural := 0;
    function Separate_End_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Starter
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Item
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
        with function Ender
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Between
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Satisfy
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
        with function Change
               (From : in Traits.Element_Type)
            return Traits.Element_Type;
    function Satisfy_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Item : in Traits.Element_Type;
    function Match
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Item : in Traits.Element_Type;
        with function Change
               (From : in Traits.Element_Type)
            return Traits.Element_Type;
    function Match_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Items : in Traits.Element_Array;
    function Multimatch
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        Number : in Positive := 1;
    function Take
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Take_While
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Test
               (Item : in Traits.Element_Type)
            return Boolean;
    function Take_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;




    function Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    generic
        with function Combo
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Not_Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;

    function End_Of_Input
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result;


private


    package Elem_Holds is new Ada.Containers.Indefinite_Holders
       (Element_Type => Traits.Element_Array,
        "="          => Traits."=");

    package Tok_Holds is new Ada.Containers.Indefinite_Holders
       (Element_Type => Traits.Tokens.Finished_Token_Array,
        "="          => Traits.Tokens."=");

    function "<"
           (Left, Right : in Elem_Holds.Holder)
        return Boolean;

    function "<"
           (Left, Right : in Tok_Holds.Holder)
        return Boolean;

    function Element
           (Hold : in Elem_Holds.Holder)
        return Traits.Element_Array;

    function Element
           (Hold : in Tok_Holds.Holder)
        return Traits.Tokens.Finished_Token_Array;




    type Combo_Key is record
        Start : Positive;
        Func  : Combinator;
    end record;

    function "<"
           (Left, Right : in Combo_Key)
        return Boolean;

    package Combo_Key_Vectors is new Ada.Containers.Vectors
       (Index_Type   => Positive,
        Element_Type => Combo_Key);

    --  This is needed to avoid some issues with using non-anonymous
    --  access values in a generic subprogram instantiation.
    function To_Key
           (Start : in Positive;
            Func  : access function
                   (Input   : in     Traits.Element_Array;
                    Context : in out Parser_Context;
                    Start   : in     Positive)
                return Combinator_Result)
        return Combo_Key;




    type Combo_Result_Part is record
        Finish : Natural := 0;
        Value  : Elem_Holds.Holder := Elem_Holds.Empty_Holder;
        Tokens : Tok_Holds.Holder := Tok_Holds.Empty_Holder;
    end record;

    function "<"
           (Left, Right : in Combo_Result_Part)
        return Boolean;

    package Result_Sets is new Ada.Containers.Ordered_Sets
       (Element_Type => Combo_Result_Part);




    function "<"
           (Left, Right : in Combinator)
        return Boolean;

    package Curtail_Maps is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combinator,
        Element_Type => Positive);




    type Combinator_Result is record
        Results  : Result_Sets.Set := Result_Sets.Empty_Set;
        Curtails : Curtail_Maps.Map := Curtail_Maps.Empty_Map;
        Status   : Result_Status := Failure;
    end record;

    Empty_Fail : constant Combinator_Result :=
       (Results  => Result_Sets.Empty_Set,
        Curtails => Curtail_Maps.Empty_Map,
        Status   => Failure);




    package Needs_Sets is new Ada.Containers.Ordered_Sets
       (Element_Type => Positive);

    package Memotables is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combo_Key,
        Element_Type => Combinator_Result);

    package Leftrectables is new Ada.Containers.Ordered_Maps
       (Key_Type     => Combo_Key,
        Element_Type => Positive);




    type Parser_Context is record
        Result_So_Far    : Graphs.Parse_Graph;
        Used_Before      : Boolean := False;
        Global_Start     : Positive := 1;
        Current_Position : Positive := 1;
        Needs_More       : Needs_Sets.Set;
        Pass_Forward     : Elem_Holds.Holder;
        Error_String     : Ada.Strings.Unbounded.Unbounded_String;
        Memotable        : Memotables.Map;
        Leftrectable     : Leftrectables.Map;
        Allow_Incomplete : Boolean := True;
    end record;

    Empty_Context : constant Parser_Context :=
       (Result_So_Far    => Graphs.Empty_Graph,
        Used_Before      => False,
        Global_Start     => 1,
        Current_Position => 1,
        Needs_More       => Needs_Sets.Empty_Set,
        Pass_Forward     => Elem_Holds.Empty_Holder,
        Error_String     => +"",
        Memotable        => Memotables.Empty_Map,
        Leftrectable     => Leftrectables.Empty_Map,
        Allow_Incomplete => True);




    function Reusable
           (Result   : in Combinator_Result;
            Position : in Positive;
            Leftrecs : in Leftrectables.Map)
        return Boolean;

    generic
        My_Key : in Combo_Key;
        with function Actual
               (Context : in out Parser_Context)
            return Combinator_Result;
    function Memoize
           (Context : in out Parser_Context)
        return Combinator_Result;




    procedure Inc_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map);

    procedure Dec_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map);

    function Exceeds_Curtail
           (Key      : in Combo_Key;
            Leftrecs : in Leftrectables.Map;
            Input    : in Traits.Element_Array)
        return Boolean;

    generic
        My_Key : in Combo_Key;
        Input  : in Traits.Element_Array;
        with function Actual
               (Context : in out Parser_Context)
            return Combinator_Result;
    function Curtailment
           (Context : in out Parser_Context)
        return Combinator_Result;




    procedure Merge
           (Target : in out Curtail_Maps.Map;
            Add    : in     Curtail_Maps.Map);

    function Merge
           (Left, Right : in Curtail_Maps.Map)
        return Curtail_Maps.Map;

    --  Should only be used to merge results obtained from
    --  the same input location.
    procedure Merge
           (Target : in out Combinator_Result;
            Add    : in     Combinator_Result);

    function Merge
           (Left, Right : in Combinator_Result)
        return Combinator_Result;

    generic
        with function Next
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result;
    function Continue
           (From    : in     Combinator_Result;
            Input   : in     Traits.Element_Array;
            Context : in out Parser_Context)
        return Combinator_Result;




    procedure Complete_Status
           (Result : in out Combinator_Result;
            Allow  : in     Boolean);




    --  Should this be factored out into the main Packrat package?
    function Slide
           (Input    : in Traits.Element_Array;
            Position : in Positive)
        return Traits.Element_Array;


end Packrat.Parsers;