OSDN Git Service

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