OSDN Git Service

2010-10-11 Gary Dismukes <dismukes@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch4.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 4                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order
28 --  by RM section rather than alphabetical
29
30 with Stringt; use Stringt;
31
32 separate (Par)
33 package body Ch4 is
34
35    --  Attributes that cannot have arguments
36
37    Is_Parameterless_Attribute : constant Attribute_Class_Array :=
38      (Attribute_Body_Version => True,
39       Attribute_External_Tag => True,
40       Attribute_Img          => True,
41       Attribute_Version      => True,
42       Attribute_Base         => True,
43       Attribute_Class        => True,
44       Attribute_Stub_Type    => True,
45       Attribute_Type_Key     => True,
46       others                 => False);
47    --  This map contains True for parameterless attributes that return a
48    --  string or a type. For those attributes, a left parenthesis after
49    --  the attribute should not be analyzed as the beginning of a parameters
50    --  list because it may denote a slice operation (X'Img (1 .. 2)) or
51    --  a type conversion (X'Class (Y)).
52
53    --  Note that this map designates the minimum set of attributes where a
54    --  construct in parentheses that is not an argument can appear right
55    --  after the attribute. For attributes like 'Size, we do not put them
56    --  in the map. If someone writes X'Size (3), that's illegal in any case,
57    --  but we get a better error message by parsing the (3) as an illegal
58    --  argument to the attribute, rather than some meaningless junk that
59    --  follows the attribute.
60
61    -----------------------
62    -- Local Subprograms --
63    -----------------------
64
65    function P_Aggregate_Or_Paren_Expr                 return Node_Id;
66    function P_Allocator                               return Node_Id;
67    function P_Case_Expression_Alternative             return Node_Id;
68    function P_Record_Or_Array_Component_Association   return Node_Id;
69    function P_Factor                                  return Node_Id;
70    function P_Primary                                 return Node_Id;
71    function P_Relation                                return Node_Id;
72    function P_Term                                    return Node_Id;
73
74    function P_Binary_Adding_Operator                  return Node_Kind;
75    function P_Logical_Operator                        return Node_Kind;
76    function P_Multiplying_Operator                    return Node_Kind;
77    function P_Relational_Operator                     return Node_Kind;
78    function P_Unary_Adding_Operator                   return Node_Kind;
79
80    procedure Bad_Range_Attribute (Loc : Source_Ptr);
81    --  Called to place complaint about bad range attribute at the given
82    --  source location. Terminates by raising Error_Resync.
83
84    procedure P_Membership_Test (N : Node_Id);
85    --  N is the node for a N_In or N_Not_In node whose right operand has not
86    --  yet been processed. It is called just after scanning out the IN keyword.
87    --  On return, either Right_Opnd or Alternatives is set, as appropriate.
88
89    function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
90    --  Scan a range attribute reference. The caller has scanned out the
91    --  prefix. The current token is known to be an apostrophe and the
92    --  following token is known to be RANGE.
93
94    -------------------------
95    -- Bad_Range_Attribute --
96    -------------------------
97
98    procedure Bad_Range_Attribute (Loc : Source_Ptr) is
99    begin
100       Error_Msg ("range attribute cannot be used in expression!", Loc);
101       Resync_Expression;
102    end Bad_Range_Attribute;
103
104    --------------------------
105    -- 4.1  Name (also 6.4) --
106    --------------------------
107
108    --  NAME ::=
109    --    DIRECT_NAME        | EXPLICIT_DEREFERENCE
110    --  | INDEXED_COMPONENT  | SLICE
111    --  | SELECTED_COMPONENT | ATTRIBUTE
112    --  | TYPE_CONVERSION    | FUNCTION_CALL
113    --  | CHARACTER_LITERAL
114
115    --  DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
116
117    --  PREFIX ::= NAME | IMPLICIT_DEREFERENCE
118
119    --  EXPLICIT_DEREFERENCE ::= NAME . all
120
121    --  IMPLICIT_DEREFERENCE ::= NAME
122
123    --  INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
124
125    --  SLICE ::= PREFIX (DISCRETE_RANGE)
126
127    --  SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
128
129    --  SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
130
131    --  ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
132
133    --  ATTRIBUTE_DESIGNATOR ::=
134    --    IDENTIFIER [(static_EXPRESSION)]
135    --  | access | delta | digits
136
137    --  FUNCTION_CALL ::=
138    --    function_NAME
139    --  | function_PREFIX ACTUAL_PARAMETER_PART
140
141    --  ACTUAL_PARAMETER_PART ::=
142    --    (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
143
144    --  PARAMETER_ASSOCIATION ::=
145    --    [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
146
147    --  EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
148
149    --  Note: syntactically a procedure call looks just like a function call,
150    --  so this routine is in practice used to scan out procedure calls as well.
151
152    --  On return, Expr_Form is set to either EF_Name or EF_Simple_Name
153
154    --  Error recovery: can raise Error_Resync
155
156    --  Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
157    --  followed by either a left paren (qualified expression case), or by
158    --  range (range attribute case). All other uses of apostrophe (i.e. all
159    --  other attributes) are handled in this routine.
160
161    --  Error recovery: can raise Error_Resync
162
163    function P_Name return Node_Id is
164       Scan_State  : Saved_Scan_State;
165       Name_Node   : Node_Id;
166       Prefix_Node : Node_Id;
167       Ident_Node  : Node_Id;
168       Expr_Node   : Node_Id;
169       Range_Node  : Node_Id;
170       Arg_Node    : Node_Id;
171
172       Arg_List  : List_Id := No_List; -- kill junk warning
173       Attr_Name : Name_Id := No_Name; -- kill junk warning
174
175    begin
176       --  Case of not a name
177
178       if Token not in Token_Class_Name then
179
180          --  If it looks like start of expression, complain and scan expression
181
182          if Token in Token_Class_Literal
183            or else Token = Tok_Left_Paren
184          then
185             Error_Msg_SC ("name expected");
186             return P_Expression;
187
188          --  Otherwise some other junk, not much we can do
189
190          else
191             Error_Msg_AP ("name expected");
192             raise Error_Resync;
193          end if;
194       end if;
195
196       --  Loop through designators in qualified name
197
198       Name_Node := Token_Node;
199
200       loop
201          Scan; -- past designator
202          exit when Token /= Tok_Dot;
203          Save_Scan_State (Scan_State); -- at dot
204          Scan; -- past dot
205
206          --  If we do not have another designator after the dot, then join
207          --  the normal circuit to handle a dot extension (may be .all or
208          --  character literal case). Otherwise loop back to scan the next
209          --  designator.
210
211          if Token not in Token_Class_Desig then
212             goto Scan_Name_Extension_Dot;
213          else
214             Prefix_Node := Name_Node;
215             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
216             Set_Prefix (Name_Node, Prefix_Node);
217             Set_Selector_Name (Name_Node, Token_Node);
218          end if;
219       end loop;
220
221       --  We have now scanned out a qualified designator. If the last token is
222       --  an operator symbol, then we certainly do not have the Snam case, so
223       --  we can just use the normal name extension check circuit
224
225       if Prev_Token = Tok_Operator_Symbol then
226          goto Scan_Name_Extension;
227       end if;
228
229       --  We have scanned out a qualified simple name, check for name extension
230       --  Note that we know there is no dot here at this stage, so the only
231       --  possible cases of name extension are apostrophe and left paren.
232
233       if Token = Tok_Apostrophe then
234          Save_Scan_State (Scan_State); -- at apostrophe
235          Scan; -- past apostrophe
236
237          --  Qualified expression in Ada 2012 mode (treated as a name)
238
239          if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
240             goto Scan_Name_Extension_Apostrophe;
241
242          --  If left paren not in Ada 2012, then it is not part of the name,
243          --  since qualified expressions are not names in prior versions of
244          --  Ada, so return with Token backed up to point to the apostrophe.
245          --  The treatment for the range attribute is similar (we do not
246          --  consider x'range to be a name in this grammar).
247
248          elsif Token = Tok_Left_Paren or else Token = Tok_Range then
249             Restore_Scan_State (Scan_State); -- to apostrophe
250             Expr_Form := EF_Simple_Name;
251             return Name_Node;
252
253          --  Otherwise we have the case of a name extended by an attribute
254
255          else
256             goto Scan_Name_Extension_Apostrophe;
257          end if;
258
259       --  Check case of qualified simple name extended by a left parenthesis
260
261       elsif Token = Tok_Left_Paren then
262          Scan; -- past left paren
263          goto Scan_Name_Extension_Left_Paren;
264
265       --  Otherwise the qualified simple name is not extended, so return
266
267       else
268          Expr_Form := EF_Simple_Name;
269          return Name_Node;
270       end if;
271
272       --  Loop scanning past name extensions. A label is used for control
273       --  transfer for this loop for ease of interfacing with the finite state
274       --  machine in the parenthesis scanning circuit, and also to allow for
275       --  passing in control to the appropriate point from the above code.
276
277       <<Scan_Name_Extension>>
278
279          --  Character literal used as name cannot be extended. Also this
280          --  cannot be a call, since the name for a call must be a designator.
281          --  Return in these cases, or if there is no name extension
282
283          if Token not in Token_Class_Namext
284            or else Prev_Token = Tok_Char_Literal
285          then
286             Expr_Form := EF_Name;
287             return Name_Node;
288          end if;
289
290       --  Merge here when we know there is a name extension
291
292       <<Scan_Name_Extension_OK>>
293
294          if Token = Tok_Left_Paren then
295             Scan; -- past left paren
296             goto Scan_Name_Extension_Left_Paren;
297
298          elsif Token = Tok_Apostrophe then
299             Save_Scan_State (Scan_State); -- at apostrophe
300             Scan; -- past apostrophe
301             goto Scan_Name_Extension_Apostrophe;
302
303          else -- Token = Tok_Dot
304             Save_Scan_State (Scan_State); -- at dot
305             Scan; -- past dot
306             goto Scan_Name_Extension_Dot;
307          end if;
308
309       --  Case of name extended by dot (selection), dot is already skipped
310       --  and the scan state at the point of the dot is saved in Scan_State.
311
312       <<Scan_Name_Extension_Dot>>
313
314          --  Explicit dereference case
315
316          if Token = Tok_All then
317             Prefix_Node := Name_Node;
318             Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
319             Set_Prefix (Name_Node, Prefix_Node);
320             Scan; -- past ALL
321             goto Scan_Name_Extension;
322
323          --  Selected component case
324
325          elsif Token in Token_Class_Name then
326             Prefix_Node := Name_Node;
327             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
328             Set_Prefix (Name_Node, Prefix_Node);
329             Set_Selector_Name (Name_Node, Token_Node);
330             Scan; -- past selector
331             goto Scan_Name_Extension;
332
333          --  Reserved identifier as selector
334
335          elsif Is_Reserved_Identifier then
336             Scan_Reserved_Identifier (Force_Msg => False);
337             Prefix_Node := Name_Node;
338             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
339             Set_Prefix (Name_Node, Prefix_Node);
340             Set_Selector_Name (Name_Node, Token_Node);
341             Scan; -- past identifier used as selector
342             goto Scan_Name_Extension;
343
344          --  If dot is at end of line and followed by nothing legal,
345          --  then assume end of name and quit (dot will be taken as
346          --  an erroneous form of some other punctuation by our caller).
347
348          elsif Token_Is_At_Start_Of_Line then
349             Restore_Scan_State (Scan_State);
350             return Name_Node;
351
352          --  Here if nothing legal after the dot
353
354          else
355             Error_Msg_AP ("selector expected");
356             raise Error_Resync;
357          end if;
358
359       --  Here for an apostrophe as name extension. The scan position at the
360       --  apostrophe has already been saved, and the apostrophe scanned out.
361
362       <<Scan_Name_Extension_Apostrophe>>
363
364          Scan_Apostrophe : declare
365             function Apostrophe_Should_Be_Semicolon return Boolean;
366             --  Checks for case where apostrophe should probably be
367             --  a semicolon, and if so, gives appropriate message,
368             --  resets the scan pointer to the apostrophe, changes
369             --  the current token to Tok_Semicolon, and returns True.
370             --  Otherwise returns False.
371
372             ------------------------------------
373             -- Apostrophe_Should_Be_Semicolon --
374             ------------------------------------
375
376             function Apostrophe_Should_Be_Semicolon return Boolean is
377             begin
378                if Token_Is_At_Start_Of_Line then
379                   Restore_Scan_State (Scan_State); -- to apostrophe
380                   Error_Msg_SC ("|""''"" should be "";""");
381                   Token := Tok_Semicolon;
382                   return True;
383                else
384                   return False;
385                end if;
386             end Apostrophe_Should_Be_Semicolon;
387
388          --  Start of processing for Scan_Apostrophe
389
390          begin
391             --  Check for qualified expression case in Ada 2012 mode
392
393             if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
394                Name_Node := P_Qualified_Expression (Name_Node);
395                goto Scan_Name_Extension;
396
397             --  If range attribute after apostrophe, then return with Token
398             --  pointing to the apostrophe. Note that in this case the prefix
399             --  need not be a simple name (cases like A.all'range). Similarly
400             --  if there is a left paren after the apostrophe, then we also
401             --  return with Token pointing to the apostrophe (this is the
402             --  aggregate case, or some error case).
403
404             elsif Token = Tok_Range or else Token = Tok_Left_Paren then
405                Restore_Scan_State (Scan_State); -- to apostrophe
406                Expr_Form := EF_Name;
407                return Name_Node;
408
409             --  Here for cases where attribute designator is an identifier
410
411             elsif Token = Tok_Identifier then
412                Attr_Name := Token_Name;
413
414                if not Is_Attribute_Name (Attr_Name) then
415                   if Apostrophe_Should_Be_Semicolon then
416                      Expr_Form := EF_Name;
417                      return Name_Node;
418
419                   --  Here for a bad attribute name
420
421                   else
422                      Signal_Bad_Attribute;
423                      Scan; -- past bad identifier
424
425                      if Token = Tok_Left_Paren then
426                         Scan; -- past left paren
427
428                         loop
429                            Discard_Junk_Node (P_Expression_If_OK);
430                            exit when not  Comma_Present;
431                         end loop;
432
433                         T_Right_Paren;
434                      end if;
435
436                      return Error;
437                   end if;
438                end if;
439
440                if Style_Check then
441                   Style.Check_Attribute_Name (False);
442                end if;
443
444             --  Here for case of attribute designator is not an identifier
445
446             else
447                if Token = Tok_Delta then
448                   Attr_Name := Name_Delta;
449
450                elsif Token = Tok_Digits then
451                   Attr_Name := Name_Digits;
452
453                elsif Token = Tok_Access then
454                   Attr_Name := Name_Access;
455
456                elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
457                   Attr_Name := Name_Mod;
458
459                elsif Apostrophe_Should_Be_Semicolon then
460                   Expr_Form := EF_Name;
461                   return Name_Node;
462
463                else
464                   Error_Msg_AP ("attribute designator expected");
465                   raise Error_Resync;
466                end if;
467
468                if Style_Check then
469                   Style.Check_Attribute_Name (True);
470                end if;
471             end if;
472
473             --  We come here with an OK attribute scanned, and the
474             --  corresponding Attribute identifier node stored in Ident_Node.
475
476             Prefix_Node := Name_Node;
477             Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
478             Scan; -- past attribute designator
479             Set_Prefix (Name_Node, Prefix_Node);
480             Set_Attribute_Name (Name_Node, Attr_Name);
481
482             --  Scan attribute arguments/designator. We skip this if we know
483             --  that the attribute cannot have an argument.
484
485             if Token = Tok_Left_Paren
486               and then not
487                 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
488             then
489                Set_Expressions (Name_Node, New_List);
490                Scan; -- past left paren
491
492                loop
493                   declare
494                      Expr : constant Node_Id := P_Expression_If_OK;
495
496                   begin
497                      if Token = Tok_Arrow then
498                         Error_Msg_SC
499                           ("named parameters not permitted for attributes");
500                         Scan; -- past junk arrow
501
502                      else
503                         Append (Expr, Expressions (Name_Node));
504                         exit when not Comma_Present;
505                      end if;
506                   end;
507                end loop;
508
509                T_Right_Paren;
510             end if;
511
512             goto Scan_Name_Extension;
513          end Scan_Apostrophe;
514
515       --  Here for left parenthesis extending name (left paren skipped)
516
517       <<Scan_Name_Extension_Left_Paren>>
518
519          --  We now have to scan through a list of items, terminated by a
520          --  right parenthesis. The scan is handled by a finite state
521          --  machine. The possibilities are:
522
523          --   (discrete_range)
524
525          --      This is a slice. This case is handled in LP_State_Init
526
527          --   (expression, expression, ..)
528
529          --      This is interpreted as an indexed component, i.e. as a
530          --      case of a name which can be extended in the normal manner.
531          --      This case is handled by LP_State_Name or LP_State_Expr.
532
533          --      Note: conditional expressions (without an extra level of
534          --      parentheses) are permitted in this context).
535
536          --   (..., identifier => expression , ...)
537
538          --      If there is at least one occurrence of identifier => (but
539          --      none of the other cases apply), then we have a call.
540
541          --  Test for Id => case
542
543          if Token = Tok_Identifier then
544             Save_Scan_State (Scan_State); -- at Id
545             Scan; -- past Id
546
547             --  Test for => (allow := as an error substitute)
548
549             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
550                Restore_Scan_State (Scan_State); -- to Id
551                Arg_List := New_List;
552                goto LP_State_Call;
553
554             else
555                Restore_Scan_State (Scan_State); -- to Id
556             end if;
557          end if;
558
559          --  Here we have an expression after all
560
561          Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
562
563          --  Check cases of discrete range for a slice
564
565          --  First possibility: Range_Attribute_Reference
566
567          if Expr_Form = EF_Range_Attr then
568             Range_Node := Expr_Node;
569
570          --  Second possibility: Simple_expression .. Simple_expression
571
572          elsif Token = Tok_Dot_Dot then
573             Check_Simple_Expression (Expr_Node);
574             Range_Node := New_Node (N_Range, Token_Ptr);
575             Set_Low_Bound (Range_Node, Expr_Node);
576             Scan; -- past ..
577             Expr_Node := P_Expression;
578             Check_Simple_Expression (Expr_Node);
579             Set_High_Bound (Range_Node, Expr_Node);
580
581          --  Third possibility: Type_name range Range
582
583          elsif Token = Tok_Range then
584             if Expr_Form /= EF_Simple_Name then
585                Error_Msg_SC ("subtype mark must precede RANGE");
586                raise Error_Resync;
587             end if;
588
589             Range_Node := P_Subtype_Indication (Expr_Node);
590
591          --  Otherwise we just have an expression. It is true that we might
592          --  have a subtype mark without a range constraint but this case
593          --  is syntactically indistinguishable from the expression case.
594
595          else
596             Arg_List := New_List;
597             goto LP_State_Expr;
598          end if;
599
600          --  Fall through here with unmistakable Discrete range scanned,
601          --  which means that we definitely have the case of a slice. The
602          --  Discrete range is in Range_Node.
603
604          if Token = Tok_Comma then
605             Error_Msg_SC ("slice cannot have more than one dimension");
606             raise Error_Resync;
607
608          elsif Token /= Tok_Right_Paren then
609             T_Right_Paren;
610             raise Error_Resync;
611
612          else
613             Scan; -- past right paren
614             Prefix_Node := Name_Node;
615             Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
616             Set_Prefix (Name_Node, Prefix_Node);
617             Set_Discrete_Range (Name_Node, Range_Node);
618
619             --  An operator node is legal as a prefix to other names,
620             --  but not for a slice.
621
622             if Nkind (Prefix_Node) = N_Operator_Symbol then
623                Error_Msg_N ("illegal prefix for slice", Prefix_Node);
624             end if;
625
626             --  If we have a name extension, go scan it
627
628             if Token in Token_Class_Namext then
629                goto Scan_Name_Extension_OK;
630
631             --  Otherwise return (a slice is a name, but is not a call)
632
633             else
634                Expr_Form := EF_Name;
635                return Name_Node;
636             end if;
637          end if;
638
639       --  In LP_State_Expr, we have scanned one or more expressions, and
640       --  so we have a call or an indexed component which is a name. On
641       --  entry we have the expression just scanned in Expr_Node and
642       --  Arg_List contains the list of expressions encountered so far
643
644       <<LP_State_Expr>>
645          Append (Expr_Node, Arg_List);
646
647          if Token = Tok_Arrow then
648             Error_Msg
649               ("expect identifier in parameter association",
650                 Sloc (Expr_Node));
651             Scan;  --   past arrow.
652
653          elsif not Comma_Present then
654             T_Right_Paren;
655             Prefix_Node := Name_Node;
656             Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
657             Set_Prefix (Name_Node, Prefix_Node);
658             Set_Expressions (Name_Node, Arg_List);
659             goto Scan_Name_Extension;
660          end if;
661
662          --  Comma present (and scanned out), test for identifier => case
663          --  Test for identifier => case
664
665          if Token = Tok_Identifier then
666             Save_Scan_State (Scan_State); -- at Id
667             Scan; -- past Id
668
669             --  Test for => (allow := as error substitute)
670
671             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
672                Restore_Scan_State (Scan_State); -- to Id
673                goto LP_State_Call;
674
675             --  Otherwise it's just an expression after all, so backup
676
677             else
678                Restore_Scan_State (Scan_State); -- to Id
679             end if;
680          end if;
681
682          --  Here we have an expression after all, so stay in this state
683
684          Expr_Node := P_Expression_If_OK;
685          goto LP_State_Expr;
686
687       --  LP_State_Call corresponds to the situation in which at least
688       --  one instance of Id => Expression has been encountered, so we
689       --  know that we do not have a name, but rather a call. We enter
690       --  it with the scan pointer pointing to the next argument to scan,
691       --  and Arg_List containing the list of arguments scanned so far.
692
693       <<LP_State_Call>>
694
695          --  Test for case of Id => Expression (named parameter)
696
697          if Token = Tok_Identifier then
698             Save_Scan_State (Scan_State); -- at Id
699             Ident_Node := Token_Node;
700             Scan; -- past Id
701
702             --  Deal with => (allow := as erroneous substitute)
703
704             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
705                Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
706                Set_Selector_Name (Arg_Node, Ident_Node);
707                T_Arrow;
708                Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
709                Append (Arg_Node, Arg_List);
710
711                --  If a comma follows, go back and scan next entry
712
713                if Comma_Present then
714                   goto LP_State_Call;
715
716                --  Otherwise we have the end of a call
717
718                else
719                   Prefix_Node := Name_Node;
720                   Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
721                   Set_Name (Name_Node, Prefix_Node);
722                   Set_Parameter_Associations (Name_Node, Arg_List);
723                   T_Right_Paren;
724
725                   if Token in Token_Class_Namext then
726                      goto Scan_Name_Extension_OK;
727
728                   --  This is a case of a call which cannot be a name
729
730                   else
731                      Expr_Form := EF_Name;
732                      return Name_Node;
733                   end if;
734                end if;
735
736             --  Not named parameter: Id started an expression after all
737
738             else
739                Restore_Scan_State (Scan_State); -- to Id
740             end if;
741          end if;
742
743          --  Here if entry did not start with Id => which means that it
744          --  is a positional parameter, which is not allowed, since we
745          --  have seen at least one named parameter already.
746
747          Error_Msg_SC
748             ("positional parameter association " &
749               "not allowed after named one");
750
751          Expr_Node := P_Expression_If_OK;
752
753          --  Leaving the '>' in an association is not unusual, so suggest
754          --  a possible fix.
755
756          if Nkind (Expr_Node) = N_Op_Eq then
757             Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
758          end if;
759
760          --  We go back to scanning out expressions, so that we do not get
761          --  multiple error messages when several positional parameters
762          --  follow a named parameter.
763
764          goto LP_State_Expr;
765
766          --  End of treatment for name extensions starting with left paren
767
768       --  End of loop through name extensions
769
770    end P_Name;
771
772    --  This function parses a restricted form of Names which are either
773    --  designators, or designators preceded by a sequence of prefixes
774    --  that are direct names.
775
776    --  Error recovery: cannot raise Error_Resync
777
778    function P_Function_Name return Node_Id is
779       Designator_Node : Node_Id;
780       Prefix_Node     : Node_Id;
781       Selector_Node   : Node_Id;
782       Dot_Sloc        : Source_Ptr := No_Location;
783
784    begin
785       --  Prefix_Node is set to the gathered prefix so far, Empty means that
786       --  no prefix has been scanned. This allows us to build up the result
787       --  in the required right recursive manner.
788
789       Prefix_Node := Empty;
790
791       --  Loop through prefixes
792
793       loop
794          Designator_Node := Token_Node;
795
796          if Token not in Token_Class_Desig then
797             return P_Identifier; -- let P_Identifier issue the error message
798
799          else -- Token in Token_Class_Desig
800             Scan; -- past designator
801             exit when Token /= Tok_Dot;
802          end if;
803
804          --  Here at a dot, with token just before it in Designator_Node
805
806          if No (Prefix_Node) then
807             Prefix_Node := Designator_Node;
808          else
809             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
810             Set_Prefix (Selector_Node, Prefix_Node);
811             Set_Selector_Name (Selector_Node, Designator_Node);
812             Prefix_Node := Selector_Node;
813          end if;
814
815          Dot_Sloc := Token_Ptr;
816          Scan; -- past dot
817       end loop;
818
819       --  Fall out of the loop having just scanned a designator
820
821       if No (Prefix_Node) then
822          return Designator_Node;
823       else
824          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
825          Set_Prefix (Selector_Node, Prefix_Node);
826          Set_Selector_Name (Selector_Node, Designator_Node);
827          return Selector_Node;
828       end if;
829
830    exception
831       when Error_Resync =>
832          return Error;
833    end P_Function_Name;
834
835    --  This function parses a restricted form of Names which are either
836    --  identifiers, or identifiers preceded by a sequence of prefixes
837    --  that are direct names.
838
839    --  Error recovery: cannot raise Error_Resync
840
841    function P_Qualified_Simple_Name return Node_Id is
842       Designator_Node : Node_Id;
843       Prefix_Node     : Node_Id;
844       Selector_Node   : Node_Id;
845       Dot_Sloc        : Source_Ptr := No_Location;
846
847    begin
848       --  Prefix node is set to the gathered prefix so far, Empty means that
849       --  no prefix has been scanned. This allows us to build up the result
850       --  in the required right recursive manner.
851
852       Prefix_Node := Empty;
853
854       --  Loop through prefixes
855
856       loop
857          Designator_Node := Token_Node;
858
859          if Token = Tok_Identifier then
860             Scan; -- past identifier
861             exit when Token /= Tok_Dot;
862
863          elsif Token not in Token_Class_Desig then
864             return P_Identifier; -- let P_Identifier issue the error message
865
866          else
867             Scan; -- past designator
868
869             if Token /= Tok_Dot then
870                Error_Msg_SP ("identifier expected");
871                return Error;
872             end if;
873          end if;
874
875          --  Here at a dot, with token just before it in Designator_Node
876
877          if No (Prefix_Node) then
878             Prefix_Node := Designator_Node;
879          else
880             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
881             Set_Prefix (Selector_Node, Prefix_Node);
882             Set_Selector_Name (Selector_Node, Designator_Node);
883             Prefix_Node := Selector_Node;
884          end if;
885
886          Dot_Sloc := Token_Ptr;
887          Scan; -- past dot
888       end loop;
889
890       --  Fall out of the loop having just scanned an identifier
891
892       if No (Prefix_Node) then
893          return Designator_Node;
894       else
895          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
896          Set_Prefix (Selector_Node, Prefix_Node);
897          Set_Selector_Name (Selector_Node, Designator_Node);
898          return Selector_Node;
899       end if;
900
901    exception
902       when Error_Resync =>
903          return Error;
904    end P_Qualified_Simple_Name;
905
906    --  This procedure differs from P_Qualified_Simple_Name only in that it
907    --  raises Error_Resync if any error is encountered. It only returns after
908    --  scanning a valid qualified simple name.
909
910    --  Error recovery: can raise Error_Resync
911
912    function P_Qualified_Simple_Name_Resync return Node_Id is
913       Designator_Node : Node_Id;
914       Prefix_Node     : Node_Id;
915       Selector_Node   : Node_Id;
916       Dot_Sloc        : Source_Ptr := No_Location;
917
918    begin
919       Prefix_Node := Empty;
920
921       --  Loop through prefixes
922
923       loop
924          Designator_Node := Token_Node;
925
926          if Token = Tok_Identifier then
927             Scan; -- past identifier
928             exit when Token /= Tok_Dot;
929
930          elsif Token not in Token_Class_Desig then
931             Discard_Junk_Node (P_Identifier); -- to issue the error message
932             raise Error_Resync;
933
934          else
935             Scan; -- past designator
936
937             if Token /= Tok_Dot then
938                Error_Msg_SP ("identifier expected");
939                raise Error_Resync;
940             end if;
941          end if;
942
943          --  Here at a dot, with token just before it in Designator_Node
944
945          if No (Prefix_Node) then
946             Prefix_Node := Designator_Node;
947          else
948             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
949             Set_Prefix (Selector_Node, Prefix_Node);
950             Set_Selector_Name (Selector_Node, Designator_Node);
951             Prefix_Node := Selector_Node;
952          end if;
953
954          Dot_Sloc := Token_Ptr;
955          Scan; -- past period
956       end loop;
957
958       --  Fall out of the loop having just scanned an identifier
959
960       if No (Prefix_Node) then
961          return Designator_Node;
962       else
963          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
964          Set_Prefix (Selector_Node, Prefix_Node);
965          Set_Selector_Name (Selector_Node, Designator_Node);
966          return Selector_Node;
967       end if;
968    end P_Qualified_Simple_Name_Resync;
969
970    ----------------------
971    -- 4.1  Direct_Name --
972    ----------------------
973
974    --  Parsed by P_Name and other functions in section 4.1
975
976    -----------------
977    -- 4.1  Prefix --
978    -----------------
979
980    --  Parsed by P_Name (4.1)
981
982    -------------------------------
983    -- 4.1  Explicit Dereference --
984    -------------------------------
985
986    --  Parsed by P_Name (4.1)
987
988    -------------------------------
989    -- 4.1  Implicit_Dereference --
990    -------------------------------
991
992    --  Parsed by P_Name (4.1)
993
994    ----------------------------
995    -- 4.1  Indexed Component --
996    ----------------------------
997
998    --  Parsed by P_Name (4.1)
999
1000    ----------------
1001    -- 4.1  Slice --
1002    ----------------
1003
1004    --  Parsed by P_Name (4.1)
1005
1006    -----------------------------
1007    -- 4.1  Selected_Component --
1008    -----------------------------
1009
1010    --  Parsed by P_Name (4.1)
1011
1012    ------------------------
1013    -- 4.1  Selector Name --
1014    ------------------------
1015
1016    --  Parsed by P_Name (4.1)
1017
1018    ------------------------------
1019    -- 4.1  Attribute Reference --
1020    ------------------------------
1021
1022    --  Parsed by P_Name (4.1)
1023
1024    -------------------------------
1025    -- 4.1  Attribute Designator --
1026    -------------------------------
1027
1028    --  Parsed by P_Name (4.1)
1029
1030    --------------------------------------
1031    -- 4.1.4  Range Attribute Reference --
1032    --------------------------------------
1033
1034    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1035
1036    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1037
1038    --  In the grammar, a RANGE attribute is simply a name, but its use is
1039    --  highly restricted, so in the parser, we do not regard it as a name.
1040    --  Instead, P_Name returns without scanning the 'RANGE part of the
1041    --  attribute, and the caller uses the following function to construct
1042    --  a range attribute in places where it is appropriate.
1043
1044    --  Note that RANGE here is treated essentially as an identifier,
1045    --  rather than a reserved word.
1046
1047    --  The caller has parsed the prefix, i.e. a name, and Token points to
1048    --  the apostrophe. The token after the apostrophe is known to be RANGE
1049    --  at this point. The prefix node becomes the prefix of the attribute.
1050
1051    --  Error_Recovery: Cannot raise Error_Resync
1052
1053    function P_Range_Attribute_Reference
1054      (Prefix_Node : Node_Id)
1055       return        Node_Id
1056    is
1057       Attr_Node  : Node_Id;
1058
1059    begin
1060       Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1061       Set_Prefix (Attr_Node, Prefix_Node);
1062       Scan; -- past apostrophe
1063
1064       if Style_Check then
1065          Style.Check_Attribute_Name (True);
1066       end if;
1067
1068       Set_Attribute_Name (Attr_Node, Name_Range);
1069       Scan; -- past RANGE
1070
1071       if Token = Tok_Left_Paren then
1072          Scan; -- past left paren
1073          Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1074          T_Right_Paren;
1075       end if;
1076
1077       return Attr_Node;
1078    end P_Range_Attribute_Reference;
1079
1080    ---------------------------------------
1081    -- 4.1.4  Range Attribute Designator --
1082    ---------------------------------------
1083
1084    --  Parsed by P_Range_Attribute_Reference (4.4)
1085
1086    --------------------
1087    -- 4.3  Aggregate --
1088    --------------------
1089
1090    --  AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1091
1092    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1093    --  an aggregate is known to be required (code statement, extension
1094    --  aggregate), in which cases this routine performs the necessary check
1095    --  that we have an aggregate rather than a parenthesized expression
1096
1097    --  Error recovery: can raise Error_Resync
1098
1099    function P_Aggregate return Node_Id is
1100       Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1101       Aggr_Node : constant Node_Id    := P_Aggregate_Or_Paren_Expr;
1102
1103    begin
1104       if Nkind (Aggr_Node) /= N_Aggregate
1105            and then
1106          Nkind (Aggr_Node) /= N_Extension_Aggregate
1107       then
1108          Error_Msg
1109            ("aggregate may not have single positional component", Aggr_Sloc);
1110          return Error;
1111       else
1112          return Aggr_Node;
1113       end if;
1114    end P_Aggregate;
1115
1116    ------------------------------------------------
1117    -- 4.3  Aggregate or Parenthesized Expression --
1118    ------------------------------------------------
1119
1120    --  This procedure parses out either an aggregate or a parenthesized
1121    --  expression (these two constructs are closely related, since a
1122    --  parenthesized expression looks like an aggregate with a single
1123    --  positional component).
1124
1125    --  AGGREGATE ::=
1126    --    RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1127
1128    --  RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1129
1130    --  RECORD_COMPONENT_ASSOCIATION_LIST ::=
1131    --     RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1132    --   | null record
1133
1134    --  RECORD_COMPONENT_ASSOCIATION ::=
1135    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1136
1137    --  COMPONENT_CHOICE_LIST ::=
1138    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1139    --  | others
1140
1141    --  EXTENSION_AGGREGATE ::=
1142    --    (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1143
1144    --  ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1145
1146    --  ARRAY_AGGREGATE ::=
1147    --    POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1148
1149    --  POSITIONAL_ARRAY_AGGREGATE ::=
1150    --    (EXPRESSION, EXPRESSION {, EXPRESSION})
1151    --  | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1152    --  | (EXPRESSION {, EXPRESSION}, others => <>)
1153
1154    --  NAMED_ARRAY_AGGREGATE ::=
1155    --    (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1156
1157    --  PRIMARY ::= (EXPRESSION);
1158
1159    --  Error recovery: can raise Error_Resync
1160
1161    --  Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1162    --        to Ada 2005 limited aggregates (AI-287)
1163
1164    function P_Aggregate_Or_Paren_Expr return Node_Id is
1165       Aggregate_Node : Node_Id;
1166       Expr_List      : List_Id;
1167       Assoc_List     : List_Id;
1168       Expr_Node      : Node_Id;
1169       Lparen_Sloc    : Source_Ptr;
1170       Scan_State     : Saved_Scan_State;
1171
1172       procedure Box_Error;
1173       --  Called if <> is encountered as positional aggregate element. Issues
1174       --  error message and sets Expr_Node to Error.
1175
1176       ---------------
1177       -- Box_Error --
1178       ---------------
1179
1180       procedure Box_Error is
1181       begin
1182          if Ada_Version < Ada_2005 then
1183             Error_Msg_SC ("box in aggregate is an Ada 2005 extension");
1184          end if;
1185
1186          --  Ada 2005 (AI-287): The box notation is allowed only with named
1187          --  notation because positional notation might be error prone. For
1188          --  example, in "(X, <>, Y, <>)", there is no type associated with
1189          --  the boxes, so you might not be leaving out the components you
1190          --  thought you were leaving out.
1191
1192          Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1193          Scan; -- past box
1194          Expr_Node := Error;
1195       end Box_Error;
1196
1197    --  Start of processsing for P_Aggregate_Or_Paren_Expr
1198
1199    begin
1200       Lparen_Sloc := Token_Ptr;
1201       T_Left_Paren;
1202
1203       --  Conditional expression case
1204
1205       if Token = Tok_If then
1206          Expr_Node := P_Conditional_Expression;
1207          T_Right_Paren;
1208          return Expr_Node;
1209
1210       --  Case expression case
1211
1212       elsif Token = Tok_Case then
1213          Expr_Node := P_Case_Expression;
1214          T_Right_Paren;
1215          return Expr_Node;
1216
1217       --  Note: the mechanism used here of rescanning the initial expression
1218       --  is distinctly unpleasant, but it saves a lot of fiddling in scanning
1219       --  out the discrete choice list.
1220
1221       --  Deal with expression and extension aggregate cases first
1222
1223       elsif Token /= Tok_Others then
1224          Save_Scan_State (Scan_State); -- at start of expression
1225
1226          --  Deal with (NULL RECORD) case
1227
1228          if Token = Tok_Null then
1229             Scan; -- past NULL
1230
1231             if Token = Tok_Record then
1232                Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1233                Set_Null_Record_Present (Aggregate_Node, True);
1234                Scan; -- past RECORD
1235                T_Right_Paren;
1236                return Aggregate_Node;
1237             else
1238                Restore_Scan_State (Scan_State); -- to NULL that must be expr
1239             end if;
1240          end if;
1241
1242          --  Scan expression, handling box appearing as positional argument
1243
1244          if Token = Tok_Box then
1245             Box_Error;
1246          else
1247             Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1248          end if;
1249
1250          --  Extension aggregate case
1251
1252          if Token = Tok_With then
1253             if Nkind (Expr_Node) = N_Attribute_Reference
1254               and then Attribute_Name (Expr_Node) = Name_Range
1255             then
1256                Bad_Range_Attribute (Sloc (Expr_Node));
1257                return Error;
1258             end if;
1259
1260             if Ada_Version = Ada_83 then
1261                Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1262             end if;
1263
1264             Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1265             Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1266             Scan; -- past WITH
1267
1268             --  Deal with WITH NULL RECORD case
1269
1270             if Token = Tok_Null then
1271                Save_Scan_State (Scan_State); -- at NULL
1272                Scan; -- past NULL
1273
1274                if Token = Tok_Record then
1275                   Scan; -- past RECORD
1276                   Set_Null_Record_Present (Aggregate_Node, True);
1277                   T_Right_Paren;
1278                   return Aggregate_Node;
1279
1280                else
1281                   Restore_Scan_State (Scan_State); -- to NULL that must be expr
1282                end if;
1283             end if;
1284
1285             if Token /= Tok_Others then
1286                Save_Scan_State (Scan_State);
1287                Expr_Node := P_Expression;
1288             else
1289                Expr_Node := Empty;
1290             end if;
1291
1292          --  Expression case
1293
1294          elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1295             if Nkind (Expr_Node) = N_Attribute_Reference
1296               and then Attribute_Name (Expr_Node) = Name_Range
1297             then
1298                Error_Msg
1299                  ("|parentheses not allowed for range attribute", Lparen_Sloc);
1300                Scan; -- past right paren
1301                return Expr_Node;
1302             end if;
1303
1304             --  Bump paren count of expression
1305
1306             if Expr_Node /= Error then
1307                Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1308             end if;
1309
1310             T_Right_Paren; -- past right paren (error message if none)
1311             return Expr_Node;
1312
1313          --  Normal aggregate case
1314
1315          else
1316             Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1317          end if;
1318
1319       --  Others case
1320
1321       else
1322          Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1323          Expr_Node := Empty;
1324       end if;
1325
1326       --  Prepare to scan list of component associations
1327
1328       Expr_List  := No_List; -- don't set yet, maybe all named entries
1329       Assoc_List := No_List; -- don't set yet, maybe all positional entries
1330
1331       --  This loop scans through component associations. On entry to the
1332       --  loop, an expression has been scanned at the start of the current
1333       --  association unless initial token was OTHERS, in which case
1334       --  Expr_Node is set to Empty.
1335
1336       loop
1337          --  Deal with others association first. This is a named association
1338
1339          if No (Expr_Node) then
1340             if No (Assoc_List) then
1341                Assoc_List := New_List;
1342             end if;
1343
1344             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1345
1346          --  Improper use of WITH
1347
1348          elsif Token = Tok_With then
1349             Error_Msg_SC ("WITH must be preceded by single expression in " &
1350                              "extension aggregate");
1351             raise Error_Resync;
1352
1353          --  Range attribute can only appear as part of a discrete choice list
1354
1355          elsif Nkind (Expr_Node) = N_Attribute_Reference
1356            and then Attribute_Name (Expr_Node) = Name_Range
1357            and then Token /= Tok_Arrow
1358            and then Token /= Tok_Vertical_Bar
1359          then
1360             Bad_Range_Attribute (Sloc (Expr_Node));
1361             return Error;
1362
1363          --  Assume positional case if comma, right paren, or literal or
1364          --  identifier or OTHERS follows (the latter cases are missing
1365          --  comma cases). Also assume positional if a semicolon follows,
1366          --  which can happen if there are missing parens
1367
1368          elsif Token = Tok_Comma
1369            or else Token = Tok_Right_Paren
1370            or else Token = Tok_Others
1371            or else Token in Token_Class_Lit_Or_Name
1372            or else Token = Tok_Semicolon
1373          then
1374             if Present (Assoc_List) then
1375                Error_Msg_BC -- CODEFIX
1376                   ("""='>"" expected (positional association cannot follow " &
1377                    "named association)");
1378             end if;
1379
1380             if No (Expr_List) then
1381                Expr_List := New_List;
1382             end if;
1383
1384             Append (Expr_Node, Expr_List);
1385
1386          --  Check for aggregate followed by left parent, maybe missing comma
1387
1388          elsif Nkind (Expr_Node) = N_Aggregate
1389            and then Token = Tok_Left_Paren
1390          then
1391             T_Comma;
1392
1393             if No (Expr_List) then
1394                Expr_List := New_List;
1395             end if;
1396
1397             Append (Expr_Node, Expr_List);
1398
1399          --  Anything else is assumed to be a named association
1400
1401          else
1402             Restore_Scan_State (Scan_State); -- to start of expression
1403
1404             if No (Assoc_List) then
1405                Assoc_List := New_List;
1406             end if;
1407
1408             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1409          end if;
1410
1411          exit when not Comma_Present;
1412
1413          --  If we are at an expression terminator, something is seriously
1414          --  wrong, so let's get out now, before we start eating up stuff
1415          --  that doesn't belong to us!
1416
1417          if Token in Token_Class_Eterm then
1418             Error_Msg_AP ("expecting expression or component association");
1419             exit;
1420          end if;
1421
1422          --  Deal with misused box
1423
1424          if Token = Tok_Box then
1425             Box_Error;
1426
1427          --  Otherwise initiate for reentry to top of loop by scanning an
1428          --  initial expression, unless the first token is OTHERS.
1429
1430          elsif Token = Tok_Others then
1431             Expr_Node := Empty;
1432
1433          else
1434             Save_Scan_State (Scan_State); -- at start of expression
1435             Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1436
1437          end if;
1438       end loop;
1439
1440       --  All component associations (positional and named) have been scanned
1441
1442       T_Right_Paren;
1443       Set_Expressions (Aggregate_Node, Expr_List);
1444       Set_Component_Associations (Aggregate_Node, Assoc_List);
1445       return Aggregate_Node;
1446    end P_Aggregate_Or_Paren_Expr;
1447
1448    ------------------------------------------------
1449    -- 4.3  Record or Array Component Association --
1450    ------------------------------------------------
1451
1452    --  RECORD_COMPONENT_ASSOCIATION ::=
1453    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1454    --  | COMPONENT_CHOICE_LIST => <>
1455
1456    --  COMPONENT_CHOICE_LIST =>
1457    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1458    --  | others
1459
1460    --  ARRAY_COMPONENT_ASSOCIATION ::=
1461    --    DISCRETE_CHOICE_LIST => EXPRESSION
1462    --  | DISCRETE_CHOICE_LIST => <>
1463
1464    --  Note: this routine only handles the named cases, including others.
1465    --  Cases where the component choice list is not present have already
1466    --  been handled directly.
1467
1468    --  Error recovery: can raise Error_Resync
1469
1470    --  Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1471    --        rules have been extended to give support to Ada 2005 limited
1472    --        aggregates (AI-287)
1473
1474    function P_Record_Or_Array_Component_Association return Node_Id is
1475       Assoc_Node : Node_Id;
1476
1477    begin
1478       Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1479       Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1480       Set_Sloc (Assoc_Node, Token_Ptr);
1481       TF_Arrow;
1482
1483       if Token = Tok_Box then
1484
1485          --  Ada 2005(AI-287): The box notation is used to indicate the
1486          --  default initialization of aggregate components
1487
1488          if Ada_Version < Ada_2005 then
1489             Error_Msg_SP
1490               ("component association with '<'> is an Ada 2005 extension");
1491             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1492          end if;
1493
1494          Set_Box_Present (Assoc_Node);
1495          Scan; -- Past box
1496       else
1497          Set_Expression (Assoc_Node, P_Expression);
1498       end if;
1499
1500       return Assoc_Node;
1501    end P_Record_Or_Array_Component_Association;
1502
1503    -----------------------------
1504    -- 4.3.1  Record Aggregate --
1505    -----------------------------
1506
1507    --  Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1508    --  All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1509
1510    ----------------------------------------------
1511    -- 4.3.1  Record Component Association List --
1512    ----------------------------------------------
1513
1514    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1515
1516    ----------------------------------
1517    -- 4.3.1  Component Choice List --
1518    ----------------------------------
1519
1520    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1521
1522    --------------------------------
1523    -- 4.3.1  Extension Aggregate --
1524    --------------------------------
1525
1526    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1527
1528    --------------------------
1529    -- 4.3.1  Ancestor Part --
1530    --------------------------
1531
1532    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1533
1534    ----------------------------
1535    -- 4.3.1  Array Aggregate --
1536    ----------------------------
1537
1538    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1539
1540    ---------------------------------------
1541    -- 4.3.1  Positional Array Aggregate --
1542    ---------------------------------------
1543
1544    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1545
1546    ----------------------------------
1547    -- 4.3.1  Named Array Aggregate --
1548    ----------------------------------
1549
1550    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1551
1552    ----------------------------------------
1553    -- 4.3.1  Array Component Association --
1554    ----------------------------------------
1555
1556    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1557
1558    ---------------------
1559    -- 4.4  Expression --
1560    ---------------------
1561
1562    --  EXPRESSION ::=
1563    --    RELATION {and RELATION} | RELATION {and then RELATION}
1564    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1565    --  | RELATION {xor RELATION}
1566
1567    --  On return, Expr_Form indicates the categorization of the expression
1568    --  EF_Range_Attr is not a possible value (if a range attribute is found,
1569    --  an error message is given, and Error is returned).
1570
1571    --  Error recovery: cannot raise Error_Resync
1572
1573    function P_Expression return Node_Id is
1574       Logical_Op      : Node_Kind;
1575       Prev_Logical_Op : Node_Kind;
1576       Op_Location     : Source_Ptr;
1577       Node1           : Node_Id;
1578       Node2           : Node_Id;
1579
1580    begin
1581       Node1 := P_Relation;
1582
1583       if Token in Token_Class_Logop then
1584          Prev_Logical_Op := N_Empty;
1585
1586          loop
1587             Op_Location := Token_Ptr;
1588             Logical_Op := P_Logical_Operator;
1589
1590             if Prev_Logical_Op /= N_Empty and then
1591                Logical_Op /= Prev_Logical_Op
1592             then
1593                Error_Msg
1594                  ("mixed logical operators in expression", Op_Location);
1595                Prev_Logical_Op := N_Empty;
1596             else
1597                Prev_Logical_Op := Logical_Op;
1598             end if;
1599
1600             Node2 := Node1;
1601             Node1 := New_Op_Node (Logical_Op, Op_Location);
1602             Set_Left_Opnd (Node1, Node2);
1603             Set_Right_Opnd (Node1, P_Relation);
1604             exit when Token not in Token_Class_Logop;
1605          end loop;
1606
1607          Expr_Form := EF_Non_Simple;
1608       end if;
1609
1610       if Token = Tok_Apostrophe then
1611          Bad_Range_Attribute (Token_Ptr);
1612          return Error;
1613       else
1614          return Node1;
1615       end if;
1616    end P_Expression;
1617
1618    --  This function is identical to the normal P_Expression, except that it
1619    --  also permits the appearence of a case of conditional expression without
1620    --  the usual surrounding parentheses.
1621
1622    function P_Expression_If_OK return Node_Id is
1623    begin
1624       if Token = Tok_Case then
1625          return P_Case_Expression;
1626       elsif Token = Tok_If then
1627          return P_Conditional_Expression;
1628       else
1629          return P_Expression;
1630       end if;
1631    end P_Expression_If_OK;
1632
1633    --  This function is identical to the normal P_Expression, except that it
1634    --  checks that the expression scan did not stop on a right paren. It is
1635    --  called in all contexts where a right parenthesis cannot legitimately
1636    --  follow an expression.
1637
1638    --  Error recovery: can not raise Error_Resync
1639
1640    function P_Expression_No_Right_Paren return Node_Id is
1641       Expr : constant Node_Id := P_Expression;
1642    begin
1643       Ignore (Tok_Right_Paren);
1644       return Expr;
1645    end P_Expression_No_Right_Paren;
1646
1647    ----------------------------------------
1648    -- 4.4  Expression_Or_Range_Attribute --
1649    ----------------------------------------
1650
1651    --  EXPRESSION ::=
1652    --    RELATION {and RELATION} | RELATION {and then RELATION}
1653    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1654    --  | RELATION {xor RELATION}
1655
1656    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1657
1658    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1659
1660    --  On return, Expr_Form indicates the categorization of the expression
1661    --  and EF_Range_Attr is one of the possibilities.
1662
1663    --  Error recovery: cannot raise Error_Resync
1664
1665    --  In the grammar, a RANGE attribute is simply a name, but its use is
1666    --  highly restricted, so in the parser, we do not regard it as a name.
1667    --  Instead, P_Name returns without scanning the 'RANGE part of the
1668    --  attribute, and P_Expression_Or_Range_Attribute handles the range
1669    --  attribute reference. In the normal case where a range attribute is
1670    --  not allowed, an error message is issued by P_Expression.
1671
1672    function P_Expression_Or_Range_Attribute return Node_Id is
1673       Logical_Op      : Node_Kind;
1674       Prev_Logical_Op : Node_Kind;
1675       Op_Location     : Source_Ptr;
1676       Node1           : Node_Id;
1677       Node2           : Node_Id;
1678       Attr_Node       : Node_Id;
1679
1680    begin
1681       Node1 := P_Relation;
1682
1683       if Token = Tok_Apostrophe then
1684          Attr_Node := P_Range_Attribute_Reference (Node1);
1685          Expr_Form := EF_Range_Attr;
1686          return Attr_Node;
1687
1688       elsif Token in Token_Class_Logop then
1689          Prev_Logical_Op := N_Empty;
1690
1691          loop
1692             Op_Location := Token_Ptr;
1693             Logical_Op := P_Logical_Operator;
1694
1695             if Prev_Logical_Op /= N_Empty and then
1696                Logical_Op /= Prev_Logical_Op
1697             then
1698                Error_Msg
1699                  ("mixed logical operators in expression", Op_Location);
1700                Prev_Logical_Op := N_Empty;
1701             else
1702                Prev_Logical_Op := Logical_Op;
1703             end if;
1704
1705             Node2 := Node1;
1706             Node1 := New_Op_Node (Logical_Op, Op_Location);
1707             Set_Left_Opnd (Node1, Node2);
1708             Set_Right_Opnd (Node1, P_Relation);
1709             exit when Token not in Token_Class_Logop;
1710          end loop;
1711
1712          Expr_Form := EF_Non_Simple;
1713       end if;
1714
1715       if Token = Tok_Apostrophe then
1716          Bad_Range_Attribute (Token_Ptr);
1717          return Error;
1718       else
1719          return Node1;
1720       end if;
1721    end P_Expression_Or_Range_Attribute;
1722
1723    --  Version that allows a non-parenthesized case or conditional expression
1724
1725    function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1726    begin
1727       if Token = Tok_Case then
1728          return P_Case_Expression;
1729       elsif Token = Tok_If then
1730          return P_Conditional_Expression;
1731       else
1732          return P_Expression_Or_Range_Attribute;
1733       end if;
1734    end P_Expression_Or_Range_Attribute_If_OK;
1735
1736    -------------------
1737    -- 4.4  Relation --
1738    -------------------
1739
1740    --  RELATION ::=
1741    --    SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1742    --  | SIMPLE_EXPRESSION [not] in RANGE
1743    --  | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1744
1745    --  On return, Expr_Form indicates the categorization of the expression
1746
1747    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1748    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1749
1750    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1751    --  expression, then tokens are scanned until either a non-expression token,
1752    --  a right paren (not matched by a left paren) or a comma, is encountered.
1753
1754    function P_Relation return Node_Id is
1755       Node1, Node2 : Node_Id;
1756       Optok        : Source_Ptr;
1757
1758    begin
1759       Node1 := P_Simple_Expression;
1760
1761       if Token not in Token_Class_Relop then
1762          return Node1;
1763
1764       else
1765          --  Here we have a relational operator following. If so then scan it
1766          --  out. Note that the assignment symbol := is treated as a relational
1767          --  operator to improve the error recovery when it is misused for =.
1768          --  P_Relational_Operator also parses the IN and NOT IN operations.
1769
1770          Optok := Token_Ptr;
1771          Node2 := New_Op_Node (P_Relational_Operator, Optok);
1772          Set_Left_Opnd (Node2, Node1);
1773
1774          --  Case of IN or NOT IN
1775
1776          if Prev_Token = Tok_In then
1777             P_Membership_Test (Node2);
1778
1779          --  Case of relational operator (= /= < <= > >=)
1780
1781          else
1782             Set_Right_Opnd (Node2, P_Simple_Expression);
1783          end if;
1784
1785          Expr_Form := EF_Non_Simple;
1786
1787          if Token in Token_Class_Relop then
1788             Error_Msg_SC ("unexpected relational operator");
1789             raise Error_Resync;
1790          end if;
1791
1792          return Node2;
1793       end if;
1794
1795    --  If any error occurs, then scan to the next expression terminator symbol
1796    --  or comma or right paren at the outer (i.e. current) parentheses level.
1797    --  The flags are set to indicate a normal simple expression.
1798
1799    exception
1800       when Error_Resync =>
1801          Resync_Expression;
1802          Expr_Form := EF_Simple;
1803          return Error;
1804    end P_Relation;
1805
1806    ----------------------------
1807    -- 4.4  Simple Expression --
1808    ----------------------------
1809
1810    --  SIMPLE_EXPRESSION ::=
1811    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1812
1813    --  On return, Expr_Form indicates the categorization of the expression
1814
1815    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1816    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1817
1818    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1819    --  expression, then tokens are scanned until either a non-expression token,
1820    --  a right paren (not matched by a left paren) or a comma, is encountered.
1821
1822    --  Note: P_Simple_Expression is called only internally by higher level
1823    --  expression routines. In cases in the grammar where a simple expression
1824    --  is required, the approach is to scan an expression, and then post an
1825    --  appropriate error message if the expression obtained is not simple. This
1826    --  gives better error recovery and treatment.
1827
1828    function P_Simple_Expression return Node_Id is
1829       Scan_State : Saved_Scan_State;
1830       Node1      : Node_Id;
1831       Node2      : Node_Id;
1832       Tokptr     : Source_Ptr;
1833
1834    begin
1835       --  Check for cases starting with a name. There are two reasons for
1836       --  special casing. First speed things up by catching a common case
1837       --  without going through several routine layers. Second the caller must
1838       --  be informed via Expr_Form when the simple expression is a name.
1839
1840       if Token in Token_Class_Name then
1841          Node1 := P_Name;
1842
1843          --  Deal with apostrophe cases
1844
1845          if Token = Tok_Apostrophe then
1846             Save_Scan_State (Scan_State); -- at apostrophe
1847             Scan; -- past apostrophe
1848
1849             --  If qualified expression, scan it out and fall through
1850
1851             if Token = Tok_Left_Paren then
1852                Node1 := P_Qualified_Expression (Node1);
1853                Expr_Form := EF_Simple;
1854
1855             --  If range attribute, then we return with Token pointing to the
1856             --  apostrophe. Note: avoid the normal error check on exit. We
1857             --  know that the expression really is complete in this case!
1858
1859             else -- Token = Tok_Range then
1860                Restore_Scan_State (Scan_State); -- to apostrophe
1861                Expr_Form := EF_Simple_Name;
1862                return Node1;
1863             end if;
1864          end if;
1865
1866          --  If an expression terminator follows, the previous processing
1867          --  completely scanned out the expression (a common case), and
1868          --  left Expr_Form set appropriately for returning to our caller.
1869
1870          if Token in Token_Class_Sterm then
1871             null;
1872
1873          --  If we do not have an expression terminator, then complete the
1874          --  scan of a simple expression. This code duplicates the code
1875          --  found in P_Term and P_Factor.
1876
1877          else
1878             if Token = Tok_Double_Asterisk then
1879                if Style_Check then
1880                   Style.Check_Exponentiation_Operator;
1881                end if;
1882
1883                Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
1884                Scan; -- past **
1885                Set_Left_Opnd (Node2, Node1);
1886                Set_Right_Opnd (Node2, P_Primary);
1887                Node1 := Node2;
1888             end if;
1889
1890             loop
1891                exit when Token not in Token_Class_Mulop;
1892                Tokptr := Token_Ptr;
1893                Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
1894
1895                if Style_Check then
1896                   Style.Check_Binary_Operator;
1897                end if;
1898
1899                Scan; -- past operator
1900                Set_Left_Opnd (Node2, Node1);
1901                Set_Right_Opnd (Node2, P_Factor);
1902                Node1 := Node2;
1903             end loop;
1904
1905             loop
1906                exit when Token not in Token_Class_Binary_Addop;
1907                Tokptr := Token_Ptr;
1908                Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
1909
1910                if Style_Check then
1911                   Style.Check_Binary_Operator;
1912                end if;
1913
1914                Scan; -- past operator
1915                Set_Left_Opnd (Node2, Node1);
1916                Set_Right_Opnd (Node2, P_Term);
1917                Node1 := Node2;
1918             end loop;
1919
1920             Expr_Form := EF_Simple;
1921          end if;
1922
1923       --  Cases where simple expression does not start with a name
1924
1925       else
1926          --  Scan initial sign and initial Term
1927
1928          if Token in Token_Class_Unary_Addop then
1929             Tokptr := Token_Ptr;
1930             Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
1931
1932             if Style_Check then
1933                Style.Check_Unary_Plus_Or_Minus;
1934             end if;
1935
1936             Scan; -- past operator
1937             Set_Right_Opnd (Node1, P_Term);
1938          else
1939             Node1 := P_Term;
1940          end if;
1941
1942          --  In the following, we special-case a sequence of concatenations of
1943          --  string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
1944          --  else mixed in. For such a sequence, we return a tree representing
1945          --  "" & "aaabbb...ccc" (a single concatenation). This is done only if
1946          --  the number of concatenations is large. If semantic analysis
1947          --  resolves the "&" to a predefined one, then this folding gives the
1948          --  right answer. Otherwise, semantic analysis will complain about a
1949          --  capacity-exceeded error. The purpose of this trick is to avoid
1950          --  creating a deeply nested tree, which would cause deep recursion
1951          --  during semantics, causing stack overflow. This way, we can handle
1952          --  enormous concatenations in the normal case of predefined "&".  We
1953          --  first build up the normal tree, and then rewrite it if
1954          --  appropriate.
1955
1956          declare
1957             Num_Concats_Threshold : constant Positive := 1000;
1958             --  Arbitrary threshold value to enable optimization
1959
1960             First_Node : constant Node_Id := Node1;
1961             Is_Strlit_Concat : Boolean;
1962             --  True iff we've parsed a sequence of concatenations of string
1963             --  literals, with nothing else mixed in.
1964
1965             Num_Concats : Natural;
1966             --  Number of "&" operators if Is_Strlit_Concat is True
1967
1968          begin
1969             Is_Strlit_Concat :=
1970               Nkind (Node1) = N_String_Literal
1971                 and then Token = Tok_Ampersand;
1972             Num_Concats := 0;
1973
1974             --  Scan out sequence of terms separated by binary adding operators
1975
1976             loop
1977                exit when Token not in Token_Class_Binary_Addop;
1978                Tokptr := Token_Ptr;
1979                Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
1980                Scan; -- past operator
1981                Set_Left_Opnd (Node2, Node1);
1982                Node1 := P_Term;
1983                Set_Right_Opnd (Node2, Node1);
1984
1985                --  Check if we're still concatenating string literals
1986
1987                Is_Strlit_Concat :=
1988                  Is_Strlit_Concat
1989                    and then Nkind (Node2) = N_Op_Concat
1990                  and then Nkind (Node1) = N_String_Literal;
1991
1992                if Is_Strlit_Concat then
1993                   Num_Concats := Num_Concats + 1;
1994                end if;
1995
1996                Node1 := Node2;
1997             end loop;
1998
1999             --  If we have an enormous series of concatenations of string
2000             --  literals, rewrite as explained above. The Is_Folded_In_Parser
2001             --  flag tells semantic analysis that if the "&" is not predefined,
2002             --  the folded value is wrong.
2003
2004             if Is_Strlit_Concat
2005               and then Num_Concats >= Num_Concats_Threshold
2006             then
2007                declare
2008                   Empty_String_Val : String_Id;
2009                   --  String_Id for ""
2010
2011                   Strlit_Concat_Val : String_Id;
2012                   --  Contains the folded value (which will be correct if the
2013                   --  "&" operators are the predefined ones).
2014
2015                   Cur_Node : Node_Id;
2016                   --  For walking up the tree
2017
2018                   New_Node : Node_Id;
2019                   --  Folded node to replace Node1
2020
2021                   Loc : constant Source_Ptr := Sloc (First_Node);
2022
2023                begin
2024                   --  Walk up the tree starting at the leftmost string literal
2025                   --  (First_Node), building up the Strlit_Concat_Val as we
2026                   --  go. Note that we do not use recursion here -- the whole
2027                   --  point is to avoid recursively walking that enormous tree.
2028
2029                   Start_String;
2030                   Store_String_Chars (Strval (First_Node));
2031
2032                   Cur_Node := Parent (First_Node);
2033                   while Present (Cur_Node) loop
2034                      pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2035                         Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2036
2037                      Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2038                      Cur_Node := Parent (Cur_Node);
2039                   end loop;
2040
2041                   Strlit_Concat_Val := End_String;
2042
2043                   --  Create new folded node, and rewrite result with a concat-
2044                   --  enation of an empty string literal and the folded node.
2045
2046                   Start_String;
2047                   Empty_String_Val := End_String;
2048                   New_Node :=
2049                     Make_Op_Concat (Loc,
2050                       Make_String_Literal (Loc, Empty_String_Val),
2051                       Make_String_Literal (Loc, Strlit_Concat_Val,
2052                         Is_Folded_In_Parser => True));
2053                   Rewrite (Node1, New_Node);
2054                end;
2055             end if;
2056          end;
2057
2058          --  All done, we clearly do not have name or numeric literal so this
2059          --  is a case of a simple expression which is some other possibility.
2060
2061          Expr_Form := EF_Simple;
2062       end if;
2063
2064       --  Come here at end of simple expression, where we do a couple of
2065       --  special checks to improve error recovery.
2066
2067       --  Special test to improve error recovery. If the current token
2068       --  is a period, then someone is trying to do selection on something
2069       --  that is not a name, e.g. a qualified expression.
2070
2071       if Token = Tok_Dot then
2072          Error_Msg_SC ("prefix for selection is not a name");
2073
2074          --  If qualified expression, comment and continue, otherwise something
2075          --  is pretty nasty so do an Error_Resync call.
2076
2077          if Ada_Version < Ada_2012
2078            and then Nkind (Node1) = N_Qualified_Expression
2079          then
2080             Error_Msg_SC ("\would be legal in Ada 2012 mode");
2081          else
2082             raise Error_Resync;
2083          end if;
2084       end if;
2085
2086       --  Special test to improve error recovery: If the current token is
2087       --  not the first token on a line (as determined by checking the
2088       --  previous token position with the start of the current line),
2089       --  then we insist that we have an appropriate terminating token.
2090       --  Consider the following two examples:
2091
2092       --   1)  if A nad B then ...
2093
2094       --   2)  A := B
2095       --       C := D
2096
2097       --  In the first example, we would like to issue a binary operator
2098       --  expected message and resynchronize to the then. In the second
2099       --  example, we do not want to issue a binary operator message, so
2100       --  that instead we will get the missing semicolon message. This
2101       --  distinction is of course a heuristic which does not always work,
2102       --  but in practice it is quite effective.
2103
2104       --  Note: the one case in which we do not go through this circuit is
2105       --  when we have scanned a range attribute and want to return with
2106       --  Token pointing to the apostrophe. The apostrophe is not normally
2107       --  an expression terminator, and is not in Token_Class_Sterm, but
2108       --  in this special case we know that the expression is complete.
2109
2110       if not Token_Is_At_Start_Of_Line
2111          and then Token not in Token_Class_Sterm
2112       then
2113          --  Normally the right error message is indeed that we expected a
2114          --  binary operator, but in the case of being between a right and left
2115          --  paren, e.g. in an aggregate, a more likely error is missing comma.
2116
2117          if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2118             T_Comma;
2119          else
2120             Error_Msg_AP ("binary operator expected");
2121          end if;
2122
2123          raise Error_Resync;
2124
2125       else
2126          return Node1;
2127       end if;
2128
2129    --  If any error occurs, then scan to next expression terminator symbol
2130    --  or comma, right paren or vertical bar at the outer (i.e. current) paren
2131    --  level. Expr_Form is set to indicate a normal simple expression.
2132
2133    exception
2134       when Error_Resync =>
2135          Resync_Expression;
2136          Expr_Form := EF_Simple;
2137          return Error;
2138    end P_Simple_Expression;
2139
2140    -----------------------------------------------
2141    -- 4.4  Simple Expression or Range Attribute --
2142    -----------------------------------------------
2143
2144    --  SIMPLE_EXPRESSION ::=
2145    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2146
2147    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2148
2149    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2150
2151    --  Error recovery: cannot raise Error_Resync
2152
2153    function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2154       Sexpr     : Node_Id;
2155       Attr_Node : Node_Id;
2156
2157    begin
2158       --  We don't just want to roar ahead and call P_Simple_Expression
2159       --  here, since we want to handle the case of a parenthesized range
2160       --  attribute cleanly.
2161
2162       if Token = Tok_Left_Paren then
2163          declare
2164             Lptr       : constant Source_Ptr := Token_Ptr;
2165             Scan_State : Saved_Scan_State;
2166
2167          begin
2168             Save_Scan_State (Scan_State);
2169             Scan; -- past left paren
2170             Sexpr := P_Simple_Expression;
2171
2172             if Token = Tok_Apostrophe then
2173                Attr_Node := P_Range_Attribute_Reference (Sexpr);
2174                Expr_Form := EF_Range_Attr;
2175
2176                if Token = Tok_Right_Paren then
2177                   Scan; -- scan past right paren if present
2178                end if;
2179
2180                Error_Msg ("parentheses not allowed for range attribute", Lptr);
2181
2182                return Attr_Node;
2183             end if;
2184
2185             Restore_Scan_State (Scan_State);
2186          end;
2187       end if;
2188
2189       --  Here after dealing with parenthesized range attribute
2190
2191       Sexpr := P_Simple_Expression;
2192
2193       if Token = Tok_Apostrophe then
2194          Attr_Node := P_Range_Attribute_Reference (Sexpr);
2195          Expr_Form := EF_Range_Attr;
2196          return Attr_Node;
2197
2198       else
2199          return Sexpr;
2200       end if;
2201    end P_Simple_Expression_Or_Range_Attribute;
2202
2203    ---------------
2204    -- 4.4  Term --
2205    ---------------
2206
2207    --  TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2208
2209    --  Error recovery: can raise Error_Resync
2210
2211    function P_Term return Node_Id is
2212       Node1, Node2 : Node_Id;
2213       Tokptr       : Source_Ptr;
2214
2215    begin
2216       Node1 := P_Factor;
2217
2218       loop
2219          exit when Token not in Token_Class_Mulop;
2220          Tokptr := Token_Ptr;
2221          Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2222          Scan; -- past operator
2223          Set_Left_Opnd (Node2, Node1);
2224          Set_Right_Opnd (Node2, P_Factor);
2225          Node1 := Node2;
2226       end loop;
2227
2228       return Node1;
2229    end P_Term;
2230
2231    -----------------
2232    -- 4.4  Factor --
2233    -----------------
2234
2235    --  FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2236
2237    --  Error recovery: can raise Error_Resync
2238
2239    function P_Factor return Node_Id is
2240       Node1 : Node_Id;
2241       Node2 : Node_Id;
2242
2243    begin
2244       if Token = Tok_Abs then
2245          Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2246
2247          if Style_Check then
2248             Style.Check_Abs_Not;
2249          end if;
2250
2251          Scan; -- past ABS
2252          Set_Right_Opnd (Node1, P_Primary);
2253          return Node1;
2254
2255       elsif Token = Tok_Not then
2256          Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2257
2258          if Style_Check then
2259             Style.Check_Abs_Not;
2260          end if;
2261
2262          Scan; -- past NOT
2263          Set_Right_Opnd (Node1, P_Primary);
2264          return Node1;
2265
2266       else
2267          Node1 := P_Primary;
2268
2269          if Token = Tok_Double_Asterisk then
2270             Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2271             Scan; -- past **
2272             Set_Left_Opnd (Node2, Node1);
2273             Set_Right_Opnd (Node2, P_Primary);
2274             return Node2;
2275          else
2276             return Node1;
2277          end if;
2278       end if;
2279    end P_Factor;
2280
2281    ------------------
2282    -- 4.4  Primary --
2283    ------------------
2284
2285    --  PRIMARY ::=
2286    --    NUMERIC_LITERAL  | null
2287    --  | STRING_LITERAL   | AGGREGATE
2288    --  | NAME             | QUALIFIED_EXPRESSION
2289    --  | ALLOCATOR        | (EXPRESSION)
2290
2291    --  Error recovery: can raise Error_Resync
2292
2293    function P_Primary return Node_Id is
2294       Scan_State : Saved_Scan_State;
2295       Node1      : Node_Id;
2296
2297    begin
2298       --  The loop runs more than once only if misplaced pragmas are found
2299
2300       loop
2301          case Token is
2302
2303             --  Name token can start a name, call or qualified expression, all
2304             --  of which are acceptable possibilities for primary. Note also
2305             --  that string literal is included in name (as operator symbol)
2306             --  and type conversion is included in name (as indexed component).
2307
2308             when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2309                Node1 := P_Name;
2310
2311                --  All done unless apostrophe follows
2312
2313                if Token /= Tok_Apostrophe then
2314                   return Node1;
2315
2316                --  Apostrophe following means that we have either just parsed
2317                --  the subtype mark of a qualified expression, or the prefix
2318                --  or a range attribute.
2319
2320                else -- Token = Tok_Apostrophe
2321                   Save_Scan_State (Scan_State); -- at apostrophe
2322                   Scan; -- past apostrophe
2323
2324                   --  If range attribute, then this is always an error, since
2325                   --  the only legitimate case (where the scanned expression is
2326                   --  a qualified simple name) is handled at the level of the
2327                   --  Simple_Expression processing. This case corresponds to a
2328                   --  usage such as 3 + A'Range, which is always illegal.
2329
2330                   if Token = Tok_Range then
2331                      Restore_Scan_State (Scan_State); -- to apostrophe
2332                      Bad_Range_Attribute (Token_Ptr);
2333                      return Error;
2334
2335                   --  If left paren, then we have a qualified expression.
2336                   --  Note that P_Name guarantees that in this case, where
2337                   --  Token = Tok_Apostrophe on return, the only two possible
2338                   --  tokens following the apostrophe are left paren and
2339                   --  RANGE, so we know we have a left paren here.
2340
2341                   else -- Token = Tok_Left_Paren
2342                      return P_Qualified_Expression (Node1);
2343
2344                   end if;
2345                end if;
2346
2347             --  Numeric or string literal
2348
2349             when Tok_Integer_Literal |
2350                  Tok_Real_Literal    |
2351                  Tok_String_Literal  =>
2352
2353                Node1 := Token_Node;
2354                Scan; -- past number
2355                return Node1;
2356
2357             --  Left paren, starts aggregate or parenthesized expression
2358
2359             when Tok_Left_Paren =>
2360                declare
2361                   Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2362
2363                begin
2364                   if Nkind (Expr) = N_Attribute_Reference
2365                     and then Attribute_Name (Expr) = Name_Range
2366                   then
2367                      Bad_Range_Attribute (Sloc (Expr));
2368                   end if;
2369
2370                   return Expr;
2371                end;
2372
2373             --  Allocator
2374
2375             when Tok_New =>
2376                return P_Allocator;
2377
2378             --  Null
2379
2380             when Tok_Null =>
2381                Scan; -- past NULL
2382                return New_Node (N_Null, Prev_Token_Ptr);
2383
2384             --  Pragma, not allowed here, so just skip past it
2385
2386             when Tok_Pragma =>
2387                P_Pragmas_Misplaced;
2388
2389             --  Deal with IF (possible unparenthesized conditional expression)
2390
2391             when Tok_If =>
2392
2393                --  If this looks like a real if, defined as an IF appearing at
2394                --  the start of a new line, then we consider we have a missing
2395                --  operand.
2396
2397                if Token_Is_At_Start_Of_Line then
2398                   Error_Msg_AP ("missing operand");
2399                   return Error;
2400
2401                --  If this looks like a conditional expression, then treat it
2402                --  that way with an error message.
2403
2404                elsif Ada_Version >= Ada_2012 then
2405                   Error_Msg_SC
2406                     ("conditional expression must be parenthesized");
2407                   return P_Conditional_Expression;
2408
2409                --  Otherwise treat as misused identifier
2410
2411                else
2412                   return P_Identifier;
2413                end if;
2414
2415             --  Deal with CASE (possible unparenthesized case expression)
2416
2417             when Tok_Case =>
2418
2419                --  If this looks like a real case, defined as a CASE appearing
2420                --  the start of a new line, then we consider we have a missing
2421                --  operand.
2422
2423                if Token_Is_At_Start_Of_Line then
2424                   Error_Msg_AP ("missing operand");
2425                   return Error;
2426
2427                --  If this looks like a case expression, then treat it that way
2428                --  with an error message.
2429
2430                elsif Ada_Version >= Ada_2012 then
2431                   Error_Msg_SC ("case expression must be parenthesized");
2432                   return P_Case_Expression;
2433
2434                --  Otherwise treat as misused identifier
2435
2436                else
2437                   return P_Identifier;
2438                end if;
2439
2440             --  Anything else is illegal as the first token of a primary, but
2441             --  we test for a reserved identifier so that it is treated nicely
2442
2443             when others =>
2444                if Is_Reserved_Identifier then
2445                   return P_Identifier;
2446
2447                elsif Prev_Token = Tok_Comma then
2448                   Error_Msg_SP -- CODEFIX
2449                     ("|extra "","" ignored");
2450                   raise Error_Resync;
2451
2452                else
2453                   Error_Msg_AP ("missing operand");
2454                   raise Error_Resync;
2455                end if;
2456
2457          end case;
2458       end loop;
2459    end P_Primary;
2460
2461    ---------------------------
2462    -- 4.5  Logical Operator --
2463    ---------------------------
2464
2465    --  LOGICAL_OPERATOR  ::=  and | or | xor
2466
2467    --  Note: AND THEN and OR ELSE are also treated as logical operators
2468    --  by the parser (even though they are not operators semantically)
2469
2470    --  The value returned is the appropriate Node_Kind code for the operator
2471    --  On return, Token points to the token following the scanned operator.
2472
2473    --  The caller has checked that the first token is a legitimate logical
2474    --  operator token (i.e. is either XOR, AND, OR).
2475
2476    --  Error recovery: cannot raise Error_Resync
2477
2478    function P_Logical_Operator return Node_Kind is
2479    begin
2480       if Token = Tok_And then
2481          if Style_Check then
2482             Style.Check_Binary_Operator;
2483          end if;
2484
2485          Scan; -- past AND
2486
2487          if Token = Tok_Then then
2488             Scan; -- past THEN
2489             return N_And_Then;
2490          else
2491             return N_Op_And;
2492          end if;
2493
2494       elsif Token = Tok_Or then
2495          if Style_Check then
2496             Style.Check_Binary_Operator;
2497          end if;
2498
2499          Scan; -- past OR
2500
2501          if Token = Tok_Else then
2502             Scan; -- past ELSE
2503             return N_Or_Else;
2504          else
2505             return N_Op_Or;
2506          end if;
2507
2508       else -- Token = Tok_Xor
2509          if Style_Check then
2510             Style.Check_Binary_Operator;
2511          end if;
2512
2513          Scan; -- past XOR
2514          return N_Op_Xor;
2515       end if;
2516    end P_Logical_Operator;
2517
2518    ------------------------------
2519    -- 4.5  Relational Operator --
2520    ------------------------------
2521
2522    --  RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2523
2524    --  The value returned is the appropriate Node_Kind code for the operator.
2525    --  On return, Token points to the operator token, NOT past it.
2526
2527    --  The caller has checked that the first token is a legitimate relational
2528    --  operator token (i.e. is one of the operator tokens listed above).
2529
2530    --  Error recovery: cannot raise Error_Resync
2531
2532    function P_Relational_Operator return Node_Kind is
2533       Op_Kind : Node_Kind;
2534       Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2535                      (Tok_Less          => N_Op_Lt,
2536                       Tok_Equal         => N_Op_Eq,
2537                       Tok_Greater       => N_Op_Gt,
2538                       Tok_Not_Equal     => N_Op_Ne,
2539                       Tok_Greater_Equal => N_Op_Ge,
2540                       Tok_Less_Equal    => N_Op_Le,
2541                       Tok_In            => N_In,
2542                       Tok_Not           => N_Not_In,
2543                       Tok_Box           => N_Op_Ne);
2544
2545    begin
2546       if Token = Tok_Box then
2547          Error_Msg_SC -- CODEFIX
2548            ("|""'<'>"" should be ""/=""");
2549       end if;
2550
2551       Op_Kind := Relop_Node (Token);
2552
2553       if Style_Check then
2554          Style.Check_Binary_Operator;
2555       end if;
2556
2557       Scan; -- past operator token
2558
2559       if Prev_Token = Tok_Not then
2560          T_In;
2561       end if;
2562
2563       return Op_Kind;
2564    end P_Relational_Operator;
2565
2566    ---------------------------------
2567    -- 4.5  Binary Adding Operator --
2568    ---------------------------------
2569
2570    --  BINARY_ADDING_OPERATOR ::= + | - | &
2571
2572    --  The value returned is the appropriate Node_Kind code for the operator.
2573    --  On return, Token points to the operator token (NOT past it).
2574
2575    --  The caller has checked that the first token is a legitimate adding
2576    --  operator token (i.e. is one of the operator tokens listed above).
2577
2578    --  Error recovery: cannot raise Error_Resync
2579
2580    function P_Binary_Adding_Operator return Node_Kind is
2581       Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2582                      (Tok_Ampersand => N_Op_Concat,
2583                       Tok_Minus     => N_Op_Subtract,
2584                       Tok_Plus      => N_Op_Add);
2585    begin
2586       return Addop_Node (Token);
2587    end P_Binary_Adding_Operator;
2588
2589    --------------------------------
2590    -- 4.5  Unary Adding Operator --
2591    --------------------------------
2592
2593    --  UNARY_ADDING_OPERATOR ::= + | -
2594
2595    --  The value returned is the appropriate Node_Kind code for the operator.
2596    --  On return, Token points to the operator token (NOT past it).
2597
2598    --  The caller has checked that the first token is a legitimate adding
2599    --  operator token (i.e. is one of the operator tokens listed above).
2600
2601    --  Error recovery: cannot raise Error_Resync
2602
2603    function P_Unary_Adding_Operator return Node_Kind is
2604       Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2605                      (Tok_Minus => N_Op_Minus,
2606                       Tok_Plus  => N_Op_Plus);
2607    begin
2608       return Addop_Node (Token);
2609    end P_Unary_Adding_Operator;
2610
2611    -------------------------------
2612    -- 4.5  Multiplying Operator --
2613    -------------------------------
2614
2615    --  MULTIPLYING_OPERATOR ::= * | / | mod | rem
2616
2617    --  The value returned is the appropriate Node_Kind code for the operator.
2618    --  On return, Token points to the operator token (NOT past it).
2619
2620    --  The caller has checked that the first token is a legitimate multiplying
2621    --  operator token (i.e. is one of the operator tokens listed above).
2622
2623    --  Error recovery: cannot raise Error_Resync
2624
2625    function P_Multiplying_Operator return Node_Kind is
2626       Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2627         (Tok_Asterisk       => N_Op_Multiply,
2628          Tok_Mod            => N_Op_Mod,
2629          Tok_Rem            => N_Op_Rem,
2630          Tok_Slash          => N_Op_Divide);
2631    begin
2632       return Mulop_Node (Token);
2633    end P_Multiplying_Operator;
2634
2635    --------------------------------------
2636    -- 4.5  Highest Precedence Operator --
2637    --------------------------------------
2638
2639    --  Parsed by P_Factor (4.4)
2640
2641    --  Note: this rule is not in fact used by the grammar at any point!
2642
2643    --------------------------
2644    -- 4.6  Type Conversion --
2645    --------------------------
2646
2647    --  Parsed by P_Primary as a Name (4.1)
2648
2649    -------------------------------
2650    -- 4.7  Qualified Expression --
2651    -------------------------------
2652
2653    --  QUALIFIED_EXPRESSION ::=
2654    --    SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2655
2656    --  The caller has scanned the name which is the Subtype_Mark parameter
2657    --  and scanned past the single quote following the subtype mark. The
2658    --  caller has not checked that this name is in fact appropriate for
2659    --  a subtype mark name (i.e. it is a selected component or identifier).
2660
2661    --  Error_Recovery: cannot raise Error_Resync
2662
2663    function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2664       Qual_Node : Node_Id;
2665    begin
2666       Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2667       Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2668       Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2669       return Qual_Node;
2670    end P_Qualified_Expression;
2671
2672    --------------------
2673    -- 4.8  Allocator --
2674    --------------------
2675
2676    --  ALLOCATOR ::=
2677    --    new [NULL_EXCLUSION] SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2678
2679    --  The caller has checked that the initial token is NEW
2680
2681    --  Error recovery: can raise Error_Resync
2682
2683    function P_Allocator return Node_Id is
2684       Alloc_Node             : Node_Id;
2685       Type_Node              : Node_Id;
2686       Null_Exclusion_Present : Boolean;
2687
2688    begin
2689       Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2690       T_New;
2691
2692       --  Scan Null_Exclusion if present (Ada 2005 (AI-231))
2693
2694       Null_Exclusion_Present := P_Null_Exclusion;
2695       Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2696       Type_Node := P_Subtype_Mark_Resync;
2697
2698       if Token = Tok_Apostrophe then
2699          Scan; -- past apostrophe
2700          Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2701       else
2702          Set_Expression
2703            (Alloc_Node,
2704             P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2705       end if;
2706
2707       return Alloc_Node;
2708    end P_Allocator;
2709
2710    -----------------------
2711    -- P_Case_Expression --
2712    -----------------------
2713
2714    function P_Case_Expression return Node_Id is
2715       Loc        : constant Source_Ptr := Token_Ptr;
2716       Case_Node  : Node_Id;
2717       Save_State : Saved_Scan_State;
2718
2719    begin
2720       if Ada_Version < Ada_2012 then
2721          Error_Msg_SC ("|case expression is an Ada 2012 feature");
2722          Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2723       end if;
2724
2725       Scan; -- past CASE
2726       Case_Node :=
2727         Make_Case_Expression (Loc,
2728           Expression   => P_Expression_No_Right_Paren,
2729           Alternatives => New_List);
2730       T_Is;
2731
2732       --  We now have scanned out CASE expression IS, scan alternatives
2733
2734       loop
2735          T_When;
2736          Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
2737
2738          --  Missing comma if WHEN (more alternatives present)
2739
2740          if Token = Tok_When then
2741             T_Comma;
2742
2743          --  If comma/WHEN, skip comma and we have another alternative
2744
2745          elsif Token = Tok_Comma then
2746             Save_Scan_State (Save_State);
2747             Scan; -- past comma
2748
2749             if Token /= Tok_When then
2750                Restore_Scan_State (Save_State);
2751                exit;
2752             end if;
2753
2754          --  If no comma or WHEN, definitely done
2755
2756          else
2757             exit;
2758          end if;
2759       end loop;
2760
2761       --  If we have an END CASE, diagnose as not needed
2762
2763       if Token = Tok_End then
2764          Error_Msg_SC ("`END CASE` not allowed at end of case expression");
2765          Scan; -- past END
2766
2767          if Token = Tok_Case then
2768             Scan; -- past CASE;
2769          end if;
2770       end if;
2771
2772       --  Return the Case_Expression node
2773
2774       return Case_Node;
2775    end P_Case_Expression;
2776
2777    -----------------------------------
2778    -- P_Case_Expression_Alternative --
2779    -----------------------------------
2780
2781    --  CASE_STATEMENT_ALTERNATIVE ::=
2782    --    when DISCRETE_CHOICE_LIST =>
2783    --      EXPRESSION
2784
2785    --  The caller has checked that and scanned past the initial WHEN token
2786    --  Error recovery: can raise Error_Resync
2787
2788    function P_Case_Expression_Alternative return Node_Id is
2789       Case_Alt_Node : Node_Id;
2790    begin
2791       Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
2792       Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
2793       TF_Arrow;
2794       Set_Expression (Case_Alt_Node, P_Expression);
2795       return Case_Alt_Node;
2796    end P_Case_Expression_Alternative;
2797
2798    ------------------------------
2799    -- P_Conditional_Expression --
2800    ------------------------------
2801
2802    function P_Conditional_Expression return Node_Id is
2803       Exprs : constant List_Id    := New_List;
2804       Loc   : constant Source_Ptr := Token_Ptr;
2805       Expr  : Node_Id;
2806       State : Saved_Scan_State;
2807
2808    begin
2809       Inside_Conditional_Expression := Inside_Conditional_Expression + 1;
2810
2811       if Token = Tok_If and then Ada_Version < Ada_2012 then
2812          Error_Msg_SC ("|conditional expression is an Ada 2012 feature");
2813          Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2814       end if;
2815
2816       Scan; -- past IF or ELSIF
2817       Append_To (Exprs, P_Condition);
2818       TF_Then;
2819       Append_To (Exprs, P_Expression);
2820
2821       --  We now have scanned out IF expr THEN expr
2822
2823       --  Check for common error of semicolon before the ELSE
2824
2825       if Token = Tok_Semicolon then
2826          Save_Scan_State (State);
2827          Scan; -- past semicolon
2828
2829          if Token = Tok_Else or else Token = Tok_Elsif then
2830             Error_Msg_SP -- CODEFIX
2831               ("|extra "";"" ignored");
2832
2833          else
2834             Restore_Scan_State (State);
2835          end if;
2836       end if;
2837
2838       --  Scan out ELSIF sequence if present
2839
2840       if Token = Tok_Elsif then
2841          Expr := P_Conditional_Expression;
2842          Set_Is_Elsif (Expr);
2843          Append_To (Exprs, Expr);
2844
2845       --  Scan out ELSE phrase if present
2846
2847       elsif Token = Tok_Else then
2848
2849          --  Scan out ELSE expression
2850
2851          Scan; -- Past ELSE
2852          Append_To (Exprs, P_Expression);
2853
2854       --  Two expression case (implied True, filled in during semantics)
2855
2856       else
2857          null;
2858       end if;
2859
2860       --  If we have an END IF, diagnose as not needed
2861
2862       if Token = Tok_End then
2863          Error_Msg_SC
2864            ("`END IF` not allowed at end of conditional expression");
2865          Scan; -- past END
2866
2867          if Token = Tok_If then
2868             Scan; -- past IF;
2869          end if;
2870       end if;
2871
2872       Inside_Conditional_Expression := Inside_Conditional_Expression - 1;
2873
2874       --  Return the Conditional_Expression node
2875
2876       return
2877         Make_Conditional_Expression (Loc,
2878           Expressions => Exprs);
2879    end P_Conditional_Expression;
2880
2881    -----------------------
2882    -- P_Membership_Test --
2883    -----------------------
2884
2885    procedure P_Membership_Test (N : Node_Id) is
2886       Alt : constant Node_Id :=
2887               P_Range_Or_Subtype_Mark
2888                 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
2889
2890    begin
2891       --  Set case
2892
2893       if Token = Tok_Vertical_Bar then
2894          if Ada_Version < Ada_2012 then
2895             Error_Msg_SC ("set notation is an Ada 2012 feature");
2896             Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2897          end if;
2898
2899          Set_Alternatives (N, New_List (Alt));
2900          Set_Right_Opnd   (N, Empty);
2901
2902          --  Loop to accumulate alternatives
2903
2904          while Token = Tok_Vertical_Bar loop
2905             Scan; -- past vertical bar
2906             Append_To
2907               (Alternatives (N),
2908                P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
2909          end loop;
2910
2911       --  Not set case
2912
2913       else
2914          Set_Right_Opnd   (N, Alt);
2915          Set_Alternatives (N, No_List);
2916       end if;
2917    end P_Membership_Test;
2918
2919 end Ch4;