ocarina-fe_aadl-parser.adb 13.5 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--               O C A R I N A . F E _ A A D L . P A R S E R                --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2020 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify 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. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27
28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
--                                                                          --
------------------------------------------------------------------------------

32
with Locations; use Locations;
yoogx's avatar
yoogx committed
33
34
with Ocarina.Namet;     use Ocarina.Namet;
with Ocarina.Output;    use Ocarina.Output;
35
36
37
38
39
40

with Ocarina.FE_AADL.Lexer;             use Ocarina.FE_AADL.Lexer;
with Ocarina.FE_AADL.Parser.Namespaces; use Ocarina.FE_AADL.Parser.Namespaces;
with Ocarina.ME_AADL.Tokens;            use Ocarina.ME_AADL.Tokens;
with Ocarina.ME_AADL.AADL_Tree.Nodes;   use Ocarina.ME_AADL.AADL_Tree.Nodes;

41
42
43
44
with Ocarina.Files;                    use Ocarina.Files;
with Ocarina.ME_AADL.AADL_Tree.Nutils; use Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.Parser;                   use Ocarina.Parser;
with Ocarina.Property_Sets;            use Ocarina.Property_Sets;
45
46
47
48
49
50
51

with GNAT.OS_Lib;       use GNAT.OS_Lib;

package body Ocarina.FE_AADL.Parser is

   Language : constant String := "aadl";
   Suffix   : constant String := ".aadl";
52
   First_Parsing     : Boolean := True;
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

   procedure Exit_On_Error (Error : Boolean; Reason : String);

   -------------------
   -- Exit_On_Error --
   -------------------

   procedure Exit_On_Error (Error : Boolean; Reason : String) is
   begin
      if Error then
         Set_Standard_Error;
         Write_Line (Reason);
         OS_Exit (1);
      end if;
   end Exit_On_Error;

   ----------
   -- Init --
   ----------

   procedure Init is
   begin
      First_Parsing     := True;
      Ocarina.Parser.Register_Parser (Language, Process'Access);
   end Init;

   ------------------
   -- P_Items_List --
   ------------------

   --  ( { Item }+ | none_statement )

   function P_Items_List
     (Func      : P_Item_Function_Ptr;
      Container : Node_Id;
88
      Code      : Parsing_Code) return Integer
89
   is
90
      Loc      : Location;
91
      Nb_Items : Integer := 0;
92
      Item     : Node_Id;
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

   begin
      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_None then
         if not P_None_Statement then
            return -1;
         end if;
      else
         Restore_Lexer (Loc);
         loop
            Save_Lexer (Loc);
            Item := Func.all (Container);
            if Present (Item) then
               Nb_Items := Nb_Items + 1;
            else
               --  Error when parsing item, restore lexer

               Restore_Lexer (Loc);
               if Nb_Items = 0 then
                  --  list must contain at least one element, {Item}+

                  Nb_Items := -1;
                  DPE (Code, EMC_List_Is_Empty);
               end if;
               exit;
            end if;
         end loop;
      end if;

      return Nb_Items;
   end P_Items_List;

   function P_Items_List
     (Func      : P_Item_Function_Ptr;
      Container : Node_Id;
129
      Code      : Parsing_Code) return List_Id
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
   is
      Loc   : Location;
      Items : List_Id;
      Item  : Node_Id;

   begin
      Save_Lexer (Loc);
      Scan_Token;
      Items := New_List (K_List_Id, Token_Location);
      if Token = T_None then
         if not P_None_Statement then
            return No_List;
         end if;

      else
         Restore_Lexer (Loc);
         loop
            Save_Lexer (Loc);
            Item := Func.all (Container);
            if Present (Item) then
               Append_Node_To_List (Item, Items);
            else
               --  Error when parsing item, restore lexer
               Restore_Lexer (Loc);
               if Is_Empty (Items) then
                  --  list must contain at least one element, { Item }+
                  DPE (Code, EMC_List_Is_Empty);
               end if;
               exit;
            end if;
         end loop;
      end if;

      return Items;
   end P_Items_List;

   function P_Items_List
     (Func         : P_Refinable_Item_Function_Ptr;
      Container    : Node_Id;
      Refinable    : Boolean;
      Code         : Parsing_Code;
171
      At_Least_One : Boolean := True) return Integer
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
   is
      Loc   : Location;
      Item  : Node_Id;
      Items : Integer := 0;

   begin
      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_None then
         if not P_None_Statement then
            return -1;
         end if;

      else
         Restore_Lexer (Loc);
         loop
            Save_Lexer (Loc);
            Item := Func.all (Container, Refinable);
            if Present (Item) then
               Items := Items + 1;
            else
               --  Error when parsing item, restore lexer

               Restore_Lexer (Loc);
               if At_Least_One and then Items = 0 then
                  --  list must contain at least one element, {Item}+

                  DPE (Code, EMC_List_Is_Empty);
                  Items := -1;
               end if;
               exit;
            end if;
         end loop;
      end if;

      return Items;
   end P_Items_List;

   --------------------
   -- P_Element_List --
   --------------------

   --  parse ( { Element }* Element Delimiter )

   function P_Elements_List
217
218
219
     (Func      : P_Item_Function_Ptr;
      Container : Node_Id;
      Delimiter : Ocarina.ME_AADL.Tokens.Token_Type) return List_Id
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
   is
      Element       : Node_Id;
      Elements_List : List_Id;
      Loc           : Location;

   begin
      Elements_List := New_List (K_List_Id, Token_Location);
      loop
         Element := Func.all (Container);
         if Present (Element) then
            Append_Node_To_List (Element, Elements_List);
         else
            Skip_Tokens (Delimiter);
            return No_List;
         end if;

         Save_Lexer (Loc);
         Scan_Token;

         if Token = Delimiter then
            exit;
         else
            Restore_Lexer (Loc);
         end if;
      end loop;

246
247
248
      Set_Loc
        (Node_Id (Elements_List),
         Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node (Elements_List)));
249
250
251
252
253
254
255
256
257
258
      return Elements_List;
   end P_Elements_List;

   ------------------
   -- P_Items_List --
   ------------------

   --  ( { Item Separator }* Item )

   function P_Items_List
259
260
261
     (Func      : P_Item_Function_Ptr;
      Container : Node_Id;
      Separator : Ocarina.ME_AADL.Tokens.Token_Type) return List_Id
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
   is
      Item  : Node_Id;
      Items : List_Id;
      Loc   : Location;

   begin
      Items := New_List (K_List_Id, Token_Location);
      loop
         Item := Func.all (Container);
         if Present (Item) then
            Append_Node_To_List (Item, Items);
         else
            return No_List;
         end if;

         Save_Lexer (Loc);
         Scan_Token;
         if Token /= Separator then
            Restore_Lexer (Loc);
            exit;
         end if;
      end loop;

285
286
287
      Set_Loc
        (Node_Id (Items),
         Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node (Items)));
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
      return Items;
   end P_Items_List;

   ------------------
   -- P_Items_List --
   ------------------

   --  ( [ { Item Separator }* Item Delimiter ] )
   --    or ( [ { Item Separator }* Item Separator Delimiter ] )

   function P_Items_List
     (Func            : P_Item_Function_Ptr;
      Container       : Node_Id;
      Separator       : Ocarina.ME_AADL.Tokens.Token_Type;
      Delimiter       : Ocarina.ME_AADL.Tokens.Token_Type;
      Code            : Parsing_Code;
304
      With_Terminator : Boolean := False) return List_Id
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
   is
      Item      : Node_Id;
      Items     : List_Id;
      Start_Loc : Location;
      Loc       : Location;

   begin
      Items := New_List (K_List_Id, Token_Location);
      loop
         Item := Func.all (Container);
         if Present (Item) then
            Append_Node_To_List (Item, Items);
         else
            return No_List;
         end if;

         Save_Lexer (Start_Loc);
         Scan_Token;

         if Token = Separator then

            if With_Terminator then
               Save_Lexer (Loc);
               Scan_Token;

               if Token = Delimiter then
                  exit;
               end if;
            end if;

         elsif Token = Delimiter then
            exit;

         else
            DPE (Code, (Separator, Delimiter));
            Skip_Tokens (Delimiter);
            return No_List;
         end if;
      end loop;

345
346
347
      Set_Loc
        (Node_Id (Items),
         Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node (Items)));
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
      return Items;
   end P_Items_List;

   ----------------------
   -- P_None_Statement --
   ----------------------

   --  none_statement ::= none ;

   function P_None_Statement return Boolean is
      Loc : Location;

   begin
      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_Semicolon then
         return True;
      else
         DPE (PC_None_Statement, T_Semicolon);
         Restore_Lexer (Loc);
         return False;
      end if;
   end P_None_Statement;

   --------------------------------------
   -- Process_Predefined_Property_Sets --
   --------------------------------------

   function Process_Predefined_Property_Sets
377
     (AADL_Root : Node_Id) return Node_Id
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
   is
      New_Root  : Node_Id := AADL_Root;
      File_Name : Name_Id;
      Buffer    : Location;
   begin
      for J in Ocarina_Property_Sets'Range loop
         Set_Str_To_Name_Buffer (Image (Ocarina_Property_Sets (J)));
         Add_Str_To_Name_Buffer (Suffix);
         File_Name := Search_File (Name_Find);
         if File_Name = No_Name then
            New_Root := No_Node;
            exit;
         end if;
         Buffer := Lexer.Load_File (File_Name);
         if Buffer = No_Location then
            New_Root := No_Node;
            exit;
         end if;
         New_Root := Process (New_Root, Buffer);
      end loop;
398
      Exit_On_Error (No (New_Root), "Cannot parse ocarina property sets");
399
400
401
402
403
404
405
406
407
408
      return New_Root;
   end Process_Predefined_Property_Sets;

   -------------
   -- Process --
   -------------

   function Process
     (AADL_Root : Node_Id;
      From      : Location;
409
410
      To        : Location := No_Location;
      Container : Node_Id  := No_Node) return Node_Id
411
412
413
414
415
   is
      New_Root  : Node_Id := AADL_Root;
      File_Name : Name_Id;
      Buffer    : Location;

416
417
      pragma Unreferenced (Container);

418
419
420
421
422
423
424
425
426
427
428
429
   begin
      if First_Parsing then
         First_Parsing := False;

         --  First parse the files declaring the standard property sets.

         for J in Standard_Property_Sets'Range loop
            Set_Str_To_Name_Buffer (Image (Standard_Property_Sets (J)));
            Add_Str_To_Name_Buffer (Suffix);
            File_Name := Search_File (Name_Find);
            if File_Name = No_Name then
               New_Root := No_Node;
430
               Exit_On_Error
431
432
433
                 (No (New_Root),
                  "Cannot find standard property set " &
                  Image (Standard_Property_Sets (J)));
434
435
436
437
438
439
440
441
442
               exit;
            end if;
            Buffer := Lexer.Load_File (File_Name);
            if Buffer = No_Location then
               New_Root := No_Node;
               exit;
            end if;
            New_Root := Process (New_Root, Buffer);
         end loop;
443
         Exit_On_Error (No (New_Root), "Cannot parse standard property sets");
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

         --  When the user explicitely wants to, parse the files
         --  declaring the Ocarina property sets.

         if Add_Pre_Prop_Sets then
            New_Root := Process_Predefined_Property_Sets (New_Root);
         end if;
      end if;

      Buffer := From;
      if To /= No_Location then
         Buffer.EOF := To.Last_Pos;
      end if;
      Restore_Lexer (Buffer);

      return P_AADL_Specification (New_Root);
   end Process;

end Ocarina.FE_AADL.Parser;