-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration-step_2.adb
284 lines (232 loc) · 12 KB
/
configuration-step_2.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
------------------------------------------------------------------------------
-- --
-- Ada User Repository Annex (AURA) --
-- ANNEXI-STRAYLINE Reference Implementation --
-- --
-- Command Line Interface --
-- --
-- ------------------------------------------------------------------------ --
-- --
-- Copyright (C) 2020, ANNEXI-STRAYLINE Trans-Human Ltd. --
-- All rights reserved. --
-- --
-- Original Contributors: --
-- * Richard Wai (ANNEXI-STRAYLINE) --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- --
-- * Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- --
-- * Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- --
-- * Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A --
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------
with Ada.Directories;
with Ada.Streams.Stream_IO;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Registrar.Source_Files;
with Unicode.UTF8_Stream_Decoder;
with Unicode.Case_Folding.Simple;
separate (Configuration)
-- Step 2 - we are creating a new configuration unit from the manifest.
--
-- This means both that the registry does not contain a unit corresponding to
-- the configuration unit, and that the registry does cointain a unit
-- corresponding to the manifest. We need to process the entire manifest unit.
--
-- The biggest part of this job is replacing the manifest's package name from
-- Target.AURA to AURA.Target. We will do text substituation, but we need to
-- comply with the Ada standard to 1) allow unicode (utf-8) identifiers, and
-- 2) apply case-folding.
--
-- The Ada standard requires all content to be in Normalization Form 'C' (202X)
-- but we don't need to check for that here as the Ada Lexical Parser will
-- pick that out during unit entry
procedure Step_2 (Target: in out Subsystem) is
use type Registrar.Source_Files.Source_File_Access;
-- These are already case folded
Manifest_Name: constant Unit_Name := Manifest_Unit_Name (Target);
Config_Name : constant Unit_Name := Config_Unit_Name (Target);
-- The manifest has to exist before a call to Step_2, so we don't need
-- to worry about pulling it out of the registry.
Manifest: constant Library_Unit
:= Reg_Qs.Lookup_Unit (Manifest_Name);
-- The basic gyst is: we are reading the manifest as a utf-8 stream,
-- looking for Expected_Name, and if we find it (we should at least
-- one), we replace that in the output stream with Substitute name.
-- Everything else passes through unchanged.
Expected_Name: constant Wide_Wide_String
:= Manifest_Name.To_String;
Substitute_Name: constant Wide_Wide_String
:= Config_Name.To_String;
Scan_Buffer : Wide_Wide_String (1 .. Expected_Name'Length);
Match_Buffer: Wide_Wide_String (Scan_Buffer'Range);
Match_Depth: Positive;
procedure Find_And_Replace
(In_Stream : not null access Ada.Streams.Root_Stream_Type'Class;
Out_Stream: not null access Ada.Streams.Root_Stream_Type'Class)
is
subtype UTF_8_String is Ada.Strings.UTF_Encoding.UTF_8_String;
use type UTF_8_String;
package UTF_8 renames Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
function Decode_Next
(UTF8_Stream: not null access Ada.Streams.Root_Stream_Type'Class
:= In_Stream)
return Wide_Wide_Character
renames Unicode.UTF8_Stream_Decoder.Decode_Next;
begin
Match_Depth := Scan_Buffer'First;
loop
-- The Scan_Buffer will hold the source while we see if it matches
-- the expected name, however to match accoring to the Ada rules,
-- we need to first do simple case folding before checking for a
-- match. Since we don't want to apply that case folding to the
-- source itself, we need to have a parallel case-folded buffer
Scan_Buffer(Match_Depth) := Decode_Next;
Match_Buffer(Match_Depth)
:= Unicode.Case_Folding.Simple (Scan_Buffer(Match_Depth));
if Match_Buffer(Match_Depth) = Expected_Name(Match_Depth) then
if Match_Depth = Scan_Buffer'Last then
-- It is not really worth the effort to check for weird things
-- showing up where this naieve replace might mangle the middle
-- of something that happens to match. It should be rare enough
-- (especially in a manifest) to not often cause a problem.
--
-- The Ada compiler will almost certainly spot it later.
--
-- If we wanted to check for that, we'd really want to bring
-- in the Ada lexical parser since we really should not be
-- handling the various rules ourselves and creating redundant
-- code. However using the parser would mess up our stream.
--
-- We considered possibly "rewriting" the unit via the parser,
-- but since the Configuration unit really should be human
-- -readable and human-changable, this seemed like the wrong
-- approach.
--
-- If this really becomes a problem, we can always make
-- improvements.
UTF_8_String'Write
(Out_Stream,
UTF_8.Encode (Substitute_Name));
Match_Depth := Scan_Buffer'First;
else
-- Keep trying
Match_Depth := Match_Depth + 1;
end if;
else
-- Flush buffer
UTF_8_String'Write
(Out_Stream,
UTF_8.Encode (Scan_Buffer(Scan_Buffer'First .. Match_Depth)));
Match_Depth := Scan_Buffer'First;
end if;
end loop;
exception
when Ada.Streams.Stream_IO.End_Error =>
-- The Ada lexical parser on entry is not used to dig down through the
-- whole spec, lets do a little check for funny business while we're
-- at it (we shouldn't get here if we were in the middle of the
-- Expected_Name)
Assert (Check => Match_Depth = Scan_Buffer'First,
Message => "Unexpected end of manifest");
return;
when others =>
raise;
end Find_And_Replace;
Config_Directory: constant String := Ada.Directories.Compose
(Containing_Directory => Ada.Directories.Current_Directory,
Name => "aura");
procedure Register (Name: in String) is
use Ada.Directories;
Search : Search_Type;
New_Reg: Directory_Entry_Type;
begin
Start_Search (Search => Search,
Directory => Config_Directory,
Pattern => Name);
Assert (Check => More_Entries (Search),
Message => "Error registering configuration unit - cannot find "
& "generated file " & Name & '!');
Get_Next_Entry (Search => Search, Directory_Entry => New_Reg);
Registrar.Registration.Enter_Unit (New_Reg);
End_Search (Search);
end Register;
begin
-- The following could be generic, but the parameters would be pretty
-- complex, and the code is so simple, it doesn't seem worth it.
-- Spec
declare
use Ada.Streams.Stream_IO;
use Registrar.Source_Files;
Spec_Base_Name: constant String
:= "aura-" & Target.Name.To_UTF8_String & ".ads";
Spec_Full_Name: constant String := Ada.Directories.Compose
(Containing_Directory => Config_Directory,
Name => Spec_Base_Name);
File : File_Type;
M_Stream : aliased Source_Stream
:= Checkout_Read_Stream (Manifest.Spec_File);
begin
Create (File => File,
Mode => Out_File,
Name => Spec_Full_Name);
Find_And_Replace (In_Stream => M_Stream'Access,
Out_Stream => Stream (File));
Close (File);
Register (Spec_Base_Name);
end;
if Manifest.Body_File /= null then
-- Body
declare
use Ada.Streams.Stream_IO;
use Registrar.Source_Files;
Body_Base_Name: constant String
:= "aura-" & Target.Name.To_UTF8_String & ".adb";
Body_Full_Name: constant String := Ada.Directories.Compose
(Containing_Directory => Config_Directory,
Name => Body_Base_Name);
File : File_Type;
M_Stream : aliased Source_Stream
:= Checkout_Read_Stream (Manifest.Body_File);
begin
Create (File => File,
Mode => Out_File,
Name => Body_Full_Name);
Find_And_Replace (In_Stream => M_Stream'Access,
Out_Stream => Stream (File));
Close (File);
Register (Body_Base_Name);
end;
end if;
-- Next is Step 3, but we need to wait for the registrar to register the
-- configuration unit before we can process it, so we need to defer
-- Step_3
Workers.Disable_Completion_Reports;
Configuration_Progress.Increment_Total_Items;
Workers.Defer_Order
(Order => Step_3a_Deferral'(Tracker => Configuration_Progress'Access,
Target => Target),
Wait_Tracker => Registrar.Registration.Entry_Progress'Access);
end Step_2;