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