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


package body Packrat.Parse_Graphs is


    function "<"
           (Left, Right : in Edge_Label_Type)
        return Boolean is
    begin
        if Left.Finish = Right.Finish then
            return Left.Order < Right.Order;
        else
            return Left.Finish < Right.Finish;
        end if;
    end "<";





    function "="
           (Left, Right : in Parse_Graph)
        return Boolean
    is
        use type Base.Graph;
    begin
        return Base.Graph (Left) = Base.Graph (Right) and
            Left.Root_Node = Right.Root_Node;
    end "=";





    function To_Graph
           (Nodes : in Node_Array;
            Edges : in Edge_Array)
        return Parse_Graph is
    begin
        return G : Parse_Graph :=
            (Base.To_Graph (Nodes, Edges) with Root_Node => No_Node);
    end To_Graph;


    function To_Graph
           (Nodes : in Node_Array;
            Edges : in Edge_Array;
            Root  : in Extended_Node_ID_Type)
        return Parse_Graph
    is
        Valid : Boolean := False;
    begin
        if Root /= No_Node then
            for N of Nodes loop
                if Root = N then
                    Valid := True;
                    exit;
                end if;
            end loop;
            if not Valid then
                raise Constraint_Error with "Root node not in graph";
            end if;
        end if;
        return G : Parse_Graph :=
            (Base.To_Graph (Nodes, Edges) with Root_Node => Root);
    end To_Graph;





    procedure Assign
           (Target : in out Parse_Graph;
            Source : in     Parse_Graph) is
    begin
        Base.Assign (Base.Graph (Target), Base.Graph (Source));
        Target.Root_Node := Source.Root_Node;
    end Assign;


    function Copy
           (Source : in Parse_Graph)
        return Parse_Graph is
    begin
        return G : Parse_Graph :=
            (Base.Copy (Base.Graph (Source)) with Root_Node => Source.Root_Node);
    end Copy;


    procedure Move
           (Target, Source : in out Parse_Graph) is
    begin
        Base.Move (Base.Graph (Target), Base.Graph (Source));
        Target.Root_Node := Source.Root_Node;
        Source.Root_Node := No_Node;
    end Move;





    function Root
           (Container : in Parse_Graph)
        return Cursor is
    begin
        if not Container.Contains (Container.Root_Node) then
            return No_Element;
        else
            return Container.To_Cursor (Container.Root_Node);
        end if;
    end Root;


    procedure Set_Root
           (Container : in out Parse_Graph;
            Node      : in     Extended_Node_ID_Type) is
    begin
        Container.Root_Node := Node;
    end Set_Root;





    function Finish_List
           (Container : in Parse_Graph;
            Node      : in Node_ID_Type)
        return Finish_Array is
    begin
        return Finish_List (Container.To_Cursor (Node));
    end Finish_List;


    function Finish_List
           (Position : in Cursor)
        return Finish_Array
    is
        function V2A is new Vector_To_Array (Finish_Type, Finish_Array, Finish_Vectors);
        Fins : Finish_Vectors.Vector;
        Current : Edge_Label_Type;
    begin
        for E of Outbound (Position) loop
            if Has_Label (Position, E) then
                Current := Label (Position, E);
                if not Fins.Contains (Current.Finish) then
                    Fins.Append (Current.Finish);
                end if;
            end if;
        end loop;
        Finsort.Sort (Fins);
        return V2A (Fins);
    end Finish_List;


    function Sub_Nodes
           (Container : in Parse_Graph;
            Node      : in Node_ID_Type;
            Finish_At : in Finish_Type)
        return Node_Array is
    begin
        return Sub_Nodes (Container.To_Cursor (Node), Finish_At);
    end Sub_Nodes;


    function Sub_Nodes
           (Position  : in Cursor;
            Finish_At : in Finish_Type)
        return Node_Array
    is
        function V2A is new Vector_To_Array (Node_ID_Type, Node_Array, Node_Vectors);
        Nodes : Node_Vectors.Vector;
        Current_Label : Edge_Label_Type;
    begin
        for E of Outbound (Position) loop
            if Has_Label (Position, E) then
                Current_Label := Label (Position, E);
                if Current_Label.Finish = Finish_At then
                    Nodes.Reference (Current_Label.Order) := E.To;
                end if;
            end if;
        end loop;
        return V2A (Nodes);
    end Sub_Nodes;





    procedure Prune
           (Container : in out Parse_Graph;
            Node      : in     Node_ID_Type)
    is
        My_Cursor : Cursor := Container.To_Cursor (Node);
    begin
        Prune (My_Cursor);
    end Prune;


    procedure Prune
           (Position : in out Cursor)
    is
        use type Ada.Containers.Count_Type;
        Active : Cursor_Vectors.Vector;
        Current : Cursor;
    begin
        if not Has_Element (Position) then
            return;
        end if;
        for N of Children (Position) loop
            if N /= Element (Position) then
                Active.Append (Cursor_To (Position, N));
            end if;
        end loop;
        Delete (Position);
        while not Active.Is_Empty loop
            for Index in reverse 1 .. Active.Last_Index loop
                Current := Active (Index);
                if Indegree (Current) = 0 then
                    for N of Children (Current) loop
                        if not Active.Contains (Cursor_To (Current, N)) then
                            Active.Append (Cursor_To (Current, N));
                        end if;
                    end loop;
                    Delete (Current);
                end if;
                Active.Delete (Index);
            end loop;
        end loop;
    end Prune;





    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;