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 | -- -----------------------------------------------------------------------------
-- bbt, the black box tester (https://github.com/LionelDraghi/bbt)
-- Author : Lionel Draghi
-- SPDX-License-Identifier: APSL-2.0
-- SPDX-FileCopyrightText: 2024, Lionel Draghi
-- -----------------------------------------------------------------------------
with BBT.Scenarios.Step_Parser.Lexer;
with Text_Utilities; use Text_Utilities;
with Ada.Containers;
with Ada.Directories; use Ada.Directories;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Maps.Constants;
with Ada.Text_IO;
with Ada.Strings.Maps;
package body BBT.Scenarios.Step_Parser is
use BBT.Scenarios.Step_Parser.Lexer;
type Tokens is ( -- Prepositions -----------------------------------------
Given,
When_P,
Then_P,
-- Subjects Attribute -----------------------------------
No_SA, -- = no Subject Attribute
-- Empty,
-- Existing,
New_SA,
-- Subjects ---------------------------------------------
No_Subject,
Subject_Dir, -- dir name
Subject_File, -- file name
Subject_Text, -- content of code span or following
-- code fenced lines, before verb
Output_Subj,
-- Verbs ------------------------------------------------
No_Verb,
Run,
Successful_Run,
Get,
Get_No,
Does_Not_Contain,
Contains,
Containing,
Is_V,
Is_No,
-- Objects ----------------------------------------------
No_Object,
Output_Obj,
Object_File,
Object_Dir, -- file or dir name
Object_Text, -- content of code span or following
-- code fenced lines, before verb
Command_List,
Error,
-- Adjectives
Unordered);
subtype Adjectives is Tokens range Unordered .. Tokens'Last;
subtype Objects is Tokens range No_Object .. Tokens'Pred (Adjectives'First);
subtype Verbs is Tokens range No_Verb .. Tokens'Pred (Objects'First);
subtype Subjects is Tokens range No_Subject .. Tokens'Pred (Verbs'First);
subtype Subject_Attrib is Tokens range No_SA .. Tokens'Pred (Subjects'First);
subtype Prepositions is Tokens range Tokens'First .. Tokens'Pred (Subject_Attrib'First);
-- -----------------------------------------------------------------------
function Image (T : Tokens) return String is
begin
case T is
when Given => return "Given";
when When_P => return "When";
when Then_P => return "Then";
when No_SA => return "";
when New_SA => return "new";
when No_Subject => return "";
when Output_Subj => return "output";
when Subject_File => return "`file`";
when Subject_Dir => return "`dir`";
when Subject_Text => return "`text`";
when No_Verb => return "";
when Run => return "run";
when Successful_Run => return "successfully run";
when Get => return "get";
when Get_No => return "get no";
when Contains => return "contains";
when Containing => return "containing";
when Does_Not_Contain => return "does not contain";
when Is_V => return "is";
when Is_No => return "is no";
when No_Object => return "";
when Output_Obj => return "output";
when Object_File => return "`file`";
when Object_Dir => return "`dir`";
when Object_Text => return "`text`";
when Command_List => return "`cmd` [or `cmd`]*";
when Error => return "error";
when Unordered => return "unordered";
end case;
end Image;
type Grammar is array (Prepositions, Subject_Attrib,
Subjects, Verbs, Objects) of Actions;
Null_Grammar : constant Grammar
:= [others => [others => [others => [others => [others => None]]]]];
-- -----------------------------------------------------------------------
function Create_Grammar return Grammar is
G : Grammar := Null_Grammar;
begin
G (Given, No_SA, No_Subject, Is_No, Object_File) := Setup_No_File; -- Given there is no `config.ini` file
G (Given, No_SA, No_Subject, Is_No, Object_Dir) := Setup_No_Dir; -- Given there is no `dir1` directory
G (Given, No_SA, No_Subject, Is_V, Object_File) := Check_File_Existence; -- Given there is a `config.ini` file
G (Given, No_SA, No_Subject, Is_V, Object_Dir) := Check_Dir_Existence; -- Given there is a `dir1` directory
G (Given, New_SA, Subject_File, Containing, Object_Text) := Erase_And_Create; -- Given the new file `config.ini` containing `lang=it`
G (Given, No_SA, Subject_File, Containing, Object_Text) := Create_File; -- Given the file `config.ini` containing `lang=it`
G (Given, New_SA, Subject_File, No_Verb, No_Object) := Erase_And_Create; -- Given the new file `config.ini` followed by code fenced content
G (Given, No_SA, Subject_File, No_Verb, No_Object) := Create_File; -- Given the file `config.ini` followed by code fenced content
G (Given, New_SA, Subject_Dir, No_Verb, No_Object) := Erase_And_Create; -- Given the new directory `dir1`
G (Given, No_SA, Subject_Dir, No_Verb, No_Object) := Create_Directory; -- Given the directory `dir1`
G (When_P, No_SA, No_Subject, Run, Object_Text) := Run_Cmd; -- when I run `cmd`
G (When_P, No_SA, No_Subject, Successful_Run, Object_Text) := Run_Without_Error; -- when i successfully run `cmd`
G (When_P, No_SA, No_Subject, Run, Command_List) := Run_Cmd; -- when I run `cmd` or `cmd2` or `cmd3`
G (When_P, No_SA, No_Subject, Successful_Run, Command_List) := Run_Without_Error; -- when i successfully run `cmd` or `cmd2` or `cmd3`
G (Then_P, No_SA, No_Subject, Is_V, Object_File) := Check_File_Existence; -- Then there is a `config.ini` file
G (Then_P, No_SA, No_Subject, Is_No, Object_File) := Check_No_File; -- Then there is no `config.ini` file
G (Then_P, No_SA, No_Subject, Is_V, Object_Dir) := Check_Dir_Existence; -- Then there is a `dir1` directory
G (Then_P, No_SA, No_Subject, Is_No, Object_Dir) := Check_No_Dir; -- Then there is no `dir1` directory
G (Then_P, No_SA, No_Subject, Get, Error) := Error_Return_Code; -- then I get error
G (Then_P, No_SA, No_Subject, Get_No, Error) := No_Error_Return_Code; -- then I get no error
G (Then_P, No_SA, No_Subject, Is_V, Error) := Error_Return_Code; -- then there is an error
G (Then_P, No_SA, No_Subject, Is_No, Error) := No_Error_Return_Code; -- then there is no error
G (Then_P, No_SA, Output_Subj, Is_V, Object_Text) := Output_Is; -- then output is `msg`
G (Then_P, No_SA, Output_Subj, Is_V, Object_File) := Output_Is; -- then output is file `expected.txt`
G (Then_P, No_SA, Output_Subj, Is_V, No_Object) := Output_Is; -- then output is followed by code fenced content
G (Then_P, No_SA, No_Subject, Get, Object_Text) := Output_Is; -- then I get `msg`
G (Then_P, No_SA, No_Subject, Get, Object_File) := Output_Is; -- Then I get file `flowers2.txt`
G (Then_P, No_SA, No_Subject, Get, No_Object) := Output_Is; -- then I get followed by code fenced content
G (Then_P, No_SA, Output_Subj, Contains, Object_Text) := Output_Contains; -- then output contains `msg`
G (Then_P, No_SA, Output_Subj, Contains, Object_File) := Output_Contains; -- Then output contains `snippet.txt` file
G (Then_P, No_SA, Output_Subj, Contains, No_Object) := Output_Contains; -- then output contains followed by code fenced content
G (Then_P, No_SA, Output_Subj, Does_Not_Contain, Object_Text) := Output_Does_Not_Contain; -- then output does not contain `msg`
G (Then_P, No_SA, Output_Subj, Does_Not_Contain, Object_File) := Output_Does_Not_Contain; -- Then output does not contain file `snippet.txt`
G (Then_P, No_SA, Output_Subj, Does_Not_Contain, No_Object) := Output_Does_Not_Contain; -- then output does not contain followed by code fenced content
G (Then_P, No_SA, Subject_File, Is_V, Object_Text) := File_Is; -- then `config.ini` is `mode=silent`
G (Then_P, No_SA, Subject_File, Is_V, Object_File) := File_Is; -- then `config.ini` is equal to file `expected/config.ini`
G (Then_P, No_SA, Subject_File, Is_V, No_Object) := File_Is; -- Then `config.ini` is followed by code fenced content
G (Then_P, No_SA, Subject_File, Contains, Object_Text) := File_Contains; -- Then `config.ini` contains `--version`
G (Then_P, No_SA, Subject_File, Contains, Object_File) := File_Contains; -- Then `config.ini` contains `snippet.txt` file
G (Then_P, No_SA, Subject_File, Contains, No_Object) := File_Contains; -- Then `config.ini` contains followed by code fenced content
G (Then_P, No_SA, Subject_File, Does_Not_Contain, Object_Text) := File_Does_Not_Contain; -- Then `config.ini` does not contain `--version`
G (Then_P, No_SA, Subject_File, Does_Not_Contain, Object_File) := File_Does_Not_Contain; -- Then `config.ini` does not contain `snippet.txt` file
G (Then_P, No_SA, Subject_File, Does_Not_Contain, No_Object) := File_Does_Not_Contain; -- Then `config.ini` does not contain followed by code fenced content
G (Then_P, No_SA, No_Subject, Get_No, Output_Obj) := No_Output; -- then I get no output
G (Then_P, No_SA, No_Subject, Is_No, Output_Obj) := No_Output; -- then there is no output
return G;
end Create_Grammar;
The_Grammar : constant Grammar := Create_Grammar;
-- -----------------------------------------------------------------------
function Get_Action (P : Prepositions;
SA : Subject_Attrib;
S : Subjects;
V : Verbs;
O : Objects) return Actions
is (The_Grammar (P, SA, S, V, O));
-- -----------------------------------------------------------------------
procedure Put_Rule (P : Prepositions;
SA : Subject_Attrib;
S : Subjects;
V : Verbs;
O : Objects;
A : Actions;
Verbosity : IO.Verbosity_Levels := IO.Normal)
is
use Ada.Text_IO;
C : Positive_Count := Col;
begin
if Is_Authorized (Verbosity) then
Ada.Text_IO.Put ("| " & Image (P)); C := @ + Prepositions'Width + 1; Set_Col (C);
Ada.Text_IO.Put (" | " & Image (SA)); C := @ + Subject_Attrib'Width; Set_Col (C);
Ada.Text_IO.Put (" | " & Image (S)); C := @ + Subjects'Width - 3; Set_Col (C);
Ada.Text_IO.Put (" | " & Image (V)); C := @ + Verbs'Width + 5; Set_Col (C);
Ada.Text_IO.Put (" | " & Image (O)); C := @ + Objects'Width + 8; Set_Col (C);
Ada.Text_IO.Put (" | " & A'Image); C := @ + Actions'Width + 3; Set_Col (C);
Ada.Text_IO.Put_Line (" | ");
end if;
end Put_Rule;
Context : Extended_Step_Categories;
-- Some line are hard to interpret without the context:
-- line that starts with "And" for example will inherit their type from
-- the previously analyzed lines.
-- This variable keep the memory of where we are between call Parse.
-- --------------------------------------------------------------------------
function Parse (Line : Unbounded_String;
Loc : Location_Type;
Cmd_List : out Cmd_Lists.Vector) return Step_Type
is
First_Token : Boolean := True;
Successfully_Met : Boolean := False;
Or_Met : Natural := 0;
Not_Met : Boolean := False;
Prep : Prepositions;
Subject_Attr : Subject_Attrib := No_SA;
Subject : Subjects := No_Subject;
Verb : Verbs := No_Verb;
Object : Objects := No_Object;
-- All component of the returned Step are initialized :
Cat : Extended_Step_Categories := Unknown;
Action : Actions := None;
Step_String : Unbounded_String := Null_Unbounded_String;
Subject_String : Unbounded_String := Null_Unbounded_String;
Object_String : Unbounded_String := Null_Unbounded_String;
Object_File_Name : Unbounded_String := Null_Unbounded_String;
-- -- Object_Text : Object_Text_Type;
-- Text_Source : Text_Sources := Code_Span;
Ignore_Order : Boolean := False;
-- by default, order of expected output is significant
File_Type : File_Kind := Ordinary_File;
Prefix : constant String := Image (Loc);
function In_Subject_Part return Boolean is (Verb = No_Verb);
function In_Object_Part return Boolean is (Verb /= No_Verb);
-- -- -----------------------------------------------------------------------
-- function Build_Object_Text (Source : Text_Sources;
-- Object_String : Unbounded_String;
-- File_Name : Unbounded_String;
-- File_Type : File_Kind)
-- return Object_Text_Type is
-- begin
-- case Source is
-- when Code_Span =>
-- return (Code_Span,
-- File_Name => File_Name,
-- Object_String => Object_String);
-- when Code_Block =>
-- return (Code_Block, File_Name, Empty_Text);
-- when File =>
-- return (File, File_Name, File_Type);
-- end case;
-- end Build_Object_Text;
begin
Step_String := Line;
Cmd_List := Cmd_Lists.Empty_Vector;
Initialize_Lexer;
-- Put_Line ("Parsing """ & To_String (Line) & """", Verbosity => IO.Debug);
Line_Processing : while More_Token loop
declare
TT : Token_Type;
Tmp : aliased constant String := To_String (Line);
Tok : constant String := Next_Token (Tmp'Access, TT);
begin
-- Put_Line ("Token = " & TT'Image & " " & Tok'Image);
case TT is
when Keyword =>
declare
Lower_Keyword : constant String := Translate
(Source => Tok,
Mapping => Ada.Strings.Maps.Constants.Lower_Case_Map);
begin
if First_Token then
if Lower_Keyword = "given" then
Cat := Given_Step;
Prep := Given;
elsif Lower_Keyword = "when" then
Cat := When_Step;
Prep := When_P;
elsif Lower_Keyword = "then" then
Cat := Then_Step;
Prep := Then_P;
elsif Lower_Keyword = "and" or else
Lower_Keyword = "but"
then
Cat := Context;
-- inherited from the context
case Context is
when Unknown => null;
when Given_Step => Prep := Given;
when When_Step => Prep := When_P;
when Then_Step => Prep := Then_P;
end case;
else
IO.Put_Warning
("Keyword : " & Tok & " ignored", Loc);
end if;
else
null;
-- given/when/then may appear later on the line, but
-- then are not considered as keywords.
end if;
if Lower_Keyword = "run" or Lower_Keyword = "running" then
if Successfully_Met then
Verb := Successful_Run;
else
Verb := Run;
end if;
elsif Lower_Keyword = "or" then
Or_Met := @ + 1;
if Object /= Command_List then
Object := Command_List;
-- there should already be a code span, let's
-- store it in the list
Cmd_List.Append (+Object_String);
Object_String := Null_Unbounded_String;
end if;
elsif Lower_Keyword = "get" then
Verb := Get;
elsif Lower_Keyword = "is" then
Verb := Is_V;
elsif Lower_Keyword = "no"
or Lower_Keyword = "not"
or Lower_Keyword = "dont"
or Lower_Keyword = "doesnt"
or Lower_Keyword = "doesn't"
then
Not_Met := True;
if Verb = Is_V then
Verb := Is_No;
elsif Verb = Get then
Verb := Get_No;
end if;
elsif Lower_Keyword = "successfully" then
Successfully_Met := True;
elsif Lower_Keyword = "error" then
Object := Error;
elsif Lower_Keyword = "output" then
if In_Subject_Part then
Subject := Output_Subj;
else
Object := Output_Obj;
end if;
elsif Lower_Keyword = "contains" or
Lower_Keyword = "contain"
then
if Not_Met then
Verb := Does_Not_Contain;
else
Verb := Contains;
end if;
elsif Lower_Keyword = "containing" then
Verb := Containing;
elsif Lower_Keyword = "new" then
Subject_Attr := New_SA;
File_Type := Ordinary_File;
-- "file" keyword is not mandatory, this is the default.
elsif Lower_Keyword = "directory" or Lower_Keyword = "dir"
then
File_Type := Directory;
if In_Subject_Part then
Subject := Subject_Dir;
elsif In_Object_Part then
Object := Object_Dir;
Object_File_Name := Object_String;
Object_String := Null_Unbounded_String;
-- Text_Source := File;
-- File_Type := Directory;
end if;
elsif Lower_Keyword = "file" then
File_Type := Ordinary_File;
if In_Subject_Part then
Subject := Subject_File;
elsif In_Object_Part then
Object := Object_File;
-- If file name was given before keyword "file",
-- then the file name is in Object_String
Object_File_Name := Object_String;
Object_String := Null_Unbounded_String;
-- Text_Source := File;
-- File_Type := Ordinary_File;
end if;
elsif Lower_Keyword = "unordered" then
Ignore_Order := True;
end if;
end;
when Identifier =>
-- Put_Line (Prefix & " Identifier : " & Tok);
null;
when Code_Span =>
if In_Subject_Part then
Subject_String := To_Unbounded_String (Tok);
else
if Object = Object_File or Object = Object_Dir then
-- "file" or "dir" keyword already meet
Object_File_Name := To_Unbounded_String (Tok);
else
-- Otherwise, we don't know yet if the Code_Span is
-- a simple string or a file name.
Object_String := To_Unbounded_String (Tok);
-- Text_Source := Code_Span;
end if;
end if;
if Object = Command_List then
Cmd_List.Append (Tok);
elsif In_Subject_Part and then Subject = No_Subject then
if File_Type = Directory then
Subject := Subject_Dir;
else
Subject := Subject_File;
end if;
-- Subject_String := To_Unbounded_String (Tok);
elsif In_Object_Part and then Object = No_Object then
case Verb is
when No_Verb |
Is_No =>
-- Verbs always followed by a file/dir
if File_Type = Directory then
Object := Object_Dir;
else
Object := Object_File;
end if;
when Run |
Successful_Run |
Get |
Get_No |
Contains |
Does_Not_Contain |
Containing =>
-- Verbs are always followed by a text
Object := Object_Text;
when Is_V =>
-- Complex case where it depends not only on the
-- verb...
if Subject = No_Subject then
-- Example : Given there is a `config.ini` file
if File_Type = Directory then
Object := Object_Dir;
else
Object := Object_File;
end if;
else
-- Then output is xxxx
-- or
-- Then `file` is xxxx
Object := Object_Text;
end if;
end case;
end if;
when Empty =>
null;
end case;
end;
First_Token := False;
end loop Line_Processing;
-- Some coherency tests to give to the user a more helpful
-- message than "Unrecognized Step"
declare
use Ada.Containers;
begin
if Verb = Run or Verb = Successful_Run then
if Or_Met > 0 and then Cmd_List.Length /= Count_Type (Or_Met + 1) then
IO.Put_Warning ("Command missing after last ""or""", Loc);
else
if Object_String = Null_Unbounded_String then
IO.Put_Warning ("No command after ""run"" keyword", Loc);
end if;
end if;
end if;
end;
Action := Get_Action (Prep, Subject_Attr, Subject, Verb, Object);
IO.Put (Prefix & " Rule = ", Verbosity => IO.Debug);
Put_Rule (Prep, Subject_Attr, Subject, Verb, Object, Action,
Verbosity => IO.Debug);
Context := Cat;
return (Cat => Cat,
Action => Action,
Step_String => Step_String,
Location => Loc,
Subject_String => Subject_String,
Object_String => Object_String,
Object_File_Name => Object_File_Name,
File_Type => File_Type,
Ignore_Order => Ignore_Order,
File_Content => Empty_Text,
Parent_Scenario => null);
end Parse;
-- --------------------------------------------------------------------------
procedure Put_Keywords renames Lexer.Put_Keywords;
-- -----------------------------------------------------------------------
procedure Put_Grammar is
begin
Ada.Text_IO.Put_Line ("| Prep | |Subject | Verb | Object | Action | ");
Ada.Text_IO.Put_Line ("|-------|-----|--------|------------------|-------------------|----------------------| ");
for P in The_Grammar'Range (1) loop -- A of G when A /= None loop
for SA in The_Grammar'Range (2) loop -- A of G when A /= None loop
for S in The_Grammar'Range (3) loop -- A of G when A /= None loop
for V in The_Grammar'Range (4) loop -- A of G when A /= None loop
for O in The_Grammar'Range (5) loop -- A of G when A /= None loop
if The_Grammar (P, SA, S, V, O) /= None then
Put_Rule (P, SA, S, V, O,
The_Grammar (P, SA, S, V, O));
end if;
end loop;
end loop;
end loop;
end loop;
end loop;
-- Ada.Text_IO.Put_Line ("|-------|-----|--------|------------------|-------------------|----------------------|");
end Put_Grammar;
end BBT.Scenarios.Step_Parser;
|