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 | ------------------------------------------------------------------------------
-- Language Server Protocol --
-- --
-- Copyright (C) 2018-2019, AdaCore --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. This software is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Ada.Directories;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with GNAT.Regpat;
with GNAT.OS_Lib;
with URIs;
package body Tester.Macros is
function Expand
(Value : GNATCOLL.JSON.JSON_Value;
Env : Spawn.Environments.Process_Environment;
Test_Dir : String) return GNATCOLL.JSON.JSON_Value;
-- Expand recursively
function Expand_URI
(Path : String;
Test_Dir : String) return String;
-- Turn Path into URI with scheme 'file://'
Pattern : constant GNAT.Regpat.Pattern_Matcher :=
GNAT.Regpat.Compile ("\${([\w]+)}|\$URI{([^}]*)}");
Replace_Slash : constant Ada.Strings.Maps.Character_Mapping :=
Ada.Strings.Maps.To_Mapping
(From => "/",
To => GNAT.OS_Lib.Directory_Separator & "");
function Expand
(Value : GNATCOLL.JSON.JSON_Value;
Env : Spawn.Environments.Process_Environment;
Test_Dir : String) return GNATCOLL.JSON.JSON_Value
is
procedure Each_Field
(Object : in out GNATCOLL.JSON.JSON_Value;
Name : String;
Value : GNATCOLL.JSON.JSON_Value);
-- Expand macros in given field
function Expand_In_String (Text : String) return Unbounded_String;
-- Expand macro in given string
procedure Map is new GNATCOLL.JSON.Gen_Map_JSON_Object
(GNATCOLL.JSON.JSON_Value);
----------------
-- Each_Field --
----------------
procedure Each_Field
(Object : in out GNATCOLL.JSON.JSON_Value;
Name : String;
Value : GNATCOLL.JSON.JSON_Value)
is
Key : constant String := To_String (Expand_In_String (Name));
begin
Object.Set_Field (Key, Expand (Value, Env, Test_Dir));
end Each_Field;
----------------------
-- Expand_In_String --
----------------------
function Expand_In_String (Text : String) return Unbounded_String
is
use type GNAT.Regpat.Match_Location;
Result : Unbounded_String;
Next : Positive := 1;
begin
while Next < Text'Length loop
declare
Found : GNAT.Regpat.Match_Array (0 .. 2);
begin
GNAT.Regpat.Match (Pattern, Text, Found, Next);
exit when Found (0) = GNAT.Regpat.No_Match;
Append (Result, Text (Next .. Found (0).First - 1));
if Found (1) /= GNAT.Regpat.No_Match then -- ${NAME}
declare
Name : constant String :=
Text (Found (1).First .. Found (1).Last);
begin
if Env.Contains (Name) then
Append (Result, Env.Value (Name));
else
raise Program_Error
with Name & " undefined, cannot expand macro";
end if;
end;
elsif Found (2) /= GNAT.Regpat.No_Match then -- $URI{x}
Append
(Result,
Expand_URI
(Text (Found (2).First .. Found (2).Last),
Test_Dir));
end if;
Next := Found (0).Last + 1;
end;
end loop;
Append (Result, Text (Next .. Text'Last));
return Result;
end Expand_In_String;
begin
case Value.Kind is
when GNATCOLL.JSON.JSON_Null_Type |
GNATCOLL.JSON.JSON_Boolean_Type |
GNATCOLL.JSON.JSON_Int_Type |
GNATCOLL.JSON.JSON_Float_Type =>
return Value;
when GNATCOLL.JSON.JSON_String_Type =>
return GNATCOLL.JSON.Create (Expand_In_String (Value.Get));
when GNATCOLL.JSON.JSON_Array_Type =>
declare
Result : GNATCOLL.JSON.JSON_Array;
Vector : constant GNATCOLL.JSON.JSON_Array := Value.Get;
begin
for J in 1 .. GNATCOLL.JSON.Length (Vector) loop
declare
Item : constant GNATCOLL.JSON.JSON_Value :=
GNATCOLL.JSON.Get (Vector, J);
begin
GNATCOLL.JSON.Append
(Result, Expand (Item, Env, Test_Dir));
end;
end loop;
return GNATCOLL.JSON.Create (Result);
end;
when GNATCOLL.JSON.JSON_Object_Type =>
declare
Result : GNATCOLL.JSON.JSON_Value :=
GNATCOLL.JSON.Create_Object;
begin
Map (Value, Each_Field'Access, Result);
return Result;
end;
end case;
end Expand;
------------
-- Expand --
------------
procedure Expand
(Test : in out GNATCOLL.JSON.JSON_Value;
Env : Spawn.Environments.Process_Environment;
Path : String)
is
Full_Name : constant String := Ada.Directories.Full_Name (Path);
Directory : constant String :=
Ada.Directories.Containing_Directory (Full_Name);
Env_With_Dir : Spawn.Environments.Process_Environment := Env;
begin
Env_With_Dir.Insert ("DIR", Directory);
Env_With_Dir.Insert ("DIR_SEP", GNAT.OS_Lib.Directory_Separator & "");
Test := Expand (Test, Env_With_Dir, Directory);
end Expand;
----------------
-- Expand_URI --
----------------
function Expand_URI
(Path : String;
Test_Dir : String) return String
is
Argument : constant String := Ada.Strings.Fixed.Translate
(Path, Replace_Slash);
begin
if Path = "" then
-- Return normalized Test_Dir
return URIs.Conversions.From_File
(Ada.Directories.Full_Name (Test_Dir));
elsif Path = "/" then
-- Return normalized Test_Dir & '/'
return URIs.Conversions.From_File
(Ada.Directories.Full_Name (Test_Dir) &
GNAT.OS_Lib.Directory_Separator);
else
-- Turn a relative path into absolute path
return URIs.Conversions.From_File
(Ada.Directories.Full_Name (Test_Dir) &
GNAT.OS_Lib.Directory_Separator &
Argument);
end if;
end Expand_URI;
end Tester.Macros;
|