summaryrefslogtreecommitdiff
path: root/src/packrat-parsers.adb
blob: 06a0b0074564956576057b5d547ab9b6ecac4253 (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
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362


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


with

    Packrat.Errors,
    System;


package body Packrat.Parsers is


    function Element
           (Hold : in Elem_Holds.Holder)
        return Traits.Element_Array is
    begin
        if Hold.Is_Empty then
            return Value : Traits.Element_Array (1 .. 0);
        else
            return Hold.Element;
        end if;
    end Element;


    function Element
           (Hold : in Tok_Holds.Holder)
        return Traits.Tokens.Finished_Token_Array is
    begin
        if Hold.Is_Empty then
            return Value : Traits.Tokens.Finished_Token_Array (1 .. 0);
        else
            return Hold.Element;
        end if;
    end Element;





    function "<"
           (Left, Right : in Elem_Holds.Holder)
        return Boolean
    is
        use Traits;
    begin
        return Element (Left) < Element (Right);
    end "<";


    function "<"
           (Left, Right : in Tok_Holds.Holder)
        return Boolean
    is
        use type Traits.Tokens.Finished_Token_Array;
    begin
        return Element (Left) < Element (Right);
    end "<";


    function "<"
           (Left, Right : in Combo_Key)
        return Boolean is
    begin
        if Left.Start = Right.Start then
            return Left.Func < Right.Func;
        else
            return Left.Start < Right.Start;
        end if;
    end "<";


    function "<"
           (Left, Right : in Combo_Result_Part)
        return Boolean
    is
        use type Elem_Holds.Holder;
    begin
        if Left.Finish = Right.Finish then
            if Left.Value = Right.Value then
                return Left.Tokens < Right.Tokens;
            else
                return Left.Value < Right.Value;
            end if;
        else
            return Left.Finish < Right.Finish;
        end if;
    end "<";


    function "<"
           (Left, Right : in Combinator)
        return Boolean
    is
        use type System.Address;
    begin
        return Left.all'Address < Right.all'Address;
    end "<";





    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 is
    begin
        return (Start => Start, Func => Func);
    end To_Key;





    function Reusable
           (Result   : in Combinator_Result;
            Position : in Positive;
            Leftrecs : in Leftrectables.Map)
        return Boolean
    is
        Working_Key : Combo_Key;
    begin
        for Cursor in Result.Curtails.Iterate loop
            Working_Key := To_Key (Position, Curtail_Maps.Key (Cursor));
            if Leftrecs.Contains (Working_Key) and then
                Curtail_Maps.Element (Cursor) > Leftrecs.Element (Working_Key)
            then
                return False;
            end if;
        end loop;
        return True;
    end Reusable;


    function Memoize
           (Context : in out Parser_Context)
        return Combinator_Result
    is
        Result : Combinator_Result;
    begin
        if Context.Memotable.Contains (My_Key) then
            Result := Context.Memotable.Element (My_Key);
            if Reusable (Result, My_Key.Start, Context.Leftrectable) then
                return Result;
            end if;
        end if;
        if My_Key.Start < Context.Current_Position then
            raise Constraint_Error;
        end if;
        Result := Actual (Context);
        if Result.Status = Needs_More or Result.Status = Optional_More then
            Context.Needs_More.Include (My_Key.Start);
        end if;
        if Context.Memotable.Contains (My_Key) then
            Context.Memotable.Replace (My_Key, Result);
        else
            Context.Memotable.Insert (My_Key, Result);
        end if;
        return Result;
    end Memoize;





    procedure Inc_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map) is
    begin
        if Leftrecs.Contains (Key) then
            Leftrecs.Replace (Key, Leftrecs.Element (Key) + 1);
        else
            Leftrecs.Insert (Key, 1);
        end if;
    end Inc_Leftrec;


    procedure Dec_Leftrec
           (Key      : in     Combo_Key;
            Leftrecs : in out Leftrectables.Map) is
    begin
        if Leftrecs.Contains (Key) then
            if Leftrecs.Element (Key) = 1 then
                Leftrecs.Delete (Key);
            else
                Leftrecs.Replace (Key, Leftrecs.Element (Key) - 1);
            end if;
        end if;
    end Dec_Leftrec;


    function Exceeds_Curtail
           (Key      : in Combo_Key;
            Leftrecs : in Leftrectables.Map;
            Input    : in Traits.Element_Array)
        return Boolean is
    begin
        return Leftrecs.Contains (Key) and then
            Leftrecs.Element (Key) > Input'Last - (Key.Start - 1) + 1;
    end Exceeds_Curtail;


    function Curtailment
           (Context : in out Parser_Context)
        return Combinator_Result
    is
        My_Result : Combinator_Result;
        My_Curtails : Curtail_Maps.Map;
    begin
        Inc_Leftrec (My_Key, Context.Leftrectable);
        if Exceeds_Curtail (My_Key, Context.Leftrectable, Input) then
            My_Curtails.Insert (My_Key.Func, Context.Leftrectable.Element (My_Key));
            My_Result :=
               (Results  => Result_Sets.Empty_Set,
                Curtails => My_Curtails,
                Status   => Failure);
        else
            My_Result := Actual (Context);
        end if;
        Dec_Leftrec (My_Key, Context.Leftrectable);
        return My_Result;
    end Curtailment;





    procedure Merge
           (Target : in out Curtail_Maps.Map;
            Add    : in     Curtail_Maps.Map) is
    begin
        for Curse in Add.Iterate loop
            if Target.Contains (Curtail_Maps.Key (Curse)) then
                if Target.Element (Curtail_Maps.Key (Curse)) >
                    Add.Element (Curtail_Maps.Key (Curse))
                then
                    Target.Replace (Curtail_Maps.Key (Curse), Curtail_Maps.Element (Curse));
                end if;
            else
                Target.Insert (Curtail_Maps.Key (Curse), Curtail_Maps.Element (Curse));
            end if;
        end loop;
    end Merge;


    function Merge
           (Left, Right : in Curtail_Maps.Map)
        return Curtail_Maps.Map is
    begin
        return Result : Curtail_Maps.Map := Left do
            Merge (Result, Right);
        end return;
    end Merge;


    procedure Merge
           (Target : in out Combinator_Result;
            Add    : in     Combinator_Result) is
    begin
        case Target.Status is
            when Success =>
                case Add.Status is
                    when Success =>
                        Target.Results.Union (Add.Results);
                    when Optional_More =>
                        Target.Results.Union (Add.Results);
                        Target.Status := Optional_More;
                    when Needs_More =>
                        Target.Status := Optional_More;
                    when Failure =>
                        null;
                end case;
            when Optional_More =>
                case Add.Status is
                    when Success | Optional_More =>
                        Target.Results.Union (Add.Results);
                    when Needs_More | Failure =>
                        null;
                end case;
            when Needs_More =>
                case Add.Status is
                    when Success | Optional_More =>
                        Target := Add;
                        Target.Status := Optional_More;
                    when Needs_More | Failure =>
                        null;
                end case;
            when Failure =>
                if Add.Status = Failure then
                    Merge (Target.Curtails, Add.Curtails);
                else
                    Target := Add;
                end if;
        end case;
    end Merge;


    function Merge
           (Left, Right : in Combinator_Result)
        return Combinator_Result is
    begin
        return Salt : Combinator_Result := Left do
            Merge (Salt, Right);
        end return;
    end Merge;


    function Continue
           (From    : in     Combinator_Result;
            Input   : in     Traits.Element_Array;
            Context : in out Parser_Context)
        return Combinator_Result
    is
        use type Traits.Element_Array;
        use type Traits.Tokens.Finished_Token_Array;
        Salt, Temp : Combinator_Result;
        Adjust : Result_Sets.Set;
    begin
        for R of From.Results loop
            Temp := Next (Input, Context, R.Finish + 1);
            Adjust.Clear;
            for N of Temp.Results loop
                Adjust.Include
                   ((Finish => Integer'Max (R.Finish, N.Finish),
                     Value  => Elem_Holds.To_Holder (Element (R.Value) & Element (N.Value)),
                     Tokens => Tok_Holds.To_Holder (Element (R.Tokens) & Element (N.Tokens))));
            end loop;
            Temp.Results := Adjust;
            Merge (Salt, Temp);
        end loop;
        return Salt;
    end Continue;





    procedure Complete_Status
           (Result : in out Combinator_Result;
            Allow  : in     Boolean) is
    begin
        if not Allow then
            if Result.Status = Optional_More then
                Result.Status := Success;
            elsif Result.Status = Needs_More then
                Result.Status := Failure;
            end if;
        end if;
    end Complete_Status;





    function Slide
           (Input    : in Traits.Element_Array;
            Position : in Positive)
        return Traits.Element_Array
    is
        subtype Slider is Traits.Element_Array
            (Position .. Position + Input'Length - 1);
    begin
        return Slider (Input);
    end Slide;





    procedure Tidy_Context
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context)
    is
        Delete_Keys : Combo_Key_Vectors.Vector;
    begin
        if Context.Result_So_Far.Has_Root then
            raise Constraint_Error;
        end if;
        Context.Needs_More.Clear;
        Context.Leftrectable.Clear;
        if not Context.Used_Before then
            Context.Used_Before := True;
            Context.Global_Start := Input'First;
            Context.Current_Position := Input'First;
        end if;
        for C in Context.Memotable.Iterate loop
            if Memotables.Element (C).Status = Optional_More or
                Memotables.Element (C).Status = Needs_More
            then
                Delete_Keys.Append (Memotables.Key (C));
            end if;
        end loop;
        for K of Delete_Keys loop
            Context.Memotable.Delete (K);
        end loop;
    end Tidy_Context;


    function Finish_Root
           (Root_Result : in     Combinator_Result;
            Context     : in out Parser_Context)
        return Graphs.Parse_Graph
    is
        Length : Natural := 0;
        Index : Positive := 1;
    begin
        for R of Root_Result.Results loop
            if not R.Tokens.Is_Empty then
                Length := Length + Integer (Element (R.Tokens)'Length);
            end if;
        end loop;
        if Length = 0 then
            return Graphs.Empty_Graph;
        end if;
        declare
            Root_Elems : Traits.Tokens.Finished_Token_Array (1 .. Length);
        begin
            for R of Root_Result.Results loop
                for T of Element (R.Tokens) loop
                    Root_Elems (Index) := T;
                    Index := Index + 1;
                end loop;
            end loop;
            Context.Result_So_Far.Set_Root (Root_Elems);
            Context.Result_So_Far.Delete_Unreachable;
            return Context.Result_So_Far;
        end;
    end Finish_Root;





    package body Parse_Parts is

        Context : Parser_Context := Empty_Context;

        procedure Parse
               (Input  : in     Traits.Element_Array;
                Result :    out Graphs.Parse_Graph) is
        begin
            Tidy_Context (Input, Context);
            Context.Allow_Incomplete := (Input'Length /= 0);
            declare
                use type Traits.Element_Array;
                Real_Input : Traits.Element_Array :=
                    (if Context.Pass_Forward.Is_Empty
                        then Slide (Input, Context.Current_Position)
                        else Element (Context.Pass_Forward) & Input);
                Root_Result : Combinator_Result :=
                    Root (Real_Input, Context, Context.Global_Start);
            begin
                if Root_Result.Status = Failure then
                    raise Parser_Error with -Context.Error_String;
                end if;
                if Input'Length = 0 then
                    Result := Finish_Root (Root_Result, Context);
                    return;
                end if;
                if not Context.Needs_More.Is_Empty then
                    Context.Current_Position := Context.Needs_More.First_Element;
                    Context.Pass_Forward.Replace_Element
                        (Real_Input (Context.Current_Position .. Real_Input'Last));
                else
                    Context.Current_Position := Real_Input'Last + 1;
                    Context.Pass_Forward.Clear;
                end if;
            end;
        end Parse;

        procedure Reset is
        begin
            Context := Empty_Context;
        end Reset;

    end Parse_Parts;


    package body Parse_Once is

        Context : Parser_Context := Empty_Context;

        function Parse
               (Input : in Traits.Element_Array)
            return Graphs.Parse_Graph is
        begin
            Tidy_Context (Input, Context);
            Context.Allow_Incomplete := False;
            declare
                use type Traits.Element_Array;
                Real_Input : Traits.Element_Array :=
                    (if Context.Pass_Forward.Is_Empty
                        then Slide (Input, Context.Current_Position)
                        else Element (Context.Pass_Forward) & Input);
                Root_Result : Combinator_Result :=
                    Root (Real_Input, Context, Context.Global_Start);
            begin
                if Root_Result.Status /= Success then
                    raise Parser_Error with -Context.Error_String;
                end if;
                return Finish_Root (Root_Result, Context);
            end;
        end Parse;

        procedure Reset is
        begin
            Context := Empty_Context;
        end Reset;

    end Parse_Once;


    package body Parse_With is

        package My_Parse is new Parse_Parts (Root);

        function Parse
               (Input : in With_Input)
            return Graphs.Parse_Graph
        is
            Result : Graphs.Parse_Graph;
        begin
            loop
                declare
                    Next_Input : Traits.Element_Array := Input.all;
                begin
                    My_Parse.Parse (Next_Input, Result);
                    exit when Next_Input'Length = 0;
                end;
            end loop;
            return Result;
        end Parse;

        procedure Reset is
        begin
            My_Parse.Reset;
        end Reset;

    end Parse_With;





    function Stamp
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Salt : Combinator_Result := Combo (Input, Context, Start);
            Current : Traits.Tokens.Finished_Token_Type;
            Processed : Result_Sets.Set;
        begin
            if Salt.Status = Failure then
                Ada.Strings.Unbounded.Append
                   (Context.Error_String,
                    Packrat.Errors.Encode (Traits.Label_Enum'Image (Label), Start));
                return Salt;
            else
                Context.Error_String := +"";
            end if;
            for R of Salt.Results loop
                Current :=
                   (Token  => Traits.Tokens.Create (Label, Start, Element (R.Value)),
                    Finish => R.Finish);
                if Salt.Status = Success then
                    if not R.Tokens.Is_Empty then
                        Context.Result_So_Far.Connect (Current, Element (R.Tokens));
                    else
                        Context.Result_So_Far.Include (Current.Token);
                    end if;
                end if;
                Processed.Include
                   ((Finish => R.Finish,
                     Value  => Elem_Holds.Empty_Holder,
                     Tokens => Tok_Holds.To_Holder ((1 => Current))));
            end loop;
            Salt.Results := Processed;
            return Salt;
        end Actual;
        function Memo is new Memoize (To_Key (Start, Stamp'Access), Actual);
    begin
        return Memo (Context);
    end Stamp;


    function Ignore
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Salt : Combinator_Result := Combo (Input, Context, Start);
            Processed : Result_Sets.Set;
        begin
            for R of Salt.Results loop
                Processed.Include
                   ((Finish => R.Finish,
                     Value  => Elem_Holds.Empty_Holder,
                     Tokens => Tok_Holds.Empty_Holder));
            end loop;
            Salt.Results := Processed;
            return Salt;
        end Actual;
        function Memo is new Memoize (To_Key (Start, Ignore'Access), Actual);
    begin
        return Memo (Context);
    end Ignore;





    package body Redirect is

        Combo : Combinator := null;

        procedure Set
               (Target : in Combinator) is
        begin
            Combo := Target;
        end Set;

        function Call
               (Input   : in     Traits.Element_Array;
                Context : in out Parser_Context;
                Start   : in     Positive)
            return Combinator_Result is
        begin
            if Combo = null then
                raise Parser_Error;
            else
                return Combo (Input, Context, Start);
            end if;
        end Call;

    end Redirect;





    function Sequence
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Salt : Combinator_Result;
        begin
            if Params'Length = 0 then
                return Empty (Input, Context, Start);
            end if;
            Salt := Params (Params'First) (Input, Context, Start);
            for I in Integer range Params'First + 1 .. Params'Last loop
                exit when Salt.Status = Failure;
                declare
                    function Cont_Param is new Continue (Params (I).all);
                begin
                    Salt := Cont_Param (Salt, Input, Context);
                end;
            end loop;
            Complete_Status (Salt, Context.Allow_Incomplete);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Sequence'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Sequence'Access), Curt);
    begin
        return Memo (Context);
    end Sequence;


    --  This exists purely to get around errors that would otherwise
    --  result from using Sequence internally due to the access types.
    function Sequence_2
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Cont_Param is new Continue (Part_Two);
            Salt : Combinator_Result;
        begin
            Salt := Part_One (Input, Context, Start);
            Salt := Cont_Param (Salt, Input, Context);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Sequence_2'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Sequence_2'Access), Curt);
    begin
        return Memo (Context);
    end Sequence_2;


    function Choice
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Salt : Combinator_Result;
        begin
            if Params'Length = 0 then
                return Empty (Input, Context, Start);
            end if;
            for C of Params loop
                Merge (Salt, C (Input, Context, Start));
            end loop;
            Complete_Status (Salt, Context.Allow_Incomplete);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Choice'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Choice'Access), Curt);
    begin
        return Memo (Context);
    end Choice;


    --  This exists because otherwise Sequence_2 would look weird.
    function Choice_2
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result is
        begin
            return Merge
               (Choice_One (Input, Context, Start),
                Choice_Two (Input, Context, Start));
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Choice_2'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Choice_2'Access), Curt);
    begin
        return Memo (Context);
    end Choice_2;


    function Count
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Cont_Param is new Continue (Param);
            Salt : Combinator_Result;
            Counter : Natural := 0;
        begin
            Salt := Param (Input, Context, Start);
            while Salt.Status /= Failure loop
                Counter := Counter + 1;
                exit when Counter = Number;
                if Salt.Status = Optional_More or Salt.Status = Needs_More or
                    (for some P of Salt.Results => P.Finish = Input'Last)
                then
                    Salt.Results.Clear;
                    Salt.Status := Needs_More;
                    exit;
                end if;
                Salt := Cont_Param (Salt, Input, Context);
            end loop;
            Complete_Status (Salt, Context.Allow_Incomplete);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Count'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Count'Access), Curt);
    begin
        return Memo (Context);
    end Count;


    function Many
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Not_Empty_Param is new Not_Empty (Param);
            function Cont_Param is new Continue (Not_Empty_Param);
            Salt, Temp : Combinator_Result;
            Counter : Natural := 0;
        begin
            if Minimum = 0 then
                Merge (Salt, Empty (Input, Context, Start));
            end if;
            Temp := Not_Empty_Param (Input, Context, Start);
            while Temp.Status /= Failure loop
                Counter := Counter + 1;
                if Counter >= Minimum then
                    Merge (Salt, Temp);
                    if Temp.Status = Optional_More or Temp.Status = Needs_More or
                        (for some P of Temp.Results => P.Finish = Input'Last)
                    then
                        Salt.Status := Optional_More;
                        exit;
                    end if;
                elsif Temp.Status = Optional_More or Temp.Status = Needs_More or
                    (for some P of Temp.Results => P.Finish = Input'Last)
                then
                    Salt.Status := Needs_More;
                    exit;
                end if;
                Temp := Cont_Param (Temp, Input, Context);
            end loop;
            Complete_Status (Salt, Context.Allow_Incomplete);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Many'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Many'Access), Curt);
    begin
        return Memo (Context);
    end Many;


    function Followed_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result is
        begin
            case Param (Input, Context, Start).Status is
                when Success | Optional_More =>
                    return Empty (Input, Context, Start);
                when Needs_More =>
                    return Salt : Combinator_Result do
                        if Context.Allow_Incomplete then
                            Salt.Status := Needs_More;
                        end if;
                    end return;
                when Failure =>
                    return Salt : Combinator_Result do
                        if Context.Allow_Incomplete and Start > Input'Last then
                            Salt.Status := Needs_More;
                        end if;
                    end return;
            end case;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Followed_By'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Followed_By'Access), Curt);
    begin
        return Memo (Context);
    end Followed_By;


    function Not_Followed_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result is
        begin
            case Param (Input, Context, Start).Status is
                when Success | Optional_More =>
                    return Salt : Combinator_Result;
                when Needs_More =>
                    if Context.Allow_Incomplete then
                        return Salt : Combinator_Result do
                            Salt.Status := Needs_More;
                        end return;
                    else
                        return Empty (Input, Context, Start);
                    end if;
                when Failure =>
                    if Context.Allow_Incomplete and Start > Input'Last then
                        return Salt : Combinator_Result do
                            Salt.Status := Needs_More;
                        end return;
                    else
                        return Empty (Input, Context, Start);
                    end if;
            end case;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Not_Followed_By'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Not_Followed_By'Access), Curt);
    begin
        return Memo (Context);
    end Not_Followed_By;





    function Many_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Not_Till is new Not_Followed_By (Test);
            function Till is new Followed_By (Test);
            function Sep_End_By is new Separate_End_By (Param, Not_Till, Till, Minimum);
        begin
            return Sep_End_By (Input, Context, Start);
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Many_Until'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Many_Until'Access), Curt);
    begin
        return Memo (Context);
    end Many_Until;


    function Optional
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Not_Empty_Param is new Not_Empty (Param);
            Salt : Combinator_Result := Empty (Input, Context, Start);
        begin
            Merge (Salt, Not_Empty_Param (Input, Context, Start));
            Complete_Status (Salt, Context.Allow_Incomplete);
            return Salt;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Optional'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Optional'Access), Curt);
    begin
        return Memo (Context);
    end Optional;


    function Separate_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Not_Empty_Item is new Not_Empty (Item);
            function Sep_Seq is new Sequence_2 (Separator, Not_Empty_Item);
            function Many_Sep_Seq is new Many
                (Sep_Seq, (if Minimum = 0 then Minimum else Minimum - 1));
            function Full_Seq is new Sequence_2 (Not_Empty_Item, Many_Sep_Seq);
        begin
            if Minimum = 0 then
                return Merge (Empty (Input, Context, Start), Full_Seq (Input, Context, Start));
            else
                return Full_Seq (Input, Context, Start);
            end if;
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Separate_By'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Separate_By'Access), Curt);
    begin
        return Memo (Context);
    end Separate_By;


    function Separate_End_By
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Sep_By is new Separate_By (Item, Separator, Minimum);
            function End_Seq is new Sequence_2 (Sep_By, Ender);
        begin
            return End_Seq (Input, Context, Start);
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Separate_End_By'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Separate_End_By'Access), Curt);
    begin
        return Memo (Context);
    end Separate_End_By;


    function Between
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            function Ignore_Start is new Ignore (Starter);
            function Ignore_End is new Ignore (Ender);
            function Part_Seq is new Sequence_2 (Ignore_Start, Item);
            function Full_Seq is new Sequence_2 (Part_Seq, Ignore_End);
        begin
            return Full_Seq (Input, Context, Start);
        end Actual;
        function Curt is new Curtailment (To_Key (Start, Between'Access), Input, Actual);
        function Memo is new Memoize (To_Key (Start, Between'Access), Curt);
    begin
        return Memo (Context);
    end Between;





    function Satisfy
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Part : Combo_Result_Part;
            Salt : Combinator_Result;
        begin
            if Start <= Input'Last and then Test (Input (Start)) then
                Part.Finish := Start;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end if;
            return Salt;
        end Actual;
        function Call is new Memoize (To_Key (Start, Satisfy'Access), Actual);
    begin
        return Call (Context);
    end Satisfy;


    function Satisfy_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Part : Combo_Result_Part;
            Salt : Combinator_Result;
        begin
            if Start <= Input'Last and then Test (Change (Input (Start))) then
                Part.Finish := Start;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end if;
            return Salt;
        end Actual;
        function Call is new Memoize (To_Key (Start, Satisfy_With'Access), Actual);
    begin
        return Call (Context);
    end Satisfy_With;


    function Match
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            use type Traits.Element_Type;
            Part : Combo_Result_Part;
            Salt : Combinator_Result;
        begin
            if Start <= Input'Last and then Input (Start) = Item then
                Part.Finish := Start;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end if;
            return Salt;
        end Actual;
        function Call is new Memoize (To_Key (Start, Match'Access), Actual);
    begin
        return Call (Context);
    end Match;


    function Match_With
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            use type Traits.Element_Type;
            Part : Combo_Result_Part;
            Salt : Combinator_Result;
        begin
            if Start <= Input'Last and then Change (Input (Start)) = Item then
                Part.Finish := Start;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end if;
            return Salt;
        end Actual;
        function Call is new Memoize (To_Key (Start, Match_With'Access), Actual);
    begin
        return Call (Context);
    end Match_With;


    function Multimatch
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            use type Traits.Element_Array;
            Part : Combo_Result_Part;
        begin
            if Start > Input'Last then
                return Salt : Combinator_Result;
            elsif Items'Length = 0 then
                return Empty (Input, Context, Start);
            end if;
            if Input'Last - Start < Items'Length - 1 then
                return Salt : Combinator_Result do
                    if Context.Allow_Incomplete and Input (Start .. Input'Last) =
                        Items (Items'First .. Items'First + Input'Last - Start)
                    then
                        Salt.Status := Needs_More;
                    end if;
                end return;
            end if;
            if Input (Start .. Start + Items'Length - 1) /= Items (Items'First .. Items'Last) then
                return Salt : Combinator_Result;
            end if;
            return Salt : Combinator_Result do
                Part.Finish := Start + Items'Length - 1;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start + Items'Length - 1));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end return;
        end Actual;
        function Call is new Memoize (To_Key (Start, Multimatch'Access), Actual);
    begin
        return Call (Context);
    end Multimatch;


    function Take
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Part : Combo_Result_Part;
        begin
            if Start > Input'Last then
                return Salt : Combinator_Result;
            end if;
            if Input'Last - Start < Number - 1 then
                return Salt : Combinator_Result do
                    if Context.Allow_Incomplete then
                        Salt.Status := Needs_More;
                    end if;
                end return;
            end if;
            return Salt : Combinator_Result do
                Part.Finish := Start + Number - 1;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. Start + Number - 1));
                Salt.Results.Include (Part);
                Salt.Status := Success;
            end return;
        end Actual;
        function Call is new Memoize (To_Key (Start, Take'Access), Actual);
    begin
        return Call (Context);
    end Take;


    function Take_While
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Part : Combo_Result_Part;
            My_Finish : Positive := Start;
        begin
            if Start > Input'Last or else not Test (Input (Start)) then
                return Empty_Fail;
            end if;
            while My_Finish <= Input'Last and then Test (Input (My_Finish)) loop
                My_Finish := My_Finish + 1;
            end loop;
            My_Finish := My_Finish - 1;
            return Salt : Combinator_Result do
                Part.Finish := My_Finish;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. My_Finish));
                Salt.Results.Include (Part);
                Salt.Status := (if My_Finish = Input'Last and Context.Allow_Incomplete
                    then Optional_More else Success);
            end return;
        end Actual;
        function Call is new Memoize (To_Key (Start, Take_While'Access), Actual);
    begin
        return Call (Context);
    end Take_While;


    function Take_Until
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        function Actual
               (Context : in out Parser_Context)
            return Combinator_Result
        is
            Part : Combo_Result_Part;
            My_Finish : Positive := Start;
        begin
            if Start > Input'Last or else Test (Input (Start)) then
                return Empty_Fail;
            end if;
            while My_Finish <= Input'Last and then not Test (Input (My_Finish)) loop
                My_Finish := My_Finish + 1;
            end loop;
            My_Finish := My_Finish - 1;
            return Salt : Combinator_Result do
                Part.Finish := My_Finish;
                Part.Value := Elem_Holds.To_Holder (Input (Start .. My_Finish));
                Salt.Results.Include (Part);
                Salt.Status := (if My_Finish = Input'Last and Context.Allow_Incomplete
                    then Optional_More else Success);
            end return;
        end Actual;
        function Call is new Memoize (To_Key (Start, Take_Until'Access), Actual);
    begin
        return Call (Context);
    end Take_Until;





    function Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        Part : Combo_Result_Part;
        Salt : Combinator_Result;
    begin
        Part.Finish := Start - 1;
        Salt.Results.Include (Part);
        Salt.Status := Success;
        return Salt;
    end Empty;


    function Not_Empty
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result
    is
        Salt : Combinator_Result := Combo (Input, Context, Start);
        Adjust : Result_Sets.Set;
    begin
        for R of Salt.Results loop
            if R.Finish >= Start then
                Adjust.Include (R);
            end if;
        end loop;
        Salt.Results.Assign (Adjust);
        return Salt;
    end Not_Empty;


    function End_Of_Input
           (Input   : in     Traits.Element_Array;
            Context : in out Parser_Context;
            Start   : in     Positive)
        return Combinator_Result is
    begin
        if Start > Input'Last then
            return Empty (Input, Context, Start);
        else
            return Salt : Combinator_Result;
        end if;
    end End_Of_Input;


end Packrat.Parsers;