ocarina-fe_aadl-parser-components-features.adb 33.6 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--               OCARINA.FE_AADL.PARSER.COMPONENTS.FEATURES                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
jhugues's avatar
jhugues committed
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2012 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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

   function P_In_Out_Item (Container     : Types.Node_Id;
                           Identifier    : Node_Id;
                           Is_Refinement : Boolean;
                           Code          : Parsing_Code)
                          return Node_Id;
   --  Parse item begins with 'in' or 'out'
   --  Examples: Port_Spec, Parameter, ... and refinements

   function P_Feature_Group_Spec (Container     : Types.Node_Id;
                                  Identifier    : Node_Id;
                                  Is_Refinement : Boolean) return Node_Id;
   --  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;
      Is_Out        : Boolean)
     return Node_Id;
   --  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;
      Is_Out        : Boolean)
     return Node_Id;
   --  Parse Parameter and Parameter_Refinement

   function P_Subprogram_Spec (Container     : Types.Node_Id;
                               Identifier    : Node_Id;
                               Is_Refinement : Boolean)
                              return Node_Id;
   --  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

   function P_Subcomponent_Access (Container     : Types.Node_Id;
                                   Identifier    : Node_Id;
                                   Is_Refinement : Boolean)
                                  return Node_Id;
   --  Parse Subcomponent_Access and Subcomponent_Access_Refinement

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

   function P_In_Out_Item (Container     : Types.Node_Id;
                           Identifier    : Node_Id;
                           Is_Refinement : Boolean;
                           Code          : Parsing_Code)
                          return Node_Id
   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
129
         when T_Data | T_Event | T_Feature =>
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
            if Code = PC_Parameter or else Code = PC_Parameter_Refinement then
               DPE (Code, T_Parameter);
               Skip_Tokens (T_Semicolon);
               return No_Node;
            else
               return P_Port_Spec (Container, Identifier,
                                   Is_Refinement, Is_In, Is_Out);
            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
               return P_Parameter (Container, Identifier,
                                   Is_Refinement, Is_In, Is_Out);
            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 }+ } ] ;

   function P_Parameter (Container     : Node_Id;
                         Identifier    : Node_Id;
                         Is_Refinement : Boolean;
                         Is_In         : Boolean;
                         Is_Out        : Boolean)
                        return Node_Id
   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;

      Param      : Node_Id := No_Node;
      Class_Ref  : Node_Id := No_Node;
      Code       : Parsing_Code;
      Loc        : Location;
      OK         : Boolean;

   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;

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

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

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

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

302
303
      elsif Token = T_Data then
         Is_Data := True;
304
305
306
307

      elsif Token = T_Feature then
         Is_Feature := True;

308
309
310
311
312
      else
         DPE (PC_Port_Type, ((T_Event, T_Data)));
         return No_Node;
      end if;

313
314
      if not Is_Feature then
         Scan_Token;
315

316
317
318
319
         if Token /= T_Port then
            DPE (PC_Port_Type, T_Port);
            return No_Node;
         end if;
320
321
      end if;

322
      if Is_Data or else Is_Feature then
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
         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;

      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,
346
         Is_Feature => Is_Feature,
347
348
349
         Is_Refinement => Is_Refinement,
         Associated_Entity => Class_Ref);

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
      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 =>
               DPE (CODE, EMC_Not_Allowed_In_AADL_V1);
               Skip_Tokens (T_Semicolon);
               return No_Node;
         end case;

      else
         Restore_Lexer (Loc);
      end if;

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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
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
709
710
711
712
713
714
715
716
717
718
719
720
      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

   function P_Subprogram_Spec (Container     : Types.Node_Id;
                               Identifier    : Node_Id;
                               Is_Refinement : Boolean)
                              return Node_Id
   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
         Subprog_Spec := Add_New_Server_Subprogram
           (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
            Name => Identifier,
            Container => Container,
            Is_Refinement => Is_Refinement);
      else
         Subprog_Spec := Add_New_Data_Subprogram_Spec
           (Loc => Ocarina.ME_AADL.AADL_Tree.Nodes.Loc (Identifier),
            Name => Identifier,
            Container => Container,
            Is_Refinement => Is_Refinement);
      end if;

      Set_Entity_Ref (Subprog_Spec, Subprog_Ref);

      Save_Lexer (Loc);
      Scan_Token;

      if Token /= T_Semicolon then
         Restore_Lexer (Loc);
         OK := P_Property_Associations (Subprog_Spec, True,
                                        PAT_Simple_Or_Contained, Code);

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

   function P_Subcomponent_Access (Container     : Types.Node_Id;
                                   Identifier    : Node_Id;
                                   Is_Refinement : Boolean)
                                  return Node_Id
   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;

      Subcomp_Access : Node_Id := No_Node;
      Subcomp_Access_Class : Node_Id := No_Node;
      Is_Provided : Boolean := False;
      Subcomponent_Category : Component_Category;
      Code : Parsing_Code;
      OK : Boolean;
      Loc : Location;

   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
            DPE (PC_Subcomponent_Access_Classifier, (T_Data, T_Bus,
                                                     T_Subprogram));
         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);
         Subcomp_Access_Class := P_Entity_Reference
           (PC_Subcomponent_Access_Classifier);

         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;

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

      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;
      Refinable : Boolean)
     return Node_Id
   is
      use Parser.Identifiers;
      use Ocarina.ME_AADL.Tokens;
      use Lexer;

      Identifier    : Node_Id;
      Is_Refinement : Boolean;
      Code          : Parsing_Code;
      OK            : Boolean;
      Node : Node_Id;
   begin
      P_Identifier_Refined_To (Refinable_To_RT (Refinable), False,
                               PC_Feature, PC_Feature_Refinement, T_Semicolon,
                               Identifier, Is_Refinement, OK);
      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;
721

722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
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
973
974
975
      case Token is
         when T_In | T_Out =>
            Node := P_In_Out_Item (Container, Identifier, Is_Refinement, Code);

         when T_Port | T_Feature =>
            Node := P_Feature_Group_Spec
              (Container, Identifier, Is_Refinement);

         when T_Server | T_Subprogram =>
            case AADL_Version is
               when AADL_V1 =>
                  Node := P_Subprogram_Spec (Container,
                                             Identifier,
                                             Is_Refinement);

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

         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;

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

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

   begin
      P_Identifier_Refined_To (RT_Refinement, False,
                               PC_Feature, PC_Feature_Refinement, T_Semicolon,
                               Identifier, Is_Refinement, OK);
      if not OK then
         return No_Node;
      end if;

      Scan_Token;
      case Token is
         when T_In | T_Out =>
            return P_In_Out_Item
              (Container, Identifier, True, PC_Feature_Refinement);

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

   function P_Feature_Group_Spec (Container     : Types.Node_Id;
                                  Identifier    : Node_Id;
                                  Is_Refinement : Boolean)
                                 return Node_Id
   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;

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

      Inverse_Of      : Node_Id := No_Node;
      Group_Type_Ref  : Node_Id := No_Node;
      Feature_Group_Spec : Node_Id := No_Node;
      Code            : Parsing_Code;
      OK              : Boolean;
      Loc             : Location;

   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);
         Group_Type_Ref := P_Entity_Reference
           (PC_Unique_Feature_Group_Type_Reference);

         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 =>
               DPE (Code,  EMC_Not_Allowed_In_AADL_V1);
               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;

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

      OK := P_Property_Associations
        (Feature_Group_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;

976
      Set_Entity_Ref (Feature_Group_Spec, Group_Type_Ref);
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
      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;
      Refinable : Boolean)
     return Node_Id
   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
      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);
      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
               return P_In_Out_Item (Container => Container,
                                  Identifier => Identifier,
                                  Is_Refinement => True,
                                  Code => PC_Port_Refinement);
            else
               return P_In_Out_Item (Container => Container,
                                     Identifier => Identifier,
                                     Is_Refinement => False,
                                     Code => PC_Port_Spec);
            end if;

            --  parse port_group_spec or port_group_refinement or
            --        feature_group_spec or feature_group_refinement
         when T_Port | T_Feature =>
            return P_Feature_Group_Spec (Container => Container,
                                         Identifier => Identifier,
                                         Is_Refinement => Is_Refinement);

         when others =>

            if Is_Refinement then
               case AADL_Version is
                  when AADL_V1 =>
                     DPE (PC_Port_Refinement_Or_Port_Group_Refinement,
                          (T_In, T_Out, T_Port));
                  when AADL_V2 =>
                     DPE (PC_Feature_Refinement_Or_Feature_Group_Refinement,
                          (T_In, T_Out, T_Port, T_Feature));
               end case;
            else
               DPE (PC_Feature_Group_Or_Port_Group_Or_Port_Spec,
                    (T_In, T_Out, T_Port, T_Feature));
            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;