ocarina-fe_aadl-parser.adb 14.4 KB
Newer Older
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
------------------------------------------------------------------------------
--                                                                          --
--                           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                                  --
--                                                                          --
--               Copyright (C) 2008-2009, GET-Telecom Paris.                --
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. 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. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Locations;                         use Locations;
with Namet;                             use Namet;
with Output;                            use Output;

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;

with Ocarina.Files;                     use Ocarina.Files;
with Ocarina.ME_AADL.AADL_Tree.Nutils;  use Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.Options;                   use Ocarina.Options;
with Ocarina.Parser;                    use Ocarina.Parser;
with Ocarina.Property_Sets;             use Ocarina.Property_Sets;

with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.OS_Lib;       use GNAT.OS_Lib;

package body Ocarina.FE_AADL.Parser is

   Language : constant String := "aadl";
   Suffix   : constant String := ".aadl";

   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
      C : Character;
   begin
      First_Parsing     := True;
      Add_Pre_Prop_Sets := False;

      Initialize_Option_Scan;
      loop
84
         C := Getopt ("* y s f I:");
85
86
87
88
         case C is
            when ASCII.NUL =>
               exit;

89
90
91
            when 'y' =>
               Auto_Load_Aadl_Files := True;

92
93
94
95
96
97
98
99
            when 'I' =>
               Add_Library_Path (Parameter);

            when 'f' | 's' =>
               Add_Pre_Prop_Sets := True;

            when others =>
               null;
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
345
346
347
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
         end case;
      end loop;
      Add_Library_Path (Get_Name_String (Default_Library_Path));

      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;
      Code      : Parsing_Code)
     return Integer
   is
      Loc   : Location;
      Nb_Items : Integer := 0;
      Item  : Node_Id;

   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;

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

   function P_Items_List
     (Func      : P_Item_Function_Ptr;
      Container : Node_Id;
      Code      : Parsing_Code)
     return List_Id
   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;

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

   function P_Items_List
     (Func         : P_Refinable_Item_Function_Ptr;
      Container    : Node_Id;
      Refinable    : Boolean;
      Code         : Parsing_Code;
      At_Least_One : Boolean                       := True)
     return Integer
   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
     (Func         : P_Item_Function_Ptr;
      Container    : Node_Id;
      Delimiter    : Ocarina.ME_AADL.Tokens.Token_Type)
     return List_Id
   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;

      Set_Loc (Node_Id (Elements_List),
               Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node
                                                      (Elements_List)));
      return Elements_List;
   end P_Elements_List;

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

   --  ( { Item Separator }* Item )

   function P_Items_List
     (Func         : P_Item_Function_Ptr;
      Container    : Node_Id;
      Separator    : Ocarina.ME_AADL.Tokens.Token_Type)
     return List_Id
   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;

      Set_Loc (Node_Id (Items),
               Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node (Items)));
      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;
      With_Terminator : Boolean                            := False)
     return List_Id
   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;

      Set_Loc (Node_Id (Items),
               Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (First_Node (Items)));
      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
     (AADL_Root : Node_Id)
     return Node_Id
   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;
      Exit_On_Error
        (No (New_Root), "Cannot parse ocarina property sets");
      return New_Root;
   end Process_Predefined_Property_Sets;

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

   function Process
     (AADL_Root : Node_Id;
      From      : Location;
      To        : Location := No_Location)
     return Node_Id
   is
      New_Root  : Node_Id := AADL_Root;
      File_Name : Name_Id;
      Buffer    : Location;

   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;
               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;
         Exit_On_Error
           (No (New_Root), "Cannot parse standard property sets");

         --  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;