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 | --
-- Copyright (C) 2021-2022, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
with Ada.Command_Line;
with Ada.Characters.Wide_Wide_Latin_1;
with Ada.Wide_Wide_Text_IO;
with VSS.Application;
with VSS.Characters;
with VSS.Characters.Latin;
with VSS.Regular_Expressions;
with VSS.Strings;
with VSS.Strings.Cursors.Markers;
with VSS.Strings.Character_Iterators;
with VSS.Strings.Conversions;
with VSS.String_Vectors;
procedure Test_RegExp_RE_Tests is
pragma Assertion_Policy (Check);
type Check_Kind is (Skip, Match, Dont_Match);
type Check_Value (Kind : Check_Kind := Skip) is record
case Kind is
when Match =>
Expr : VSS.Strings.Virtual_String;
Expect : VSS.Strings.Virtual_String;
when others =>
null;
end case;
end record;
procedure Parse_Line
(Line : Wide_Wide_String;
Pattern : out VSS.Strings.Virtual_String;
Sample : out VSS.Strings.Virtual_String;
Check : out Check_Value);
procedure Verify
(Line : Wide_Wide_String;
Match : VSS.Regular_Expressions.Regular_Expression_Match;
Expr : VSS.Strings.Virtual_String;
Expect : VSS.Strings.Virtual_String;
Total : in out Natural);
function Image (Value : VSS.Strings.Character_Count)
return VSS.Strings.Virtual_String;
function Expand_Sample (Value : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String;
-- Interprete `\n`, `\x{AA}`
Tab : constant VSS.Characters.Virtual_Character :=
VSS.Characters.Virtual_Character
(Ada.Characters.Wide_Wide_Latin_1.HT);
-------------------
-- Expand_Sample --
-------------------
function Expand_Sample (Value : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String
is
List : constant VSS.String_Vectors.Virtual_String_Vector :=
Value.Split ('\');
Result : VSS.Strings.Virtual_String := List (1);
begin
for J in 2 .. List.Length loop
if List (J).Starts_With ("x{") then
declare
use type VSS.Characters.Virtual_Character;
Item : constant VSS.Strings.Virtual_String := List (J);
Value : VSS.Strings.Virtual_String;
X : VSS.Strings.Character_Iterators.Character_Iterator :=
Item.At_First_Character;
begin
if X.Forward then -- Skip x
while X.Forward and then X.Element /= '}' loop
Value.Append (X.Element);
end loop;
Result.Append
(VSS.Characters.Virtual_Character'Val
(Positive'Value
("16#" &
VSS.Strings.Conversions.To_UTF_8_String (Value) &
"#")));
if X.Forward then -- Skip }
Result.Append (Item.Slice (X, Item.At_Last_Character));
end if;
end if;
end;
elsif List (J).Starts_With ("n") then
Result.Append (VSS.Characters.Latin.Line_Feed);
else
Result.Append ('\');
Result.Append (List (J));
end if;
end loop;
return Result;
end Expand_Sample;
-----------
-- Image --
-----------
function Image (Value : VSS.Strings.Character_Count)
return VSS.Strings.Virtual_String
is
Img : constant Wide_Wide_String :=
VSS.Strings.Character_Count'Base'Wide_Wide_Image (Value);
begin
return VSS.Strings.To_Virtual_String (Img (2 .. Img'Last));
end Image;
----------------
-- Parse_Line --
----------------
procedure Parse_Line
(Line : Wide_Wide_String;
Pattern : out VSS.Strings.Virtual_String;
Sample : out VSS.Strings.Virtual_String;
Check : out Check_Value)
is
List : constant VSS.String_Vectors.Virtual_String_Vector :=
VSS.Strings.To_Virtual_String (Line).Split (Tab);
begin
-- Parse 5 columns
-- 1 => pattern
-- 2 => sample
-- 3 => has match
-- 4 => expression to evaluate
-- 5 => evaluated value
if not List (1).Starts_With ("/") then
Pattern := List (1);
elsif List (1).Ends_With ("/") then -- Pattern is `/regexp/`
Pattern := List (1);
declare
From : VSS.Strings.Character_Iterators.Character_Iterator :=
Pattern.At_First_Character;
To : VSS.Strings.Character_Iterators.Character_Iterator :=
Pattern.At_Last_Character;
begin
if From.Forward and To.Backward then
Pattern := Pattern.Slice (From, To);
else
raise Program_Error;
end if;
end;
else -- Pattern in form of `/regexp/FLAGS` isn't supported yet
Check := (Kind => Skip);
return;
end if;
Sample := List (2);
Sample := Expand_Sample (Sample);
declare
Status : constant VSS.Strings.Virtual_String := List (3);
Expr : constant VSS.Strings.Virtual_String := List (4);
Expect : constant VSS.Strings.Virtual_String := List (5);
begin
case Status.At_First_Character.Element is
when 'y' =>
Check := (Match, Expr, Expand_Sample (Expect));
when 'n' =>
Check := (Kind => Dont_Match);
when others => -- 'c' -- compile error
Check := (Kind => Skip);
end case;
end;
end Parse_Line;
------------
-- Verify --
------------
procedure Verify
(Line : Wide_Wide_String;
Match : VSS.Regular_Expressions.Regular_Expression_Match;
Expr : VSS.Strings.Virtual_String;
Expect : VSS.Strings.Virtual_String;
Total : in out Natural)
is
use type VSS.Strings.Character_Count;
use type VSS.Strings.Virtual_String;
use type VSS.Characters.Virtual_Character;
function Read_Natural
(Cursor : in out VSS.Strings.Character_Iterators.Character_Iterator)
return Natural;
-- Read natural number from given cursor. Move the cursor to next char.
function Read_Natural
(Cursor : in out VSS.Strings.Character_Iterators.Character_Iterator)
return Natural
is
To : VSS.Strings.Cursors.Markers.Character_Marker;
From : constant VSS.Strings.Cursors.Markers.Character_Marker :=
Cursor.Marker;
begin
while Cursor.Element in '0' .. '9' loop
To := Cursor.Marker;
exit when not Cursor.Forward;
end loop;
return Natural'Wide_Wide_Value
(VSS.Strings.Conversions.To_Wide_Wide_String
(Expr.Slice (From, To)));
end Read_Natural;
Result : VSS.Strings.Virtual_String;
Cursor : VSS.Strings.Character_Iterators.Character_Iterator :=
Expr.At_First_Character;
begin
loop
case Cursor.Element is
when '$' =>
pragma Assert (Cursor.Forward);
case Cursor.Element is
when '&' => -- $& - whole match
Result.Append (Match.Captured);
when '1' .. '9' => -- $X - the X group
declare
Index : constant Positive := Read_Natural (Cursor);
begin
Result.Append (Match.Captured (Index));
-- step back to last digit:
pragma Assert (Cursor.Backward);
end;
when '-' => -- &-[X] - the X group start offset
pragma Assert (Cursor.Forward);
pragma Assert (Cursor.Element = '[');
pragma Assert (Cursor.Forward);
pragma Assert (Cursor.Element in '0' .. '9');
declare
Index : constant Natural := Read_Natural (Cursor);
Marker : constant
VSS.Strings.Cursors.Markers.Character_Marker :=
Match.First_Marker (Index);
begin
if Marker.Is_Valid then
Result.Append (Image (Marker.Character_Index - 1));
end if;
end;
pragma Assert (Cursor.Element = ']');
when '+' => -- &+[X] - the X group end offset
pragma Assert (Cursor.Forward);
pragma Assert (Cursor.Element = '[');
pragma Assert (Cursor.Forward);
pragma Assert (Cursor.Element in '0' .. '9');
declare
Index : constant Natural := Read_Natural (Cursor);
Marker : constant
VSS.Strings.Cursors.Markers.Character_Marker :=
Match.Last_Marker (Index);
begin
if Marker.Is_Valid then
Result.Append (Image (Marker.Character_Index));
end if;
end;
pragma Assert (Cursor.Element = ']');
when others =>
raise Program_Error;
end case;
when others =>
Result.Append (Cursor.Element);
end case;
exit when not Cursor.Forward;
end loop;
if Result = Expect then
Total := Total + 1; -- PASS
else
Ada.Wide_Wide_Text_IO.Put ("DIFF: ");
Ada.Wide_Wide_Text_IO.Put (Line);
Ada.Wide_Wide_Text_IO.Put (" /= <");
Ada.Wide_Wide_Text_IO.Put
(VSS.Strings.Conversions.To_Wide_Wide_String (Result));
Ada.Wide_Wide_Text_IO.Put_Line (">");
end if;
end Verify;
Total : Natural := 0;
Expect : Natural; -- Expectation for final Total value
Last_Pattern : VSS.Regular_Expressions.Regular_Expression;
Last_Sample : VSS.Strings.Virtual_String;
Last_Match : VSS.Regular_Expressions.Regular_Expression_Match;
begin
if VSS.Application.Arguments.Is_Empty then
Expect := 0;
elsif VSS.Application.Arguments.Last_Element.Starts_With ("-") then
Ada.Wide_Wide_Text_IO.Put_Line ("Usage: test_regexp_re_tests [count]");
Ada.Wide_Wide_Text_IO.New_Line;
Ada.Wide_Wide_Text_IO.Put_Line
("where 'count' a number of requred tests to pass.");
return;
else
Expect := Natural'Wide_Wide_Value
(VSS.Strings.Conversions.To_Wide_Wide_String
(VSS.Application.Arguments.Last_Element));
end if;
while not Ada.Wide_Wide_Text_IO.End_Of_File loop
declare
Line : constant Wide_Wide_String := Ada.Wide_Wide_Text_IO.Get_Line;
begin
if Line in "" | "__END__"
or else Line (1) = '#'
then
-- skip comments and special lines
null;
elsif Line (1) not in ''' then
declare
use type VSS.Strings.Virtual_String;
Pattern : VSS.Strings.Virtual_String;
Sample : VSS.Strings.Virtual_String;
Check : Check_Value;
begin
Parse_Line (Line, Pattern, Sample, Check);
if Check.Kind /= Skip then
if Last_Pattern.Pattern /= Pattern then
Last_Pattern :=
VSS.Regular_Expressions.To_Regular_Expression (Pattern);
Last_Sample := "not-a-sample";
end if;
if Last_Sample /= Sample then
Last_Sample := Sample;
Last_Match := Last_Pattern.Match (Last_Sample);
end if;
case Check.Kind is
when Match =>
if Last_Match.Has_Match then
Verify
(Line,
Last_Match,
Check.Expr,
Check.Expect,
Total);
else
Ada.Wide_Wide_Text_IO.Put ("DONT: ");
Ada.Wide_Wide_Text_IO.Put_Line (Line);
end if;
when Dont_Match =>
if Last_Match.Has_Match then
Ada.Wide_Wide_Text_IO.Put ("MISS: ");
Ada.Wide_Wide_Text_IO.Put_Line (Line);
else
Total := Total + 1; -- PASS
end if;
when Skip =>
null;
end case;
else
Ada.Wide_Wide_Text_IO.Put ("SKIP: ");
Ada.Wide_Wide_Text_IO.Put_Line (Line);
end if;
exception
when others =>
Ada.Wide_Wide_Text_IO.Put ("CRASH: ");
Ada.Wide_Wide_Text_IO.Put_Line (Line);
end;
end if;
end;
end loop;
Ada.Wide_Wide_Text_IO.Put ("PASSED:");
Ada.Wide_Wide_Text_IO.Put_Line (Total'Wide_Wide_Image);
if Expect > Total then
Ada.Wide_Wide_Text_IO.Put ("Execution failed! Not enough PASSED tests!");
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end Test_RegExp_RE_Tests;
|