ocarina-fe_aadl-parser-components-features.adb 33.2 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--               OCARINA.FE_AADL.PARSER.COMPONENTS.FEATURES                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
--                                                                          --
-- 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
33
34
35
--                                                                          --
------------------------------------------------------------------------------

with Locations;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.FE_AADL.Lexer;
with Ocarina.ME_AADL.Tokens;
36
with Ocarina.FE_AADL.Parser.Components.Arrays;
37
38
39
40
41
42
with Ocarina.FE_AADL.Parser.Identifiers;
with Ocarina.FE_AADL.Parser.Properties;
with Ocarina.Builder.AADL.Components.Features;

package body Ocarina.FE_AADL.Parser.Components.Features is

43
44
45
46
47
   function P_In_Out_Item
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code) return Node_Id;
48
49
50
   --  Parse item begins with 'in' or 'out'
   --  Examples: Port_Spec, Parameter, ... and refinements

51
52
53
54
   function P_Feature_Group_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id;
55
56
57
58
59
60
61
62
63
64
   --  Current token must be reserved word 'feature' or 'port'
   --  If Is_Refinement = TRUE,
   --  And If Rules is AADL_V1 parse a Port_Group_Spec
   --  And If Rules is AADL_V2 parse a Feature_Group_Spec

   function P_Port_Spec
     (Container     : Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Is_In         : Boolean;
65
      Is_Out        : Boolean) return Node_Id;
66
67
68
69
70
71
72
73
   --  Current token must be reserved word 'data' or 'event'
   --  If Is_Refinement = TRUE, parse a Port_Refinement

   function P_Parameter
     (Container     : Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Is_In         : Boolean;
74
      Is_Out        : Boolean) return Node_Id;
75
76
   --  Parse Parameter and Parameter_Refinement

77
78
79
80
   function P_Subprogram_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id;
81
82
83
84
85
   --  Current token must be reserved word 'subprogram' or 'server'
   --  If Is_Refinement = TRUE, parse Data_Subprogram_Refinement or
   --                                 Server_Subprogram_Refinement
   --  else, parse Data_Subprogram_Spec or Server_Subprogram

86
87
88
89
   function P_Subcomponent_Access
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id;
90
91
92
93
94
95
   --  Parse Subcomponent_Access and Subcomponent_Access_Refinement

   -------------------
   -- P_In_Out_Item --
   -------------------

96
97
98
99
100
   function P_In_Out_Item
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code) return Node_Id
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
   is
      use Ocarina.ME_AADL.Tokens;
      use Lexer;

      Is_In  : Boolean := False;
      Is_Out : Boolean := False;

   begin
      if Token = T_In then
         Is_In := True;
         Scan_Token;
         if Token = T_Out then
            Is_Out := True;
            Scan_Token;
         end if;
      else
         Is_Out := True;
         Scan_Token;
         if Token = T_In then
            Is_In := True;
            Scan_Token;
         end if;
      end if;

      case Token is
126
         when T_Data | T_Event | T_Feature =>
127
128
129
130
131
            if Code = PC_Parameter or else Code = PC_Parameter_Refinement then
               DPE (Code, T_Parameter);
               Skip_Tokens (T_Semicolon);
               return No_Node;
            else
132
133
134
135
136
137
               return P_Port_Spec
                   (Container,
                    Identifier,
                    Is_Refinement,
                    Is_In,
                    Is_Out);
138
139
140
141
142
143
144
145
            end if;

         when T_Parameter =>
            if Code = PC_Port_Spec or else Code = PC_Port_Refinement then
               DPE (Code, (T_Data, T_Event));
               Skip_Tokens (T_Semicolon);
               return No_Node;
            else
146
147
148
149
150
151
               return P_Parameter
                   (Container,
                    Identifier,
                    Is_Refinement,
                    Is_In,
                    Is_Out);
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            end if;

         when others =>
            DPE (Code);
            Skip_Tokens (T_Semicolon);
            return No_Node;
      end case;
   end P_In_Out_Item;

   -----------------
   -- P_Parameter --
   -----------------

   --  parameter ::=
   --     defining_parameter_identifier :
   --        ( in | out | in out ) parameter data_classifier_reference
   --     [ { { parameter_property_association }+ } ] ;

   --  parameter_refinement ::=
   --     defining_parameter_identifier : refined to
   --        ( in | out | in out ) parameter data_classifier_reference
   --     [ { { parameter_property_association }+ } ] ;

175
176
177
178
179
180
   function P_Parameter
     (Container     : Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Is_In         : Boolean;
      Is_Out        : Boolean) return Node_Id
181
182
183
184
185
186
187
188
189
   is
      use Locations;
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Parser.Properties;
      use Parser.Identifiers;
      use Ocarina.Builder.AADL.Components.Features;

190
191
192
193
194
      Param     : Node_Id := No_Node;
      Class_Ref : Node_Id := No_Node;
      Code      : Parsing_Code;
      Loc       : Location;
      OK        : Boolean;
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

   begin
      if Is_Refinement then
         Code := PC_Parameter_Refinement;
      else
         Code := PC_Parameter;
      end if;

      Save_Lexer (Loc);
      Scan_Token;   --  next token and ignore 'parameter'

      --  The data classifier is not mandatory
      if Token = T_Identifier then
         Restore_Lexer (Loc);
         Class_Ref := P_Entity_Reference (Code);

         if No (Class_Ref) then
            Skip_Tokens (T_Semicolon);
            return No_Node;
         end if;
      else
         Restore_Lexer (Loc);
         Class_Ref := No_Node;
      end if;

220
221
222
223
224
225
226
227
      Param :=
        Add_New_Parameter
          (Loc           => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
           Container     => Container,
           Name          => Identifier,
           Is_In         => Is_In,
           Is_Out        => Is_Out,
           Is_Refinement => Is_Refinement);
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

      OK := P_Property_Associations (Param, True, PAT_Simple, Code);

      if not OK then
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token /= T_Semicolon then
         DPE (Code, T_Semicolon);
         Restore_Lexer (Loc);
         return No_Node;
      end if;

      if Param /= No_Node then
         Set_Entity_Ref (Param, Class_Ref);
      end if;

      return Param;
   end P_Parameter;

   -----------------
   -- P_Port_Spec --
   -----------------

   --  port_spec ::=
   --     defining_port_identifier : ( in | out | in out ) port_type
256
   --  XXX dimensions ???
257
258
259
260
261
262
263
264
265
266
267
268
   --        [ { { port_property_association }+ } ] ;

   --  port_refinement ::=
   --     defining_port_identifier : refined to
   --        ( in | out | in out ) port_type
   --        [ { { port_property_association }+ } ] ;

   function P_Port_Spec
     (Container     : Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Is_In         : Boolean;
269
      Is_Out        : Boolean) return Node_Id
270
271
272
273
274
275
276
277
   is
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Locations;
      use Parser.Properties;
      use Parser.Identifiers;
      use Ocarina.Builder.AADL.Components.Features;
278
      use Parser.Components.Arrays;
279

280
281
282
283
284
285
286
287
288
      Class_Ref        : Node_Id := No_Node;
      Port_Spec        : Node_Id := No_Node;
      Is_Data          : Boolean := False;
      Is_Event         : Boolean := False;
      Is_Feature       : Boolean := False;
      Code             : Parsing_Code;
      OK               : Boolean;
      Loc              : Location;
      Array_Dimensions : Node_Id;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

   begin
      if Is_Refinement then
         Code := PC_Port_Refinement;
      else
         Code := PC_Port_Spec;
      end if;

      if Token = T_Event then
         Is_Event := True;
         Save_Lexer (Loc);
         Scan_Token;
         if Token = T_Data then
            Is_Data := True;
         else
            Restore_Lexer (Loc);
         end if;
306

307
308
      elsif Token = T_Data then
         Is_Data := True;
309
310
311
312

      elsif Token = T_Feature then
         Is_Feature := True;

313
314
315
316
317
      else
         DPE (PC_Port_Type, ((T_Event, T_Data)));
         return No_Node;
      end if;

318
319
      if not Is_Feature then
         Scan_Token;
320

321
322
323
324
         if Token /= T_Port then
            DPE (PC_Port_Type, T_Port);
            return No_Node;
         end if;
325
326
      end if;

327
      if Is_Data or else Is_Feature then
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
         Save_Lexer (Loc);
         Scan_Token;
         if Token = T_Identifier then
            Restore_Lexer (Loc);
            Class_Ref := P_Entity_Reference (PC_Port_Type);

            if No (Class_Ref) then
               --  Error when parsing Classifier_Reference, quit
               return No_Node;
            end if;
         else
            Restore_Lexer (Loc);
         end if;
      end if;

343
344
345
346
347
348
349
350
351
352
353
354
      Port_Spec :=
        Add_New_Port_Spec
          (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
           Container         => Container,
           Name              => Identifier,
           Is_In             => Is_In,
           Is_Out            => Is_Out,
           Is_Event          => Is_Event,
           Is_Data           => Is_Data,
           Is_Feature        => Is_Feature,
           Is_Refinement     => Is_Refinement,
           Associated_Entity => Class_Ref);
355

356
357
358
359
360
361
362
363
364
365
366
367
368
369
      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_Left_Square_Bracket then
         case AADL_Version is
            when AADL_V2 =>
               Array_Dimensions := P_Array_Dimensions (Port_Spec);
               if No (Array_Dimensions) then
                  DPE (Code);
                  Skip_Tokens (T_Semicolon);
                  return No_Node;
               end if;
               Set_Array_Dimensions (Port_Spec, Array_Dimensions);

            when AADL_V1 =>
370
               DPE (Code, EMC_Not_Allowed_In_AADL_V1);
371
372
373
374
375
376
377
378
               Skip_Tokens (T_Semicolon);
               return No_Node;
         end case;

      else
         Restore_Lexer (Loc);
      end if;

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
      OK := P_Property_Associations (Port_Spec, True, PAT_Simple, Code);

      if not OK then
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token /= T_Semicolon then
         DPE (Code, T_Semicolon);
         Restore_Lexer (Loc);
         return No_Node;
      end if;

      return Port_Spec;
   end P_Port_Spec;

   -----------------------
   -- P_Subprogram_Spec --
   -----------------------

   --  data_subprogram_spec ::=
   --     defining_subprogram_identifier_list : subprogram
   --     [ unique_subprogram_reference ]
   --     [ { { subprogram_property_association }+ } ] ;

   --  data_subprogram_refinement ::=
   --     defining_subprogram_identifier_list : refined to subprogram
   --     [ unique_subprogram_reference ]
   --     [ { { subprogram_property_association }+ } ] ;

   --  server_subprogram ::=
   --     defining_subprogram_identifier_list : server subprogram
   --     [ unique_subprogram_reference ]
   --     [ { { subprogram_property_association }+ } ] ;

   --  server_subprogram_refinement ::=
   --     defining_subprogram_identifier_list : refined to server subprogram
   --     [ unique_subprogram_reference ]
   --     [ { { subprogram_property_association }+ } ] ;

   --  unique_subprogram_reference ::=
   --     subprogram_classifier_reference |
   --     subprogram_feature_classifier_reference

   --  subprogram_feature_classifier_reference ::=
   --     [ package_name :: ] data_type_identifier . subprogram_identifier

427
428
429
430
   function P_Subprogram_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
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
   is
      use Locations;
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Parser.Properties;
      use Ocarina.FE_AADL.Parser.Identifiers;
      use Ocarina.Builder.AADL.Components.Features;

      Subprog_Spec : Node_Id := No_Node;
      Is_Server    : Boolean := False;
      Subprog_Ref  : Node_Id := No_Node;
      Loc          : Location;
      Code         : Parsing_Code;
      OK           : Boolean;

      --  Sub-function determining parsing code to display error messages

      function Subprogram_Parsing_Code return Parsing_Code;
      pragma Inline (Subprogram_Parsing_Code);

      function Subprogram_Parsing_Code return Parsing_Code is
      begin
         if Is_Refinement then
            if Is_Server then
               return PC_Server_Subprogram_Refinement;
            else
               return PC_Data_Subprogram_Refinement;
            end if;
         else
            if Is_Server then
               return PC_Server_Subprogram;
            else
               return PC_Data_Subprogram_Spec;
            end if;
         end if;
      end Subprogram_Parsing_Code;

   begin
      if Token = T_Server then
         Is_Server := True;
         Scan_Token;
      end if;

      Code := Subprogram_Parsing_Code;

      if Token /= T_Subprogram then
         DPE (Code, T_Subprogram);
         Skip_Tokens (T_Semicolon);
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_Identifier then
         Restore_Lexer (Loc);
         Subprog_Ref := P_Entity_Reference (Code);

         if No (Subprog_Ref) then
            Skip_Tokens (T_Semicolon);
            return No_Node;
         end if;
      else
         Restore_Lexer (Loc);
      end if;

      if Is_Server then
498
499
500
501
502
503
         Subprog_Spec :=
           Add_New_Server_Subprogram
             (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
              Name          => Identifier,
              Container     => Container,
              Is_Refinement => Is_Refinement);
504
      else
505
506
507
508
509
510
         Subprog_Spec :=
           Add_New_Data_Subprogram_Spec
             (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
              Name          => Identifier,
              Container     => Container,
              Is_Refinement => Is_Refinement);
511
512
513
514
515
516
517
518
519
      end if;

      Set_Entity_Ref (Subprog_Spec, Subprog_Ref);

      Save_Lexer (Loc);
      Scan_Token;

      if Token /= T_Semicolon then
         Restore_Lexer (Loc);
520
521
522
523
524
525
         OK :=
           P_Property_Associations
             (Subprog_Spec,
              True,
              PAT_Simple_Or_Contained,
              Code);
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

         if not OK then
            Subprog_Spec := No_Node;
         else

            Save_Lexer (Loc);
            Scan_Token;
            if Token /= T_Semicolon then
               DPE (Code, T_Semicolon);
               Restore_Lexer (Loc);
               return No_Node;
            end if;
         end if;
      end if;

      return Subprog_Spec;
   end P_Subprogram_Spec;

   ---------------------------
   -- P_Subcomponent_Access --
   ---------------------------

   --  AADL_V1
   --  subcomponent_access ::=
   --      defining_subcomponent_access_identifier :
   --          subcomponent_access_classifier
   --      [ { { access_property_association }+ } ] ;

   --  subcomponent_access_refinement ::=
   --      defining_subcomponent_access_identifier : refined to
   --          subcomponent_access_classifier
   --      [ { { access_property_association }+ } ] ;

   --  AADL_V2
   --  subprogram_access ::=
   --      defining_subprogram_access_identifier :
   --         ( provides | requires ) subprogram access
   --            [ subprogram_unique_component_classifier_reference
   --            | prototype_identifier ]
   --      [ { { access_property_association }+ } ] ;

567
568
569
570
   function P_Subcomponent_Access
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
571
572
573
574
575
576
577
578
579
   is
      use Locations;
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Parser.Properties;
      use Parser.Identifiers;
      use Ocarina.Builder.AADL.Components.Features;

580
581
582
      Subcomp_Access        : Node_Id := No_Node;
      Subcomp_Access_Class  : Node_Id := No_Node;
      Is_Provided           : Boolean := False;
583
      Subcomponent_Category : Component_Category;
584
585
586
      Code                  : Parsing_Code;
      OK                    : Boolean;
      Loc                   : Location;
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

   begin
      if Token = T_Provides then
         Is_Provided := True;
      elsif Token /= T_Requires then
         DPE (PC_Subcomponent_Access_Classifier, (T_Provides, T_Requires));
         return No_Node;
      end if;

      Scan_Token;
      if Token = T_Data then
         Subcomponent_Category := CC_Data;
      elsif Token = T_Bus then
         Subcomponent_Category := CC_Bus;
      elsif Token = T_Subprogram then
         Save_Lexer (Loc);
         Scan_Token;
         if Token = T_Group then
            Subcomponent_Category := CC_Subprogram_Group;
         else
            Subcomponent_Category := CC_Subprogram;
            Restore_Lexer (Loc);
         end if;
      else
         if AADL_Version = AADL_V1 then
            DPE (PC_Subcomponent_Access_Classifier, (T_Data, T_Bus));
         else
614
615
616
            DPE
              (PC_Subcomponent_Access_Classifier,
               (T_Data, T_Bus, T_Subprogram));
617
618
619
620
621
622
623
624
625
626
627
628
629
630
         end if;
         return No_Node;
      end if;

      Scan_Token;
      if Token /= T_Access then
         DPE (PC_Subcomponent_Access_Classifier, T_Access);
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_Identifier then
         Restore_Lexer (Loc);
631
632
         Subcomp_Access_Class :=
           P_Entity_Reference (PC_Subcomponent_Access_Classifier);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

         if Subcomp_Access_Class = No_Node then
            --  Error when parsing Subcomponent_Access_Classifier, quit
            Skip_Tokens (T_Semicolon);
            return No_Node;
         end if;
      else
         Restore_Lexer (Loc);
      end if;

      if Is_Refinement then
         Code := PC_Subcomponent_Access_Refinement;
      else
         Code := PC_Subcomponent_Access;
      end if;

649
650
651
652
653
654
655
656
      Subcomp_Access :=
        Add_New_Subcomponent_Access
          (Loc           => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
           Name          => Identifier,
           Container     => Container,
           Is_Refinement => Is_Refinement,
           Category      => Subcomponent_Category,
           Is_Provided   => Is_Provided);
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708

      OK := P_Property_Associations (Subcomp_Access, True, PAT_Access, Code);

      if not OK then
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token /= T_Semicolon then
         DPE (Code, T_Semicolon);
         Restore_Lexer (Loc);
         return No_Node;
      end if;

      if Subcomp_Access /= No_Node then
         Set_Entity_Ref (Subcomp_Access, Subcomp_Access_Class);
      end if;

      return Subcomp_Access;
   end P_Subcomponent_Access;

   ---------------
   -- P_Feature --
   ---------------

   --  AADL_V1
   --  feature ::=   port_spec | port_group_spec
   --              | server_subprogram | data_subprogram_spec
   --              | subcomponent_access | parameter
   --
   --  feature_refinement ::=   port_refinement
   --                         | port_group_refinement
   --                         | server_subprogram_refinement
   --                         | data_subprogram_refinement
   --                         | subcomponent_access_refinement
   --                         | parameter_refinement
   --  Note : Feature Group design Port Group in AADL_V1

   --  AADL_V2
   --  feature ::=   port_spec |
   --              | feature_group_spec
   --              | subcomponent_access
   --              | parameter
   --
   --  feature_refinement ::=   port_refinement
   --                         | feature_group_refinement
   --                         | subcomponent_access_refinement
   --                         | parameter_refinement

   function P_Feature
     (Container : Types.Node_Id;
709
      Refinable : Boolean) return Node_Id
710
711
712
713
714
715
716
717
718
   is
      use Parser.Identifiers;
      use Ocarina.ME_AADL.Tokens;
      use Lexer;

      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code;
      OK            : Boolean;
719
      Node          : Node_Id;
720
   begin
721
722
723
724
725
726
727
728
729
      P_Identifier_Refined_To
        (Refinable_To_RT (Refinable),
         False,
         PC_Feature,
         PC_Feature_Refinement,
         T_Semicolon,
         Identifier,
         Is_Refinement,
         OK);
730
731
732
733
734
735
736
737
738
739
      if not OK then
         return No_Node;
      end if;
      if Is_Refinement then
         Code := PC_Feature_Refinement;
      else
         Code := PC_Feature;
      end if;

      Scan_Token;
740

741
742
743
744
745
      case Token is
         when T_In | T_Out =>
            Node := P_In_Out_Item (Container, Identifier, Is_Refinement, Code);

         when T_Port | T_Feature =>
746
747
            Node :=
              P_Feature_Group_Spec (Container, Identifier, Is_Refinement);
748
749
750
751

         when T_Server | T_Subprogram =>
            case AADL_Version is
               when AADL_V1 =>
752
753
                  Node :=
                    P_Subprogram_Spec (Container, Identifier, Is_Refinement);
754
755
756
757
758
759
760
761

               when AADL_V2 =>
                  DPE (Code, EMC_Not_Allowed_In_AADL_V2);
                  Skip_Tokens (T_Semicolon);
                  Node := No_Node;
            end case;

         when T_Provides | T_Requires =>
762
763
            Node :=
              P_Subcomponent_Access (Container, Identifier, Is_Refinement);
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797

         when others =>
            DPE (Code);
            Skip_Tokens (T_Semicolon);
            Node := No_Node;
      end case;

      return Node;
   end P_Feature;

   --------------------------
   -- P_Feature_Refinement --
   --------------------------

   --  AADL_V1
   --  feature_refinement ::=   port_refinement
   --                         | port_group_refinement
   --                         | server_subprogram_refinement
   --                         | data_subprogram_refinement
   --                         | subcomponent_access_refinement
   --                         | parameter_refinement

   --  AADL_V2
   --  feature_refinement ::=   port_refinement
   --                         | feature_group_refinement
   --                         | subcomponent_access_refinement
   --                         | parameter_refinement

   function P_Feature_Refinement (Container : Types.Node_Id) return Node_Id is
      use Parser.Identifiers;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Ocarina.ME_AADL.AADL_Tree.Nodes;

798
799
800
801
802
803
      pragma Assert
        (Container /= No_Node
         and then
         (Kind (Container) = K_Component_Implementation
          or else Kind (Container) = K_Component_Type
          or else Kind (Container) = K_Feature_Group_Type));
804
805
806
807
808
809

      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      OK            : Boolean;

   begin
810
811
812
813
814
815
816
817
818
      P_Identifier_Refined_To
        (RT_Refinement,
         False,
         PC_Feature,
         PC_Feature_Refinement,
         T_Semicolon,
         Identifier,
         Is_Refinement,
         OK);
819
820
821
822
823
824
825
826
      if not OK then
         return No_Node;
      end if;

      Scan_Token;
      case Token is
         when T_In | T_Out =>
            return P_In_Out_Item
827
828
829
830
                (Container,
                 Identifier,
                 True,
                 PC_Feature_Refinement);
831
832
833
834
835
836
837
838
839
840

         when T_Port =>
            return P_Feature_Group_Spec (Container, Identifier, True);

         when T_Server =>
            case AADL_Version is
               when AADL_V1 =>
                  return P_Subprogram_Spec (Container, Identifier, True);

               when others =>
841
                  DPE (PC_Feature_Refinement, EMC_Not_Allowed_In_AADL_V2);
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
                  Skip_Tokens ((T_End, T_Semicolon));
                  return No_Node;
            end case;

         when T_Subprogram =>
            return P_Subprogram_Spec (Container, Identifier, True);

         when T_Provides | T_Requires =>
            return P_Subcomponent_Access (Container, Identifier, True);

         when others =>
            DPE (PC_Feature_Refinement);
            Skip_Tokens (T_Semicolon);
            return No_Node;
      end case;
   end P_Feature_Refinement;

   --------------------------
   -- P_Feature_Group_Spec --
   --------------------------

   --  AADL_V1

   --  port_group_spec ::=
   --     defining_port_group_identifier : port group
   --                                      unique_port_group_type_reference
   --     [ { { portgroup_property_association }+ } ] ;

   --  port_group_refinement ::=
   --     defining_port_group_identifier : refined to port group
   --                                      unique_port_group_type_reference
   --     [ { { portgroup_property_association }+ } ] ;

   --  unique_port_group_type_reference ::=
   --     [ package_name :: ] port_group_type_identifier

   --  AADL_V2

   --  feature_group_spec ::=
   --     defining_feature_group_identifier : feature group
   --     [ [ inverse of ]
   --     ( unique_feature_group_type_reference [ prototype_bindings ] )
   --     | prototype_identifier ) ]
   --     [ { { featuregroup_property_association }+ } ] ;

   --  feature_group_refinement ::=
   --     defining_feature_group_identifier : refined to feature group
   --     [ [ inverse of ]
   --       ( unique_feature_group_type_reference [ prototype_bindings ] )
   --       | prototype_identifier )
   --     ]
   --     [ { { featuregroup_property_association }+ } ] ;

   --  unique_feature_group_type_reference ::=
   --     [ package_name :: ] feature_group_type_identifier

898
899
900
901
   function P_Feature_Group_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
902
903
904
905
906
907
908
909
910
   is
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Lexer;
      use Ocarina.ME_AADL.Tokens;
      use Locations;
      use Parser.Properties;
      use Parser.Identifiers;
      use Ocarina.Builder.AADL.Components.Features;

911
912
913
914
915
916
      pragma Assert
        (Container /= No_Node
         and then
         (Kind (Container) = K_Component_Implementation
          or else Kind (Container) = K_Component_Type
          or else Kind (Container) = K_Feature_Group_Type));
917

918
919
      Inverse_Of         : Node_Id := No_Node;
      Group_Type_Ref     : Node_Id := No_Node;
920
      Feature_Group_Spec : Node_Id := No_Node;
921
922
923
      Code               : Parsing_Code;
      OK                 : Boolean;
      Loc                : Location;
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944

   begin
      if Is_Refinement then
         Code := PC_Feature_Group_Or_Port_Group_Refinement;
      else
         Code := PC_Port_Group_Spec_Or_Feature_Group_Spec;
      end if;

      --  ignore 'port' or 'feature'
      Scan_Token;

      if Token /= T_Group then
         DPE (Code, T_Group);
         Skip_Tokens (T_Semicolon);
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token = T_Identifier then
         Restore_Lexer (Loc);
945
946
         Group_Type_Ref :=
           P_Entity_Reference (PC_Unique_Feature_Group_Type_Reference);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972

         if No (Group_Type_Ref) then
            Skip_Tokens (T_Semicolon);
            return No_Node;
         end if;

      elsif Token = T_Inverse then
         case AADL_Version is
            when AADL_V2 =>
               Scan_Token;
               if Token /= T_Of then
                  DPE (Code, T_Of);
                  Skip_Tokens ((T_End, T_Semicolon));
                  return No_Node;
               end if;

               Inverse_Of := P_Entity_Reference (Code);

               if No (Inverse_Of) then
                  --  Error when parsing
                  --  Unique_Feature_Group_Type_Reference, quit
                  Skip_Tokens ((T_End, T_Semicolon));
                  return No_Node;
               end if;

            when others =>
973
               DPE (Code, EMC_Not_Allowed_In_AADL_V1);
974
975
976
977
978
979
980
981
982
983
               Skip_Tokens ((T_End, T_Semicolon));
               return No_Node;
         end case;

      else
         Restore_Lexer (Loc);
         Group_Type_Ref := No_Node;
         --  OK, no port group type is given
      end if;

984
985
986
987
988
989
      Feature_Group_Spec :=
        Add_New_Feature_Group_Spec
          (Loc           => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
           Name          => Identifier,
           Container     => Container,
           Is_Refinement => Is_Refinement);
990

991
992
      OK :=
        P_Property_Associations (Feature_Group_Spec, True, PAT_Simple, Code);
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005

      if not OK then
         return No_Node;
      end if;

      Save_Lexer (Loc);
      Scan_Token;
      if Token /= T_Semicolon then
         DPE (Code, T_Semicolon);
         Restore_Lexer (Loc);
         return No_Node;
      end if;

1006
      Set_Entity_Ref (Feature_Group_Spec, Group_Type_Ref);
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
      Set_Inverse_Of (Feature_Group_Spec, Inverse_Of);

      return Feature_Group_Spec;
   end P_Feature_Group_Spec;

   -------------------------------------------------
   -- P_Feature_Group_or_Port_Group_or_Port_Spec  --
   -------------------------------------------------

   function P_Feature_Group_Or_Port_Group_Or_Port_Spec
     (Container : Types.Node_Id;
1018
      Refinable : Boolean) return Node_Id
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
   is
      use Ocarina.ME_AADL.AADL_Tree.Nodes;
      use Ocarina.ME_AADL.Tokens;
      use Lexer;
      use Ocarina.FE_AADL.Parser.Identifiers;

      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      OK            : Boolean;

   begin
1030
1031
1032
1033
1034
1035
1036
1037
1038
      P_Identifier_Refined_To
        (Refinable_To_RT (Refinable),
         False,
         PC_Feature_Group_Or_Port_Group_Or_Port_Spec,
         PC_Feature,
         T_Semicolon,
         Identifier,
         Is_Refinement,
         OK);
1039
1040
1041
1042
1043
1044
1045
1046
      if not OK then
         return No_Node;
      end if;

      Scan_Token;
      case Token is
         when T_In | T_Out =>  --  parse port_spec or port_refinement
            if Is_Refinement then
1047
1048
1049
1050
1051
               return P_In_Out_Item
                   (Container     => Container,
                    Identifier    => Identifier,
                    Is_Refinement => True,
                    Code          => PC_Port_Refinement);
1052
            else
1053
1054
1055
1056
1057
               return P_In_Out_Item
                   (Container     => Container,
                    Identifier    => Identifier,
                    Is_Refinement => False,
                    Code          => PC_Port_Spec);
1058
1059
            end if;

1060
1061
         --  parse port_group_spec or port_group_refinement or
         --        feature_group_spec or feature_group_refinement
1062
         when T_Port | T_Feature =>
1063
1064
1065
1066
            return P_Feature_Group_Spec
                (Container     => Container,
                 Identifier    => Identifier,
                 Is_Refinement => Is_Refinement);
1067
1068
1069
1070
1071

         when others =>
            if Is_Refinement then
               case AADL_Version is
                  when AADL_V1 =>
1072
1073
1074
                     DPE
                       (PC_Port_Refinement_Or_Port_Group_Refinement,
                        (T_In, T_Out, T_Port));
1075
                  when AADL_V2 =>
1076
1077
1078
                     DPE
                       (PC_Feature_Refinement_Or_Feature_Group_Refinement,
                        (T_In, T_Out, T_Port, T_Feature));
1079
1080
               end case;
            else
1081
1082
1083
               DPE
                 (PC_Feature_Group_Or_Port_Group_Or_Port_Spec,
                  (T_In, T_Out, T_Port, T_Feature));
1084
1085
1086
1087
1088
1089
1090
            end if;
            Skip_Tokens (T_Semicolon);
            return No_Node;
      end case;
   end P_Feature_Group_Or_Port_Group_Or_Port_Spec;

end Ocarina.FE_AADL.Parser.Components.Features;