ocarina-fe_aadl-parser-components-features.adb 33.3 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--               OCARINA.FE_AADL.PARSER.COMPONENTS.FEATURES                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- 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.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
34
35
36
37
--                                                                          --
------------------------------------------------------------------------------

with Locations;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.FE_AADL.Lexer;
with Ocarina.ME_AADL.Tokens;
38
with Ocarina.FE_AADL.Parser.Components.Arrays;
39
40
41
42
43
44
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

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

53
54
55
56
   function P_Feature_Group_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id;
57
58
59
60
61
62
63
64
65
66
   --  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;
67
      Is_Out        : Boolean) return Node_Id;
68
69
70
71
72
73
74
75
   --  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;
76
      Is_Out        : Boolean) return Node_Id;
77
78
   --  Parse Parameter and Parameter_Refinement

79
80
81
82
   function P_Subprogram_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id;
83
84
85
86
87
   --  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

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

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

98
99
100
101
102
   function P_In_Out_Item
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code) return Node_Id
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
   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
128
         when T_Data | T_Event | T_Feature =>
129
130
131
132
133
            if Code = PC_Parameter or else Code = PC_Parameter_Refinement then
               DPE (Code, T_Parameter);
               Skip_Tokens (T_Semicolon);
               return No_Node;
            else
134
135
136
137
138
139
               return P_Port_Spec
                   (Container,
                    Identifier,
                    Is_Refinement,
                    Is_In,
                    Is_Out);
140
141
142
143
144
145
146
147
            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
148
149
150
151
152
153
               return P_Parameter
                   (Container,
                    Identifier,
                    Is_Refinement,
                    Is_In,
                    Is_Out);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
            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 }+ } ] ;

177
178
179
180
181
182
   function P_Parameter
     (Container     : Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Is_In         : Boolean;
      Is_Out        : Boolean) return Node_Id
183
184
185
186
187
188
189
190
191
   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;

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

   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;

222
223
224
225
226
227
228
229
      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);
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

      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
258
   --  XXX dimensions ???
259
260
261
262
263
264
265
266
267
268
269
270
   --        [ { { 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;
271
      Is_Out        : Boolean) return Node_Id
272
273
274
275
276
277
278
279
   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;
280
      use Parser.Components.Arrays;
281

282
283
284
285
286
287
288
289
290
      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;
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

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

309
310
      elsif Token = T_Data then
         Is_Data := True;
311
312
313
314

      elsif Token = T_Feature then
         Is_Feature := True;

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

320
321
      if not Is_Feature then
         Scan_Token;
322

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

329
      if Is_Data or else Is_Feature then
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
         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;

345
346
347
348
349
350
351
352
353
354
355
356
      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);
357

358
359
360
361
362
363
364
365
366
367
368
369
370
371
      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 =>
372
               DPE (Code, EMC_Not_Allowed_In_AADL_V1);
373
374
375
376
377
378
379
380
               Skip_Tokens (T_Semicolon);
               return No_Node;
         end case;

      else
         Restore_Lexer (Loc);
      end if;

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

429
430
431
432
   function P_Subprogram_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
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
   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
500
501
502
503
504
505
         Subprog_Spec :=
           Add_New_Server_Subprogram
             (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
              Name          => Identifier,
              Container     => Container,
              Is_Refinement => Is_Refinement);
506
      else
507
508
509
510
511
512
         Subprog_Spec :=
           Add_New_Data_Subprogram_Spec
             (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
              Name          => Identifier,
              Container     => Container,
              Is_Refinement => Is_Refinement);
513
514
515
516
517
518
519
520
521
      end if;

      Set_Entity_Ref (Subprog_Spec, Subprog_Ref);

      Save_Lexer (Loc);
      Scan_Token;

      if Token /= T_Semicolon then
         Restore_Lexer (Loc);
522
523
524
525
526
527
         OK :=
           P_Property_Associations
             (Subprog_Spec,
              True,
              PAT_Simple_Or_Contained,
              Code);
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
567
568

         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 }+ } ] ;

569
570
571
572
   function P_Subcomponent_Access
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
573
574
575
576
577
578
579
580
581
   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;

582
583
584
      Subcomp_Access        : Node_Id := No_Node;
      Subcomp_Access_Class  : Node_Id := No_Node;
      Is_Provided           : Boolean := False;
585
      Subcomponent_Category : Component_Category;
586
587
588
      Code                  : Parsing_Code;
      OK                    : Boolean;
      Loc                   : Location;
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
614
615

   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
616
617
618
            DPE
              (PC_Subcomponent_Access_Classifier,
               (T_Data, T_Bus, T_Subprogram));
619
620
621
622
623
624
625
626
627
628
629
630
631
632
         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);
633
634
         Subcomp_Access_Class :=
           P_Entity_Reference (PC_Subcomponent_Access_Classifier);
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

         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;

651
652
653
654
655
656
657
658
      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);
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
709
710

      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;
711
      Refinable : Boolean) return Node_Id
712
713
714
715
716
717
718
719
720
   is
      use Parser.Identifiers;
      use Ocarina.ME_AADL.Tokens;
      use Lexer;

      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code;
      OK            : Boolean;
721
      Node          : Node_Id;
722
   begin
723
724
725
726
727
728
729
730
731
      P_Identifier_Refined_To
        (Refinable_To_RT (Refinable),
         False,
         PC_Feature,
         PC_Feature_Refinement,
         T_Semicolon,
         Identifier,
         Is_Refinement,
         OK);
732
733
734
735
736
737
738
739
740
741
      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;
742

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

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

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

               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 =>
764
765
            Node :=
              P_Subcomponent_Access (Container, Identifier, Is_Refinement);
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
798
799

         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;

800
801
802
803
804
805
      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));
806
807
808
809
810
811

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

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

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

         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 =>
843
                  DPE (PC_Feature_Refinement, EMC_Not_Allowed_In_AADL_V2);
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
898
899
                  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

900
901
902
903
   function P_Feature_Group_Spec
     (Container     : Types.Node_Id;
      Identifier    : Node_Id;
      Is_Refinement : Boolean) return Node_Id
904
905
906
907
908
909
910
911
912
   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;

913
914
915
916
917
918
      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));
919

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

   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);
947
948
         Group_Type_Ref :=
           P_Entity_Reference (PC_Unique_Feature_Group_Type_Reference);
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974

         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 =>
975
               DPE (Code, EMC_Not_Allowed_In_AADL_V1);
976
977
978
979
980
981
982
983
984
985
               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;

986
987
988
989
990
991
      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);
992

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

      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;

1008
      Set_Entity_Ref (Feature_Group_Spec, Group_Type_Ref);
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
      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;
1020
      Refinable : Boolean) return Node_Id
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
   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
1032
1033
1034
1035
1036
1037
1038
1039
1040
      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);
1041
1042
1043
1044
1045
1046
1047
1048
      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
1049
1050
1051
1052
1053
               return P_In_Out_Item
                   (Container     => Container,
                    Identifier    => Identifier,
                    Is_Refinement => True,
                    Code          => PC_Port_Refinement);
1054
            else
1055
1056
1057
1058
1059
               return P_In_Out_Item
                   (Container     => Container,
                    Identifier    => Identifier,
                    Is_Refinement => False,
                    Code          => PC_Port_Spec);
1060
1061
            end if;

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

         when others =>

            if Is_Refinement then
               case AADL_Version is
                  when AADL_V1 =>
1075
1076
1077
                     DPE
                       (PC_Port_Refinement_Or_Port_Group_Refinement,
                        (T_In, T_Out, T_Port));
1078
                  when AADL_V2 =>
1079
1080
1081
                     DPE
                       (PC_Feature_Refinement_Or_Feature_Group_Refinement,
                        (T_In, T_Out, T_Port, T_Feature));
1082
1083
               end case;
            else
1084
1085
1086
               DPE
                 (PC_Feature_Group_Or_Port_Group_Or_Port_Spec,
                  (T_In, T_Out, T_Port, T_Feature));
1087
1088
1089
1090
1091
1092
1093
            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;