Natools

Check-in [59e052eab5]
Login
Overview
Comment:static_hash_maps: add a new option for pragma Pure or Preelaborate on generated packages
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 59e052eab5e47c20dc45874d4ecb8190078c5655
User & Date: nat on 2014-05-30 20:11:23
Other Links: manifest | tags
Context
2014-05-31
15:04
static_hash_maps-s_expressions: add binding for the new package categorization option check-in: cf4a28a612 user: nat tags: trunk
2014-05-30
20:11
static_hash_maps: add a new option for pragma Pure or Preelaborate on generated packages check-in: 59e052eab5 user: nat tags: trunk
2014-05-29
18:36
static_hash_maps-s_expressions-tests: new test around generated function for S-expression interpreter check-in: e3eb7b3d2d user: nat tags: trunk
Changes

Modified src/natools-static_hash_maps.adb from [dedb598c26] to [d09dd75265].

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
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
with Ada.Characters.Handling;

with Ada.Directories;
with Ada.Text_IO;

with GNAT.Perfect_Hash_Generators;

package body Natools.Static_Hash_Maps is









   function File_Name (Package_Name : in String) return String;
      --  Convert a package name into a file name, the GNAT way

   function Image (Pos : Natural) return String;
      --  Trimmed image, for suffix construction

   function Image (Offset : Ada.Calendar.Time_Zones.Time_Offset) return String;






   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type);
   procedure Write_Map_Hash_Package (Map : in Map_Description);
   procedure Write_Map_Private_Spec







>






>
>
>
>
>
>
>
>








>
>
>
>
>







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
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
with Ada.Characters.Handling;
with Ada.Containers.Indefinite_Doubly_Linked_Lists;
with Ada.Directories;
with Ada.Text_IO;

with GNAT.Perfect_Hash_Generators;

package body Natools.Static_Hash_Maps is

   package String_Lists is new Ada.Containers.Indefinite_Doubly_Linked_Lists
     (String);


   procedure Add_Categorization
     (Path : in String;
      Categorization : in Package_Categorization);

   function File_Name (Package_Name : in String) return String;
      --  Convert a package name into a file name, the GNAT way

   function Image (Pos : Natural) return String;
      --  Trimmed image, for suffix construction

   function Image (Offset : Ada.Calendar.Time_Zones.Time_Offset) return String;

   procedure Put_Categorization
     (Output : in Ada.Text_IO.File_Type;
      Categorization : in Package_Categorization;
      Name : in String := "");

   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type);
   procedure Write_Map_Hash_Package (Map : in Map_Description);
   procedure Write_Map_Private_Spec
61
62
63
64
65
66
67




































68
69
70
71
72
73
74
      File : in Ada.Text_IO.File_Type);
      --  Output test loop for the hash function


   ------------------------
   -- Package Generators --
   ------------------------





































   function File_Name (Package_Name : in String) return String is
      Result : String := Ada.Characters.Handling.To_Lower (Package_Name);
   begin
      for I in Result'Range loop
         if Result (I) = '.' then
            Result (I) := '-';







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
      File : in Ada.Text_IO.File_Type);
      --  Output test loop for the hash function


   ------------------------
   -- Package Generators --
   ------------------------

   procedure Add_Categorization
     (Path : in String;
      Categorization : in Package_Categorization)
   is
      File : Ada.Text_IO.File_Type;
      Matches : Natural := 0;
      Contents : String_Lists.List;
   begin
      if Categorization = Default_Categorization then
         return;
      end if;

      Ada.Text_IO.Open (File, Ada.Text_IO.In_File, Path);
      while not Ada.Text_IO.End_Of_File (File) loop
         Contents.Append (Ada.Text_IO.Get_Line (File));
      end loop;
      Ada.Text_IO.Close (File);

      Ada.Text_IO.Open (File, Ada.Text_IO.Out_File, Path);
      for Line of Contents loop
         Ada.Text_IO.Put_Line (File, Line);

         if Line'Length >= 8
           and then Line (Line'First .. Line'First + 7) = "package "
           and then Line (Line'Last - 2 .. Line'Last) = " is"
         then
            Matches := Matches + 1;
            Put_Categorization (File, Categorization);
         end if;
      end loop;
      Ada.Text_IO.Close (File);

      pragma Assert (Matches = 1);
   end Add_Categorization;


   function File_Name (Package_Name : in String) return String is
      Result : String := Ada.Characters.Handling.To_Lower (Package_Name);
   begin
      for I in Result'Range loop
         if Result (I) = '.' then
            Result (I) := '-';
102
103
104
105
106
107
108






































109
110
111
112
113
114
115
      return String'(1 => Sign,
         2 => Character'Val (48 + H / 10),
         3 => Character'Val (48 + H mod 10),
         4 => Character'Val (48 + M / 10),
         5 => Character'Val (48 + M mod 10));
   end Image;








































   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type) is
   begin
      Ada.Text_IO.Put_Line







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
      return String'(1 => Sign,
         2 => Character'Val (48 + H / 10),
         3 => Character'Val (48 + H mod 10),
         4 => Character'Val (48 + M / 10),
         5 => Character'Val (48 + M mod 10));
   end Image;


   procedure Put_Categorization
     (Output : in Ada.Text_IO.File_Type;
      Categorization : in Package_Categorization;
      Name : in String := "")
   is
      function Prefix return String;
      function Suffix return String;

      function Prefix return String is
      begin
         if Name = "" then
            return "   pragma ";
         else
            return "pragma ";
         end if;
      end Prefix;

      function Suffix return String is
      begin
         if Name = "" then
            return "";
         else
            return " (" & Name & ')';
         end if;
      end Suffix;
   begin
      case Categorization is
         when Pure =>
            Ada.Text_IO.Put_Line (Output, Prefix & "Pure" & Suffix & ';');
         when Preelaborate =>
            Ada.Text_IO.Put_Line
              (Output, Prefix & "Preelaborate" & Suffix & ';');
         when Default_Categorization =>
            null;
      end case;
   end Put_Categorization;


   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type) is
   begin
      Ada.Text_IO.Put_Line
305
306
307
308
309
310
311






312
313
314
315
316
317
318

      procedure Process (Element : in Map_Description) is
         Prefix : constant String := "Map_" & Image (Map_Pos + 1);
      begin
         case Current_Stage is
            when Hash_Package =>
               Write_Map_Hash_Package (Element);






            when Public_Spec =>
               Write_Map_Public_Spec (Element, Spec_File);
            when Private_Spec =>
               Ada.Text_IO.New_Line (Spec_File);
               Write_Map_Private_Spec (Element, Prefix, Spec_File);
            when Body_With =>
               Write_Map_With (Element, Body_File);







>
>
>
>
>
>







393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

      procedure Process (Element : in Map_Description) is
         Prefix : constant String := "Map_" & Image (Map_Pos + 1);
      begin
         case Current_Stage is
            when Hash_Package =>
               Write_Map_Hash_Package (Element);
               Add_Categorization
                 (Ada.Directories.Compose
                    ("",
                     File_Name (To_String (Element.Hash_Package_Name)),
                     "ads"),
                  Pkg.Categorization);
            when Public_Spec =>
               Write_Map_Public_Spec (Element, Spec_File);
            when Private_Spec =>
               Ada.Text_IO.New_Line (Spec_File);
               Write_Map_Private_Spec (Element, Prefix, Spec_File);
            when Body_With =>
               Write_Map_With (Element, Body_File);
363
364
365
366
367
368
369

370
371
372
373
374
375
376
            Name : constant String
              := To_String (Pkg.Name)
               & '.'
               & To_String (Pkg.Test_Child);
         begin
            Ada.Text_IO.Put_Line (Spec_File, "function " & Name);
            Ada.Text_IO.Put_Line (Spec_File, "  return Boolean;");


            Current_Stage := Body_With;
            Map_Pos := 0;
            Pkg.Maps.Iterate (Query'Access);

            Ada.Text_IO.Put_Line (Body_File, "function " & Name);
            Ada.Text_IO.Put_Line (Body_File, "  return Boolean is");







>







457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
            Name : constant String
              := To_String (Pkg.Name)
               & '.'
               & To_String (Pkg.Test_Child);
         begin
            Ada.Text_IO.Put_Line (Spec_File, "function " & Name);
            Ada.Text_IO.Put_Line (Spec_File, "  return Boolean;");
            Put_Categorization (Spec_File, Pkg.Categorization, Name);

            Current_Stage := Body_With;
            Map_Pos := 0;
            Pkg.Maps.Iterate (Query'Access);

            Ada.Text_IO.Put_Line (Body_File, "function " & Name);
            Ada.Text_IO.Put_Line (Body_File, "  return Boolean is");
388
389
390
391
392
393
394

395
396
397
398
399
400
401
      end if;

      if Pkg.Priv then
         Ada.Text_IO.Put (Spec_File, "private ");
      end if;
      Ada.Text_IO.Put_Line
        (Spec_File, "package " & To_String (Pkg.Name) & " is");

      Ada.Text_IO.New_Line (Spec_File);

      declare
         Declarations : constant String := To_String (Pkg.Extra_Declarations);
      begin
         if Declarations'Length > 0 then
            Ada.Text_IO.Put_Line (Spec_File, Declarations);







>







483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
      end if;

      if Pkg.Priv then
         Ada.Text_IO.Put (Spec_File, "private ");
      end if;
      Ada.Text_IO.Put_Line
        (Spec_File, "package " & To_String (Pkg.Name) & " is");
      Put_Categorization (Spec_File, Pkg.Categorization);
      Ada.Text_IO.New_Line (Spec_File);

      declare
         Declarations : constant String := To_String (Pkg.Extra_Declarations);
      begin
         if Declarations'Length > 0 then
            Ada.Text_IO.Put_Line (Spec_File, Declarations);
562
563
564
565
566
567
568








569
570
571
572
573
574
575

   procedure Close (Self : in out Map_Package) is
   begin
      Self.Name := Hold ("");
      Self.Maps.Clear;
   end Close;










   procedure Set_Description
     (Self : in out Map_Package;
      Description : in String) is
   begin
      Self.Description := Hold (Description);
   end Set_Description;







>
>
>
>
>
>
>
>







658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679

   procedure Close (Self : in out Map_Package) is
   begin
      Self.Name := Hold ("");
      Self.Maps.Clear;
   end Close;


   procedure Set_Categorization
     (Self : in out Map_Package;
      Categorization : in Package_Categorization) is
   begin
      Self.Categorization := Categorization;
   end Set_Categorization;


   procedure Set_Description
     (Self : in out Map_Package;
      Description : in String) is
   begin
      Self.Description := Hold (Description);
   end Set_Description;
692
693
694
695
696
697
698
699
      for I in Maps'Range loop
         Add_Map (Object, Maps (I));
      end loop;
      Commit (Object);
   end Generate_Package;

end Natools.Static_Hash_Maps;








<
796
797
798
799
800
801
802

      for I in Maps'Range loop
         Add_Map (Object, Maps (I));
      end loop;
      Commit (Object);
   end Generate_Package;

end Natools.Static_Hash_Maps;

Modified src/natools-static_hash_maps.ads from [dfc823b5d2] to [b53cc2dad2].

22
23
24
25
26
27
28


29
30
31
32
33
34
35
------------------------------------------------------------------------------

private with Ada.Strings.Unbounded;
private with Ada.Containers.Doubly_Linked_Lists;

package Natools.Static_Hash_Maps is



   type Map_Node is private;

   function Node (Key, Name : String) return Map_Node;

   type Node_Array is array (Positive range <>) of Map_Node;

   type Map_Description is private;







>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
------------------------------------------------------------------------------

private with Ada.Strings.Unbounded;
private with Ada.Containers.Doubly_Linked_Lists;

package Natools.Static_Hash_Maps is

   type Package_Categorization is (Pure, Preelaborate, Default_Categorization);

   type Map_Node is private;

   function Node (Key, Name : String) return Map_Node;

   type Node_Array is array (Positive range <>) of Map_Node;

   type Map_Description is private;
81
82
83
84
85
86
87




88
89
90
91
92
93
94

   procedure Open
     (Self : in out Map_Package;
      Name : in String;
      Private_Child : in Boolean := False);
      --  Reset Self and initialize it with the givan package Name





   procedure Set_Description
     (Self : in out Map_Package;
      Description : in String);

   procedure Set_Extra_Declarations
     (Self : in out Map_Package;
      Declarations : in String);







>
>
>
>







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

   procedure Open
     (Self : in out Map_Package;
      Name : in String;
      Private_Child : in Boolean := False);
      --  Reset Self and initialize it with the givan package Name

   procedure Set_Categorization
     (Self : in out Map_Package;
      Categorization : in Package_Categorization);

   procedure Set_Description
     (Self : in out Map_Package;
      Description : in String);

   procedure Set_Extra_Declarations
     (Self : in out Map_Package;
      Declarations : in String);
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165


   package Map_Lists is new Ada.Containers.Doubly_Linked_Lists
     (Map_Description);

   type Map_Package is record
      Name : String_Holder;

      Description : String_Holder;
      Extra_Declarations : String_Holder;
      Test_Child : String_Holder;
      Priv : Boolean;
      Maps : Map_Lists.List;
   end record;

end Natools.Static_Hash_Maps;







>








157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172


   package Map_Lists is new Ada.Containers.Doubly_Linked_Lists
     (Map_Description);

   type Map_Package is record
      Name : String_Holder;
      Categorization : Package_Categorization := Default_Categorization;
      Description : String_Holder;
      Extra_Declarations : String_Holder;
      Test_Child : String_Holder;
      Priv : Boolean;
      Maps : Map_Lists.List;
   end record;

end Natools.Static_Hash_Maps;