taste-aadl_parser.adb 9.74 KB
Newer Older
1
2
--  ************************ TASTE AADL Parser **************************  --
--  Based on Ocarina ****************************************************  --
3
--  (c) 2018 European Space Agency - maxime.perrotin@esa.int
4
5
--  LGPL license, see LICENSE file

6
with System.Assertions,
7
8
     Ada.Exceptions,
     Ada.Text_IO,
9
10
     Ada.Directories,
     GNAT.Command_Line,
11
12
13
14
15
     Errors,
     Locations,
     Ocarina.Namet,
     Ocarina.Configuration,
     Ocarina.Files,
16
     Ocarina.Parser,
17
     Ocarina.FE_AADL.Parser,
Maxime Perrotin's avatar
Maxime Perrotin committed
18
19
     TASTE.Backend.Build_Script,
     TASTE.Backend.Skeletons;
20
21
22

use Ada.Text_IO,
    Ada.Exceptions,
23
    Ada.Directories,
24
25
26
27
28
29
30
    Locations,
    Ocarina.Namet,
    Ocarina;

package body TASTE.AADL_Parser is

   function Initialize return Taste_Configuration is
Maxime Perrotin's avatar
Maxime Perrotin committed
31
32
33
      File_Name  : Name_Id;
      File_Descr : Location;
      Cfg        : Taste_Configuration;
34
35
36
37
   begin
      Banner;
      --  Parse arguments before initializing Ocarina, otherwise Ocarina eats
      --  some arguments (all file parameters).
Maxime Perrotin's avatar
Maxime Perrotin committed
38
      Parse_Command_Line (Cfg);
39
40
41
42
      Initialize_Ocarina;

      AADL_Language := Get_String_Name ("aadl");

Maxime Perrotin's avatar
Maxime Perrotin committed
43
      if Cfg.Interface_View.all'Length = 0 and not Cfg.Check_Data_View then
Maxime Perrotin's avatar
Maxime Perrotin committed
44
45
46
         --  Use "InterfaceView.aadl" by default, if nothing else is specified
         --  and if the tool is not only called to check the data view
         Cfg.Interface_View := Default_Interface_View'Access;
47
48
      end if;

Maxime Perrotin's avatar
Maxime Perrotin committed
49
50
51
      --  An interface view is expected, look for it and parse it
      if Cfg.Interface_View.all'Length > 0 then
         Set_Str_To_Name_Buffer (Cfg.Interface_View.all);
52

Maxime Perrotin's avatar
Maxime Perrotin committed
53
54
55
56
57
58
59
         File_Name := Ocarina.Files.Search_File (Name_Find);
         if File_Name = No_Name then
            raise AADL_Parser_Error
              with "File not found : " & Cfg.Interface_View.all;
         end if;

         File_Descr := Ocarina.Files.Load_File (File_Name);
60

Maxime Perrotin's avatar
Maxime Perrotin committed
61
62
         Interface_Root := Ocarina.Parser.Parse
           (AADL_Language, Interface_Root, File_Descr);
63

Maxime Perrotin's avatar
Maxime Perrotin committed
64
65
66
67
68
69
70
         --  Parse TASTE_IV_Properties.aadl
         Set_Str_To_Name_Buffer ("TASTE_IV_Properties.aadl");
         File_Name  := Ocarina.Files.Search_File (Name_Find);
         File_Descr := Ocarina.Files.Load_File (File_Name);
         Interface_Root := Ocarina.Parser.Parse (AADL_Language,
                                                 Interface_Root, File_Descr);
      end if;
71

Maxime Perrotin's avatar
Maxime Perrotin committed
72
73
74
75
76
      if Cfg.Glue then
         --  Look for a deployment view (or DeploymentView.aadl by default)
         --  if the glue generation is requested. Not needed for skeletons.
         if Cfg.Deployment_View.all'Length = 0 then
            Cfg.Deployment_View := Default_Deployment_View'Access;
77
78
         end if;

Maxime Perrotin's avatar
Maxime Perrotin committed
79
         Set_Str_To_Name_Buffer (Cfg.Deployment_View.all);
80
81
82
83

         File_Name := Ocarina.Files.Search_File (Name_Find);
         if File_Name = No_Name then
            raise AADL_Parser_Error
Maxime Perrotin's avatar
Maxime Perrotin committed
84
              with "File not found : " & Cfg.Deployment_View.all;
85
86
87
88
89
90
91
92
93
94
95
         end if;

         File_Descr := Ocarina.Files.Load_File (File_Name);
         Deployment_Root := Ocarina.Parser.Parse
           (AADL_Language, Deployment_Root, File_Descr);

         if Deployment_Root = No_Node then
            raise AADL_Parser_Error with "Deployment View is incorrect";
         end if;
      end if;

Maxime Perrotin's avatar
Maxime Perrotin committed
96
97
98
      for Each of Cfg.Other_Files loop
         --  Add other files to the Interface and (if any) deployment roots
         --  (List of files specified in the command line)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
         Set_Str_To_Name_Buffer (Each);
         File_Name := Ocarina.Files.Search_File (Name_Find);
         if File_Name = No_Name then
            raise AADL_Parser_Error with "File not found: " & Each;
         end if;
         File_Descr := Ocarina.Files.Load_File (File_Name);

         Interface_Root := Ocarina.Parser.Parse
           (AADL_Language, Interface_Root, File_Descr);
         if Deployment_Root /= No_Node then
            Deployment_Root := Ocarina.Parser.Parse
              (AADL_Language, Deployment_Root, File_Descr);
         end if;
      end loop;

Maxime Perrotin's avatar
Maxime Perrotin committed
114
115
      if Cfg.Data_View.all'Length > 0 then
         Set_Str_To_Name_Buffer (Cfg.Data_View.all);
116
117
         File_Name := Ocarina.Files.Search_File (Name_Find);
         if File_Name = No_Name then
Maxime Perrotin's avatar
Maxime Perrotin committed
118
            raise AADL_Parser_Error with "Could not find " & Cfg.Data_View.all;
119
120
         end if;
      else
Maxime Perrotin's avatar
Maxime Perrotin committed
121
         --  Try with default name (DataView.aadl)
122
123
124
         Set_Str_To_Name_Buffer (Default_Data_View);
         File_Name := Ocarina.Files.Search_File (Name_Find);
         if File_Name /= No_Name then
Maxime Perrotin's avatar
Maxime Perrotin committed
125
126
127
128
            Cfg.Data_View := Default_Data_View'Access;
         elsif Cfg.Check_Data_View then
            --  No dataview found, while user asked explicitly for a check
            raise AADL_Parser_Error with "Could not find DataView.aadl";
129
130
131
132
         end if;
      end if;

      if File_Name /= No_Name then
Maxime Perrotin's avatar
Maxime Perrotin committed
133
134
         Put_Info ("Parsing " & Cfg.Data_View.all);

135
136
         File_Descr := Ocarina.Files.Load_File (File_Name);

Maxime Perrotin's avatar
Maxime Perrotin committed
137
138
139
140
141
         --  Add the Data View to the Interface View root, if any
         if Interface_Root /= No_Node then
            Interface_Root := Ocarina.Parser.Parse
              (AADL_Language, Interface_Root, File_Descr);
         end if;
142
143
144
145
146
147

         --  Add the Data View to the Deployment View root, if any
         if Deployment_Root /= No_Node then
            Deployment_Root := Ocarina.Parser.Parse
               (AADL_Language, Deployment_Root, File_Descr);
         end if;
148
         --  Also parse the data view as a root component
149
         Ocarina.FE_AADL.Parser.Add_Pre_Prop_Sets := False;
150
151
152
         Dataview_root := Ocarina.Parser.Parse
                            (AADL_Language, Dataview_root, File_Descr);
      end if;
Maxime Perrotin's avatar
Maxime Perrotin committed
153
      return Cfg;
154
155
156
157
158
159
160
   end Initialize;

   function Parse_Project return TASTE_Model is
      Result : TASTE_Model;
   begin
      Result.Configuration := Initialize;

Maxime Perrotin's avatar
Maxime Perrotin committed
161
162
163
164
165
166
167
168
      if Interface_Root /= No_Node then
         --  Parse Interface and Deployment View
         begin
            Result.Interface_View := Parse_Interface_View (Interface_Root);
         exception
            when System.Assertions.Assert_Failure =>
               raise AADL_Parser_Error with "Interface view parsing error";
         end;
169

Maxime Perrotin's avatar
Maxime Perrotin committed
170
171
172
173
         if Result.Configuration.Deployment_View.all'Length > 0 then
            AADL_Lib.Append (Result.Configuration.Interface_View.all);
            Result.Deployment_View := Parse_Deployment_View (Deployment_Root);
         end if;
174
175
      end if;

176
177
178
      if Result.Configuration.Data_View.all'Length > 0 then
         begin
            Result.Data_View := Parse_Data_View (Dataview_root);
179
            Result.Data_View.Check_Files;
180
181
182
183
184
         exception
            when Constraint_Error =>
               raise Data_View_Error with "Update your data view!";
         end;
      end if;
185

186
187
188
      Ocarina.Configuration.Reset_Modules;
      Ocarina.Reset;

Maxime Perrotin's avatar
Maxime Perrotin committed
189
190
191
192
      if Result.Configuration.Check_Data_View then
         raise Quit_Taste;
      end if;

193
194
195
196
197
198
199
      return Result;
   exception
      when Error : AADL_Parser_Error
         | Interface_Error
         | Function_Error
         | No_RCM_Error
         | Deployment_View_Error
200
         | Data_View_Error
201
         | Device_Driver_Error =>
202
         Put_Error (Exception_Message (Error));
203
204
205
         raise Quit_Taste;
      when GNAT.Command_Line.Exit_From_Command_Line =>
         New_Line;
206
207
         Put_Info ("For more information, visit " & Underline & White_Bold
                   & "https://taste.tools");
208
209
210
211
         raise Quit_Taste;
      when GNAT.Command_Line.Invalid_Switch
         | GNAT.Command_Line.Invalid_Parameter
         | GNAT.Command_Line.Invalid_Section =>
212
         Put_Error ("Invalid switch or parameter (try --help)" & No_Color);
213
         raise Quit_Taste;
Maxime Perrotin's avatar
Maxime Perrotin committed
214
215
      when Quit_Taste =>
         raise;
216
217
218
219
220
221
      when E : others =>
         Errors.Display_Bug_Box (E);
         raise Quit_Taste;
   end Parse_Project;

   procedure Dump (Model : TASTE_Model) is
222
223
224
      Output_Path : constant String := Model.Configuration.Output_Dir.all
                      & "/Debug";
      Output  : File_Type;
225
   begin
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      if Model.Configuration.Debug_Flag then
         Create_Path (Output_Path);
         Create (File => Output,
                 Mode => Out_File,
                 Name => Output_Path & "/InterfaceView.dump");
         Put_Info ("Dump of the Interface View");
         Model.Interface_View.Debug_Dump (Output);
         Close (Output);

         if Model.Configuration.Deployment_View.all'Length > 0 then
            Create (File => Output,
                    Mode => Out_File,
                    Name => Output_Path & "/DeploymentView.dump");
            Put_Info ("Dump of the Deployment View");
            Model.Deployment_View.Debug_Dump (Output);
            Close (Output);
         end if;
         Create (File => Output,
                 Mode => Out_File,
                 Name => Output_Path & "/DataView.dump");
         Put_Info ("Dump of the Data View");
Maxime Perrotin's avatar
Maxime Perrotin committed
247
         Model.Data_View.Debug_Dump (Output);
248
249
250
251
252
         Close (Output);
         Create (File => Output,
                 Mode => Out_File,
                 Name => Output_Path & "/commandline.dump");
         Put_Info ("Dump of the Command Line");
Maxime Perrotin's avatar
Maxime Perrotin committed
253
         Model.Configuration.Debug_Dump (Output);
254
         Close (Output);
255
      end if;
256
257
258
259
   exception
      when Error : others =>
         Put_Error ("Debug Dump : " & Exception_Message (Error));
         raise Quit_Taste;
260
261
   end Dump;

262
263
264
265
266
   procedure Generate_Build_Script (Model : TASTE_Model) is
   begin
      TASTE.Backend.Build_Script.Generate (Model);
   end Generate_Build_Script;

Maxime Perrotin's avatar
Maxime Perrotin committed
267
268
269
   procedure Generate_Skeletons (Model : TASTE_Model) is
   begin
      TASTE.Backend.Skeletons.Generate (Model);
270
271
272
273
   exception
      when Error : TASTE.Backend.Skeletons.Skeleton_Error =>
         Put_Error (Exception_Message (Error));
         raise Quit_Taste;
274
275
      when Error : others =>
         Errors.Display_Bug_Box (Error);
276
         raise Quit_Taste;
Maxime Perrotin's avatar
Maxime Perrotin committed
277
   end Generate_Skeletons;
278
end TASTE.AADL_Parser;