forked from AdaCore/langkit-query-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlkql-queries.adb
260 lines (221 loc) · 8.99 KB
/
lkql-queries.adb
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
------------------------------------------------------------------------------
-- --
-- LKQL --
-- --
-- Copyright (C) 2019-2022, AdaCore --
-- --
-- LKQL is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be --
-- useful but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and a --
-- copy of the GCC Runtime Library Exception along with this program; see --
-- the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
------------------------------------------------------------------------------
with GNAT.Traceback.Symbolic;
with Ada.Text_IO; use Ada.Text_IO;
with Langkit_Support.Errors; use Langkit_Support.Errors;
with LKQL.Patterns; use LKQL.Patterns;
with LKQL.Primitives; use LKQL.Primitives;
with LKQL.Evaluation; use LKQL.Evaluation;
with LKQL.Patterns.Match; use LKQL.Patterns.Match;
with LKQL.Error_Handling; use LKQL.Error_Handling;
with LKQL.Errors; use LKQL.Errors;
with Ada.Exceptions; use Ada.Exceptions;
package body LKQL.Queries is
-------------------------
-- Make_Query_Iterator --
-------------------------
function Make_Query_Iterator
(Ctx : Eval_Context;
Node : L.Query) return Lk_Node_Iterator'Class
is
function Roots return Lk_Node_Iterator_Access;
-----------
-- Roots --
-----------
function Roots return Lk_Node_Iterator_Access is
begin
if Node.F_From_Expr.Is_Null then
-- First case, there is no "from" in the query. In that case, the
-- implicit roots of the query are the roots of the LKQL eval
-- context.
return new Lk_Node_Iterator'Class'
(Lk_Node_Iterator'Class
(Make_Child_Iterator (Ctx.AST_Roots.all)));
else
-- Second case, there is a "from" clause in the query.
declare
-- First, eval the expression.
Eval_From_Expr : constant Primitive :=
Eval (Ctx, Node.F_From_Expr);
Vec : Lk_Node_Vector;
begin
case Eval_From_Expr.Kind is
-- If it's a single node, create an array with just this
-- element.
when Kind_Node =>
Vec.Append (Eval_From_Expr.Node_Val);
-- If it's a list, it needs to be a list of nodes. Create a
-- vector from it to create the iterator from.
when Kind_List =>
for El of Eval_From_Expr.List_Val.Elements loop
if El.Kind /= Kind_Node then
-- TODO: For the moment it's impossible to exert
-- this check in queries, because only queries
-- return lists (comprehensions return iterators
-- and selectors selector lists). We need to unify
-- the sequence types somehow, because having a
-- list comprehension in a "from" appears
-- potentially useful, and is not possible yet.
Raise_And_Record_Error
(Ctx,
Make_Eval_Error
(Node.F_From_Expr.As_Lkql_Node,
"Wrong kind of element in list for "
& "`from clause`"));
end if;
Vec.Append (El.Node_Val);
end loop;
-- If it's any other kind of node, then it's an error
when others =>
Raise_And_Record_Error
(Ctx,
Make_Eval_Error
(Node.F_From_Expr.As_Lkql_Node,
"Wrong kind of element in `from clause`"));
end case;
return new Lk_Node_Iterator'Class'
(Lk_Node_Iterator'Class
(Make_Child_Iterator (Vec)));
end;
end if;
end Roots;
begin
case Node.F_Pattern.Kind is
when LCO.Lkql_Chained_Node_Pattern_Range =>
declare
Chained : constant Chained_Pattern_Iterator :=
Make_Chained_Pattern_Iterator
(Ctx,
Roots,
Node.F_Pattern.P_Value_Part.As_Chained_Node_Pattern);
begin
return Chained_Pattern_Query_Iter'
(Ctx => Ctx.Ref_Frame,
Iter => Chained);
end;
when others =>
declare
Predicate : constant Lk_Node_Predicate_Access :=
Lk_Node_Predicate_Access
(Make_Query_Predicate (Ctx, Node.F_Pattern));
begin
return Lk_Node_Iterators.Filter (Roots, Predicate);
end;
end case;
end Make_Query_Iterator;
--------------------------
-- Make_Query_Predicate --
--------------------------
function Make_Query_Predicate
(Ctx : Eval_Context; Pattern : L.Base_Pattern)
return Query_Predicate_Access
is
begin
return new Query_Predicate'(Ctx.Ref_Frame, Pattern);
end Make_Query_Predicate;
--------------
-- Evaluate --
--------------
overriding function Evaluate
(Self : in out Query_Predicate; Node : Lk_Node) return Boolean
is
begin
declare
Match : constant Match_Result :=
Match_Pattern (Self.Ctx,
Self.Pattern,
To_Primitive (Node, Self.Ctx.Pool));
begin
return Match.Is_Success;
end;
exception
when E : Property_Error =>
case Property_Error_Recovery is
when Continue_And_Log =>
Eval_Trace.Trace ("Evaluating query predicate failed");
Eval_Trace.Trace ("pattern => " & Self.Pattern.Image);
Eval_Trace.Trace
("ada node => " & Node.Image);
Eval_Trace.Trace (Exception_Information (E));
Eval_Trace.Trace
(GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
when Continue_And_Warn =>
-- TODO: Use Langkit_Support.Diagnostics.Output
Put_Line (Standard_Error, "Evaluating query predicate failed");
Put_Line (Standard_Error, "pattern => " & Self.Pattern.Image);
Put_Line
(Standard_Error, "ada node => "
& Node.Image);
when Raise_Error =>
raise;
end case;
return False;
end Evaluate;
-----------
-- Clone --
-----------
overriding function Clone
(Self : Query_Predicate) return Query_Predicate
is
begin
return Query_Predicate'(Self.Ctx, Self.Pattern);
end Clone;
-------------
-- Release --
-------------
overriding procedure Release (Self : in out Query_Predicate) is
begin
Self.Ctx.Release_Current_Frame;
end Release;
----------
-- Next --
----------
overriding function Next
(Iter : in out Chained_Pattern_Query_Iter;
Result : out Lk_Node) return Boolean
is
Match : Match_Result;
begin
-- The inner iterator is empty: return false
if not Iter.Iter.Next (Match) then
return False;
end if;
Result := Node_Val (Match.Get_Matched_Value);
return True;
end Next;
-----------
-- Clone --
-----------
overriding function Clone (Iter : Chained_Pattern_Query_Iter)
return Chained_Pattern_Query_Iter
is
(Iter.Ctx.Ref_Frame, Iter.Iter.Clone);
-------------
-- Release --
-------------
overriding procedure Release (Iter : in out Chained_Pattern_Query_Iter) is
begin
Iter.Ctx.Release_Current_Frame;
Iter.Iter.Release;
end Release;
end LKQL.Queries;