OSDN Git Service

Update FSF address
[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-2005 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 pragma Style_Checks (All_Checks);
28 --  Turn off subprogram body ordering check. Subprograms are in order
29 --  by RM section rather than alphabetical
30
31 separate (Par)
32 package body Ch4 is
33
34    -----------------------
35    -- Local Subprograms --
36    -----------------------
37
38    function P_Aggregate_Or_Paren_Expr                 return Node_Id;
39    function P_Allocator                               return Node_Id;
40    function P_Record_Or_Array_Component_Association   return Node_Id;
41    function P_Factor                                  return Node_Id;
42    function P_Primary                                 return Node_Id;
43    function P_Relation                                return Node_Id;
44    function P_Term                                    return Node_Id;
45
46    function P_Binary_Adding_Operator                  return Node_Kind;
47    function P_Logical_Operator                        return Node_Kind;
48    function P_Multiplying_Operator                    return Node_Kind;
49    function P_Relational_Operator                     return Node_Kind;
50    function P_Unary_Adding_Operator                   return Node_Kind;
51
52    procedure Bad_Range_Attribute (Loc : Source_Ptr);
53    --  Called to place complaint about bad range attribute at the given
54    --  source location. Terminates by raising Error_Resync.
55
56    function P_Range_Attribute_Reference
57      (Prefix_Node : Node_Id)
58       return        Node_Id;
59    --  Scan a range attribute reference. The caller has scanned out the
60    --  prefix. The current token is known to be an apostrophe and the
61    --  following token is known to be RANGE.
62
63    procedure Set_Op_Name (Node : Node_Id);
64    --  Procedure to set name field (Chars) in operator node
65
66    -------------------------
67    -- Bad_Range_Attribute --
68    -------------------------
69
70    procedure Bad_Range_Attribute (Loc : Source_Ptr) is
71    begin
72       Error_Msg ("range attribute cannot be used in expression", Loc);
73       Resync_Expression;
74    end Bad_Range_Attribute;
75
76    ------------------
77    -- Set_Op_Name --
78    ------------------
79
80    procedure Set_Op_Name (Node : Node_Id) is
81       type Name_Of_Type is array (N_Op) of Name_Id;
82       Name_Of : constant Name_Of_Type := Name_Of_Type'(
83          N_Op_And                    => Name_Op_And,
84          N_Op_Or                     => Name_Op_Or,
85          N_Op_Xor                    => Name_Op_Xor,
86          N_Op_Eq                     => Name_Op_Eq,
87          N_Op_Ne                     => Name_Op_Ne,
88          N_Op_Lt                     => Name_Op_Lt,
89          N_Op_Le                     => Name_Op_Le,
90          N_Op_Gt                     => Name_Op_Gt,
91          N_Op_Ge                     => Name_Op_Ge,
92          N_Op_Add                    => Name_Op_Add,
93          N_Op_Subtract               => Name_Op_Subtract,
94          N_Op_Concat                 => Name_Op_Concat,
95          N_Op_Multiply               => Name_Op_Multiply,
96          N_Op_Divide                 => Name_Op_Divide,
97          N_Op_Mod                    => Name_Op_Mod,
98          N_Op_Rem                    => Name_Op_Rem,
99          N_Op_Expon                  => Name_Op_Expon,
100          N_Op_Plus                   => Name_Op_Add,
101          N_Op_Minus                  => Name_Op_Subtract,
102          N_Op_Abs                    => Name_Op_Abs,
103          N_Op_Not                    => Name_Op_Not,
104
105          --  We don't really need these shift operators, since they never
106          --  appear as operators in the source, but the path of least
107          --  resistance is to put them in (the aggregate must be complete)
108
109          N_Op_Rotate_Left            => Name_Rotate_Left,
110          N_Op_Rotate_Right           => Name_Rotate_Right,
111          N_Op_Shift_Left             => Name_Shift_Left,
112          N_Op_Shift_Right            => Name_Shift_Right,
113          N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
114
115    begin
116       if Nkind (Node) in N_Op then
117          Set_Chars (Node, Name_Of (Nkind (Node)));
118       end if;
119    end Set_Op_Name;
120
121    --------------------------
122    -- 4.1  Name (also 6.4) --
123    --------------------------
124
125    --  NAME ::=
126    --    DIRECT_NAME        | EXPLICIT_DEREFERENCE
127    --  | INDEXED_COMPONENT  | SLICE
128    --  | SELECTED_COMPONENT | ATTRIBUTE
129    --  | TYPE_CONVERSION    | FUNCTION_CALL
130    --  | CHARACTER_LITERAL
131
132    --  DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
133
134    --  PREFIX ::= NAME | IMPLICIT_DEREFERENCE
135
136    --  EXPLICIT_DEREFERENCE ::= NAME . all
137
138    --  IMPLICIT_DEREFERENCE ::= NAME
139
140    --  INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
141
142    --  SLICE ::= PREFIX (DISCRETE_RANGE)
143
144    --  SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
145
146    --  SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
147
148    --  ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
149
150    --  ATTRIBUTE_DESIGNATOR ::=
151    --    IDENTIFIER [(static_EXPRESSION)]
152    --  | access | delta | digits
153
154    --  FUNCTION_CALL ::=
155    --    function_NAME
156    --  | function_PREFIX ACTUAL_PARAMETER_PART
157
158    --  ACTUAL_PARAMETER_PART ::=
159    --    (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
160
161    --  PARAMETER_ASSOCIATION ::=
162    --    [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
163
164    --  EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
165
166    --  Note: syntactically a procedure call looks just like a function call,
167    --  so this routine is in practice used to scan out procedure calls as well.
168
169    --  On return, Expr_Form is set to either EF_Name or EF_Simple_Name
170
171    --  Error recovery: can raise Error_Resync
172
173    --  Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
174    --  followed by either a left paren (qualified expression case), or by
175    --  range (range attribute case). All other uses of apostrophe (i.e. all
176    --  other attributes) are handled in this routine.
177
178    --  Error recovery: can raise Error_Resync
179
180    function P_Name return Node_Id is
181       Scan_State  : Saved_Scan_State;
182       Name_Node   : Node_Id;
183       Prefix_Node : Node_Id;
184       Ident_Node  : Node_Id;
185       Expr_Node   : Node_Id;
186       Range_Node  : Node_Id;
187       Arg_Node    : Node_Id;
188
189       Arg_List  : List_Id := No_List; -- kill junk warning
190       Attr_Name : Name_Id := No_Name; -- kill junk warning
191
192    begin
193       if Token not in Token_Class_Name then
194          Error_Msg_AP ("name expected");
195          raise Error_Resync;
196       end if;
197
198       --  Loop through designators in qualified name
199
200       Name_Node := Token_Node;
201
202       loop
203          Scan; -- past designator
204          exit when Token /= Tok_Dot;
205          Save_Scan_State (Scan_State); -- at dot
206          Scan; -- past dot
207
208          --  If we do not have another designator after the dot, then join
209          --  the normal circuit to handle a dot extension (may be .all or
210          --  character literal case). Otherwise loop back to scan the next
211          --  designator.
212
213          if Token not in Token_Class_Desig then
214             goto Scan_Name_Extension_Dot;
215          else
216             Prefix_Node := Name_Node;
217             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
218             Set_Prefix (Name_Node, Prefix_Node);
219             Set_Selector_Name (Name_Node, Token_Node);
220          end if;
221       end loop;
222
223       --  We have now scanned out a qualified designator. If the last token is
224       --  an operator symbol, then we certainly do not have the Snam case, so
225       --  we can just use the normal name extension check circuit
226
227       if Prev_Token = Tok_Operator_Symbol then
228          goto Scan_Name_Extension;
229       end if;
230
231       --  We have scanned out a qualified simple name, check for name extension
232       --  Note that we know there is no dot here at this stage, so the only
233       --  possible cases of name extension are apostrophe and left paren.
234
235       if Token = Tok_Apostrophe then
236          Save_Scan_State (Scan_State); -- at apostrophe
237          Scan; -- past apostrophe
238
239          --  If left paren, then this might be a qualified expression, but we
240          --  are only in the business of scanning out names, so return with
241          --  Token backed up to point to the apostrophe. The treatment for
242          --  the range attribute is similar (we do not consider x'range to
243          --  be a name in this grammar).
244
245          if Token = Tok_Left_Paren or else Token = Tok_Range then
246             Restore_Scan_State (Scan_State); -- to apostrophe
247             Expr_Form := EF_Simple_Name;
248             return Name_Node;
249
250          --  Otherwise we have the case of a name extended by an attribute
251
252          else
253             goto Scan_Name_Extension_Apostrophe;
254          end if;
255
256       --  Check case of qualified simple name extended by a left parenthesis
257
258       elsif Token = Tok_Left_Paren then
259          Scan; -- past left paren
260          goto Scan_Name_Extension_Left_Paren;
261
262       --  Otherwise the qualified simple name is not extended, so return
263
264       else
265          Expr_Form := EF_Simple_Name;
266          return Name_Node;
267       end if;
268
269       --  Loop scanning past name extensions. A label is used for control
270       --  transfer for this loop for ease of interfacing with the finite state
271       --  machine in the parenthesis scanning circuit, and also to allow for
272       --  passing in control to the appropriate point from the above code.
273
274       <<Scan_Name_Extension>>
275
276          --  Character literal used as name cannot be extended. Also this
277          --  cannot be a call, since the name for a call must be a designator.
278          --  Return in these cases, or if there is no name extension
279
280          if Token not in Token_Class_Namext
281            or else Prev_Token = Tok_Char_Literal
282          then
283             Expr_Form := EF_Name;
284             return Name_Node;
285          end if;
286
287       --  Merge here when we know there is a name extension
288
289       <<Scan_Name_Extension_OK>>
290
291          if Token = Tok_Left_Paren then
292             Scan; -- past left paren
293             goto Scan_Name_Extension_Left_Paren;
294
295          elsif Token = Tok_Apostrophe then
296             Save_Scan_State (Scan_State); -- at apostrophe
297             Scan; -- past apostrophe
298             goto Scan_Name_Extension_Apostrophe;
299
300          else -- Token = Tok_Dot
301             Save_Scan_State (Scan_State); -- at dot
302             Scan; -- past dot
303             goto Scan_Name_Extension_Dot;
304          end if;
305
306       --  Case of name extended by dot (selection), dot is already skipped
307       --  and the scan state at the point of the dot is saved in Scan_State.
308
309       <<Scan_Name_Extension_Dot>>
310
311          --  Explicit dereference case
312
313          if Token = Tok_All then
314             Prefix_Node := Name_Node;
315             Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
316             Set_Prefix (Name_Node, Prefix_Node);
317             Scan; -- past ALL
318             goto Scan_Name_Extension;
319
320          --  Selected component case
321
322          elsif Token in Token_Class_Name then
323             Prefix_Node := Name_Node;
324             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
325             Set_Prefix (Name_Node, Prefix_Node);
326             Set_Selector_Name (Name_Node, Token_Node);
327             Scan; -- past selector
328             goto Scan_Name_Extension;
329
330          --  Reserved identifier as selector
331
332          elsif Is_Reserved_Identifier then
333             Scan_Reserved_Identifier (Force_Msg => False);
334             Prefix_Node := Name_Node;
335             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
336             Set_Prefix (Name_Node, Prefix_Node);
337             Set_Selector_Name (Name_Node, Token_Node);
338             Scan; -- past identifier used as selector
339             goto Scan_Name_Extension;
340
341          --  If dot is at end of line and followed by nothing legal,
342          --  then assume end of name and quit (dot will be taken as
343          --  an erroneous form of some other punctuation by our caller).
344
345          elsif Token_Is_At_Start_Of_Line then
346             Restore_Scan_State (Scan_State);
347             return Name_Node;
348
349          --  Here if nothing legal after the dot
350
351          else
352             Error_Msg_AP ("selector expected");
353             raise Error_Resync;
354          end if;
355
356       --  Here for an apostrophe as name extension. The scan position at the
357       --  apostrophe has already been saved, and the apostrophe scanned out.
358
359       <<Scan_Name_Extension_Apostrophe>>
360
361          Scan_Apostrophe : declare
362             function Apostrophe_Should_Be_Semicolon return Boolean;
363             --  Checks for case where apostrophe should probably be
364             --  a semicolon, and if so, gives appropriate message,
365             --  resets the scan pointer to the apostrophe, changes
366             --  the current token to Tok_Semicolon, and returns True.
367             --  Otherwise returns False.
368
369             function Apostrophe_Should_Be_Semicolon return Boolean is
370             begin
371                if Token_Is_At_Start_Of_Line then
372                   Restore_Scan_State (Scan_State); -- to apostrophe
373                   Error_Msg_SC ("""''"" should be "";""");
374                   Token := Tok_Semicolon;
375                   return True;
376                else
377                   return False;
378                end if;
379             end Apostrophe_Should_Be_Semicolon;
380
381          --  Start of processing for Scan_Apostrophe
382
383          begin
384             --  If range attribute after apostrophe, then return with Token
385             --  pointing to the apostrophe. Note that in this case the prefix
386             --  need not be a simple name (cases like A.all'range). Similarly
387             --  if there is a left paren after the apostrophe, then we also
388             --  return with Token pointing to the apostrophe (this is the
389             --  qualified expression case).
390
391             if Token = Tok_Range or else Token = Tok_Left_Paren then
392                Restore_Scan_State (Scan_State); -- to apostrophe
393                Expr_Form := EF_Name;
394                return Name_Node;
395
396             --  Here for cases where attribute designator is an identifier
397
398             elsif Token = Tok_Identifier then
399                Attr_Name := Token_Name;
400
401                if not Is_Attribute_Name (Attr_Name) then
402                   if Apostrophe_Should_Be_Semicolon then
403                      Expr_Form := EF_Name;
404                      return Name_Node;
405
406                   --  Here for a bad attribute name
407
408                   else
409                      Signal_Bad_Attribute;
410                      Scan; -- past bad identifier
411
412                      if Token = Tok_Left_Paren then
413                         Scan; -- past left paren
414
415                         loop
416                            Discard_Junk_Node (P_Expression);
417                            exit when not Comma_Present;
418                         end loop;
419
420                         T_Right_Paren;
421                      end if;
422
423                      return Error;
424                   end if;
425                end if;
426
427                if Style_Check then
428                   Style.Check_Attribute_Name (False);
429                end if;
430
431                Delete_Node (Token_Node);
432
433             --  Here for case of attribute designator is not an identifier
434
435             else
436                if Token = Tok_Delta then
437                   Attr_Name := Name_Delta;
438
439                elsif Token = Tok_Digits then
440                   Attr_Name := Name_Digits;
441
442                elsif Token = Tok_Access then
443                   Attr_Name := Name_Access;
444
445                elsif Token = Tok_Mod and then Ada_Version = Ada_05 then
446                   Attr_Name := Name_Mod;
447
448                elsif Apostrophe_Should_Be_Semicolon then
449                   Expr_Form := EF_Name;
450                   return Name_Node;
451
452                else
453                   Error_Msg_AP ("attribute designator expected");
454                   raise Error_Resync;
455                end if;
456
457                if Style_Check then
458                   Style.Check_Attribute_Name (True);
459                end if;
460             end if;
461
462             --  We come here with an OK attribute scanned, and the
463             --  corresponding Attribute identifier node stored in Ident_Node.
464
465             Prefix_Node := Name_Node;
466             Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
467             Scan; -- past attribute designator
468             Set_Prefix (Name_Node, Prefix_Node);
469             Set_Attribute_Name (Name_Node, Attr_Name);
470
471             --  Scan attribute arguments/designator
472
473             if Token = Tok_Left_Paren then
474                Set_Expressions (Name_Node, New_List);
475                Scan; -- past left paren
476
477                loop
478                   declare
479                      Expr : constant Node_Id := P_Expression;
480
481                   begin
482                      if Token = Tok_Arrow then
483                         Error_Msg_SC
484                           ("named parameters not permitted for attributes");
485                         Scan; -- past junk arrow
486
487                      else
488                         Append (Expr, Expressions (Name_Node));
489                         exit when not Comma_Present;
490                      end if;
491                   end;
492                end loop;
493
494                T_Right_Paren;
495             end if;
496
497             goto Scan_Name_Extension;
498          end Scan_Apostrophe;
499
500       --  Here for left parenthesis extending name (left paren skipped)
501
502       <<Scan_Name_Extension_Left_Paren>>
503
504          --  We now have to scan through a list of items, terminated by a
505          --  right parenthesis. The scan is handled by a finite state
506          --  machine. The possibilities are:
507
508          --   (discrete_range)
509
510          --      This is a slice. This case is handled in LP_State_Init.
511
512          --   (expression, expression, ..)
513
514          --      This is interpreted as an indexed component, i.e. as a
515          --      case of a name which can be extended in the normal manner.
516          --      This case is handled by LP_State_Name or LP_State_Expr.
517
518          --   (..., identifier => expression , ...)
519
520          --      If there is at least one occurrence of identifier => (but
521          --      none of the other cases apply), then we have a call.
522
523          --  Test for Id => case
524
525          if Token = Tok_Identifier then
526             Save_Scan_State (Scan_State); -- at Id
527             Scan; -- past Id
528
529             --  Test for => (allow := as an error substitute)
530
531             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
532                Restore_Scan_State (Scan_State); -- to Id
533                Arg_List := New_List;
534                goto LP_State_Call;
535
536             else
537                Restore_Scan_State (Scan_State); -- to Id
538             end if;
539          end if;
540
541          --  Here we have an expression after all
542
543          Expr_Node := P_Expression_Or_Range_Attribute;
544
545          --  Check cases of discrete range for a slice
546
547          --  First possibility: Range_Attribute_Reference
548
549          if Expr_Form = EF_Range_Attr then
550             Range_Node := Expr_Node;
551
552          --  Second possibility: Simple_expression .. Simple_expression
553
554          elsif Token = Tok_Dot_Dot then
555             Check_Simple_Expression (Expr_Node);
556             Range_Node := New_Node (N_Range, Token_Ptr);
557             Set_Low_Bound (Range_Node, Expr_Node);
558             Scan; -- past ..
559             Expr_Node := P_Expression;
560             Check_Simple_Expression (Expr_Node);
561             Set_High_Bound (Range_Node, Expr_Node);
562
563          --  Third possibility: Type_name range Range
564
565          elsif Token = Tok_Range then
566             if Expr_Form /= EF_Simple_Name then
567                Error_Msg_SC ("subtype mark must precede RANGE");
568                raise Error_Resync;
569             end if;
570
571             Range_Node := P_Subtype_Indication (Expr_Node);
572
573          --  Otherwise we just have an expression. It is true that we might
574          --  have a subtype mark without a range constraint but this case
575          --  is syntactically indistinguishable from the expression case.
576
577          else
578             Arg_List := New_List;
579             goto LP_State_Expr;
580          end if;
581
582          --  Fall through here with unmistakable Discrete range scanned,
583          --  which means that we definitely have the case of a slice. The
584          --  Discrete range is in Range_Node.
585
586          if Token = Tok_Comma then
587             Error_Msg_SC ("slice cannot have more than one dimension");
588             raise Error_Resync;
589
590          elsif Token /= Tok_Right_Paren then
591             T_Right_Paren;
592             raise Error_Resync;
593
594          else
595             Scan; -- past right paren
596             Prefix_Node := Name_Node;
597             Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
598             Set_Prefix (Name_Node, Prefix_Node);
599             Set_Discrete_Range (Name_Node, Range_Node);
600
601             --  An operator node is legal as a prefix to other names,
602             --  but not for a slice.
603
604             if Nkind (Prefix_Node) = N_Operator_Symbol then
605                Error_Msg_N ("illegal prefix for slice", Prefix_Node);
606             end if;
607
608             --  If we have a name extension, go scan it
609
610             if Token in Token_Class_Namext then
611                goto Scan_Name_Extension_OK;
612
613             --  Otherwise return (a slice is a name, but is not a call)
614
615             else
616                Expr_Form := EF_Name;
617                return Name_Node;
618             end if;
619          end if;
620
621       --  In LP_State_Expr, we have scanned one or more expressions, and
622       --  so we have a call or an indexed component which is a name. On
623       --  entry we have the expression just scanned in Expr_Node and
624       --  Arg_List contains the list of expressions encountered so far
625
626       <<LP_State_Expr>>
627          Append (Expr_Node, Arg_List);
628
629          if Token = Tok_Arrow then
630             Error_Msg
631               ("expect identifier in parameter association",
632                 Sloc (Expr_Node));
633             Scan;  --   past arrow.
634
635          elsif not Comma_Present then
636             T_Right_Paren;
637             Prefix_Node := Name_Node;
638             Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
639             Set_Prefix (Name_Node, Prefix_Node);
640             Set_Expressions (Name_Node, Arg_List);
641             goto Scan_Name_Extension;
642          end if;
643
644          --  Comma present (and scanned out), test for identifier => case
645          --  Test for identifier => case
646
647          if Token = Tok_Identifier then
648             Save_Scan_State (Scan_State); -- at Id
649             Scan; -- past Id
650
651             --  Test for => (allow := as error substitute)
652
653             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
654                Restore_Scan_State (Scan_State); -- to Id
655                goto LP_State_Call;
656
657             --  Otherwise it's just an expression after all, so backup
658
659             else
660                Restore_Scan_State (Scan_State); -- to Id
661             end if;
662          end if;
663
664          --  Here we have an expression after all, so stay in this state
665
666          Expr_Node := P_Expression;
667          goto LP_State_Expr;
668
669       --  LP_State_Call corresponds to the situation in which at least
670       --  one instance of Id => Expression has been encountered, so we
671       --  know that we do not have a name, but rather a call. We enter
672       --  it with the scan pointer pointing to the next argument to scan,
673       --  and Arg_List containing the list of arguments scanned so far.
674
675       <<LP_State_Call>>
676
677          --  Test for case of Id => Expression (named parameter)
678
679          if Token = Tok_Identifier then
680             Save_Scan_State (Scan_State); -- at Id
681             Ident_Node := Token_Node;
682             Scan; -- past Id
683
684             --  Deal with => (allow := as erroneous substitute)
685
686             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
687                Arg_Node :=
688                  New_Node (N_Parameter_Association, Prev_Token_Ptr);
689                Set_Selector_Name (Arg_Node, Ident_Node);
690                T_Arrow;
691                Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
692                Append (Arg_Node, Arg_List);
693
694                --  If a comma follows, go back and scan next entry
695
696                if Comma_Present then
697                   goto LP_State_Call;
698
699                --  Otherwise we have the end of a call
700
701                else
702                   Prefix_Node := Name_Node;
703                   Name_Node :=
704                     New_Node (N_Function_Call, Sloc (Prefix_Node));
705                   Set_Name (Name_Node, Prefix_Node);
706                   Set_Parameter_Associations (Name_Node, Arg_List);
707                   T_Right_Paren;
708
709                   if Token in Token_Class_Namext then
710                      goto Scan_Name_Extension_OK;
711
712                   --  This is a case of a call which cannot be a name
713
714                   else
715                      Expr_Form := EF_Name;
716                      return Name_Node;
717                   end if;
718                end if;
719
720             --  Not named parameter: Id started an expression after all
721
722             else
723                Restore_Scan_State (Scan_State); -- to Id
724             end if;
725          end if;
726
727          --  Here if entry did not start with Id => which means that it
728          --  is a positional parameter, which is not allowed, since we
729          --  have seen at least one named parameter already.
730
731          Error_Msg_SC
732             ("positional parameter association " &
733               "not allowed after named one");
734
735          Expr_Node := P_Expression;
736
737          --  Leaving the '>' in an association is not unusual, so suggest
738          --  a possible fix.
739
740          if Nkind (Expr_Node) = N_Op_Eq then
741             Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
742          end if;
743
744          --  We go back to scanning out expressions, so that we do not get
745          --  multiple error messages when several positional parameters
746          --  follow a named parameter.
747
748          goto LP_State_Expr;
749
750          --  End of treatment for name extensions starting with left paren
751
752       --  End of loop through name extensions
753
754    end P_Name;
755
756    --  This function parses a restricted form of Names which are either
757    --  designators, or designators preceded by a sequence of prefixes
758    --  that are direct names.
759
760    --  Error recovery: cannot raise Error_Resync
761
762    function P_Function_Name return Node_Id is
763       Designator_Node : Node_Id;
764       Prefix_Node     : Node_Id;
765       Selector_Node   : Node_Id;
766       Dot_Sloc        : Source_Ptr := No_Location;
767
768    begin
769       --  Prefix_Node is set to the gathered prefix so far, Empty means that
770       --  no prefix has been scanned. This allows us to build up the result
771       --  in the required right recursive manner.
772
773       Prefix_Node := Empty;
774
775       --  Loop through prefixes
776
777       loop
778          Designator_Node := Token_Node;
779
780          if Token not in Token_Class_Desig then
781             return P_Identifier; -- let P_Identifier issue the error message
782
783          else -- Token in Token_Class_Desig
784             Scan; -- past designator
785             exit when Token /= Tok_Dot;
786          end if;
787
788          --  Here at a dot, with token just before it in Designator_Node
789
790          if No (Prefix_Node) then
791             Prefix_Node := Designator_Node;
792          else
793             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
794             Set_Prefix (Selector_Node, Prefix_Node);
795             Set_Selector_Name (Selector_Node, Designator_Node);
796             Prefix_Node := Selector_Node;
797          end if;
798
799          Dot_Sloc := Token_Ptr;
800          Scan; -- past dot
801       end loop;
802
803       --  Fall out of the loop having just scanned a designator
804
805       if No (Prefix_Node) then
806          return 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          return Selector_Node;
812       end if;
813
814    exception
815       when Error_Resync =>
816          return Error;
817
818    end P_Function_Name;
819
820    --  This function parses a restricted form of Names which are either
821    --  identifiers, or identifiers preceded by a sequence of prefixes
822    --  that are direct names.
823
824    --  Error recovery: cannot raise Error_Resync
825
826    function P_Qualified_Simple_Name return Node_Id is
827       Designator_Node : Node_Id;
828       Prefix_Node     : Node_Id;
829       Selector_Node   : Node_Id;
830       Dot_Sloc        : Source_Ptr := No_Location;
831
832    begin
833       --  Prefix node is set to the gathered prefix so far, Empty means that
834       --  no prefix has been scanned. This allows us to build up the result
835       --  in the required right recursive manner.
836
837       Prefix_Node := Empty;
838
839       --  Loop through prefixes
840
841       loop
842          Designator_Node := Token_Node;
843
844          if Token = Tok_Identifier then
845             Scan; -- past identifier
846             exit when Token /= Tok_Dot;
847
848          elsif Token not in Token_Class_Desig then
849             return P_Identifier; -- let P_Identifier issue the error message
850
851          else
852             Scan; -- past designator
853
854             if Token /= Tok_Dot then
855                Error_Msg_SP ("identifier expected");
856                return Error;
857             end if;
858          end if;
859
860          --  Here at a dot, with token just before it in Designator_Node
861
862          if No (Prefix_Node) then
863             Prefix_Node := Designator_Node;
864          else
865             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
866             Set_Prefix (Selector_Node, Prefix_Node);
867             Set_Selector_Name (Selector_Node, Designator_Node);
868             Prefix_Node := Selector_Node;
869          end if;
870
871          Dot_Sloc := Token_Ptr;
872          Scan; -- past dot
873       end loop;
874
875       --  Fall out of the loop having just scanned an identifier
876
877       if No (Prefix_Node) then
878          return Designator_Node;
879       else
880          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
881          Set_Prefix (Selector_Node, Prefix_Node);
882          Set_Selector_Name (Selector_Node, Designator_Node);
883          return Selector_Node;
884       end if;
885
886    exception
887       when Error_Resync =>
888          return Error;
889
890    end P_Qualified_Simple_Name;
891
892    --  This procedure differs from P_Qualified_Simple_Name only in that it
893    --  raises Error_Resync if any error is encountered. It only returns after
894    --  scanning a valid qualified simple name.
895
896    --  Error recovery: can raise Error_Resync
897
898    function P_Qualified_Simple_Name_Resync return Node_Id is
899       Designator_Node : Node_Id;
900       Prefix_Node     : Node_Id;
901       Selector_Node   : Node_Id;
902       Dot_Sloc        : Source_Ptr := No_Location;
903
904    begin
905       Prefix_Node := Empty;
906
907       --  Loop through prefixes
908
909       loop
910          Designator_Node := Token_Node;
911
912          if Token = Tok_Identifier then
913             Scan; -- past identifier
914             exit when Token /= Tok_Dot;
915
916          elsif Token not in Token_Class_Desig then
917             Discard_Junk_Node (P_Identifier); -- to issue the error message
918             raise Error_Resync;
919
920          else
921             Scan; -- past designator
922
923             if Token /= Tok_Dot then
924                Error_Msg_SP ("identifier expected");
925                raise Error_Resync;
926             end if;
927          end if;
928
929          --  Here at a dot, with token just before it in Designator_Node
930
931          if No (Prefix_Node) then
932             Prefix_Node := Designator_Node;
933          else
934             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
935             Set_Prefix (Selector_Node, Prefix_Node);
936             Set_Selector_Name (Selector_Node, Designator_Node);
937             Prefix_Node := Selector_Node;
938          end if;
939
940          Dot_Sloc := Token_Ptr;
941          Scan; -- past period
942       end loop;
943
944       --  Fall out of the loop having just scanned an identifier
945
946       if No (Prefix_Node) then
947          return Designator_Node;
948       else
949          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
950          Set_Prefix (Selector_Node, Prefix_Node);
951          Set_Selector_Name (Selector_Node, Designator_Node);
952          return Selector_Node;
953       end if;
954
955    end P_Qualified_Simple_Name_Resync;
956
957    ----------------------
958    -- 4.1  Direct_Name --
959    ----------------------
960
961    --  Parsed by P_Name and other functions in section 4.1
962
963    -----------------
964    -- 4.1  Prefix --
965    -----------------
966
967    --  Parsed by P_Name (4.1)
968
969    -------------------------------
970    -- 4.1  Explicit Dereference --
971    -------------------------------
972
973    --  Parsed by P_Name (4.1)
974
975    -------------------------------
976    -- 4.1  Implicit_Dereference --
977    -------------------------------
978
979    --  Parsed by P_Name (4.1)
980
981    ----------------------------
982    -- 4.1  Indexed Component --
983    ----------------------------
984
985    --  Parsed by P_Name (4.1)
986
987    ----------------
988    -- 4.1  Slice --
989    ----------------
990
991    --  Parsed by P_Name (4.1)
992
993    -----------------------------
994    -- 4.1  Selected_Component --
995    -----------------------------
996
997    --  Parsed by P_Name (4.1)
998
999    ------------------------
1000    -- 4.1  Selector Name --
1001    ------------------------
1002
1003    --  Parsed by P_Name (4.1)
1004
1005    ------------------------------
1006    -- 4.1  Attribute Reference --
1007    ------------------------------
1008
1009    --  Parsed by P_Name (4.1)
1010
1011    -------------------------------
1012    -- 4.1  Attribute Designator --
1013    -------------------------------
1014
1015    --  Parsed by P_Name (4.1)
1016
1017    --------------------------------------
1018    -- 4.1.4  Range Attribute Reference --
1019    --------------------------------------
1020
1021    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1022
1023    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1024
1025    --  In the grammar, a RANGE attribute is simply a name, but its use is
1026    --  highly restricted, so in the parser, we do not regard it as a name.
1027    --  Instead, P_Name returns without scanning the 'RANGE part of the
1028    --  attribute, and the caller uses the following function to construct
1029    --  a range attribute in places where it is appropriate.
1030
1031    --  Note that RANGE here is treated essentially as an identifier,
1032    --  rather than a reserved word.
1033
1034    --  The caller has parsed the prefix, i.e. a name, and Token points to
1035    --  the apostrophe. The token after the apostrophe is known to be RANGE
1036    --  at this point. The prefix node becomes the prefix of the attribute.
1037
1038    --  Error_Recovery: Cannot raise Error_Resync
1039
1040    function P_Range_Attribute_Reference
1041      (Prefix_Node : Node_Id)
1042       return        Node_Id
1043    is
1044       Attr_Node  : Node_Id;
1045
1046    begin
1047       Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1048       Set_Prefix (Attr_Node, Prefix_Node);
1049       Scan; -- past apostrophe
1050
1051       if Style_Check then
1052          Style.Check_Attribute_Name (True);
1053       end if;
1054
1055       Set_Attribute_Name (Attr_Node, Name_Range);
1056       Scan; -- past RANGE
1057
1058       if Token = Tok_Left_Paren then
1059          Scan; -- past left paren
1060          Set_Expressions (Attr_Node, New_List (P_Expression));
1061          T_Right_Paren;
1062       end if;
1063
1064       return Attr_Node;
1065    end P_Range_Attribute_Reference;
1066
1067    ---------------------------------------
1068    -- 4.1.4  Range Attribute Designator --
1069    ---------------------------------------
1070
1071    --  Parsed by P_Range_Attribute_Reference (4.4)
1072
1073    --------------------
1074    -- 4.3  Aggregate --
1075    --------------------
1076
1077    --  AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1078
1079    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1080    --  an aggregate is known to be required (code statement, extension
1081    --  aggregate), in which cases this routine performs the necessary check
1082    --  that we have an aggregate rather than a parenthesized expression
1083
1084    --  Error recovery: can raise Error_Resync
1085
1086    function P_Aggregate return Node_Id is
1087       Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1088       Aggr_Node : constant Node_Id    := P_Aggregate_Or_Paren_Expr;
1089
1090    begin
1091       if Nkind (Aggr_Node) /= N_Aggregate
1092            and then
1093          Nkind (Aggr_Node) /= N_Extension_Aggregate
1094       then
1095          Error_Msg
1096            ("aggregate may not have single positional component", Aggr_Sloc);
1097          return Error;
1098       else
1099          return Aggr_Node;
1100       end if;
1101    end P_Aggregate;
1102
1103    -------------------------------------------------
1104    -- 4.3  Aggregate or Parenthesized Expresssion --
1105    -------------------------------------------------
1106
1107    --  This procedure parses out either an aggregate or a parenthesized
1108    --  expression (these two constructs are closely related, since a
1109    --  parenthesized expression looks like an aggregate with a single
1110    --  positional component).
1111
1112    --  AGGREGATE ::=
1113    --    RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1114
1115    --  RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1116
1117    --  RECORD_COMPONENT_ASSOCIATION_LIST ::=
1118    --     RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1119    --   | null record
1120
1121    --  RECORD_COMPONENT_ASSOCIATION ::=
1122    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1123
1124    --  COMPONENT_CHOICE_LIST ::=
1125    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1126    --  | others
1127
1128    --  EXTENSION_AGGREGATE ::=
1129    --    (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1130
1131    --  ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1132
1133    --  ARRAY_AGGREGATE ::=
1134    --    POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1135
1136    --  POSITIONAL_ARRAY_AGGREGATE ::=
1137    --    (EXPRESSION, EXPRESSION {, EXPRESSION})
1138    --  | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1139    --  | (EXPRESSION {, EXPRESSION}, others => <>)
1140
1141    --  NAMED_ARRAY_AGGREGATE ::=
1142    --    (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1143
1144    --  PRIMARY ::= (EXPRESSION);
1145
1146    --  Error recovery: can raise Error_Resync
1147
1148    --  Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1149    --        to Ada 2005 limited aggregates (AI-287)
1150
1151    function P_Aggregate_Or_Paren_Expr return Node_Id is
1152       Aggregate_Node : Node_Id;
1153       Expr_List      : List_Id;
1154       Assoc_List     : List_Id;
1155       Expr_Node      : Node_Id;
1156       Lparen_Sloc    : Source_Ptr;
1157       Scan_State     : Saved_Scan_State;
1158
1159    begin
1160       Lparen_Sloc := Token_Ptr;
1161       T_Left_Paren;
1162
1163       --  Note: the mechanism used here of rescanning the initial expression
1164       --  is distinctly unpleasant, but it saves a lot of fiddling in scanning
1165       --  out the discrete choice list.
1166
1167       --  Deal with expression and extension aggregate cases first
1168
1169       if Token /= Tok_Others then
1170          Save_Scan_State (Scan_State); -- at start of expression
1171
1172          --  Deal with (NULL RECORD) case
1173
1174          if Token = Tok_Null then
1175             Scan; -- past NULL
1176
1177             if Token = Tok_Record then
1178                Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1179                Set_Null_Record_Present (Aggregate_Node, True);
1180                Scan; -- past RECORD
1181                T_Right_Paren;
1182                return Aggregate_Node;
1183             else
1184                Restore_Scan_State (Scan_State); -- to NULL that must be expr
1185             end if;
1186          end if;
1187
1188          --  Ada 2005 (AI-287): The box notation is allowed only with named
1189          --  notation because positional notation might be error prone. For
1190          --  example, in "(X, <>, Y, <>)", there is no type associated with
1191          --  the boxes, so you might not be leaving out the components you
1192          --  thought you were leaving out.
1193
1194          if Ada_Version >= Ada_05 and then Token = Tok_Box then
1195             Error_Msg_SC ("(Ada 2005) box notation only allowed with "
1196                           & "named notation");
1197             Scan; --  past BOX
1198             Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1199             return Aggregate_Node;
1200          end if;
1201
1202          Expr_Node := P_Expression_Or_Range_Attribute;
1203
1204          --  Extension aggregate case
1205
1206          if Token = Tok_With then
1207
1208             if Nkind (Expr_Node) = N_Attribute_Reference
1209               and then Attribute_Name (Expr_Node) = Name_Range
1210             then
1211                Bad_Range_Attribute (Sloc (Expr_Node));
1212                return Error;
1213             end if;
1214
1215             if Ada_Version = Ada_83 then
1216                Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1217             end if;
1218
1219             Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1220             Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1221             Scan; -- past WITH
1222
1223             --  Deal with WITH NULL RECORD case
1224
1225             if Token = Tok_Null then
1226                Save_Scan_State (Scan_State); -- at NULL
1227                Scan; -- past NULL
1228
1229                if Token = Tok_Record then
1230                   Scan; -- past RECORD
1231                   Set_Null_Record_Present (Aggregate_Node, True);
1232                   T_Right_Paren;
1233                   return Aggregate_Node;
1234
1235                else
1236                   Restore_Scan_State (Scan_State); -- to NULL that must be expr
1237                end if;
1238             end if;
1239
1240             if Token /= Tok_Others then
1241                Save_Scan_State (Scan_State);
1242                Expr_Node := P_Expression;
1243             else
1244                Expr_Node := Empty;
1245             end if;
1246
1247          --  Expression case
1248
1249          elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1250
1251             if Nkind (Expr_Node) = N_Attribute_Reference
1252               and then Attribute_Name (Expr_Node) = Name_Range
1253             then
1254                Bad_Range_Attribute (Sloc (Expr_Node));
1255                return Error;
1256             end if;
1257
1258             --  Bump paren count of expression, note that if the paren count
1259             --  is already at the maximum, then we leave it alone. This will
1260             --  cause some failures in pathalogical conformance tests, which
1261             --  we do not shed a tear over!
1262
1263             if Expr_Node /= Error then
1264                if Paren_Count (Expr_Node) /= Paren_Count_Type'Last then
1265                   Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1266                end if;
1267             end if;
1268
1269             T_Right_Paren; -- past right paren (error message if none)
1270             return Expr_Node;
1271
1272          --  Normal aggregate case
1273
1274          else
1275             Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1276          end if;
1277
1278       --  Others case
1279
1280       else
1281          Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1282          Expr_Node := Empty;
1283       end if;
1284
1285       --  Prepare to scan list of component associations
1286
1287       Expr_List  := No_List; -- don't set yet, maybe all named entries
1288       Assoc_List := No_List; -- don't set yet, maybe all positional entries
1289
1290       --  This loop scans through component associations. On entry to the
1291       --  loop, an expression has been scanned at the start of the current
1292       --  association unless initial token was OTHERS, in which case
1293       --  Expr_Node is set to Empty.
1294
1295       loop
1296          --  Deal with others association first. This is a named association
1297
1298          if No (Expr_Node) then
1299             if No (Assoc_List) then
1300                Assoc_List := New_List;
1301             end if;
1302
1303             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1304
1305          --  Improper use of WITH
1306
1307          elsif Token = Tok_With then
1308             Error_Msg_SC ("WITH must be preceded by single expression in " &
1309                              "extension aggregate");
1310             raise Error_Resync;
1311
1312          --  A range attribute can only appear as part of a discrete choice
1313          --  list.
1314
1315          elsif Nkind (Expr_Node) = N_Attribute_Reference
1316            and then Attribute_Name (Expr_Node) = Name_Range
1317            and then Token /= Tok_Arrow
1318            and then Token /= Tok_Vertical_Bar
1319          then
1320             Bad_Range_Attribute (Sloc (Expr_Node));
1321             return Error;
1322
1323          --  Assume positional case if comma, right paren, or literal or
1324          --  identifier or OTHERS follows (the latter cases are missing
1325          --  comma cases). Also assume positional if a semicolon follows,
1326          --  which can happen if there are missing parens
1327
1328          elsif Token = Tok_Comma
1329            or else Token = Tok_Right_Paren
1330            or else Token = Tok_Others
1331            or else Token in Token_Class_Lit_Or_Name
1332            or else Token = Tok_Semicolon
1333          then
1334             if Present (Assoc_List) then
1335                Error_Msg_BC
1336                   ("""='>"" expected (positional association cannot follow " &
1337                    "named association)");
1338             end if;
1339
1340             if No (Expr_List) then
1341                Expr_List := New_List;
1342             end if;
1343
1344             Append (Expr_Node, Expr_List);
1345
1346          --  Anything else is assumed to be a named association
1347
1348          else
1349             Restore_Scan_State (Scan_State); -- to start of expression
1350
1351             if No (Assoc_List) then
1352                Assoc_List := New_List;
1353             end if;
1354
1355             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1356          end if;
1357
1358          exit when not Comma_Present;
1359
1360          --  If we are at an expression terminator, something is seriously
1361          --  wrong, so let's get out now, before we start eating up stuff
1362          --  that doesn't belong to us!
1363
1364          if Token in Token_Class_Eterm then
1365             Error_Msg_AP ("expecting expression or component association");
1366             exit;
1367          end if;
1368
1369          --  Otherwise initiate for reentry to top of loop by scanning an
1370          --  initial expression, unless the first token is OTHERS.
1371
1372          if Token = Tok_Others then
1373             Expr_Node := Empty;
1374          else
1375             Save_Scan_State (Scan_State); -- at start of expression
1376             Expr_Node := P_Expression_Or_Range_Attribute;
1377
1378          end if;
1379       end loop;
1380
1381       --  All component associations (positional and named) have been scanned
1382
1383       T_Right_Paren;
1384       Set_Expressions (Aggregate_Node, Expr_List);
1385       Set_Component_Associations (Aggregate_Node, Assoc_List);
1386       return Aggregate_Node;
1387    end P_Aggregate_Or_Paren_Expr;
1388
1389    ------------------------------------------------
1390    -- 4.3  Record or Array Component Association --
1391    ------------------------------------------------
1392
1393    --  RECORD_COMPONENT_ASSOCIATION ::=
1394    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1395    --  | COMPONENT_CHOICE_LIST => <>
1396
1397    --  COMPONENT_CHOICE_LIST =>
1398    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1399    --  | others
1400
1401    --  ARRAY_COMPONENT_ASSOCIATION ::=
1402    --    DISCRETE_CHOICE_LIST => EXPRESSION
1403    --  | DISCRETE_CHOICE_LIST => <>
1404
1405    --  Note: this routine only handles the named cases, including others.
1406    --  Cases where the component choice list is not present have already
1407    --  been handled directly.
1408
1409    --  Error recovery: can raise Error_Resync
1410
1411    --  Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1412    --        rules have been extended to give support to Ada 2005 limited
1413    --        aggregates (AI-287)
1414
1415    function P_Record_Or_Array_Component_Association return Node_Id is
1416       Assoc_Node : Node_Id;
1417
1418    begin
1419       Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1420       Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1421       Set_Sloc (Assoc_Node, Token_Ptr);
1422       TF_Arrow;
1423
1424       if Token = Tok_Box then
1425
1426          --  Ada 2005(AI-287): The box notation is used to indicate the
1427          --  default initialization of limited aggregate components
1428
1429          if Ada_Version < Ada_05 then
1430             Error_Msg_SP
1431               ("limited aggregate is an Ada 2005 extension");
1432             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1433          end if;
1434
1435          Set_Box_Present (Assoc_Node);
1436          Scan; -- Past box
1437       else
1438          Set_Expression (Assoc_Node, P_Expression);
1439       end if;
1440
1441       return Assoc_Node;
1442    end P_Record_Or_Array_Component_Association;
1443
1444    -----------------------------
1445    -- 4.3.1  Record Aggregate --
1446    -----------------------------
1447
1448    --  Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1449    --  All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1450
1451    ----------------------------------------------
1452    -- 4.3.1  Record Component Association List --
1453    ----------------------------------------------
1454
1455    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1456
1457    ----------------------------------
1458    -- 4.3.1  Component Choice List --
1459    ----------------------------------
1460
1461    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1462
1463    --------------------------------
1464    -- 4.3.1  Extension Aggregate --
1465    --------------------------------
1466
1467    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1468
1469    --------------------------
1470    -- 4.3.1  Ancestor Part --
1471    --------------------------
1472
1473    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1474
1475    ----------------------------
1476    -- 4.3.1  Array Aggregate --
1477    ----------------------------
1478
1479    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1480
1481    ---------------------------------------
1482    -- 4.3.1  Positional Array Aggregate --
1483    ---------------------------------------
1484
1485    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1486
1487    ----------------------------------
1488    -- 4.3.1  Named Array Aggregate --
1489    ----------------------------------
1490
1491    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1492
1493    ----------------------------------------
1494    -- 4.3.1  Array Component Association --
1495    ----------------------------------------
1496
1497    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1498
1499    ---------------------
1500    -- 4.4  Expression --
1501    ---------------------
1502
1503    --  EXPRESSION ::=
1504    --    RELATION {and RELATION} | RELATION {and then RELATION}
1505    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1506    --  | RELATION {xor RELATION}
1507
1508    --  On return, Expr_Form indicates the categorization of the expression
1509    --  EF_Range_Attr is not a possible value (if a range attribute is found,
1510    --  an error message is given, and Error is returned).
1511
1512    --  Error recovery: cannot raise Error_Resync
1513
1514    function P_Expression return Node_Id is
1515       Logical_Op      : Node_Kind;
1516       Prev_Logical_Op : Node_Kind;
1517       Op_Location     : Source_Ptr;
1518       Node1           : Node_Id;
1519       Node2           : Node_Id;
1520
1521    begin
1522       Node1 := P_Relation;
1523
1524       if Token in Token_Class_Logop then
1525          Prev_Logical_Op := N_Empty;
1526
1527          loop
1528             Op_Location := Token_Ptr;
1529             Logical_Op := P_Logical_Operator;
1530
1531             if Prev_Logical_Op /= N_Empty and then
1532                Logical_Op /= Prev_Logical_Op
1533             then
1534                Error_Msg
1535                  ("mixed logical operators in expression", Op_Location);
1536                Prev_Logical_Op := N_Empty;
1537             else
1538                Prev_Logical_Op := Logical_Op;
1539             end if;
1540
1541             Node2 := Node1;
1542             Node1 := New_Node (Logical_Op, Op_Location);
1543             Set_Left_Opnd (Node1, Node2);
1544             Set_Right_Opnd (Node1, P_Relation);
1545             Set_Op_Name (Node1);
1546             exit when Token not in Token_Class_Logop;
1547          end loop;
1548
1549          Expr_Form := EF_Non_Simple;
1550       end if;
1551
1552       if Token = Tok_Apostrophe then
1553          Bad_Range_Attribute (Token_Ptr);
1554          return Error;
1555       else
1556          return Node1;
1557       end if;
1558
1559    end P_Expression;
1560
1561    --  This function is identical to the normal P_Expression, except that it
1562    --  checks that the expression scan did not stop on a right paren. It is
1563    --  called in all contexts where a right parenthesis cannot legitimately
1564    --  follow an expression.
1565
1566    function P_Expression_No_Right_Paren return Node_Id is
1567    begin
1568       return No_Right_Paren (P_Expression);
1569    end P_Expression_No_Right_Paren;
1570
1571    ----------------------------------------
1572    -- 4.4  Expression_Or_Range_Attribute --
1573    ----------------------------------------
1574
1575    --  EXPRESSION ::=
1576    --    RELATION {and RELATION} | RELATION {and then RELATION}
1577    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1578    --  | RELATION {xor RELATION}
1579
1580    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1581
1582    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1583
1584    --  On return, Expr_Form indicates the categorization of the expression
1585    --  and EF_Range_Attr is one of the possibilities.
1586
1587    --  Error recovery: cannot raise Error_Resync
1588
1589    --  In the grammar, a RANGE attribute is simply a name, but its use is
1590    --  highly restricted, so in the parser, we do not regard it as a name.
1591    --  Instead, P_Name returns without scanning the 'RANGE part of the
1592    --  attribute, and P_Expression_Or_Range_Attribute handles the range
1593    --  attribute reference. In the normal case where a range attribute is
1594    --  not allowed, an error message is issued by P_Expression.
1595
1596    function P_Expression_Or_Range_Attribute return Node_Id is
1597       Logical_Op      : Node_Kind;
1598       Prev_Logical_Op : Node_Kind;
1599       Op_Location     : Source_Ptr;
1600       Node1           : Node_Id;
1601       Node2           : Node_Id;
1602       Attr_Node       : Node_Id;
1603
1604    begin
1605       Node1 := P_Relation;
1606
1607       if Token = Tok_Apostrophe then
1608          Attr_Node := P_Range_Attribute_Reference (Node1);
1609          Expr_Form := EF_Range_Attr;
1610          return Attr_Node;
1611
1612       elsif Token in Token_Class_Logop then
1613          Prev_Logical_Op := N_Empty;
1614
1615          loop
1616             Op_Location := Token_Ptr;
1617             Logical_Op := P_Logical_Operator;
1618
1619             if Prev_Logical_Op /= N_Empty and then
1620                Logical_Op /= Prev_Logical_Op
1621             then
1622                Error_Msg
1623                  ("mixed logical operators in expression", Op_Location);
1624                Prev_Logical_Op := N_Empty;
1625             else
1626                Prev_Logical_Op := Logical_Op;
1627             end if;
1628
1629             Node2 := Node1;
1630             Node1 := New_Node (Logical_Op, Op_Location);
1631             Set_Left_Opnd (Node1, Node2);
1632             Set_Right_Opnd (Node1, P_Relation);
1633             Set_Op_Name (Node1);
1634             exit when Token not in Token_Class_Logop;
1635          end loop;
1636
1637          Expr_Form := EF_Non_Simple;
1638       end if;
1639
1640       if Token = Tok_Apostrophe then
1641          Bad_Range_Attribute (Token_Ptr);
1642          return Error;
1643       else
1644          return Node1;
1645       end if;
1646    end P_Expression_Or_Range_Attribute;
1647
1648    -------------------
1649    -- 4.4  Relation --
1650    -------------------
1651
1652    --  RELATION ::=
1653    --    SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1654    --  | SIMPLE_EXPRESSION [not] in RANGE
1655    --  | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1656
1657    --  On return, Expr_Form indicates the categorization of the expression
1658
1659    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1660    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1661
1662    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1663    --  expression, then tokens are scanned until either a non-expression token,
1664    --  a right paren (not matched by a left paren) or a comma, is encountered.
1665
1666    function P_Relation return Node_Id is
1667       Node1, Node2 : Node_Id;
1668       Optok        : Source_Ptr;
1669
1670    begin
1671       Node1 := P_Simple_Expression;
1672
1673       if Token not in Token_Class_Relop then
1674          return Node1;
1675
1676       else
1677          --  Here we have a relational operator following. If so then scan it
1678          --  out. Note that the assignment symbol := is treated as a relational
1679          --  operator to improve the error recovery when it is misused for =.
1680          --  P_Relational_Operator also parses the IN and NOT IN operations.
1681
1682          Optok := Token_Ptr;
1683          Node2 := New_Node (P_Relational_Operator, Optok);
1684          Set_Left_Opnd (Node2, Node1);
1685          Set_Op_Name (Node2);
1686
1687          --  Case of IN or NOT IN
1688
1689          if Prev_Token = Tok_In then
1690             Set_Right_Opnd (Node2, P_Range_Or_Subtype_Mark);
1691
1692          --  Case of relational operator (= /= < <= > >=)
1693
1694          else
1695             Set_Right_Opnd (Node2, P_Simple_Expression);
1696          end if;
1697
1698          Expr_Form := EF_Non_Simple;
1699
1700          if Token in Token_Class_Relop then
1701             Error_Msg_SC ("unexpected relational operator");
1702             raise Error_Resync;
1703          end if;
1704
1705          return Node2;
1706       end if;
1707
1708    --  If any error occurs, then scan to the next expression terminator symbol
1709    --  or comma or right paren at the outer (i.e. current) parentheses level.
1710    --  The flags are set to indicate a normal simple expression.
1711
1712    exception
1713       when Error_Resync =>
1714          Resync_Expression;
1715          Expr_Form := EF_Simple;
1716          return Error;
1717    end P_Relation;
1718
1719    ----------------------------
1720    -- 4.4  Simple Expression --
1721    ----------------------------
1722
1723    --  SIMPLE_EXPRESSION ::=
1724    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1725
1726    --  On return, Expr_Form indicates the categorization of the expression
1727
1728    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1729    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1730
1731    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1732    --  expression, then tokens are scanned until either a non-expression token,
1733    --  a right paren (not matched by a left paren) or a comma, is encountered.
1734
1735    --  Note: P_Simple_Expression is called only internally by higher level
1736    --  expression routines. In cases in the grammar where a simple expression
1737    --  is required, the approach is to scan an expression, and then post an
1738    --  appropriate error message if the expression obtained is not simple. This
1739    --  gives better error recovery and treatment.
1740
1741    function P_Simple_Expression return Node_Id is
1742       Scan_State : Saved_Scan_State;
1743       Node1      : Node_Id;
1744       Node2      : Node_Id;
1745       Tokptr     : Source_Ptr;
1746
1747    begin
1748       --  Check for cases starting with a name. There are two reasons for
1749       --  special casing. First speed things up by catching a common case
1750       --  without going through several routine layers. Second the caller must
1751       --  be informed via Expr_Form when the simple expression is a name.
1752
1753       if Token in Token_Class_Name then
1754          Node1 := P_Name;
1755
1756          --  Deal with apostrophe cases
1757
1758          if Token = Tok_Apostrophe then
1759             Save_Scan_State (Scan_State); -- at apostrophe
1760             Scan; -- past apostrophe
1761
1762             --  If qualified expression, scan it out and fall through
1763
1764             if Token = Tok_Left_Paren then
1765                Node1 := P_Qualified_Expression (Node1);
1766                Expr_Form := EF_Simple;
1767
1768             --  If range attribute, then we return with Token pointing to the
1769             --  apostrophe. Note: avoid the normal error check on exit. We
1770             --  know that the expression really is complete in this case!
1771
1772             else -- Token = Tok_Range then
1773                Restore_Scan_State (Scan_State); -- to apostrophe
1774                Expr_Form := EF_Simple_Name;
1775                return Node1;
1776             end if;
1777          end if;
1778
1779          --  If an expression terminator follows, the previous processing
1780          --  completely scanned out the expression (a common case), and
1781          --  left Expr_Form set appropriately for returning to our caller.
1782
1783          if Token in Token_Class_Sterm then
1784             null;
1785
1786          --  If we do not have an expression terminator, then complete the
1787          --  scan of a simple expression. This code duplicates the code
1788          --  found in P_Term and P_Factor.
1789
1790          else
1791             if Token = Tok_Double_Asterisk then
1792                if Style_Check then Style.Check_Exponentiation_Operator; end if;
1793                Node2 := New_Node (N_Op_Expon, Token_Ptr);
1794                Scan; -- past **
1795                Set_Left_Opnd (Node2, Node1);
1796                Set_Right_Opnd (Node2, P_Primary);
1797                Set_Op_Name (Node2);
1798                Node1 := Node2;
1799             end if;
1800
1801             loop
1802                exit when Token not in Token_Class_Mulop;
1803                Tokptr := Token_Ptr;
1804                Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1805                if Style_Check then Style.Check_Binary_Operator; end if;
1806                Scan; -- past operator
1807                Set_Left_Opnd (Node2, Node1);
1808                Set_Right_Opnd (Node2, P_Factor);
1809                Set_Op_Name (Node2);
1810                Node1 := Node2;
1811             end loop;
1812
1813             loop
1814                exit when Token not in Token_Class_Binary_Addop;
1815                Tokptr := Token_Ptr;
1816                Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1817                if Style_Check then Style.Check_Binary_Operator; end if;
1818                Scan; -- past operator
1819                Set_Left_Opnd (Node2, Node1);
1820                Set_Right_Opnd (Node2, P_Term);
1821                Set_Op_Name (Node2);
1822                Node1 := Node2;
1823             end loop;
1824
1825             Expr_Form := EF_Simple;
1826          end if;
1827
1828       --  Cases where simple expression does not start with a name
1829
1830       else
1831          --  Scan initial sign and initial Term
1832
1833          if Token in Token_Class_Unary_Addop then
1834             Tokptr := Token_Ptr;
1835             Node1 := New_Node (P_Unary_Adding_Operator, Tokptr);
1836             if Style_Check then Style.Check_Unary_Plus_Or_Minus; end if;
1837             Scan; -- past operator
1838             Set_Right_Opnd (Node1, P_Term);
1839             Set_Op_Name (Node1);
1840          else
1841             Node1 := P_Term;
1842          end if;
1843
1844          --  Scan out sequence of terms separated by binary adding operators
1845
1846          loop
1847             exit when Token not in Token_Class_Binary_Addop;
1848             Tokptr := Token_Ptr;
1849             Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1850             Scan; -- past operator
1851             Set_Left_Opnd (Node2, Node1);
1852             Set_Right_Opnd (Node2, P_Term);
1853             Set_Op_Name (Node2);
1854             Node1 := Node2;
1855          end loop;
1856
1857          --  All done, we clearly do not have name or numeric literal so this
1858          --  is a case of a simple expression which is some other possibility.
1859
1860          Expr_Form := EF_Simple;
1861       end if;
1862
1863       --  Come here at end of simple expression, where we do a couple of
1864       --  special checks to improve error recovery.
1865
1866       --  Special test to improve error recovery. If the current token
1867       --  is a period, then someone is trying to do selection on something
1868       --  that is not a name, e.g. a qualified expression.
1869
1870       if Token = Tok_Dot then
1871          Error_Msg_SC ("prefix for selection is not a name");
1872          raise Error_Resync;
1873       end if;
1874
1875       --  Special test to improve error recovery: If the current token is
1876       --  not the first token on a line (as determined by checking the
1877       --  previous token position with the start of the current line),
1878       --  then we insist that we have an appropriate terminating token.
1879       --  Consider the following two examples:
1880
1881       --   1)  if A nad B then ...
1882
1883       --   2)  A := B
1884       --       C := D
1885
1886       --  In the first example, we would like to issue a binary operator
1887       --  expected message and resynchronize to the then. In the second
1888       --  example, we do not want to issue a binary operator message, so
1889       --  that instead we will get the missing semicolon message. This
1890       --  distinction is of course a heuristic which does not always work,
1891       --  but in practice it is quite effective.
1892
1893       --  Note: the one case in which we do not go through this circuit is
1894       --  when we have scanned a range attribute and want to return with
1895       --  Token pointing to the apostrophe. The apostrophe is not normally
1896       --  an expression terminator, and is not in Token_Class_Sterm, but
1897       --  in this special case we know that the expression is complete.
1898
1899       if not Token_Is_At_Start_Of_Line
1900          and then Token not in Token_Class_Sterm
1901       then
1902          Error_Msg_AP ("binary operator expected");
1903          raise Error_Resync;
1904       else
1905          return Node1;
1906       end if;
1907
1908    --  If any error occurs, then scan to next expression terminator symbol
1909    --  or comma, right paren or vertical bar at the outer (i.e. current) paren
1910    --  level. Expr_Form is set to indicate a normal simple expression.
1911
1912    exception
1913       when Error_Resync =>
1914          Resync_Expression;
1915          Expr_Form := EF_Simple;
1916          return Error;
1917
1918    end P_Simple_Expression;
1919
1920    -----------------------------------------------
1921    -- 4.4  Simple Expression or Range Attribute --
1922    -----------------------------------------------
1923
1924    --  SIMPLE_EXPRESSION ::=
1925    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1926
1927    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1928
1929    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1930
1931    --  Error recovery: cannot raise Error_Resync
1932
1933    function P_Simple_Expression_Or_Range_Attribute return Node_Id is
1934       Sexpr     : Node_Id;
1935       Attr_Node : Node_Id;
1936
1937    begin
1938       Sexpr := P_Simple_Expression;
1939
1940       if Token = Tok_Apostrophe then
1941          Attr_Node := P_Range_Attribute_Reference (Sexpr);
1942          Expr_Form := EF_Range_Attr;
1943          return Attr_Node;
1944
1945       else
1946          return Sexpr;
1947       end if;
1948    end P_Simple_Expression_Or_Range_Attribute;
1949
1950    ---------------
1951    -- 4.4  Term --
1952    ---------------
1953
1954    --  TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
1955
1956    --  Error recovery: can raise Error_Resync
1957
1958    function P_Term return Node_Id is
1959       Node1, Node2 : Node_Id;
1960       Tokptr       : Source_Ptr;
1961
1962    begin
1963       Node1 := P_Factor;
1964
1965       loop
1966          exit when Token not in Token_Class_Mulop;
1967          Tokptr := Token_Ptr;
1968          Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1969          Scan; -- past operator
1970          Set_Left_Opnd (Node2, Node1);
1971          Set_Right_Opnd (Node2, P_Factor);
1972          Set_Op_Name (Node2);
1973          Node1 := Node2;
1974       end loop;
1975
1976       return Node1;
1977    end P_Term;
1978
1979    -----------------
1980    -- 4.4  Factor --
1981    -----------------
1982
1983    --  FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
1984
1985    --  Error recovery: can raise Error_Resync
1986
1987    function P_Factor return Node_Id is
1988       Node1 : Node_Id;
1989       Node2 : Node_Id;
1990
1991    begin
1992       if Token = Tok_Abs then
1993          Node1 := New_Node (N_Op_Abs, Token_Ptr);
1994          if Style_Check then Style.Check_Abs_Not; end if;
1995          Scan; -- past ABS
1996          Set_Right_Opnd (Node1, P_Primary);
1997          Set_Op_Name (Node1);
1998          return Node1;
1999
2000       elsif Token = Tok_Not then
2001          Node1 := New_Node (N_Op_Not, Token_Ptr);
2002          if Style_Check then Style.Check_Abs_Not; end if;
2003          Scan; -- past NOT
2004          Set_Right_Opnd (Node1, P_Primary);
2005          Set_Op_Name (Node1);
2006          return Node1;
2007
2008       else
2009          Node1 := P_Primary;
2010
2011          if Token = Tok_Double_Asterisk then
2012             Node2 := New_Node (N_Op_Expon, Token_Ptr);
2013             Scan; -- past **
2014             Set_Left_Opnd (Node2, Node1);
2015             Set_Right_Opnd (Node2, P_Primary);
2016             Set_Op_Name (Node2);
2017             return Node2;
2018          else
2019             return Node1;
2020          end if;
2021       end if;
2022    end P_Factor;
2023
2024    ------------------
2025    -- 4.4  Primary --
2026    ------------------
2027
2028    --  PRIMARY ::=
2029    --    NUMERIC_LITERAL  | null
2030    --  | STRING_LITERAL   | AGGREGATE
2031    --  | NAME             | QUALIFIED_EXPRESSION
2032    --  | ALLOCATOR        | (EXPRESSION)
2033
2034    --  Error recovery: can raise Error_Resync
2035
2036    function P_Primary return Node_Id is
2037       Scan_State : Saved_Scan_State;
2038       Node1      : Node_Id;
2039
2040    begin
2041       --  The loop runs more than once only if misplaced pragmas are found
2042
2043       loop
2044          case Token is
2045
2046             --  Name token can start a name, call or qualified expression, all
2047             --  of which are acceptable possibilities for primary. Note also
2048             --  that string literal is included in name (as operator symbol)
2049             --  and type conversion is included in name (as indexed component).
2050
2051             when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2052                Node1 := P_Name;
2053
2054                --  All done unless apostrophe follows
2055
2056                if Token /= Tok_Apostrophe then
2057                   return Node1;
2058
2059                --  Apostrophe following means that we have either just parsed
2060                --  the subtype mark of a qualified expression, or the prefix
2061                --  or a range attribute.
2062
2063                else -- Token = Tok_Apostrophe
2064                   Save_Scan_State (Scan_State); -- at apostrophe
2065                   Scan; -- past apostrophe
2066
2067                   --  If range attribute, then this is always an error, since
2068                   --  the only legitimate case (where the scanned expression is
2069                   --  a qualified simple name) is handled at the level of the
2070                   --  Simple_Expression processing. This case corresponds to a
2071                   --  usage such as 3 + A'Range, which is always illegal.
2072
2073                   if Token = Tok_Range then
2074                      Restore_Scan_State (Scan_State); -- to apostrophe
2075                      Bad_Range_Attribute (Token_Ptr);
2076                      return Error;
2077
2078                   --  If left paren, then we have a qualified expression.
2079                   --  Note that P_Name guarantees that in this case, where
2080                   --  Token = Tok_Apostrophe on return, the only two possible
2081                   --  tokens following the apostrophe are left paren and
2082                   --  RANGE, so we know we have a left paren here.
2083
2084                   else -- Token = Tok_Left_Paren
2085                      return P_Qualified_Expression (Node1);
2086
2087                   end if;
2088                end if;
2089
2090             --  Numeric or string literal
2091
2092             when Tok_Integer_Literal |
2093                  Tok_Real_Literal    |
2094                  Tok_String_Literal  =>
2095
2096                Node1 := Token_Node;
2097                Scan; -- past number
2098                return Node1;
2099
2100             --  Left paren, starts aggregate or parenthesized expression
2101
2102             when Tok_Left_Paren =>
2103                return P_Aggregate_Or_Paren_Expr;
2104
2105             --  Allocator
2106
2107             when Tok_New =>
2108                return P_Allocator;
2109
2110             --  Null
2111
2112             when Tok_Null =>
2113                Scan; -- past NULL
2114                return New_Node (N_Null, Prev_Token_Ptr);
2115
2116             --  Pragma, not allowed here, so just skip past it
2117
2118             when Tok_Pragma =>
2119                P_Pragmas_Misplaced;
2120
2121             --  Anything else is illegal as the first token of a primary, but
2122             --  we test for a reserved identifier so that it is treated nicely
2123
2124             when others =>
2125                if Is_Reserved_Identifier then
2126                   return P_Identifier;
2127
2128                elsif Prev_Token = Tok_Comma then
2129                   Error_Msg_SP ("extra "","" ignored");
2130                   raise Error_Resync;
2131
2132                else
2133                   Error_Msg_AP ("missing operand");
2134                   raise Error_Resync;
2135                end if;
2136
2137          end case;
2138       end loop;
2139    end P_Primary;
2140
2141    ---------------------------
2142    -- 4.5  Logical Operator --
2143    ---------------------------
2144
2145    --  LOGICAL_OPERATOR  ::=  and | or | xor
2146
2147    --  Note: AND THEN and OR ELSE are also treated as logical operators
2148    --  by the parser (even though they are not operators semantically)
2149
2150    --  The value returned is the appropriate Node_Kind code for the operator
2151    --  On return, Token points to the token following the scanned operator.
2152
2153    --  The caller has checked that the first token is a legitimate logical
2154    --  operator token (i.e. is either XOR, AND, OR).
2155
2156    --  Error recovery: cannot raise Error_Resync
2157
2158    function P_Logical_Operator return Node_Kind is
2159    begin
2160       if Token = Tok_And then
2161          if Style_Check then Style.Check_Binary_Operator; end if;
2162          Scan; -- past AND
2163
2164          if Token = Tok_Then then
2165             Scan; -- past THEN
2166             return N_And_Then;
2167          else
2168             return N_Op_And;
2169          end if;
2170
2171       elsif Token = Tok_Or then
2172          if Style_Check then Style.Check_Binary_Operator; end if;
2173          Scan; -- past OR
2174
2175          if Token = Tok_Else then
2176             Scan; -- past ELSE
2177             return N_Or_Else;
2178          else
2179             return N_Op_Or;
2180          end if;
2181
2182       else -- Token = Tok_Xor
2183          if Style_Check then Style.Check_Binary_Operator; end if;
2184          Scan; -- past XOR
2185          return N_Op_Xor;
2186       end if;
2187    end P_Logical_Operator;
2188
2189    ------------------------------
2190    -- 4.5  Relational Operator --
2191    ------------------------------
2192
2193    --  RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2194
2195    --  The value returned is the appropriate Node_Kind code for the operator.
2196    --  On return, Token points to the operator token, NOT past it.
2197
2198    --  The caller has checked that the first token is a legitimate relational
2199    --  operator token (i.e. is one of the operator tokens listed above).
2200
2201    --  Error recovery: cannot raise Error_Resync
2202
2203    function P_Relational_Operator return Node_Kind is
2204       Op_Kind : Node_Kind;
2205       Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2206         (Tok_Less           => N_Op_Lt,
2207          Tok_Equal          => N_Op_Eq,
2208          Tok_Greater        => N_Op_Gt,
2209          Tok_Not_Equal      => N_Op_Ne,
2210          Tok_Greater_Equal  => N_Op_Ge,
2211          Tok_Less_Equal     => N_Op_Le,
2212          Tok_In             => N_In,
2213          Tok_Not            => N_Not_In,
2214          Tok_Box            => N_Op_Ne);
2215
2216    begin
2217       if Token = Tok_Box then
2218          Error_Msg_SC ("""'<'>"" should be ""/=""");
2219       end if;
2220
2221       Op_Kind := Relop_Node (Token);
2222       if Style_Check then Style.Check_Binary_Operator; end if;
2223       Scan; -- past operator token
2224
2225       if Prev_Token = Tok_Not then
2226          T_In;
2227       end if;
2228
2229       return Op_Kind;
2230    end P_Relational_Operator;
2231
2232    ---------------------------------
2233    -- 4.5  Binary Adding Operator --
2234    ---------------------------------
2235
2236    --  BINARY_ADDING_OPERATOR ::= + | - | &
2237
2238    --  The value returned is the appropriate Node_Kind code for the operator.
2239    --  On return, Token points to the operator token (NOT past it).
2240
2241    --  The caller has checked that the first token is a legitimate adding
2242    --  operator token (i.e. is one of the operator tokens listed above).
2243
2244    --  Error recovery: cannot raise Error_Resync
2245
2246    function P_Binary_Adding_Operator return Node_Kind is
2247       Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2248         (Tok_Ampersand      => N_Op_Concat,
2249          Tok_Minus          => N_Op_Subtract,
2250          Tok_Plus           => N_Op_Add);
2251    begin
2252       return Addop_Node (Token);
2253    end P_Binary_Adding_Operator;
2254
2255    --------------------------------
2256    -- 4.5  Unary Adding Operator --
2257    --------------------------------
2258
2259    --  UNARY_ADDING_OPERATOR ::= + | -
2260
2261    --  The value returned is the appropriate Node_Kind code for the operator.
2262    --  On return, Token points to the operator token (NOT past it).
2263
2264    --  The caller has checked that the first token is a legitimate adding
2265    --  operator token (i.e. is one of the operator tokens listed above).
2266
2267    --  Error recovery: cannot raise Error_Resync
2268
2269    function P_Unary_Adding_Operator return Node_Kind is
2270       Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2271         (Tok_Minus          => N_Op_Minus,
2272          Tok_Plus           => N_Op_Plus);
2273    begin
2274       return Addop_Node (Token);
2275    end P_Unary_Adding_Operator;
2276
2277    -------------------------------
2278    -- 4.5  Multiplying Operator --
2279    -------------------------------
2280
2281    --  MULTIPLYING_OPERATOR ::= * | / | mod | rem
2282
2283    --  The value returned is the appropriate Node_Kind code for the operator.
2284    --  On return, Token points to the operator token (NOT past it).
2285
2286    --  The caller has checked that the first token is a legitimate multiplying
2287    --  operator token (i.e. is one of the operator tokens listed above).
2288
2289    --  Error recovery: cannot raise Error_Resync
2290
2291    function P_Multiplying_Operator return Node_Kind is
2292       Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2293         (Tok_Asterisk       => N_Op_Multiply,
2294          Tok_Mod            => N_Op_Mod,
2295          Tok_Rem            => N_Op_Rem,
2296          Tok_Slash          => N_Op_Divide);
2297    begin
2298       return Mulop_Node (Token);
2299    end P_Multiplying_Operator;
2300
2301    --------------------------------------
2302    -- 4.5  Highest Precedence Operator --
2303    --------------------------------------
2304
2305    --  Parsed by P_Factor (4.4)
2306
2307    --  Note: this rule is not in fact used by the grammar at any point!
2308
2309    --------------------------
2310    -- 4.6  Type Conversion --
2311    --------------------------
2312
2313    --  Parsed by P_Primary as a Name (4.1)
2314
2315    -------------------------------
2316    -- 4.7  Qualified Expression --
2317    -------------------------------
2318
2319    --  QUALIFIED_EXPRESSION ::=
2320    --    SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2321
2322    --  The caller has scanned the name which is the Subtype_Mark parameter
2323    --  and scanned past the single quote following the subtype mark. The
2324    --  caller has not checked that this name is in fact appropriate for
2325    --  a subtype mark name (i.e. it is a selected component or identifier).
2326
2327    --  Error_Recovery: cannot raise Error_Resync
2328
2329    function  P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2330       Qual_Node : Node_Id;
2331    begin
2332       Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2333       Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2334       Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2335       return Qual_Node;
2336    end P_Qualified_Expression;
2337
2338    --------------------
2339    -- 4.8  Allocator --
2340    --------------------
2341
2342    --  ALLOCATOR ::=
2343    --    new [NULL_EXCLUSION] SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2344
2345    --  The caller has checked that the initial token is NEW
2346
2347    --  Error recovery: can raise Error_Resync
2348
2349    function P_Allocator return Node_Id is
2350       Alloc_Node             : Node_Id;
2351       Type_Node              : Node_Id;
2352       Null_Exclusion_Present : Boolean;
2353
2354    begin
2355       Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2356       T_New;
2357
2358       --  Scan Null_Exclusion if present (Ada 2005 (AI-231))
2359
2360       Null_Exclusion_Present := P_Null_Exclusion;
2361       Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2362       Type_Node := P_Subtype_Mark_Resync;
2363
2364       if Token = Tok_Apostrophe then
2365          Scan; -- past apostrophe
2366          Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2367       else
2368          Set_Expression
2369            (Alloc_Node,
2370             P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2371       end if;
2372
2373       return Alloc_Node;
2374    end P_Allocator;
2375
2376 end Ch4;