taste-parser_utils.adb 9.48 KB
Newer Older
1
--  *************************** taste aadl parser *************************  --
Maxime Perrotin's avatar
Maxime Perrotin committed
2
3
4
5
--  (c) 2008-2017 European Space Agency - maxime.perrotin@esa.int
--  LGPL license, see LICENSE file

with Ada.Text_IO,
6
7
     Ada.Command_Line,
     GNAT.Directory_Operations,
8
9
     GNAT.OS_Lib,
     GNAT.Command_Line,
10
     Templates_Parser,
Maxime Perrotin's avatar
Maxime Perrotin committed
11
     Ocarina.AADL_Values,
12
13
     Ocarina.Configuration,
     Ocarina.FE_AADL.Parser,
Maxime Perrotin's avatar
Maxime Perrotin committed
14
     Ocarina.Instances.Queries,
15
16
     TASTE.Parser_Version,
     TASTE.Templates;
Maxime Perrotin's avatar
Maxime Perrotin committed
17

18
package body TASTE.Parser_Utils is
Maxime Perrotin's avatar
Maxime Perrotin committed
19
20

   use Ada.Text_IO,
21
       GNAT.Directory_Operations,
22
       GNAT.OS_Lib,
23
       GNAT.Command_Line,
24
       Templates_Parser,
25
       Ocarina.Instances.Queries,
26
27
       Ocarina.ME_AADL,
       TASTE.Templates;
Maxime Perrotin's avatar
Maxime Perrotin committed
28
29
30

   procedure Banner is
      The_Banner : constant String :=
Maxime Perrotin's avatar
Maxime Perrotin committed
31
        Yellow_Bold & "TASTE AADL Parser" & No_Color & " (Version "
32
33
34
        & TASTE.Parser_Version.Parser_Release & ")"
        & ASCII.LF & ASCII.CR & White_Bold
        & "Copyright (C) European Space Agency"
Maxime Perrotin's avatar
Maxime Perrotin committed
35
        & ASCII.LF & ASCII.CR & No_Color
36
        & "Based on " & TASTE.Parser_Version.Ocarina_Version;
Maxime Perrotin's avatar
Maxime Perrotin committed
37
38
   begin
      Put_Line (The_Banner);
39
      New_Line;
Maxime Perrotin's avatar
Maxime Perrotin committed
40
41
   end Banner;

Maxime Perrotin's avatar
Maxime Perrotin committed
42
   procedure Parse_Command_Line (Result : out Taste_Configuration) is
43
44
      Config : Command_Line_Configuration;
   begin
45
46
      Result.Binary_Path :=
          new String'(Dir_Name (Ada.Command_Line.Command_Name));
47
48
49
50
51
52
53
54
55
56
57
58
      Define_Switch (Config, Output => Result.Interface_View'Access,
                     Switch   => "-i:", Long_Switch => "--interfaceview=",
                     Help     => "Mandatory interface view (AADL model)",
                     Argument => "InterfaceView.aadl");
      Define_Switch (Config, Output => Result.Deployment_View'Access,
                     Switch   => "-c:", Long_Switch => "--deploymentview=",
                     Help     => "Optional deployment view (AADL model)",
                     Argument => "DeploymentView.aadl");
      Define_Switch (Config, Output => Result.Data_View'Access,
                     Switch   => "-d:", Long_Switch => "--dataview=",
                     Help     => "Optional data view (AADL model)",
                     Argument => "DataView.aadl");
Maxime Perrotin's avatar
Maxime Perrotin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      Define_Switch (Config, Output => Result.Output_Dir'Access,
                     Switch   => "-o:", Long_Switch => "--output=",
                     Help     => "Output directory (to be created if absent)",
                     Argument => "Folder");
      Define_Switch (Config, Output => Result.Skeletons'Access,
                     Switch   => "-w", Long_Switch => "--gw",
                     Help     => "Generate models and code skeletons");
      Define_Switch (Config, Output => Result.Glue'Access,
                     Switch   => "-l", Long_Switch => "--glue",
                     Help     => "Generate glue code");
      Define_Switch (Config, Output => Result.Use_POHIC'Access,
                     Switch   => "-p", Long_Switch => "--polyorb-hi-c",
                     Help     => "Use PolyORB-HI-C runtime in place of Ada");
      Define_Switch (Config, Output => Result.Timer_Resolution'Access,
73
74
                     Switch   => "-x:", Long_Switch => "--timer=",
                     Initial  => 100,
Maxime Perrotin's avatar
Maxime Perrotin committed
75
76
77
78
79
80
81
                     Help     => "Specify timer resolution (default 100 ms)");
      Define_Switch (Config, Output => Result.Debug_Flag'Access,
                     Switch   => "-g", Long_Switch => "--debug",
                     Help     => "Set debug mode");
      Define_Switch (Config, Output => Result.Version'Access,
                     Switch   => "-v", Long_Switch => "--version",
                     Help     => "Display tool version");
82
      Getopt (Config);
Maxime Perrotin's avatar
Maxime Perrotin committed
83

84
85
86
87
88
      loop
         declare
            S : constant String := Get_Argument;
         begin
            exit when S'Length = 0;
Maxime Perrotin's avatar
Maxime Perrotin committed
89
            Result.Other_Files.Append (S);
90
91
         end;
      end loop;
Maxime Perrotin's avatar
Maxime Perrotin committed
92

Maxime Perrotin's avatar
Maxime Perrotin committed
93
94
95
      if Result.Version then
         raise Exit_From_Command_Line;
      end if;
96
97
   end Parse_Command_Line;

98
   procedure Debug_Dump (Config : Taste_Configuration) is
99
      Vec : Tag;
Maxime Perrotin's avatar
Maxime Perrotin committed
100
   begin
101
      Put_Line ("Command line (raw dump):");
Maxime Perrotin's avatar
Maxime Perrotin committed
102
103
104
105
106
107
108
109
110
111
112
113
114
      Put_Line ("  |_ Interface View  : " & Config.Interface_View.all);
      Put_Line ("  |_ Deployment View : " & Config.Deployment_View.all);
      Put_Line ("  |_ Data View       : " & Config.Data_View.all);
      Put_Line ("  |_ Output Dir      : " & Config.Output_Dir.all);
      Put_Line ("  |_ Use POHIC       : " & Config.Use_POHIC'Img);
      Put_Line ("  |_ Glue            : " & Config.Use_POHIC'Img);
      Put_Line ("  |_ Skeletons       : " & Config.Skeletons'Img);
      Put_Line ("  |_ Timer Res       : " & Config.Timer_Resolution'Img);
      Put_Line ("  |_ Version         : " & Config.Version'Img);
      Put_Line ("  |_ Debug           : " & Config.Debug_Flag'Img);
      for Each of Config.Other_Files loop
         Put_Line ("  |_ Other file      : " & Each);
      end loop;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

      Put_Line ("Dump generated from a template file:");
      New_Set;
      Tmpl_Map ("Interface_View",  Config.Interface_View.all);
      Tmpl_Map ("Deployment_View", Config.Deployment_View.all);
      Tmpl_Map ("Data_View", Config.Data_View.all);
      Tmpl_Map ("Output_Dir", Config.Output_Dir.all);
      for Each of Config.Other_Files loop
         Vec := Vec & Each;
      end loop;
      Tmpl_Map ("Other_Files",      Vec);
      Tmpl_Map ("Skeletons",        Config.Skeletons);
      Tmpl_Map ("Glue",             Config.Glue);
      Tmpl_Map ("Use_POHIC",        Config.Use_POHIC);
      Tmpl_Map ("Debug_Flag",       Config.Debug_Flag);
      Tmpl_Map ("Version",          Config.Version);
      Tmpl_Map ("Timer_Resolution", Config.Timer_Resolution);
      Put_Line (Generate (Config.Binary_Path.all
                          & "templates/configuration.tmplt"));
134
   end Debug_Dump;
Maxime Perrotin's avatar
Maxime Perrotin committed
135

Maxime Perrotin's avatar
Maxime Perrotin committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
   -----------------------
   -- Get_APLC_Binding --
   -----------------------

   function Get_APLC_Binding (E : Node_Id) return List_Id is
      APLC_Binding : constant Name_Id :=
          Get_String_Name ("taste::aplc_binding");
   begin
      if Is_Defined_Property (E, APLC_Binding) then
         return Get_List_Property (E, APLC_Binding);
      else
         return No_List;
      end if;
   end Get_APLC_Binding;

151
152
153
154
155
156
157
158
159
160
161
   ------------------------
   -- Get_Interface_Name --
   ------------------------

   function Get_Interface_Name (D : Node_Id) return Name_Id is
      Interface_Name : constant Name_id :=
         Get_String_Name ("taste::interfacename");
   begin
      return Get_String_Property (D, Interface_Name);
   end Get_Interface_Name;

Maxime Perrotin's avatar
Maxime Perrotin committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
   --------------------------------------------
   -- Get all properties as a Map Key/String --
   -- Input parameter is an AADL instance    --
   --------------------------------------------
   function Get_Properties_Map (D : Node_Id) return Property_Maps.Map is
      properties : constant List_Id  := AIN.Properties (D);
      result     : Property_Maps.Map := Property_Maps.Empty_Map;
      property   : Node_Id           := AIN.First_Node (properties);
      prop_value : Node_Id;
      single_val : Node_Id;
   begin
      while Present (property) loop
         prop_value := AIN.Property_Association_Value (property);
         if Present (ATN.Single_Value (prop_value)) then
            --  Only support single-value properties for now
            single_val := ATN.Single_Value (prop_value);
            result.Insert (Key => AIN_Case (property),
179
180
181
                           New_Item => (Name  => US (AIN_Case (property)),
                                        Value =>
              US (case ATN.Kind (single_val) is
Maxime Perrotin's avatar
Maxime Perrotin committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
                 when ATN.K_Signed_AADLNumber =>
                   Ocarina.AADL_Values.Image
                      (ATN.Value (ATN.Number_Value (single_val))) &
                      (if Present (ATN.Unit_Identifier (single_val)) then " " &
                      Get_Name_String
                          (ATN.Display_Name (ATN.Unit_Identifier (single_val)))
                      else ""),
                 when ATN.K_Literal =>
                    Ocarina.AADL_Values.Image (ATN.Value (single_val),
                                               Quoted => False),
                 when ATN.K_Reference_Term =>
                    Get_Name_String
                       (ATN.Display_Name (ATN.First_Node --  XXX must iterate
                          (ATN.List_Items (ATN.Reference_Term (single_val))))),
                 when ATN.K_Enumeration_Term =>
                    Get_Name_String
                       (ATN.Display_Name (ATN.Identifier (single_val))),
                 when ATN.K_Number_Range_Term =>
                    "RANGE NOT SUPPORTED!",
                 when others => "ERROR! Unsupported kind: "
202
                                & ATN.Kind (single_val)'Img)));
Maxime Perrotin's avatar
Maxime Perrotin committed
203
204
205
206
207
208
         end if;
         property := AIN.Next_Node (property);
      end loop;
      return result;
   end Get_Properties_Map;

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
   --  Initialization step: we look for ocarina on path to define
   --  OCARINA_PATH env. variable. This will indicate Ocarina librrary
   --  where to find AADL default property sets, and Ocarina specific
   --  packages and property sets.
   procedure Initialize_Ocarina is
      S : constant GNAT.OS_Lib.String_Access :=
        GNAT.OS_Lib.Locate_Exec_On_Path ("ocarina");
   begin
      if S = null then
         raise AADL_Parser_Error with "Ocarina is not in your PATH";
      end if;
      GNAT.OS_Lib.Setenv ("OCARINA_PATH", S.all (S'First .. S'Last - 12));
      Ocarina.Initialize;
      Ocarina.AADL_Version := Ocarina.AADL_V2;
      Ocarina.Configuration.Init_Modules;
      Ocarina.FE_AADL.Parser.Add_Pre_Prop_Sets := True;
   end Initialize_Ocarina;
226
end TASTE.Parser_Utils;