summaryrefslogtreecommitdiff
path: root/src/packrat-parse_graphs.adb
blob: 28a03c993776bf1fbe29ff50e8c86fcffe82a164 (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


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


with

    Ada.Strings.Fixed,
    Ada.Characters.Latin_1;


package body Packrat.Parse_Graphs is


    package SU renames Ada.Strings.Unbounded;
    package Latin renames Ada.Characters.Latin_1;




    function "="
           (Left, Right : in Parse_Graph)
        return Boolean
    is
        use type Base.Graph;
        use type Finished_Token_Vectors.Vector;
        use type Node_Label_Maps.Map;
    begin
        return Left.Internal_Graph = Right.Internal_Graph and
            Left.Root_Elems = Right.Root_Elems and
            Left.Label_Map = Right.Label_Map;
    end "=";


    function "<"
           (Left, Right : in Finished_Token_Vectors.Vector)
        return Boolean
    is
        use type Ada.Containers.Count_Type;
        use type Traits.Tokens.Finished_Token_Type;
        Left_Index : Positive := Left.First_Index;
        Right_Index : Positive := Right.First_Index;
    begin
        while Left_Index <= Left.Last_Index and Right_Index <= Right.Last_Index loop
            if Left.Element (Left_Index) < Right.Element (Right_Index) then
                return True;
            elsif Left.Element (Left_Index) /= Right.Element (Right_Index) then
                return False;
            end if;
            Left_Index := Left_Index + 1;
            Right_Index := Right_Index + 1;
        end loop;
        return Left.Length < Right.Length;
    end "<";


    function "<"
           (Left, Right : in Token_Group)
        return Boolean
    is
        use type Finished_Token_Vectors.Vector;
    begin
        if Traits.Tokens.Start (Left.Parent.Token) = Traits.Tokens.Start (Right.Parent.Token) then
            if Finish (Left) = Finish (Right) then
                return Left.Elems < Right.Elems;
            else
                return Finish (Left) < Finish (Right);
            end if;
        else
            return Traits.Tokens.Start (Left.Parent.Token) <
                Traits.Tokens.Start (Right.Parent.Token);
        end if;
    end "<";





    procedure Assign
           (Target : in out Parse_Graph;
            Source : in     Parse_Graph) is
    begin
        Target.Internal_Graph.Assign (Source.Internal_Graph);
        Target.Root_Elems.Assign (Source.Root_Elems);
        Target.Label_Map.Assign (Source.Label_Map);
    end Assign;


    function Copy
           (Source : in Parse_Graph)
        return Parse_Graph is
    begin
        return G : Parse_Graph :=
           (Internal_Graph => Source.Internal_Graph.Copy,
            Root_Elems     => Source.Root_Elems.Copy,
            Label_Map      => Source.Label_Map.Copy);
    end Copy;


    procedure Move
           (Target, Source : in out Parse_Graph) is
    begin
        Target.Internal_Graph.Move (Source.Internal_Graph);
        Target.Root_Elems.Move (Source.Root_Elems);
        Target.Label_Map.Move (Source.Label_Map);
    end Move;





    function Is_Empty
           (Container : in Parse_Graph)
        return Boolean is
    begin
        return Container.Label_Map.Is_Empty;
    end Is_Empty;


    procedure Clear
           (Container : in out Parse_Graph) is
    begin
        Container.Internal_Graph.Clear;
        Container.Root_Elems.Clear;
        Container.Label_Map.Clear;
    end Clear;





    function In_Finishes
           (Container : in Parse_Graph;
            Node      : in Node_ID_Type)
        return Finish_Vectors.Vector
    is
        Result : Finish_Vectors.Vector;
        Current : Traits.Tokens.Finish_Type;
        Node_Label : Traits.Tokens.Token_Type;
    begin
        Node_Label := Container.Internal_Graph.Label (Node);
        for Fin_Token of Container.Root_Elems loop
            if Fin_Token.Token = Node_Label and then
                not Result.Contains (Fin_Token.Finish)
            then
                Result.Append (Fin_Token.Finish);
            end if;
        end loop;
        for Edge of Container.Internal_Graph.Inbound (Node) loop
            Current := Container.Internal_Graph.Label (Edge).Subnode_Finish;
            if not Result.Contains (Current) then
                Result.Append (Current);
            end if;
        end loop;
        return Result;
    end In_Finishes;





    function Debug_String
           (Container : in Parse_Graph)
        return String
    is
        function Generate_Map
               (Container : in Parse_Graph)
            return Enum_Node_Maps.Map
        is
            Result : Enum_Node_Maps.Map;
            Current : Traits.Label_Enum;
        begin
            for Node of Container.Internal_Graph.Nodes loop
                Current := Traits.Tokens.Label (Container.Internal_Graph.Label (Node));
                if not Result.Contains (Current) then
                    Result.Insert (Current, Node_Vectors.Empty_Vector);
                end if;
                Result.Reference (Current).Append (Node);
            end loop;
            return Result;
        end Generate_Map;

        function Image
               (Input : in Integer)
            return String is
        begin
            return Ada.Strings.Fixed.Trim (Integer'Image (Input), Ada.Strings.Left);
        end Image;

        Mapping  : Enum_Node_Maps.Map := Generate_Map (Container);
        Current  : Traits.Tokens.Token_Type;
        Result   : SU.Unbounded_String;
    begin
        if Container.Is_Empty then
            return "";
        end if;
        for Iter in Mapping.Iterate loop
            SU.Append (Result, Traits.Label_Enum'Image (Enum_Node_Maps.Key (Iter)) & Latin.LF);
            for Node of Enum_Node_Maps.Element (Iter) loop
                Current := Container.Internal_Graph.Label (Node);
                SU.Append (Result, " " & Image (Traits.Tokens.Start (Current)) & " ->" & Latin.HT);
                for Fin of In_Finishes (Container, Node) loop
                    SU.Append (Result, Image (Fin) & " ->" & Latin.HT);
                    declare
                        A_Fin_Token : Traits.Tokens.Finished_Token_Type := (Current, Fin);
                        Groupings : Token_Group_Array := Container.Subgroups (A_Fin_Token);
                    begin
                        if Groupings'Length = 0 then
                            SU.Append (Result, "Leaf" & Latin.LF);
                            SU.Append (Result, SU."*" (2, Latin.HT));
                        else
                            for Grouping of Groupings loop
                                for Fin_Token of Elements (Grouping) loop
                                    SU.Append (Result, "Sub " &
                                        Traits.Label_Enum'Image
                                            (Traits.Tokens.Label (Fin_Token.Token)) &
                                        " (" & Image (Traits.Tokens.Start (Fin_Token.Token)) &
                                        "," & Image (Fin_Token.Finish) & "), ");
                                end loop;
                                SU.Delete (Result, SU.Length (Result) - 1, SU.Length (Result));
                                SU.Append (Result, Latin.LF);
                                SU.Append (Result, SU."*" (2, Latin.HT));
                            end loop;
                        end if;
                    end;
                    SU.Delete (Result, SU.Length (Result), SU.Length (Result));
                end loop;
                SU.Delete (Result, SU.Length (Result), SU.Length (Result));
            end loop;
            SU.Delete (Result, SU.Length (Result), SU.Length (Result));
            SU.Append (Result, Latin.LF & Latin.LF);
        end loop;
        if SU.Length (Result) > 1 then
            SU.Delete (Result, SU.Length (Result) - 1, SU.Length (Result));
        end if;
        SU.Append (Result, Latin.LF);
        return SU.To_String (Result);
    end Debug_String;





    function Contains
           (Container : in Parse_Graph;
            Token     : in Traits.Tokens.Token_Type)
        return Boolean is
    begin
        return Container.Label_Map.Contains (Token);
    end Contains;


    function Contains
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Boolean
    is
        use type Traits.Tokens.Finished_Token_Type;
        Node : Node_ID_Type;
    begin
        if not Container.Contains (Position.Token) then
            return False;
        end if;
        for F of Container.Root_Elements loop
            if F = Position then
                return True;
            end if;
        end loop;
        Node := Container.Label_Map.Element (Position.Token);
        return (for some Edge of Container.Internal_Graph.Inbound (Node) =>
            Container.Internal_Graph.Label (Edge).Subnode_Finish = Position.Finish);
    end Contains;


    function Contains
           (Container : in Parse_Graph;
            Grouping  : in Token_Group)
        return Boolean is
    begin
        return Container.Contains (Grouping.Parent, Elements (Grouping));
    end Contains;


    function Contains
           (Container : in Parse_Graph;
            Parent    : in Traits.Tokens.Finished_Token_Type;
            Subtokens : in Traits.Tokens.Finished_Token_Array)
        return Boolean
    is
        Groups  : Group_ID_Vectors.Vector;
        Next_ID : Group_ID_Type;
        Parent_Node : Node_ID_Type;
    begin
        if not Container.Contains (Parent.Token) then
            return False;
        end if;
        for Fin_Token of Subtokens loop
            if not Container.Contains (Fin_Token.Token) then
                return False;
            end if;
        end loop;
        if not Valid_Starts_Finishes (Parent, Subtokens) then
            return False;
        end if;
        Parent_Node := Container.Label_Map.Element (Parent.Token);
        for Edge of Container.Internal_Graph.Outbound (Parent_Node) loop
            Next_ID := Container.Internal_Graph.Label (Edge).Group_ID;
            if not Groups.Contains (Next_ID) then
                Groups.Append (Next_ID);
            end if;
        end loop;
        return (for some ID of Groups =>
            (for all Sub of Subtokens =>
                (for some Edge of Container.Internal_Graph.Between
                  (Parent_Node, Container.Label_Map.Element (Sub.Token)) =>
                    Container.Internal_Graph.Label (Edge) =
                       (Group_ID => ID,
                        Group_Finish => Parent.Finish,
                        Subnode_Finish => Sub.Finish))));
    end Contains;


    function Reachable
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Boolean
    is
        --  This is basically a depth first search function.
        function Finder
               (Current : in Traits.Tokens.Finished_Token_Type)
            return Boolean
        is
            use type Traits.Tokens.Finished_Token_Type;
        begin
            return Current = Position or else
                (for some Grouping of Container.Subgroups (Current) =>
                    (for some Fin_Token of Elements (Grouping) => Finder (Fin_Token)));
        end Finder;
    begin
        return (for some Fin_Token of Container.Root_Elements => Finder (Fin_Token));
    end Reachable;


    function Locally_Reachable
           (Container : in Parse_Graph;
            Node      : in Node_ID_Type)
        return Boolean
    is
        use type Ada.Containers.Count_Type;
    begin
        for Fin_Token of Container.Root_Elems loop
            if Container.Label_Map.Element (Fin_Token.Token) = Node then
                return True;
            end if;
        end loop;
        if Container.Internal_Graph.Indegree (Node) > 0 then
            return True;
        end if;
        return False;
    end Locally_Reachable;


    function Unreachable_Outbound
           (Container : in Parse_Graph;
            Node      : in Node_ID_Type)
        return Base.Edge_Array
    is
        Outedges : Base.Edge_Array := Container.Internal_Graph.Outbound (Node);
        Unreachout : Base.Edge_Array (1 .. Outedges'Length);
        In_Fins : Finish_Vectors.Vector;
        Position : Positive := 1;
    begin
        if Outedges'Length = 0 then
            return Unreachout;
        end if;
        In_Fins := In_Finishes (Container, Node);
        for Edge of Outedges loop
            if not In_Fins.Contains (Container.Internal_Graph.Label (Edge).Group_Finish) then
                Unreachout (Position) := Edge;
                Position := Position + 1;
            end if;
        end loop;
        return Unreachout (1 .. Position - 1);
    end Unreachable_Outbound;


    function All_Reachable
           (Container : in Parse_Graph)
        return Boolean is
    begin
        return (for all Node of Container.Internal_Graph.Nodes =>
            Locally_Reachable (Container, Node) and
            Unreachable_Outbound (Container, Node)'Length = 0);
    end All_Reachable;


    function Valid_Token
           (Fin_Token : in Traits.Tokens.Finished_Token_Type)
        return Boolean is
    begin
        return Fin_Token.Finish + 1 >= Traits.Tokens.Start (Fin_Token.Token);
    end Valid_Token;


    function Valid_Starts_Finishes
           (Parent    : in Traits.Tokens.Finished_Token_Type;
            Subtokens : in Traits.Tokens.Finished_Token_Array)
        return Boolean
    is
        Subvec : Finished_Token_Vectors.Vector;
    begin
        for Sub of Subtokens loop
            if Traits.Tokens.Start (Sub.Token) > Sub.Finish + 1 then
                return False;
            end if;
            Subvec.Append (Sub);
        end loop;
        Finished_Token_Sort.Sort (Subvec);
        for Index in Subvec.First_Index .. Subvec.Last_Index - 1 loop
            if Subvec (Index).Finish >= Traits.Tokens.Start (Subvec (Index + 1).Token) then
                return False;
            end if;
        end loop;
        if Parent.Finish < Subvec.Last_Element.Finish or else
            Traits.Tokens.Start (Parent.Token) > Traits.Tokens.Start (Subvec.First_Element.Token)
        then
            return False;
        end if;
        return True;
    end Valid_Starts_Finishes;


    function Loops_Introduced
           (Container : in Parse_Graph;
            Parent    : in Traits.Tokens.Finished_Token_Type;
            Subtokens : in Traits.Tokens.Finished_Token_Array)
        return Boolean
    is
        function Looper
               (Current : in Traits.Tokens.Finished_Token_Type)
            return Boolean
        is
            use type Traits.Tokens.Finished_Token_Type;
        begin
            if not Container.Contains (Current.Token) then
                return False;
            elsif Current = Parent then
                return True;
            elsif Traits.Tokens.Start (Current.Token) > Traits.Tokens.Start (Parent.Token) then
                return False;
            else
                return (for some Grouping of Container.Subgroups (Current) =>
                    (for some Sub of Elements (Grouping) => Looper (Sub)));
            end if;
        end Looper;
    begin
        return Container.Contains (Parent.Token) and then
            (for some Sub of Subtokens => Looper (Sub));
    end Loops_Introduced;


    function Is_Sorted
           (Finishes : in Traits.Tokens.Finish_Array)
        return Boolean
    is
        function Actual is new Sorted
            (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array);
    begin
        return Actual (Finishes);
    end Is_Sorted;


    function Is_Sorted
           (Positions : in Traits.Tokens.Finished_Token_Array)
        return Boolean
    is
        function Actual is new Sorted
           (Traits.Tokens.Finished_Token_Type,
            Traits.Tokens.Finished_Token_Array,
            Traits.Tokens."<");
    begin
        return Actual (Positions);
    end Is_Sorted;


    function Is_Sorted
           (Groupings : in Token_Group_Array)
        return Boolean
    is
        function Actual is new Sorted (Token_Group, Token_Group_Array);
    begin
        return Actual (Groupings);
    end Is_Sorted;


    function No_Duplicates
           (Finishes : in Traits.Tokens.Finish_Array)
        return Boolean
    is
        function Actual is new No_Dups
            (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array);
    begin
        return Actual (Finishes);
    end No_Duplicates;


    function No_Duplicates
           (Positions : in Traits.Tokens.Finished_Token_Array)
        return Boolean
    is
        function Actual is new No_Dups
           (Traits.Tokens.Finished_Token_Type,
            Traits.Tokens.Finished_Token_Array,
            Traits.Tokens."=");
    begin
        return Actual (Positions);
    end No_Duplicates;


    function No_Duplicates
           (Groupings : in Token_Group_Array)
        return Boolean
    is
        function Actual is new No_Dups (Token_Group, Token_Group_Array);
    begin
        return Actual (Groupings);
    end No_Duplicates;





    procedure Include
           (Container : in out Parse_Graph;
            Token     : in     Traits.Tokens.Token_Type)
    is
        Node_ID : Node_ID_Type;
    begin
        if not Container.Contains (Token) then
            Node_ID := Container.Internal_Graph.Unused;
            Container.Internal_Graph.Insert (Base.Labeled_Node_Type'(Node_ID, Token));
            Container.Label_Map.Insert (Token, Node_ID);
        end if;
    end Include;


    procedure Connect
           (Container : in out Parse_Graph;
            Parent    : in     Traits.Tokens.Finished_Token_Type;
            Subtokens : in     Traits.Tokens.Finished_Token_Array)
    is
        Parent_ID : Node_ID_Type;
        Use_GID   : Group_ID_Type;
        New_Edge  : Base.Edge_Type;
        New_Label : Edge_Label_Type;
    begin
        if Container.Contains (Parent, Subtokens) then
            return;
        end if;
        Container.Include (Parent.Token);
        for Sub of Subtokens loop
            Container.Include (Sub.Token);
        end loop;
        Parent_ID := Container.Label_Map.Element (Parent.Token);
        declare
            Outedges : Base.Edge_Array := Container.Internal_Graph.Outbound (Parent_ID);
        begin
            Use_GID := Group_ID_Type'First;
            while (for some E of Outedges =>
                Container.Internal_Graph.Label (E).Group_ID = Use_GID)
            loop
                Use_GID := Use_GID + 1;
            end loop;
        end;
        for Sub of Subtokens loop
            New_Edge :=
               (ID   => Container.Internal_Graph.Unused,
                From => Parent_ID,
                To   => Container.Label_Map.Element (Sub.Token));
            New_Label := (Use_GID, Parent.Finish, Sub.Finish);
            Container.Internal_Graph.Insert (Base.Labeled_Edge_Type'(New_Edge, New_Label));
        end loop;
    end Connect;


    procedure Prune
           (Container : in out Parse_Graph;
            Token     : in     Traits.Tokens.Token_Type) is
    begin
        if not Container.Contains (Token) then
            return;
        end if;
        for I in reverse 1 .. Container.Root_Elems.Last_Index loop
            if Container.Root_Elems.Element (I).Token = Token then
                Container.Root_Elems.Delete (I);
            end if;
        end loop;
        Container.Internal_Graph.Delete (Container.Label_Map (Token));
        Container.Label_Map.Delete (Token);
    end Prune;


    procedure Prune
           (Container : in out Parse_Graph;
            Position  : in     Traits.Tokens.Finished_Token_Type)
    is
        use type Traits.Tokens.Finished_Token_Type;
        Node : Node_ID_Type;
    begin
        if not Container.Contains (Position.Token) then
            return;
        end if;
        for I in reverse 1 .. Container.Root_Elems.Last_Index loop
            if Container.Root_Elems.Element (I) = Position then
                Container.Root_Elems.Delete (I);
            end if;
        end loop;
        Node := Container.Label_Map.Element (Position.Token);
        for Edge of Container.Internal_Graph.Inbound (Node) loop
            if Container.Internal_Graph.Label (Edge).Subnode_Finish = Position.Finish then
                Container.Internal_Graph.Delete (Edge);
            end if;
        end loop;
    end Prune;


    procedure Prune
           (Container : in out Parse_Graph;
            Grouping  : in     Token_Group)
    is
        Group_IDs   : Group_ID_Vectors.Vector;
        Parent_Node : Node_ID_Type;
        Current_ID  : Group_ID_Type;
    begin
        --  Short circuit checks
        if not Container.Contains (Grouping.Parent) then
            return;
        end if;
        for Fin_Token of Elements (Grouping) loop
            if not Container.Contains (Fin_Token) then
                return;
            end if;
        end loop;

        Parent_Node := Container.To_Node (Grouping.Parent);

        --  Gather up the IDs of groups for deletion
        for Edge of Container.Internal_Graph.Between
            (Parent_Node, Container.To_Node (Element (Grouping, 1)))
        loop
            Current_ID := Container.Internal_Graph.Label (Edge).Group_ID;
            if not Group_IDs.Contains (Current_ID) and then
                (for all Index in 2 .. Last_Index (Grouping) =>
                    (for some Other of Container.Internal_Graph.Between
                        (Parent_Node, Container.To_Node (Element (Grouping, Index))) =>
                        Container.Internal_Graph.Label (Other).Group_ID = Current_ID))
            then
                Group_IDs.Append (Current_ID);
            end if;
        end loop;

        --  Delete all relevant edges
        for Fin_Token of Elements (Grouping) loop
            for Edge of Container.Internal_Graph.Between
                (Parent_Node, Container.To_Node (Fin_Token))
            loop
                if Group_IDs.Contains (Container.Internal_Graph.Label (Edge).Group_ID) then
                    Container.Internal_Graph.Delete (Edge);
                end if;
            end loop;
        end loop;
    end Prune;


    procedure Delete_Unreachable
           (Container : in out Parse_Graph)
    is
        Examine, Next : Node_Vectors.Vector;
    begin
        if not Container.Has_Root then
            Container.Clear;
            return;
        end if;
        for Node of Container.Internal_Graph.Nodes loop
            Examine.Append (Node);
        end loop;
        while not Examine.Is_Empty loop
            for Node of Examine loop
                if Container.Internal_Graph.Contains (Node) then
                    if not Locally_Reachable (Container, Node) then
                        for Outnode of Container.Internal_Graph.Children (Node) loop
                            Next.Append (Outnode);
                        end loop;
                        Container.Internal_Graph.Delete (Node);
                    else
                        for Edge of Unreachable_Outbound (Container, Node) loop
                            Next.Append (Edge.To);
                            Container.Internal_Graph.Delete (Edge);
                        end loop;
                    end if;
                end if;
            end loop;
            Examine.Move (Next);
        end loop;
    end Delete_Unreachable;





    function Has_Root
           (Container : in Parse_Graph)
        return Boolean is
    begin
        return not Container.Root_Elems.Is_Empty;
    end Has_Root;


    procedure Set_Root
           (Container : in out Parse_Graph;
            Tokens    : in     Traits.Tokens.Finished_Token_Array) is
    begin
        Container.Clear_Root;
        for Fin_Token of Tokens loop
            if not Container.Root_Elems.Contains (Fin_Token) then
                Container.Root_Elems.Append (Fin_Token);
            end if;
        end loop;
        Finished_Token_Sort.Sort (Container.Root_Elems);
    end Set_Root;


    procedure Clear_Root
           (Container : in out Parse_Graph) is
    begin
        Container.Root_Elems.Clear;
    end Clear_Root;


    function Root_Elements
           (Container : in Parse_Graph)
        return Traits.Tokens.Finished_Token_Array
    is
        function V2A is new Vector_To_Array
           (Traits.Tokens.Finished_Token_Type,
            Traits.Tokens.Finished_Token_Array,
            Finished_Token_Vectors);
    begin
        return V2A (Container.Root_Elems);
    end Root_Elements;





    function Finish_List
           (Container : in Parse_Graph;
            Token     : in Traits.Tokens.Token_Type)
        return Traits.Tokens.Finish_Array
    is
        function V2A is new Vector_To_Array
            (Traits.Tokens.Finish_Type, Traits.Tokens.Finish_Array, Finish_Vectors);
        Result : Finish_Vectors.Vector;
    begin
        for Fin_Token of Container.Root_Elems loop
            if Fin_Token.Token = Token and not Result.Contains (Fin_Token.Finish) then
                Result.Append (Fin_Token.Finish);
            end if;
        end loop;
        for Edge of Container.Internal_Graph.Outbound (Container.Label_Map.Element (Token)) loop
            if not Result.Contains (Container.Internal_Graph.Label (Edge).Group_Finish) then
                Result.Append (Container.Internal_Graph.Label (Edge).Group_Finish);
            end if;
        end loop;
        Finish_Sort.Sort (Result);
        return V2A (Result);
    end Finish_List;


    function Is_Leaf
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Boolean is
    begin
        for Edge of Container.Internal_Graph.Outbound
            (Container.Label_Map.Element (Position.Token))
        loop
            if Container.Internal_Graph.Label (Edge).Group_Finish = Position.Finish then
                return False;
            end if;
        end loop;
        return True;
    end Is_Leaf;


    function Is_Branch
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Boolean is
    begin
        return not Container.Is_Leaf (Position);
    end Is_Branch;





    function Subgroups
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Token_Group_Array
    is
        function V2A is new Vector_To_Array
            (Token_Group, Token_Group_Array, Token_Group_Vectors);
        Groupings  : Group_Finished_Token_Maps.Map;
        Edge_Label : Edge_Label_Type;
        Next_Token : Traits.Tokens.Finished_Token_Type;
        Result     : Token_Group_Vectors.Vector;
    begin
        for Edge of Container.Internal_Graph.Outbound
            (Container.Label_Map.Element (Position.Token))
        loop
            Edge_Label := Container.Internal_Graph.Label (Edge);
            if Edge_Label.Group_Finish = Position.Finish then
                Next_Token :=
                   (Token  => Container.Internal_Graph.Label (Edge.To),
                    Finish => Edge_Label.Subnode_Finish);
                if not Groupings.Contains (Edge_Label.Group_ID) then
                    Groupings.Insert (Edge_Label.Group_ID, Finished_Token_Vectors.Empty_Vector);
                end if;
                Groupings.Reference (Edge_Label.Group_ID).Append (Next_Token);
            end if;
        end loop;
        for Raw_Group of Groupings loop
            Finished_Token_Sort.Sort (Raw_Group);
            Result.Append
              ((Parent => Position,
                Elems  => Raw_Group));
        end loop;
        Token_Group_Sort.Sort (Result);
        return V2A (Result);
    end Subgroups;


    function First_Index
           (Grouping : in Token_Group)
        return Positive is
    begin
        return Grouping.Elems.First_Index;
    end First_Index;


    function Last_Index
           (Grouping : in Token_Group)
        return Positive is
    begin
        return Grouping.Elems.Last_Index;
    end Last_Index;


    function Length
           (Grouping : in Token_Group)
        return Ada.Containers.Count_Type is
    begin
        return Grouping.Elems.Length;
    end Length;


    function Element
           (Grouping : in Token_Group;
            Index    : in Positive)
        return Traits.Tokens.Finished_Token_Type is
    begin
        return Grouping.Elems.Element (Index);
    end Element;


    function Elements
           (Grouping : in Token_Group)
        return Traits.Tokens.Finished_Token_Array
    is
        function V2A is new Vector_To_Array
           (Traits.Tokens.Finished_Token_Type,
            Traits.Tokens.Finished_Token_Array,
            Finished_Token_Vectors);
    begin
        return V2A (Grouping.Elems);
    end Elements;


    function Parent
           (Grouping : in Token_Group)
        return Traits.Tokens.Finished_Token_Type is
    begin
        return Grouping.Parent;
    end Parent;


    function Finish
           (Grouping : in Token_Group)
        return Traits.Tokens.Finish_Type is
    begin
        return Grouping.Parent.Finish;
    end Finish;





    function Is_Root_Ambiguous
           (Container : in Parse_Graph)
        return Boolean
    is
        use type Ada.Containers.Count_Type;
    begin
        return Container.Root_Elems.Length /= 1;
    end Is_Root_Ambiguous;


    function Is_Ambiguous
           (Container : in Parse_Graph)
        return Boolean
    is
        Seen_Finishes : Finish_Group_Maps.Map;
        Edge_Label : Edge_Label_Type;
    begin
        if Container.Has_Root and then Is_Root_Ambiguous (Container) then
            return True;
        end if;
        for Node of Container.Internal_Graph.Nodes loop
            for Edge of Container.Internal_Graph.Outbound (Node) loop
                Edge_Label := Container.Internal_Graph.Label (Edge);
                if Seen_Finishes.Contains (Edge_Label.Group_Finish) then
                    if Seen_Finishes.Element (Edge_Label.Group_Finish) /= Edge_Label.Group_ID then
                        return True;
                    end if;
                else
                    Seen_Finishes.Insert (Edge_Label.Group_Finish, Edge_Label.Group_ID);
                end if;
            end loop;
            Seen_Finishes.Clear;
        end loop;
        return False;
    end Is_Ambiguous;


    function Ambiguities
           (Container      : in     Parse_Graph;
            Ambiguous_Root :    out Boolean)
        return Traits.Tokens.Finished_Token_Array
    is
        function V2A is new Vector_To_Array
           (Traits.Tokens.Finished_Token_Type,
            Traits.Tokens.Finished_Token_Array,
            Finished_Token_Vectors);
        Seen_Finishes : Finish_Group_Maps.Map;
        Edge_Label : Edge_Label_Type;
        Next_Token : Traits.Tokens.Finished_Token_Type;
        Result : Finished_Token_Vectors.Vector;
    begin
        Ambiguous_Root := Container.Has_Root and then Container.Is_Root_Ambiguous;
        for Node of Container.Internal_Graph.Nodes loop
            for Edge of Container.Internal_Graph.Outbound (Node) loop
                Edge_Label := Container.Internal_Graph.Label (Edge);
                if Seen_Finishes.Contains (Edge_Label.Group_Finish) then
                    if Seen_Finishes.Element (Edge_Label.Group_Finish) /= Edge_Label.Group_ID then
                        Next_Token :=
                           (Token  => Container.Internal_Graph.Label (Node),
                            Finish => Edge_Label.Group_Finish);
                        if not Result.Contains (Next_Token) then
                            Result.Append (Next_Token);
                        end if;
                    end if;
                else
                    Seen_Finishes.Insert (Edge_Label.Group_Finish, Edge_Label.Group_ID);
                end if;
            end loop;
            Seen_Finishes.Clear;
        end loop;
        Finished_Token_Sort.Sort (Result);
        return V2A (Result);
    end Ambiguities;





    --  there is an isomorphism between two finished_tokens in a graph iff:
    --    - the starting and finishing points of the tokens slid along a consistent offset are equal
    --    - the enum_labels and values of the tokens are equal
    --    - there is some isomorphic match between the token_groups of each finished_token (n^2)

    --  there is an isomorphism between two token_groups iff:
    --    - the length of both groups is the same
    --    - each pair of successive finished_tokens, in order, is isomorphic

    --  should be possible to keep track of finished_tokens that are considered to be isomorphic
    --  with a finished_token -> finished_token map, and if a finished_token is encountered that is
    --  already in the map then if it matches up short circuit true

    function Group_Isomorph
           (Left_Graph        : in     Parse_Graph;
            Left_Token_Group  : in     Token_Group;
            Right_Graph       : in     Parse_Graph;
            Right_Token_Group : in     Token_Group;
            Offset            : in     Integer;
            Mapping           : in out Isomorph_Maps.Map)
        return Boolean
    is
        use type Ada.Containers.Count_Type;
    begin
        if Length (Left_Token_Group) /= Length (Right_Token_Group) then
            return False;
        end if;
        for Index in First_Index (Left_Token_Group) .. Last_Index (Left_Token_Group) loop
            if not Token_Isomorph
               (Left_Graph, Element (Left_Token_Group, Index),
                Right_Graph, Element (Right_Token_Group, Index),
                Offset, Mapping)
            then
                return False;
            end if;
        end loop;
        return True;
    end Group_Isomorph;


    function Token_Isomorph
           (Left_Graph     : in     Parse_Graph;
            Left_Position  : in     Traits.Tokens.Finished_Token_Type;
            Right_Graph    : in     Parse_Graph;
            Right_Position : in     Traits.Tokens.Finished_Token_Type;
            Offset         : in     Integer;
            Mapping        : in out Isomorph_Maps.Map)
        return Boolean
    is
        Left_Groups  : Token_Group_Array := Left_Graph.Subgroups (Left_Position);
        Right_Groups : Token_Group_Array := Right_Graph.Subgroups (Right_Position);
    begin
        if Mapping.Contains (Left_Position) and then
            Mapping.Constant_Reference (Left_Position).Contains (Right_Position)
        then
            return True;
        end if;
        if Traits.Tokens.Start (Left_Position.Token) + Offset /=
            Traits.Tokens.Start (Right_Position.Token) or else
            Left_Position.Finish + Offset /= Right_Position.Finish
        then
            return False;
        end if;
        if Traits.Tokens.Label (Left_Position.Token) /=
            Traits.Tokens.Label (Right_Position.Token) or else
            Traits.Tokens.Value (Left_Position.Token) /=
            Traits.Tokens.Value (Right_Position.Token)
        then
            return False;
        end if;
        declare
            Left_Groups  : Token_Group_Array := Left_Graph.Subgroups (Left_Position);
            Right_Groups : Token_Group_Array := Right_Graph.Subgroups (Right_Position);
        begin
            if Left_Groups'Length /= Right_Groups'Length then
                return False;
            end if;
            --  This loop only works because of the Subgroups already being sorted
            --  and any isomorphism only differing in the starts/finishes by a constant.
            for Index in Left_Groups'Range loop
                if not Group_Isomorph
                   (Left_Graph, Left_Groups (Index),
                    Right_Graph, Right_Groups (Index),
                    Offset, Mapping)
                then
                    return False;
                end if;
            end loop;
        end;
        if not Mapping.Contains (Left_Position) then
            Mapping.Insert (Left_Position, Finished_Token_Vectors.Empty_Vector);
        end if;
        Mapping.Reference (Left_Position).Append (Right_Position);
        return True;
    end Token_Isomorph;


    function Isomorphic
           (Left, Right : in Parse_Graph)
        return Boolean
    is
        use type Ada.Containers.Count_Type;
        Offset : Integer :=
            Traits.Tokens.Start (Right.Root_Elems.Element (1).Token) -
            Traits.Tokens.Start (Left.Root_Elems.Element (1).Token);
        Mapping : Isomorph_Maps.Map;
    begin
        if Left.Root_Elems.Length /= Right.Root_Elems.Length then
            return False;
        end if;
        return (for all Index in 1 .. Left.Root_Elems.Last_Index =>
            Token_Isomorph
               (Left, Left.Root_Elems (Index),
                Right, Right.Root_Elems (Index),
                Offset, Mapping));
    end Isomorphic;


    function Isomorphic_Subgraph
           (Left_Graph     : in Parse_Graph;
            Left_Position  : in Traits.Tokens.Finished_Token_Type;
            Right_Graph    : in Parse_Graph;
            Right_Position : in Traits.Tokens.Finished_Token_Type)
        return Boolean
    is
        Offset : Integer :=
            Traits.Tokens.Start (Right_Position.Token) - Traits.Tokens.Start (Left_Position.Token);
        Mapping : Isomorph_Maps.Map;
    begin
        return Token_Isomorph
           (Left_Graph, Left_Position,
            Right_Graph, Right_Position,
            Offset, Mapping);
    end Isomorphic_Subgraph;





    function To_Node
           (Container : in Parse_Graph;
            Token     : in Traits.Tokens.Token_Type)
        return Node_ID_Type is
    begin
        return Container.Label_Map.Element (Token);
    end To_Node;


    function To_Node
           (Container : in Parse_Graph;
            Position  : in Traits.Tokens.Finished_Token_Type)
        return Node_ID_Type is
    begin
        return Container.Label_Map.Element (Position.Token);
    end To_Node;





    function Sorted
           (Input : in Array_Type)
        return Boolean is
    begin
        for Index in Input'First .. Input'Last - 1 loop
            if Input (Index + 1) < Input (Index) then
                return False;
            end if;
        end loop;
        return True;
    end Sorted;


    function No_Dups
           (Input : in Array_Type)
        return Boolean is
    begin
        for X in Input'First .. Input'Last - 1 loop
            for Y in X + 1 .. Input'Last loop
                if Input (X) = Input (Y) then
                    return False;
                end if;
            end loop;
        end loop;
        return True;
    end No_Dups;


    function Vector_To_Array
           (Input : in Type_Vectors.Vector)
        return Array_Type is
    begin
        return Result : Array_Type (1 .. Input.Last_Index) do
            for I in Result'Range loop
                Result (I) := Input (I);
            end loop;
        end return;
    end Vector_To_Array;


end Packrat.Parse_Graphs;