Debian Codegraph @

gcc/ada/scng.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 with Atree; use Atree;
27 with Csets; use Csets;
28 with Errout; use Errout;
29 with Hostparm; use Hostparm;
30 with Lib; use Lib;
31 with Namet; use Namet;
32 with Opt; use Opt;
33 with Sinput; use Sinput;
34 with Snames; use Snames;
35 with Stringt; use Stringt;
36 with Stylesw; use Stylesw;
37 with Uintp; use Uintp;
38 with Urealp; use Urealp;
39 with Widechar; use Widechar;
40
41 pragma Warnings (Off);
42 -- This package is used also by gnatcoll
43 with System.Case_Util;
44 with System.CRC32;
45 with System.UTF_32; use System.UTF_32;
46 with System.WCh_Con; use System.WCh_Con;
47 pragma Warnings (On);
48
49 package body Scng is
50
51 use ASCII;
52 -- Make control characters visible
53
54 Special_Characters : array (Character) of Boolean := (others => False);
55 -- For characters that are Special token, the value is True
56
57 End_Of_Line_Is_Token : Boolean := False;
58 -- True if End_Of_Line is a token
59
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
63
64 procedure Accumulate_Token_Checksum;
65 pragma Inline (Accumulate_Token_Checksum);
66 -- Called after each numeric literal and identifier/keyword. For keywords,
67 -- the token used is Tok_Identifier. This allows detection of additional
68 -- spaces added in sources when using the builder switch -m.
69
70 procedure Accumulate_Checksum (C : Character);
71 pragma Inline (Accumulate_Checksum);
72 -- This routine accumulates the checksum given character C. During the
73 -- scanning of a source file, this routine is called with every character
74 -- in the source, excluding blanks, and all control characters (except
75 -- that ESC is included in the checksum). Upper case letters not in string
76 -- literals are folded by the caller. See Sinput spec for the documentation
77 -- of the checksum algorithm. Note: checksum values are only used if we
78 -- generate code, so it is not necessary to worry about making the right
79 -- sequence of calls in any error situation.
80
81 procedure Accumulate_Checksum (C : Char_Code);
82 pragma Inline (Accumulate_Checksum);
83 -- This version is identical, except that the argument, C, is a character
84 -- code value instead of a character. This is used when wide characters
85 -- are scanned. We use the character code rather than the ASCII characters
86 -- so that the checksum is independent of wide character encoding method.
87
88 procedure Initialize_Checksum;
89 pragma Inline (Initialize_Checksum);
90 -- Initialize checksum value
91
92 -------------------------
93 -- Accumulate_Checksum --
94 -------------------------
95
96 procedure Accumulate_Checksum (C : Character) is
97 begin
98 System.CRC32.Update (System.CRC32.CRC32 (Checksum), C);
99 end Accumulate_Checksum;
100
101 procedure Accumulate_Checksum (C : Char_Code) is
102 begin
103 if C > 16#FFFF# then
104 Accumulate_Checksum (Character'Val (C / 2 ** 24));
105 Accumulate_Checksum (Character'Val ((C / 2 ** 16) mod 256));
106 Accumulate_Checksum (Character'Val ((C / 256) mod 256));
107 else
108 Accumulate_Checksum (Character'Val (C / 256));
109 end if;
110
111 Accumulate_Checksum (Character'Val (C mod 256));
112 end Accumulate_Checksum;
113
114 -------------------------------
115 -- Accumulate_Token_Checksum --
116 -------------------------------
117
118 procedure Accumulate_Token_Checksum is
119 begin
120 System.CRC32.Update
121 (System.CRC32.CRC32 (Checksum),
122 Character'Val (Token_Type'Pos (Token)));
123 end Accumulate_Token_Checksum;
124
125 -----------------------
126 -- Check_End_Of_Line --
127 -----------------------
128
129 procedure Check_End_Of_Line is
130 Len : constant Int :=
131 Int (Scan_Ptr - Current_Line_Start) - Wide_Char_Byte_Count;
132
133 begin
134 if Style_Check then
135 Style.Check_Line_Terminator (Len);
136 end if;
137
138 -- Deal with checking maximum line length
139
140 if Style_Check and Style_Check_Max_Line_Length then
141 Style.Check_Line_Max_Length (Len);
142
143 -- If style checking is inactive, check maximum line length against
144 -- standard value.
145
146 elsif Len > Max_Line_Length then
147 Error_Msg
148 ("this line is too long",
149 Current_Line_Start + Source_Ptr (Max_Line_Length));
150 end if;
151
152 -- Now one more checking circuit. Normally we are only enforcing a limit
153 -- of physical characters, with tabs counting as one character. But if
154 -- after tab expansion we would have a total line length that exceeded
155 -- 32766, that would really cause trouble, because column positions
156 -- would exceed the maximum we allow for a column count. Note: the limit
157 -- is 32766 rather than 32767, since we use a value of 32767 for special
158 -- purposes (see Sinput). Now we really do not want to go messing with
159 -- tabs in the normal case, so what we do is to check for a line that
160 -- has more than 4096 physical characters. Any shorter line could not
161 -- be a problem, even if it was all tabs.
162
163 if Len >= 4096 then
164 declare
165 Col : Natural;
166 Ptr : Source_Ptr;
167
168 begin
169 Col := 1;
170 Ptr := Current_Line_Start;
171 loop
172 exit when Ptr = Scan_Ptr;
173
174 if Source (Ptr) = ASCII.HT then
175 Col := (Col - 1 + 8) / 8 * 8 + 1;
176 else
177 Col := Col + 1;
178 end if;
179
180 if Col > 32766 then
181 Error_Msg
182 ("this line is longer than 32766 characters",
183 Current_Line_Start);
184 raise Unrecoverable_Error;
185 end if;
186
187 Ptr := Ptr + 1;
188 end loop;
189 end;
190 end if;
191
192 -- Reset wide character byte count for next line
193
194 Wide_Char_Byte_Count := 0;
195 end Check_End_Of_Line;
196
197 ----------------------------
198 -- Determine_Token_Casing --
199 ----------------------------
200
201 function Determine_Token_Casing return Casing_Type is
202 begin
203 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
204 end Determine_Token_Casing;
205
206 -------------------------
207 -- Initialize_Checksum --
208 -------------------------
209
210 procedure Initialize_Checksum is
211 begin
212 System.CRC32.Initialize (System.CRC32.CRC32 (Checksum));
213 end Initialize_Checksum;
214
215 ------------------------
216 -- Initialize_Scanner --
217 ------------------------
218
219 procedure Initialize_Scanner (Index : Source_File_Index) is
220 begin
221 -- Establish reserved words
222
223 Scans.Initialize_Ada_Keywords;
224
225 -- Initialize scan control variables
226
227 Current_Source_File := Index;
228 Source := Source_Text (Current_Source_File);
229 Scan_Ptr := Source_First (Current_Source_File);
230 Token := No_Token;
231 Token_Ptr := Scan_Ptr;
232 Current_Line_Start := Scan_Ptr;
233 Token_Node := Empty;
234 Token_Name := No_Name;
235 Start_Column := Set_Start_Column;
236 First_Non_Blank_Location := Scan_Ptr;
237
238 Initialize_Checksum;
239 Wide_Char_Byte_Count := 0;
240
241 -- Do not call Scan, otherwise the License stuff does not work in Scn
242
243 end Initialize_Scanner;
244
245 ------------------------------
246 -- Reset_Special_Characters --
247 ------------------------------
248
249 procedure Reset_Special_Characters is
250 begin
251 Special_Characters := (others => False);
252 end Reset_Special_Characters;
253
254 ----------
255 -- Scan --
256 ----------
257
258 procedure Scan is
259
260 Underline_Found : Boolean;
261 -- During scanning of an identifier, set to True if last character
262 -- scanned was an underline or other punctuation character. This
263 -- is used to flag the error of two underlines/punctuations in a
264 -- row or ending an identifier with a underline/punctuation. Here
265 -- punctuation means any UTF_32 character in the Unicode category
266 -- Punctuation,Connector.
267
268 Wptr : Source_Ptr;
269 -- Used to remember start of last wide character scanned
270
271 function Double_Char_Token (C : Character) return Boolean;
272 -- This function is used for double character tokens like := or <>. It
273 -- checks if the character following Source (Scan_Ptr) is C, and if so
274 -- bumps Scan_Ptr past the pair of characters and returns True. A space
275 -- between the two characters is also recognized with an appropriate
276 -- error message being issued. If C is not present, False is returned.
277 -- Note that Double_Char_Token can only be used for tokens defined in
278 -- the Ada syntax (it's use for error cases like && is not appropriate
279 -- since we do not want a junk message for a case like &-space-&).
280
281 procedure Error_Illegal_Character;
282 -- Give illegal character error, Scan_Ptr points to character. On
283 -- return, Scan_Ptr is bumped past the illegal character.
284
285 procedure Error_Illegal_Wide_Character;
286 -- Give illegal wide character message. On return, Scan_Ptr is bumped
287 -- past the illegal character, which may still leave us pointing to
288 -- junk, not much we can do if the escape sequence is messed up.
289
290 procedure Error_No_Double_Underline;
291 -- Signal error of two underline or punctuation characters in a row.
292 -- Called with Scan_Ptr pointing to second underline/punctuation char.
293
294 procedure Nlit;
295 -- This is the procedure for scanning out numeric literals. On entry,
296 -- Scan_Ptr points to the digit that starts the numeric literal (the
297 -- checksum for this character has not been accumulated yet). On return
298 -- Scan_Ptr points past the last character of the numeric literal, Token
299 -- and Token_Node are set appropriately, and the checksum is updated.
300
301 procedure Slit;
302 -- This is the procedure for scanning out string literals. On entry,
303 -- Scan_Ptr points to the opening string quote (the checksum for this
304 -- character has not been accumulated yet). On return Scan_Ptr points
305 -- past the closing quote of the string literal, Token and Token_Node
306 -- are set appropriately, and the checksum is updated.
307
308 procedure Skip_Other_Format_Characters;
309 -- Skips past any "other format" category characters at the current
310 -- cursor location (does not skip past spaces or any other characters).
311
312 function Start_Of_Wide_Character return Boolean;
313 -- Returns True if the scan pointer is pointing to the start of a wide
314 -- character sequence, does not modify the scan pointer in any case.
315
316 procedure Check_Bidi (Code : Char_Code);
317 -- Give a warning if Code is a bidirectional character, which can cause
318 -- security vulnerabilities. See the following article:
319 --
320 -- @article{boucher_trojansource_2021,
321 -- title = {Trojan {Source}: {Invisible} {Vulnerabilities}},
322 -- author = {Nicholas Boucher and Ross Anderson},
323 -- year = {2021},
324 -- journal = {Preprint},
325 -- eprint = {2111.00169},
326 -- archivePrefix = {arXiv},
327 -- primaryClass = {cs.CR},
328 -- url = {https://arxiv.org/abs/2111.00169}
329 -- }
330
331 ----------------
332 -- Check_Bidi --
333 ----------------
334
335 type Bidi_Characters is
336 (LRE, RLE, LRO, RLO, LRI, RLI, FSI, PDF, PDI);
337 Bidi_Character_Codes : constant array (Bidi_Characters) of Char_Code :=
338 (LRE => 16#202A#,
339 RLE => 16#202B#,
340 LRO => 16#202D#,
341 RLO => 16#202E#,
342 LRI => 16#2066#,
343 RLI => 16#2067#,
344 FSI => 16#2068#,
345 PDF => 16#202C#,
346 PDI => 16#2069#);
347 -- Above are the bidirectional characters, along with their Unicode code
348 -- points.
349
350 procedure Check_Bidi (Code : Char_Code) is
351 begin
352 for Bidi_Code of Bidi_Character_Codes loop
353 if Code = Bidi_Code then
354 Error_Msg ("??bidirectional wide character", Wptr);
355 end if;
356 end loop;
357 end Check_Bidi;
358
359 -----------------------
360 -- Double_Char_Token --
361 -----------------------
362
363 function Double_Char_Token (C : Character) return Boolean is
364 begin
365 if Source (Scan_Ptr + 1) = C then
366 Accumulate_Checksum (C);
367 Scan_Ptr := Scan_Ptr + 2;
368 return True;
369
370 elsif Source (Scan_Ptr + 1) = ' '
371 and then Source (Scan_Ptr + 2) = C
372 then
373 Scan_Ptr := Scan_Ptr + 1;
374 Error_Msg_S -- CODEFIX
375 ("no space allowed here");
376 Scan_Ptr := Scan_Ptr + 2;
377 return True;
378
379 else
380 return False;
381 end if;
382 end Double_Char_Token;
383
384 -----------------------------
385 -- Error_Illegal_Character --
386 -----------------------------
387
388 procedure Error_Illegal_Character is
389 begin
390 Error_Msg_S ("illegal character");
391 Scan_Ptr := Scan_Ptr + 1;
392 end Error_Illegal_Character;
393
394 ----------------------------------
395 -- Error_Illegal_Wide_Character --
396 ----------------------------------
397
398 procedure Error_Illegal_Wide_Character is
399 begin
400 Scan_Ptr := Scan_Ptr + 1;
401 Error_Msg ("illegal wide character", Wptr);
402 end Error_Illegal_Wide_Character;
403
404 -------------------------------
405 -- Error_No_Double_Underline --
406 -------------------------------
407
408 procedure Error_No_Double_Underline is
409 begin
410 Underline_Found := False;
411
412 -- There are four cases, and we special case the messages
413
414 if Source (Scan_Ptr) = '_' then
415 if Source (Scan_Ptr - 1) = '_' then
416 Error_Msg_S -- CODEFIX
417 ("two consecutive underlines not permitted");
418 else
419 Error_Msg_S ("underline cannot follow punctuation character");
420 end if;
421
422 else
423 if Source (Scan_Ptr - 1) = '_' then
424 Error_Msg_S ("punctuation character cannot follow underline");
425 else
426 Error_Msg_S
427 ("two consecutive punctuation characters not permitted");
428 end if;
429 end if;
430 end Error_No_Double_Underline;
431
432 ----------
433 -- Nlit --
434 ----------
435
436 procedure Nlit is
437
438 C : Character;
439 -- Current source program character
440
441 Base_Char : Character;
442 -- Either # or : (character at start of based number)
443
444 Base : Int;
445 -- Value of base
446
447 UI_Base : Uint;
448 -- Value of base in Uint format
449
450 UI_Int_Value : Uint;
451 -- Value of integer scanned by Scan_Integer in Uint format
452
453 UI_Num_Value : Uint;
454 -- Value of integer in numeric value being scanned
455
456 Scale : Int;
457 -- Scale value for real literal
458
459 UI_Scale : Uint;
460 -- Scale in Uint format
461
462 Exponent_Is_Negative : Boolean;
463 -- Set true for negative exponent
464
465 Extended_Digit_Value : Int;
466 -- Extended digit value
467
468 Point_Scanned : Boolean;
469 -- Flag for decimal point scanned in numeric literal
470
471 -----------------------
472 -- Local Subprograms --
473 -----------------------
474
475 procedure Error_Digit_Expected;
476 -- Signal error of bad digit, Scan_Ptr points to the location at
477 -- which the digit was expected on input, and is unchanged on return.
478
479 procedure Scan_Integer;
480 -- Scan integer literal. On entry, Scan_Ptr points to a digit, on
481 -- exit Scan_Ptr points past the last character of the integer.
482 --
483 -- For each digit encountered, UI_Int_Value is multiplied by 10, and
484 -- the value of the digit added to the result. In addition, the value
485 -- in Scale is decremented by one for each actual digit scanned.
486
487 --------------------------
488 -- Error_Digit_Expected --
489 --------------------------
490
491 procedure Error_Digit_Expected is
492 begin
493 Error_Msg_S ("digit expected");
494 end Error_Digit_Expected;
495
496 ------------------
497 -- Scan_Integer --
498 ------------------
499
500 procedure Scan_Integer is
501 C : Character;
502 -- Next character scanned
503
504 begin
505 C := Source (Scan_Ptr);
506
507 -- Loop through digits (allowing underlines)
508
509 loop
510 Accumulate_Checksum (C);
511 UI_Int_Value :=
512 UI_Int_Value * 10 + (Character'Pos (C) - Character'Pos ('0'));
513 Scan_Ptr := Scan_Ptr + 1;
514 Scale := Scale - 1;
515 C := Source (Scan_Ptr);
516
517 -- Case of underline encountered
518
519 if C = '_' then
520
521 -- We do not accumulate the '_' in the checksum, so that
522 -- 1_234 is equivalent to 1234, and does not trigger
523 -- compilation for "minimal recompilation" (gnatmake -m).
524
525 loop
526 Scan_Ptr := Scan_Ptr + 1;
527 C := Source (Scan_Ptr);
528 exit when C /= '_';
529 Error_No_Double_Underline;
530 end loop;
531
532 if C not in '0' .. '9' then
533 Error_Digit_Expected;
534 exit;
535 end if;
536
537 else
538 exit when C not in '0' .. '9';
539 end if;
540 end loop;
541 end Scan_Integer;
542
543 -- Start of processing for Nlit
544
545 begin
546 Base := 10;
547 UI_Base := Uint_10;
548 UI_Int_Value := Uint_0;
549 Based_Literal_Uses_Colon := False;
550 Scale := 0;
551 Scan_Integer;
552 Point_Scanned := False;
553 UI_Num_Value := UI_Int_Value;
554
555 -- Various possibilities now for continuing the literal are period,
556 -- E/e (for exponent), or :/# (for based literal).
557
558 Scale := 0;
559 C := Source (Scan_Ptr);
560
561 if C = '.' then
562
563 -- Scan out point, but do not scan past .. which is a range
564 -- sequence, and must not be eaten up scanning a numeric literal.
565
566 while C = '.' and then Source (Scan_Ptr + 1) /= '.' loop
567 Accumulate_Checksum ('.');
568
569 if Point_Scanned then
570 Error_Msg_S ("duplicate point ignored");
571 end if;
572
573 Point_Scanned := True;
574 Scan_Ptr := Scan_Ptr + 1;
575 C := Source (Scan_Ptr);
576
577 if C not in '0' .. '9' then
578 Error_Msg
579 ("real literal cannot end with point", Scan_Ptr - 1);
580 else
581 Scan_Integer;
582 UI_Num_Value := UI_Int_Value;
583 end if;
584 end loop;
585
586 -- Based literal case. The base is the value we already scanned.
587 -- In the case of colon, we insist that the following character
588 -- is indeed an extended digit or a period. This catches a number
589 -- of common errors, as well as catching the well known tricky
590 -- bug otherwise arising from "x : integer range 1 .. 10:= 6;"
591
592 elsif C = '#'
593 or else (C = ':' and then
594 (Source (Scan_Ptr + 1) = '.'
595 or else
596 Source (Scan_Ptr + 1) in '0' .. '9'
597 or else
598 Source (Scan_Ptr + 1) in 'A' .. 'Z'
599 or else
600 Source (Scan_Ptr + 1) in 'a' .. 'z'))
601 then
602 Accumulate_Checksum (C);
603 Base_Char := C;
604 UI_Base := UI_Int_Value;
605
606 if Base_Char = ':' then
607 Based_Literal_Uses_Colon := True;
608 end if;
609
610 if UI_Base < 2 or else UI_Base > 16 then
611 Error_Msg_SC ("base not 2-16");
612 UI_Base := Uint_16;
613 end if;
614
615 Base := UI_To_Int (UI_Base);
616 Scan_Ptr := Scan_Ptr + 1;
617
618 -- Scan out extended integer [. integer]
619
620 C := Source (Scan_Ptr);
621 UI_Int_Value := Uint_0;
622 Scale := 0;
623
624 loop
625 if C in '0' .. '9' then
626 Accumulate_Checksum (C);
627 Extended_Digit_Value :=
628 Int'(Character'Pos (C)) - Int'(Character'Pos ('0'));
629
630 elsif C in 'A' .. 'F' then
631 Accumulate_Checksum (Character'Val (Character'Pos (C) + 32));
632 Extended_Digit_Value :=
633 Int'(Character'Pos (C)) - Int'(Character'Pos ('A')) + 10;
634
635 elsif C in 'a' .. 'f' then
636 Accumulate_Checksum (C);
637 Extended_Digit_Value :=
638 Int'(Character'Pos (C)) - Int'(Character'Pos ('a')) + 10;
639
640 else
641 Error_Msg_S ("extended digit expected");
642 exit;
643 end if;
644
645 if Extended_Digit_Value >= Base then
646 Error_Msg_S ("digit '>= base");
647 end if;
648
649 UI_Int_Value := UI_Int_Value * UI_Base + Extended_Digit_Value;
650 Scale := Scale - 1;
651 Scan_Ptr := Scan_Ptr + 1;
652 C := Source (Scan_Ptr);
653
654 if C = '_' then
655 loop
656 Accumulate_Checksum ('_');
657 Scan_Ptr := Scan_Ptr + 1;
658 C := Source (Scan_Ptr);
659 exit when C /= '_';
660 Error_No_Double_Underline;
661 end loop;
662
663 elsif C = '.' then
664 Accumulate_Checksum ('.');
665
666 if Point_Scanned then
667 Error_Msg_S ("duplicate point ignored");
668 end if;
669
670 Scan_Ptr := Scan_Ptr + 1;
671 C := Source (Scan_Ptr);
672 Point_Scanned := True;
673 Scale := 0;
674
675 elsif C = Base_Char then
676 Accumulate_Checksum (C);
677 Scan_Ptr := Scan_Ptr + 1;
678 exit;
679
680 elsif C = '#' or else C = ':' then
681 Error_Msg_S ("based number delimiters must match");
682 Scan_Ptr := Scan_Ptr + 1;
683 exit;
684
685 elsif not Identifier_Char (C) then
686 if Base_Char = '#' then
687 Error_Msg_S -- CODEFIX
688 ("missing '#");
689 else
690 Error_Msg_S -- CODEFIX
691 ("missing ':");
692 end if;
693
694 exit;
695 end if;
696
697 end loop;
698
699 UI_Num_Value := UI_Int_Value;
700 end if;
701
702 -- Scan out exponent
703
704 if not Point_Scanned then
705 Scale := 0;
706 UI_Scale := Uint_0;
707 else
708 UI_Scale := UI_From_Int (Scale);
709 end if;
710
711 if Source (Scan_Ptr) = 'e' or else Source (Scan_Ptr) = 'E' then
712 Accumulate_Checksum ('e');
713 Scan_Ptr := Scan_Ptr + 1;
714 Exponent_Is_Negative := False;
715
716 if Source (Scan_Ptr) = '+' then
717 Accumulate_Checksum ('+');
718 Scan_Ptr := Scan_Ptr + 1;
719
720 elsif Source (Scan_Ptr) = '-' then
721 Accumulate_Checksum ('-');
722
723 if not Point_Scanned then
724 Error_Msg_S
725 ("negative exponent not allowed for integer literal");
726 else
727 Exponent_Is_Negative := True;
728 end if;
729
730 Scan_Ptr := Scan_Ptr + 1;
731 end if;
732
733 UI_Int_Value := Uint_0;
734
735 if Source (Scan_Ptr) in '0' .. '9' then
736 Scan_Integer;
737 else
738 Error_Digit_Expected;
739 end if;
740
741 if Exponent_Is_Negative then
742 UI_Scale := UI_Scale - UI_Int_Value;
743 else
744 UI_Scale := UI_Scale + UI_Int_Value;
745 end if;
746 end if;
747
748 -- Case of real literal to be returned
749
750 if Point_Scanned then
751 Token := Tok_Real_Literal;
752 Real_Literal_Value :=
753 UR_From_Components (
754 Num => UI_Num_Value,
755 Den => -UI_Scale,
756 Rbase => Base);
757
758 -- Case of integer literal to be returned
759
760 else
761 Token := Tok_Integer_Literal;
762
763 if UI_Scale = 0 then
764 Int_Literal_Value := UI_Num_Value;
765
766 -- When the exponent is large, computing is expected to take a
767 -- rather unreasonable time. We generate an error so that it
768 -- does not appear that the compiler has gotten stuck. Such a
769 -- large exponent is most likely a typo anyway.
770
771 elsif UI_Scale >= 800_000 then
772 Error_Msg_SC ("exponent too large");
773 Int_Literal_Value := No_Uint;
774
775 -- Avoid doing possibly expensive calculations in cases like
776 -- parsing 163E800_000# when semantics will not be done anyway.
777 -- This is especially useful when parsing garbled input.
778
779 elsif Operating_Mode /= Check_Syntax
780 and then (Serious_Errors_Detected = 0 or else Try_Semantics)
781 then
782 Int_Literal_Value := UI_Num_Value * UI_Base ** UI_Scale;
783
784 else
785 Int_Literal_Value := No_Uint;
786 end if;
787 end if;
788
789 Accumulate_Token_Checksum;
790 end Nlit;
791
792 ----------
793 -- Slit --
794 ----------
795
796 procedure Slit is
797
798 Delimiter : Character;
799 -- Delimiter (first character of string)
800
801 C : Character;
802 -- Current source program character
803
804 Code : Char_Code;
805 -- Current character code value
806
807 Err : Boolean;
808 -- Error flag for Scan_Wide call
809
810 String_Start : Source_Ptr;
811 -- Point to first character of string
812
813 procedure Error_Bad_String_Char;
814 -- Signal bad character in string/character literal. On entry
815 -- Scan_Ptr points to the improper character encountered during the
816 -- scan. Scan_Ptr is not modified, so it still points to the bad
817 -- character on return.
818
819 procedure Error_Unterminated_String;
820 -- Procedure called if a line terminator character is encountered
821 -- during scanning a string, meaning that the string is not properly
822 -- terminated.
823
824 procedure Set_String;
825 -- Procedure used to distinguish between string and operator symbol.
826 -- On entry the string has been scanned out, and its characters start
827 -- at Token_Ptr and end one character before Scan_Ptr. On exit Token
828 -- is set to Tok_String_Literal/Tok_Operator_Symbol as appropriate,
829 -- and Token_Node is appropriately initialized. In addition, in the
830 -- operator symbol case, Token_Name is appropriately set, and the
831 -- flags [Wide_]Wide_Character_Found are set appropriately.
832
833 ---------------------------
834 -- Error_Bad_String_Char --
835 ---------------------------
836
837 procedure Error_Bad_String_Char is
838 C : constant Character := Source (Scan_Ptr);
839
840 begin
841 if C = HT then
842 Error_Msg_S ("horizontal tab not allowed in string");
843
844 elsif C = VT or else C = FF then
845 Error_Msg_S ("format effector not allowed in string");
846
847 elsif C in Upper_Half_Character then
848 Error_Msg_S ("(Ada 83) upper half character not allowed");
849
850 else
851 Error_Msg_S ("control character not allowed in string");
852 end if;
853 end Error_Bad_String_Char;
854
855 -------------------------------
856 -- Error_Unterminated_String --
857 -------------------------------
858
859 procedure Error_Unterminated_String is
860 S : Source_Ptr;
861
862 begin
863 -- An interesting little refinement. Consider the following
864 -- examples:
865
866 -- A := "this is an unterminated string;
867 -- A := "this is an unterminated string &
868 -- P(A, "this is a parameter that didn't get terminated);
869 -- P("this is a parameter that didn't get terminated, A);
870
871 -- We fiddle a little to do slightly better placement in these
872 -- cases also if there is white space at the end of the line we
873 -- place the flag at the start of this white space, not at the
874 -- end. Note that we only have to test for blanks, since tabs
875 -- aren't allowed in strings in the first place and would have
876 -- caused an error message.
877
878 -- Two more cases that we treat specially are:
879
880 -- A := "this string uses the wrong terminator'
881 -- A := "this string uses the wrong terminator' &
882
883 -- In these cases we give a different error message as well
884
885 -- We actually reposition the scan pointer to the point where we
886 -- place the flag in these cases, since it seems a better bet on
887 -- the original intention.
888
889 while Source (Scan_Ptr - 1) = ' '
890 or else Source (Scan_Ptr - 1) = '&'
891 loop
892 Scan_Ptr := Scan_Ptr - 1;
893 Unstore_String_Char;
894 end loop;
895
896 -- Check for case of incorrect string terminator, but single quote
897 -- is not considered incorrect if the opening terminator misused
898 -- a single quote (error message already given).
899
900 if Delimiter /= '''
901 and then Source (Scan_Ptr - 1) = '''
902 then
903 Unstore_String_Char;
904 Error_Msg
905 ("incorrect string terminator character", Scan_Ptr - 1);
906 return;
907 end if;
908
909 -- Backup over semicolon or right-paren/semicolon sequence
910
911 if Source (Scan_Ptr - 1) = ';' then
912 Scan_Ptr := Scan_Ptr - 1;
913 Unstore_String_Char;
914
915 if Source (Scan_Ptr - 1) = ')' then
916 Scan_Ptr := Scan_Ptr - 1;
917 Unstore_String_Char;
918 end if;
919 end if;
920
921 -- See if there is a comma in the string, if so, guess that
922 -- the first comma terminates the string.
923
924 S := String_Start;
925 while S < Scan_Ptr loop
926 if Source (S) = ',' then
927 while Scan_Ptr > S loop
928 Scan_Ptr := Scan_Ptr - 1;
929 Unstore_String_Char;
930 end loop;
931
932 exit;
933 end if;
934
935 S := S + 1;
936 end loop;
937
938 -- Now we have adjusted the scan pointer, give message
939
940 Error_Msg_S -- CODEFIX
941 ("missing string quote");
942 end Error_Unterminated_String;
943
944 ----------------
945 -- Set_String --
946 ----------------
947
948 procedure Set_String is
949 Slen : constant Int := Int (Scan_Ptr - Token_Ptr - 2);
950 C1 : Character;
951 C2 : Character;
952 C3 : Character;
953
954 begin
955 -- Skip processing operator symbols if we are scanning an
956 -- interpolated string literal.
957
958 if Inside_Interpolated_String_Literal
959 and then not Inside_Interpolated_String_Expression
960 then
961 null;
962
963 -- Token_Name is currently set to Error_Name. The following
964 -- section of code resets Token_Name to the proper Name_Op_xx
965 -- value if the string is a valid operator symbol, otherwise it is
966 -- left set to Error_Name.
967
968 elsif Slen = 1 then
969 C1 := Source (Token_Ptr + 1);
970
971 case C1 is
972 when '=' =>
973 Token_Name := Name_Op_Eq;
974
975 when '>' =>
976 Token_Name := Name_Op_Gt;
977
978 when '<' =>
979 Token_Name := Name_Op_Lt;
980
981 when '+' =>
982 Token_Name := Name_Op_Add;
983
984 when '-' =>
985 Token_Name := Name_Op_Subtract;
986
987 when '&' =>
988 Token_Name := Name_Op_Concat;
989
990 when '*' =>
991 Token_Name := Name_Op_Multiply;
992
993 when '/' =>
994 Token_Name := Name_Op_Divide;
995
996 when others =>
997 null;
998 end case;
999
1000 elsif Slen = 2 then
1001 C1 := Source (Token_Ptr + 1);
1002 C2 := Source (Token_Ptr + 2);
1003
1004 if C1 = '*' and then C2 = '*' then
1005 Token_Name := Name_Op_Expon;
1006
1007 elsif C2 = '=' then
1008
1009 if C1 = '/' then
1010 Token_Name := Name_Op_Ne;
1011 elsif C1 = '<' then
1012 Token_Name := Name_Op_Le;
1013 elsif C1 = '>' then
1014 Token_Name := Name_Op_Ge;
1015 end if;
1016
1017 elsif (C1 = 'O' or else C1 = 'o') and then -- OR
1018 (C2 = 'R' or else C2 = 'r')
1019 then
1020 Token_Name := Name_Op_Or;
1021 end if;
1022
1023 elsif Slen = 3 then
1024 C1 := Source (Token_Ptr + 1);
1025 C2 := Source (Token_Ptr + 2);
1026 C3 := Source (Token_Ptr + 3);
1027
1028 if (C1 = 'A' or else C1 = 'a') and then -- AND
1029 (C2 = 'N' or else C2 = 'n') and then
1030 (C3 = 'D' or else C3 = 'd')
1031 then
1032 Token_Name := Name_Op_And;
1033
1034 elsif (C1 = 'A' or else C1 = 'a') and then -- ABS
1035 (C2 = 'B' or else C2 = 'b') and then
1036 (C3 = 'S' or else C3 = 's')
1037 then
1038 Token_Name := Name_Op_Abs;
1039
1040 elsif (C1 = 'M' or else C1 = 'm') and then -- MOD
1041 (C2 = 'O' or else C2 = 'o') and then
1042 (C3 = 'D' or else C3 = 'd')
1043 then
1044 Token_Name := Name_Op_Mod;
1045
1046 elsif (C1 = 'N' or else C1 = 'n') and then -- NOT
1047 (C2 = 'O' or else C2 = 'o') and then
1048 (C3 = 'T' or else C3 = 't')
1049 then
1050 Token_Name := Name_Op_Not;
1051
1052 elsif (C1 = 'R' or else C1 = 'r') and then -- REM
1053 (C2 = 'E' or else C2 = 'e') and then
1054 (C3 = 'M' or else C3 = 'm')
1055 then
1056 Token_Name := Name_Op_Rem;
1057
1058 elsif (C1 = 'X' or else C1 = 'x') and then -- XOR
1059 (C2 = 'O' or else C2 = 'o') and then
1060 (C3 = 'R' or else C3 = 'r')
1061 then
1062 Token_Name := Name_Op_Xor;
1063 end if;
1064
1065 end if;
1066
1067 -- If it is an operator symbol, then Token_Name is set. If it is
1068 -- some other string value, then Token_Name still contains
1069 -- Error_Name.
1070
1071 if Token_Name = Error_Name then
1072 Token := Tok_String_Literal;
1073
1074 else
1075 Token := Tok_Operator_Symbol;
1076 end if;
1077 end Set_String;
1078
1079 -- Start of processing for Slit
1080
1081 begin
1082 -- On entry, Scan_Ptr points to the opening character of the string
1083 -- which is either a percent, double quote, or apostrophe (single
1084 -- quote). The latter case is an error detected by the character
1085 -- literal circuit.
1086
1087 String_Start := Scan_Ptr;
1088
1089 -- Continuation of interpolated string literal
1090
1091 if Inside_Interpolated_String_Literal
1092 and then Prev_Token = Tok_Right_Curly_Bracket
1093 then
1094 Scan_Ptr := Scan_Ptr - 1;
1095 Delimiter := '"';
1096
1097 -- Common case
1098
1099 else
1100 Delimiter := Source (Scan_Ptr);
1101 Accumulate_Checksum (Delimiter);
1102 end if;
1103
1104 Start_String;
1105 Wide_Character_Found := False;
1106 Wide_Wide_Character_Found := False;
1107 Scan_Ptr := Scan_Ptr + 1;
1108
1109 -- Loop to scan out characters of string literal
1110
1111 loop
1112 C := Source (Scan_Ptr);
1113
1114 if C = Delimiter then
1115 Accumulate_Checksum (C);
1116 Scan_Ptr := Scan_Ptr + 1;
1117 exit when Source (Scan_Ptr) /= Delimiter;
1118
1119 -- Unlike normal string literals, doubled delimiter has no
1120 -- special significance in interpolated string literals.
1121
1122 if Inside_Interpolated_String_Literal then
1123 Error_Msg_S
1124 ("double quotations not allowed in interpolated string");
1125 end if;
1126
1127 Code := Get_Char_Code (C);
1128 Accumulate_Checksum (C);
1129 Scan_Ptr := Scan_Ptr + 1;
1130
1131 else
1132 if C = '"' and then Delimiter = '%' then
1133 Error_Msg_S
1134 ("quote not allowed in percent delimited string");
1135 Code := Get_Char_Code (C);
1136 Scan_Ptr := Scan_Ptr + 1;
1137
1138 -- Found interpolated expression
1139
1140 elsif Inside_Interpolated_String_Literal
1141 and then C = '{'
1142 then
1143 Accumulate_Checksum (C);
1144 exit;
1145
1146 -- Escaped character in interpolated string literal
1147
1148 elsif Inside_Interpolated_String_Literal
1149 and then C = '\'
1150 then
1151 Accumulate_Checksum (C);
1152 Scan_Ptr := Scan_Ptr + 1;
1153 C := Source (Scan_Ptr);
1154 Accumulate_Checksum (C);
1155 Scan_Ptr := Scan_Ptr + 1;
1156
1157 case C is
1158 when 'a' => Code := Get_Char_Code (ASCII.BEL);
1159 when 'b' => Code := Get_Char_Code (ASCII.BS);
1160 when 'f' => Code := Get_Char_Code (ASCII.FF);
1161 when 'n' => Code := Get_Char_Code (ASCII.LF);
1162 when 'r' => Code := Get_Char_Code (ASCII.CR);
1163 when 't' => Code := Get_Char_Code (ASCII.HT);
1164 when 'v' => Code := Get_Char_Code (ASCII.VT);
1165 when '0' => Code := Get_Char_Code (ASCII.NUL);
1166 when '\' | '"' | '{' | '}'
1167 => Code := Get_Char_Code (C);
1168 when others =>
1169 Code := Get_Char_Code ('?');
1170 Error_Msg_S ("illegal escaped character");
1171 end case;
1172
1173 elsif Start_Of_Wide_Character then
1174 Wptr := Scan_Ptr;
1175 Scan_Wide (Source, Scan_Ptr, Code, Err);
1176
1177 if Err then
1178 Error_Illegal_Wide_Character;
1179 Code := Get_Char_Code (' ');
1180 else
1181 Check_Bidi (Code);
1182 end if;
1183
1184 Accumulate_Checksum (Code);
1185
1186 -- In Ada 95 mode we allow any wide characters in a string
1187 -- but in Ada 2005, the set of characters allowed has been
1188 -- restricted to graphic characters.
1189
1190 if Ada_Version >= Ada_2005
1191 and then Is_UTF_32_Non_Graphic (UTF_32 (Code))
1192 then
1193 Error_Msg
1194 ("(Ada 2005) non-graphic character not permitted " &
1195 "in string literal", Wptr);
1196 end if;
1197
1198 else
1199 Accumulate_Checksum (C);
1200
1201 if C not in Graphic_Character then
1202 if C in Line_Terminator then
1203 Error_Unterminated_String;
1204 exit;
1205
1206 elsif C in Upper_Half_Character then
1207 if Ada_Version = Ada_83 then
1208 Error_Bad_String_Char;
1209 end if;
1210
1211 else
1212 Error_Bad_String_Char;
1213 end if;
1214 end if;
1215
1216 Code := Get_Char_Code (C);
1217 Scan_Ptr := Scan_Ptr + 1;
1218 end if;
1219 end if;
1220
1221 Store_String_Char (Code);
1222
1223 if not In_Character_Range (Code) then
1224 if In_Wide_Character_Range (Code) then
1225 Wide_Character_Found := True;
1226 else
1227 Wide_Wide_Character_Found := True;
1228 end if;
1229 end if;
1230 end loop;
1231
1232 String_Literal_Id := End_String;
1233 Set_String;
1234 return;
1235 end Slit;
1236
1237 ----------------------------------
1238 -- Skip_Other_Format_Characters --
1239 ----------------------------------
1240
1241 procedure Skip_Other_Format_Characters is
1242 P : Source_Ptr;
1243 Code : Char_Code;
1244 Err : Boolean;
1245
1246 begin
1247 while Start_Of_Wide_Character loop
1248 P := Scan_Ptr;
1249 Scan_Wide (Source, Scan_Ptr, Code, Err);
1250
1251 if not Is_UTF_32_Other (UTF_32 (Code)) then
1252 Scan_Ptr := P;
1253 return;
1254 end if;
1255 end loop;
1256 end Skip_Other_Format_Characters;
1257
1258 -----------------------------
1259 -- Start_Of_Wide_Character --
1260 -----------------------------
1261
1262 function Start_Of_Wide_Character return Boolean is
1263 C : constant Character := Source (Scan_Ptr);
1264
1265 begin
1266 -- ESC encoding method with ESC present
1267
1268 if C = ESC
1269 and then Wide_Character_Encoding_Method in WC_ESC_Encoding_Method
1270 then
1271 return True;
1272
1273 -- Upper half character with upper half encoding
1274
1275 elsif C in Upper_Half_Character and then Upper_Half_Encoding then
1276 return True;
1277
1278 -- Brackets encoding
1279
1280 elsif C = '['
1281 and then Source (Scan_Ptr + 1) = '"'
1282 and then Identifier_Char (Source (Scan_Ptr + 2))
1283 then
1284 return True;
1285
1286 -- Not the start of a wide character
1287
1288 else
1289 return False;
1290 end if;
1291 end Start_Of_Wide_Character;
1292
1293 Token_Contains_Uppercase : Boolean;
1294
1295 -- Start of processing for Scan
1296
1297 begin
1298 Prev_Token := Token;
1299 Prev_Token_Ptr := Token_Ptr;
1300 Token_Name := Error_Name;
1301
1302 if Inside_Interpolated_String_Literal
1303 and then Prev_Token = Tok_Right_Curly_Bracket
1304 then
1305 -- Consecutive interpolated expressions
1306
1307 if Source (Scan_Ptr) = '{' then
1308 null;
1309
1310 -- Ending delimiter placed immediately after interpolated expression
1311
1312 elsif Source (Scan_Ptr) = '"' then
1313 Scan_Ptr := Scan_Ptr + 1;
1314 Prev_Token := Tok_String_Literal;
1315
1316 -- String literal placed after interpolated expression
1317
1318 else
1319 Slit;
1320 Post_Scan;
1321 return;
1322 end if;
1323 end if;
1324
1325 -- The following loop runs more than once only if a format effector
1326 -- (tab, vertical tab, form feed, line feed, carriage return) is
1327 -- encountered and skipped, or some error situation, such as an
1328 -- illegal character, is encountered.
1329
1330 <<Scan_Next_Character>>
1331
1332 loop
1333 -- Skip past blanks, loop is opened up for speed
1334
1335 while Source (Scan_Ptr) = ' ' loop
1336 if Source (Scan_Ptr + 1) /= ' ' then
1337 Scan_Ptr := Scan_Ptr + 1;
1338 exit;
1339 end if;
1340
1341 if Source (Scan_Ptr + 2) /= ' ' then
1342 Scan_Ptr := Scan_Ptr + 2;
1343 exit;
1344 end if;
1345
1346 if Source (Scan_Ptr + 3) /= ' ' then
1347 Scan_Ptr := Scan_Ptr + 3;
1348 exit;
1349 end if;
1350
1351 if Source (Scan_Ptr + 4) /= ' ' then
1352 Scan_Ptr := Scan_Ptr + 4;
1353 exit;
1354 end if;
1355
1356 if Source (Scan_Ptr + 5) /= ' ' then
1357 Scan_Ptr := Scan_Ptr + 5;
1358 exit;
1359 end if;
1360
1361 if Source (Scan_Ptr + 6) /= ' ' then
1362 Scan_Ptr := Scan_Ptr + 6;
1363 exit;
1364 end if;
1365
1366 if Source (Scan_Ptr + 7) /= ' ' then
1367 Scan_Ptr := Scan_Ptr + 7;
1368 exit;
1369 end if;
1370
1371 Scan_Ptr := Scan_Ptr + 8;
1372 end loop;
1373
1374 -- We are now at a non-blank character, which is the first character
1375 -- of the token we will scan, and hence the value of Token_Ptr.
1376
1377 Token_Ptr := Scan_Ptr;
1378
1379 Token_Contains_Uppercase := False;
1380
1381 -- Here begins the main case statement which transfers control on the
1382 -- basis of the non-blank character we have encountered.
1383
1384 case Source (Scan_Ptr) is
1385
1386 -- Line terminator characters
1387
1388 when CR | LF | FF | VT =>
1389 goto Scan_Line_Terminator;
1390
1391 -- Horizontal tab, just skip past it
1392
1393 when HT =>
1394 if Style_Check then
1395 Style.Check_HT;
1396 end if;
1397
1398 Scan_Ptr := Scan_Ptr + 1;
1399
1400 -- End of file character, treated as an end of file only if it is
1401 -- the last character in the buffer, otherwise it is ignored.
1402
1403 when EOF =>
1404 if Scan_Ptr = Source_Last (Current_Source_File) then
1405 Check_End_Of_Line;
1406
1407 if Style_Check then
1408 Style.Check_EOF;
1409 end if;
1410
1411 Token := Tok_EOF;
1412 return;
1413 else
1414 Scan_Ptr := Scan_Ptr + 1;
1415 end if;
1416
1417 -- Ampersand
1418
1419 when '&' =>
1420 Accumulate_Checksum ('&');
1421
1422 if Source (Scan_Ptr + 1) = '&' then
1423 Error_Msg_S -- CODEFIX
1424 ("'&'& should be `AND THEN`");
1425 Scan_Ptr := Scan_Ptr + 2;
1426 Token := Tok_And;
1427 return;
1428
1429 else
1430 Scan_Ptr := Scan_Ptr + 1;
1431 Token := Tok_Ampersand;
1432 return;
1433 end if;
1434
1435 -- AI12-0125-03 : @ is target_name
1436
1437 when '@' =>
1438 Error_Msg_Ada_2022_Feature ("target name", Token_Ptr);
1439
1440 Accumulate_Checksum ('@');
1441 Scan_Ptr := Scan_Ptr + 1;
1442 Token := Tok_At_Sign;
1443 return;
1444
1445 -- Asterisk (can be multiplication operator or double asterisk which
1446 -- is the exponentiation compound delimiter).
1447
1448 when '*' =>
1449 Accumulate_Checksum ('*');
1450
1451 if Source (Scan_Ptr + 1) = '*' then
1452 Accumulate_Checksum ('*');
1453 Scan_Ptr := Scan_Ptr + 2;
1454 Token := Tok_Double_Asterisk;
1455 return;
1456
1457 else
1458 Scan_Ptr := Scan_Ptr + 1;
1459 Token := Tok_Asterisk;
1460 return;
1461 end if;
1462
1463 -- Colon, which can either be an isolated colon, or part of an
1464 -- assignment compound delimiter.
1465
1466 when ':' =>
1467 Accumulate_Checksum (':');
1468
1469 if Double_Char_Token ('=') then
1470 Token := Tok_Colon_Equal;
1471
1472 if Style_Check then
1473 Style.Check_Colon_Equal;
1474 end if;
1475
1476 return;
1477
1478 elsif Source (Scan_Ptr + 1) = '-'
1479 and then Source (Scan_Ptr + 2) /= '-'
1480 then
1481 Token := Tok_Colon_Equal;
1482 Error_Msg -- CODEFIX
1483 (":- should be :=", Scan_Ptr);
1484 Scan_Ptr := Scan_Ptr + 2;
1485 return;
1486
1487 else
1488 Scan_Ptr := Scan_Ptr + 1;
1489 Token := Tok_Colon;
1490
1491 if Style_Check then
1492 Style.Check_Colon;
1493 end if;
1494
1495 return;
1496 end if;
1497
1498 -- Left parenthesis
1499
1500 when '(' =>
1501 Accumulate_Checksum ('(');
1502 Scan_Ptr := Scan_Ptr + 1;
1503 Token := Tok_Left_Paren;
1504
1505 if Style_Check then
1506 Style.Check_Left_Paren_Square_Bracket;
1507 end if;
1508
1509 return;
1510
1511 -- Left bracket
1512
1513 when '[' =>
1514
1515 -- [] under -gnat2022 is an aggregate notation and the special
1516 -- wide character notation becomes unsupported since the two
1517 -- are ambiguous.
1518
1519 if Ada_Version >= Ada_2022 then
1520 Scan_Ptr := Scan_Ptr + 1;
1521 Token := Tok_Left_Bracket;
1522
1523 if Style_Check then
1524 Style.Check_Left_Paren_Square_Bracket;
1525 end if;
1526
1527 return;
1528
1529 elsif Source (Scan_Ptr + 1) = '"' then
1530 goto Scan_Wide_Character;
1531
1532 else
1533 Error_Msg_S ("illegal character, replaced by ""(""");
1534 Scan_Ptr := Scan_Ptr + 1;
1535 Token := Tok_Left_Paren;
1536 return;
1537 end if;
1538
1539 -- Left curly bracket, treated as right paren but proper delimiter
1540 -- of interpolated string literals when core extensions are allowed.
1541
1542 when '{' =>
1543 if Core_Extensions_Allowed then
1544 Scan_Ptr := Scan_Ptr + 1;
1545 Token := Tok_Left_Curly_Bracket;
1546
1547 else
1548 Error_Msg_S ("illegal character, replaced by ""(""");
1549 Scan_Ptr := Scan_Ptr + 1;
1550 Token := Tok_Left_Paren;
1551 end if;
1552
1553 return;
1554
1555 -- Comma
1556
1557 when ',' =>
1558 Accumulate_Checksum (',');
1559 Scan_Ptr := Scan_Ptr + 1;
1560 Token := Tok_Comma;
1561
1562 if Style_Check then
1563 Style.Check_Comma;
1564 end if;
1565
1566 return;
1567
1568 -- Dot, which is either an isolated period, or part of a double dot
1569 -- compound delimiter sequence. We also check for the case of a
1570 -- digit following the period, to give a better error message.
1571
1572 when '.' =>
1573 Accumulate_Checksum ('.');
1574
1575 if Double_Char_Token ('.') then
1576 Token := Tok_Dot_Dot;
1577
1578 if Style_Check then
1579 Style.Check_Dot_Dot;
1580 end if;
1581
1582 return;
1583
1584 elsif Source (Scan_Ptr + 1) in '0' .. '9' then
1585 Error_Msg_S ("numeric literal cannot start with point");
1586 Scan_Ptr := Scan_Ptr + 1;
1587
1588 else
1589 Scan_Ptr := Scan_Ptr + 1;
1590 Token := Tok_Dot;
1591 return;
1592 end if;
1593
1594 -- Equal, which can either be an equality operator, or part of the
1595 -- arrow (=>) compound delimiter.
1596
1597 when '=' =>
1598 Accumulate_Checksum ('=');
1599
1600 if Double_Char_Token ('>') then
1601 Token := Tok_Arrow;
1602
1603 if Style_Check then
1604 Style.Check_Arrow (Inside_Depends);
1605 end if;
1606
1607 return;
1608
1609 elsif Source (Scan_Ptr + 1) = '=' then
1610 Error_Msg_S -- CODEFIX
1611 ("== should be =");
1612 Scan_Ptr := Scan_Ptr + 1;
1613 end if;
1614
1615 Scan_Ptr := Scan_Ptr + 1;
1616 Token := Tok_Equal;
1617 return;
1618
1619 -- Greater than, which can be a greater than operator, greater than
1620 -- or equal operator, or first character of a right label bracket.
1621
1622 when '>' =>
1623 Accumulate_Checksum ('>');
1624
1625 if Double_Char_Token ('=') then
1626 Token := Tok_Greater_Equal;
1627 return;
1628
1629 elsif Double_Char_Token ('>') then
1630 Token := Tok_Greater_Greater;
1631 return;
1632
1633 else
1634 Scan_Ptr := Scan_Ptr + 1;
1635 Token := Tok_Greater;
1636 return;
1637 end if;
1638
1639 -- Less than, which can be a less than operator, less than or equal
1640 -- operator, or the first character of a left label bracket, or the
1641 -- first character of a box (<>) compound delimiter.
1642
1643 when '<' =>
1644 Accumulate_Checksum ('<');
1645
1646 if Double_Char_Token ('=') then
1647 Token := Tok_Less_Equal;
1648 return;
1649
1650 elsif Double_Char_Token ('>') then
1651 Token := Tok_Box;
1652
1653 if Style_Check then
1654 Style.Check_Box;
1655 end if;
1656
1657 return;
1658
1659 elsif Double_Char_Token ('<') then
1660 Token := Tok_Less_Less;
1661 return;
1662
1663 else
1664 Scan_Ptr := Scan_Ptr + 1;
1665 Token := Tok_Less;
1666 return;
1667 end if;
1668
1669 -- Minus, which is either a subtraction operator, or the first
1670 -- character of double minus starting a comment
1671
1672 when '-' => Minus_Case : begin
1673 if Source (Scan_Ptr + 1) = '>' then
1674 Error_Msg_S ("invalid token");
1675 Scan_Ptr := Scan_Ptr + 2;
1676 Token := Tok_Arrow;
1677 return;
1678
1679 elsif Source (Scan_Ptr + 1) /= '-' then
1680 Accumulate_Checksum ('-');
1681 Scan_Ptr := Scan_Ptr + 1;
1682 Token := Tok_Minus;
1683 return;
1684
1685 -- Comment
1686
1687 else -- Source (Scan_Ptr + 1) = '-' then
1688 if Style_Check then
1689 Style.Check_Comment;
1690 end if;
1691
1692 Scan_Ptr := Scan_Ptr + 2;
1693
1694 -- If we are in preprocessor mode with Replace_In_Comments set,
1695 -- then we return the "--" as a token on its own.
1696
1697 if Replace_In_Comments then
1698 Token := Tok_Comment;
1699 return;
1700 end if;
1701
1702 -- Loop to scan comment (this loop runs more than once only if
1703 -- a horizontal tab or other non-graphic character is scanned)
1704
1705 loop
1706 -- Scan to non graphic character (opened up for speed)
1707
1708 -- Note that we just eat left brackets, which means that
1709 -- bracket notation cannot be used for end of line
1710 -- characters in comments. This seems a reasonable choice,
1711 -- since no one would ever use brackets notation in a real
1712 -- program in this situation, and if we allow brackets
1713 -- notation, we forbid some valid comments which contain a
1714 -- brackets sequence that happens to match an end of line
1715 -- character.
1716
1717 loop
1718 exit when Source (Scan_Ptr) not in Graphic_Character;
1719 Scan_Ptr := Scan_Ptr + 1;
1720 exit when Source (Scan_Ptr) not in Graphic_Character;
1721 Scan_Ptr := Scan_Ptr + 1;
1722 exit when Source (Scan_Ptr) not in Graphic_Character;
1723 Scan_Ptr := Scan_Ptr + 1;
1724 exit when Source (Scan_Ptr) not in Graphic_Character;
1725 Scan_Ptr := Scan_Ptr + 1;
1726 exit when Source (Scan_Ptr) not in Graphic_Character;
1727 Scan_Ptr := Scan_Ptr + 1;
1728 end loop;
1729
1730 -- Keep going if horizontal tab
1731
1732 if Source (Scan_Ptr) = HT then
1733 if Style_Check then
1734 Style.Check_HT;
1735 end if;
1736
1737 Scan_Ptr := Scan_Ptr + 1;
1738
1739 -- Terminate scan of comment if line terminator
1740
1741 elsif Source (Scan_Ptr) in Line_Terminator then
1742 exit;
1743
1744 -- Terminate scan of comment if end of file encountered
1745 -- (embedded EOF character or real last character in file)
1746
1747 elsif Source (Scan_Ptr) = EOF then
1748 exit;
1749
1750 -- If we have a wide character, we have to scan it out,
1751 -- because it might be a legitimate line terminator
1752
1753 elsif Start_Of_Wide_Character then
1754 declare
1755 Code : Char_Code;
1756 Err : Boolean;
1757
1758 begin
1759 Wptr := Scan_Ptr;
1760 Scan_Wide (Source, Scan_Ptr, Code, Err);
1761
1762 -- If not well formed wide character, then just skip
1763 -- past it and ignore it.
1764
1765 if Err then
1766 Scan_Ptr := Wptr + 1;
1767
1768 -- If UTF_32 terminator, terminate comment scan
1769
1770 elsif Is_UTF_32_Line_Terminator (UTF_32 (Code)) then
1771 Scan_Ptr := Wptr;
1772 exit;
1773 else
1774 Check_Bidi (Code);
1775 end if;
1776 end;
1777
1778 -- Keep going if character in 80-FF range, or is ESC. These
1779 -- characters are allowed in comments by RM-2.1(1), 2.7(2).
1780 -- They are allowed even in Ada 83 mode according to the
1781 -- approved AI. ESC was added to the AI in June 93.
1782
1783 elsif Source (Scan_Ptr) in Upper_Half_Character
1784 or else Source (Scan_Ptr) = ESC
1785 then
1786 Scan_Ptr := Scan_Ptr + 1;
1787
1788 -- Otherwise we have an illegal comment character, ignore
1789 -- this error in relaxed semantics mode.
1790
1791 else
1792 if Relaxed_RM_Semantics then
1793 Scan_Ptr := Scan_Ptr + 1;
1794 else
1795 Error_Illegal_Character;
1796 end if;
1797 end if;
1798 end loop;
1799
1800 -- Note that we do not return here; instead we fall through to
1801 -- reexecute the scan loop to look for a token.
1802 end if;
1803 end Minus_Case;
1804
1805 -- Double quote or percent starting a string literal
1806
1807 when '"' | '%' =>
1808 Slit;
1809 Post_Scan;
1810 return;
1811
1812 -- Apostrophe. This can either be the start of a character literal,
1813 -- or an isolated apostrophe used in a qualified expression or an
1814 -- attribute. In the following:
1815
1816 -- A := CHARACTER'('A');
1817
1818 -- the first apostrophe is treated as an isolated apostrophe, and the
1819 -- second one is treated as the start of the character literal 'A'.
1820 -- Note that RM-2.2(7) does not require a separator between "'" and
1821 -- "(" in the above, so we cannot use lookahead to distinguish the
1822 -- cases; we use look-back instead. Analysis of the grammar shows
1823 -- that some tokens can be followed by an apostrophe, and some by a
1824 -- character literal, but none by both. Some cannot be followed by
1825 -- either, so it doesn't matter what we do in those cases, except to
1826 -- get good error behavior.
1827
1828 when ''' => Char_Literal_Case : declare
1829 Code : Char_Code;
1830 Err : Boolean;
1831
1832 begin
1833 Accumulate_Checksum (''');
1834 Scan_Ptr := Scan_Ptr + 1;
1835
1836 -- Distinguish between apostrophe and character literal. It's an
1837 -- apostrophe if the previous token is one of the following.
1838 -- Reserved words are included for things like A.all'Address and
1839 -- T'Digits'Img. Strings literals are included for things like
1840 -- "abs"'Address. Other literals are included to give better error
1841 -- behavior for illegal cases like 123'Img.
1842 --
1843 -- In Ada 2022, a target name (i.e. @) is a valid prefix of an
1844 -- attribute, and functions like a name.
1845
1846 if Prev_Token in Tok_All | Tok_At_Sign | Tok_Delta | Tok_Digits |
1847 Tok_Identifier | Tok_Project | Tok_Right_Paren |
1848 Tok_Right_Bracket | Token_Class_Literal
1849 then
1850 Token := Tok_Apostrophe;
1851
1852 if Style_Check then
1853 Style.Check_Apostrophe;
1854 end if;
1855
1856 return;
1857
1858 -- Otherwise the apostrophe starts a character literal
1859
1860 else
1861 -- Case of wide character literal
1862
1863 if Start_Of_Wide_Character then
1864 Wptr := Scan_Ptr;
1865 Scan_Wide (Source, Scan_Ptr, Code, Err);
1866
1867 if Err then
1868 Error_Illegal_Wide_Character;
1869 Code := Character'Pos (' ');
1870
1871 -- In Ada 95 mode we allow any wide character in a character
1872 -- literal, but in later versions, the set of characters
1873 -- allowed is restricted to graphic characters.
1874
1875 elsif Ada_Version >= Ada_2005
1876 and then Is_UTF_32_Non_Graphic (UTF_32 (Code))
1877 then
1878 Error_Msg -- CODEFIX
1879 ("(Ada 2005) non-graphic character not permitted " &
1880 "in character literal", Wptr);
1881 else
1882 Check_Bidi (Code);
1883 end if;
1884
1885 Accumulate_Checksum (Code);
1886
1887 if Source (Scan_Ptr) /= ''' then
1888 Error_Msg_S ("missing apostrophe");
1889 else
1890 Scan_Ptr := Scan_Ptr + 1;
1891 end if;
1892
1893 -- If we do not find a closing quote in the expected place then
1894 -- assume that we have a misguided attempt at a string literal.
1895
1896 -- However, if previous token is RANGE, then we return an
1897 -- apostrophe instead since this gives better error recovery
1898
1899 elsif Source (Scan_Ptr + 1) /= ''' then
1900 if Prev_Token = Tok_Range then
1901 Token := Tok_Apostrophe;
1902 return;
1903
1904 else
1905 Scan_Ptr := Scan_Ptr - 1;
1906 Error_Msg_S
1907 ("strings are delimited by double quote character");
1908 Slit;
1909 Post_Scan;
1910 return;
1911 end if;
1912
1913 -- Otherwise we have a (non-wide) character literal
1914
1915 else
1916 Accumulate_Checksum (Source (Scan_Ptr));
1917
1918 if Source (Scan_Ptr) not in Graphic_Character then
1919 if Source (Scan_Ptr) in Upper_Half_Character then
1920 if Ada_Version = Ada_83 then
1921 Error_Illegal_Character;
1922 end if;
1923
1924 else
1925 Error_Illegal_Character;
1926 end if;
1927 end if;
1928
1929 Code := Get_Char_Code (Source (Scan_Ptr));
1930 Scan_Ptr := Scan_Ptr + 2;
1931 end if;
1932
1933 -- Fall through here with Scan_Ptr updated past the closing
1934 -- quote, and Code set to the Char_Code value for the literal
1935
1936 Accumulate_Checksum (''');
1937 Token := Tok_Char_Literal;
1938 Set_Character_Literal_Name (Code);
1939 Token_Name := Name_Find;
1940 Character_Code := Code;
1941 Post_Scan;
1942 return;
1943 end if;
1944 end Char_Literal_Case;
1945
1946 -- Right parenthesis
1947
1948 when ')' =>
1949 Accumulate_Checksum (')');
1950 Scan_Ptr := Scan_Ptr + 1;
1951 Token := Tok_Right_Paren;
1952
1953 if Style_Check then
1954 Style.Check_Right_Paren;
1955 end if;
1956
1957 return;
1958
1959 -- Right bracket or right brace, treated as right paren but proper
1960 -- aggregate delimiter in Ada 2022.
1961
1962 when ']' =>
1963 if Ada_Version >= Ada_2022 then
1964 Token := Tok_Right_Bracket;
1965
1966 else
1967 Error_Msg_S ("illegal character, replaced by "")""");
1968 Token := Tok_Right_Paren;
1969 end if;
1970
1971 Scan_Ptr := Scan_Ptr + 1;
1972 return;
1973
1974 -- Right curly bracket, treated as right paren but proper delimiter
1975 -- of interpolated string literals when core extensions are allowed.
1976
1977 when '}' =>
1978 if Core_Extensions_Allowed then
1979 Token := Tok_Right_Curly_Bracket;
1980
1981 else
1982 Error_Msg_S ("illegal character, replaced by "")""");
1983 Token := Tok_Right_Paren;
1984 end if;
1985
1986 Scan_Ptr := Scan_Ptr + 1;
1987 return;
1988
1989 -- Slash (can be division operator or first character of not equal)
1990
1991 when '/' =>
1992 Accumulate_Checksum ('/');
1993
1994 if Double_Char_Token ('=') then
1995 Token := Tok_Not_Equal;
1996 return;
1997 else
1998 Scan_Ptr := Scan_Ptr + 1;
1999 Token := Tok_Slash;
2000 return;
2001 end if;
2002
2003 -- Semicolon
2004
2005 when ';' =>
2006 Accumulate_Checksum (';');
2007 Scan_Ptr := Scan_Ptr + 1;
2008 Token := Tok_Semicolon;
2009
2010 if Style_Check then
2011 Style.Check_Semicolon;
2012 end if;
2013
2014 return;
2015
2016 -- Vertical bar
2017
2018 when '|' => Vertical_Bar_Case : begin
2019 Accumulate_Checksum ('|');
2020
2021 -- Special check for || to give nice message
2022
2023 if Source (Scan_Ptr + 1) = '|' then
2024 Error_Msg_S -- CODEFIX
2025 ("""'|'|"" should be `OR ELSE`");
2026 Scan_Ptr := Scan_Ptr + 2;
2027 Token := Tok_Or;
2028 return;
2029
2030 else
2031 Scan_Ptr := Scan_Ptr + 1;
2032 Token := Tok_Vertical_Bar;
2033
2034 if Style_Check then
2035 Style.Check_Vertical_Bar;
2036 end if;
2037
2038 Post_Scan;
2039 return;
2040 end if;
2041 end Vertical_Bar_Case;
2042
2043 -- Exclamation, replacement character for vertical bar
2044
2045 when '!' => Exclamation_Case : begin
2046 Accumulate_Checksum ('!');
2047
2048 if Source (Scan_Ptr + 1) = '=' then
2049 Error_Msg_S -- CODEFIX
2050 ("'!= should be /=");
2051 Scan_Ptr := Scan_Ptr + 2;
2052 Token := Tok_Not_Equal;
2053 return;
2054
2055 else
2056 Scan_Ptr := Scan_Ptr + 1;
2057 Token := Tok_Vertical_Bar;
2058 Post_Scan;
2059 return;
2060 end if;
2061 end Exclamation_Case;
2062
2063 -- Plus
2064
2065 when '+' => Plus_Case : begin
2066 Accumulate_Checksum ('+');
2067 Scan_Ptr := Scan_Ptr + 1;
2068 Token := Tok_Plus;
2069 return;
2070 end Plus_Case;
2071
2072 -- Digits starting a numeric literal
2073
2074 when '0' .. '9' =>
2075
2076 -- First a bit of a scan ahead to see if we have a case of an
2077 -- identifier starting with a digit (remembering exponent case).
2078
2079 declare
2080 C : constant Character := Source (Scan_Ptr + 1);
2081
2082 begin
2083 -- OK literal if digit followed by digit or underscore
2084
2085 if C in '0' .. '9' or else C = '_' then
2086 null;
2087
2088 -- OK literal if digit not followed by identifier char
2089
2090 elsif not Identifier_Char (C) then
2091 null;
2092
2093 -- OK literal if digit followed by e/E followed by digit/sign.
2094 -- We also allow underscore after the E, which is an error, but
2095 -- better handled by Nlit than deciding this is an identifier.
2096
2097 elsif (C = 'e' or else C = 'E')
2098 and then (Source (Scan_Ptr + 2) in '0' .. '9'
2099 or else Source (Scan_Ptr + 2) = '+'
2100 or else Source (Scan_Ptr + 2) = '-'
2101 or else Source (Scan_Ptr + 2) = '_')
2102 then
2103 null;
2104
2105 -- Here we have what really looks like an identifier that
2106 -- starts with a digit, so give error msg.
2107
2108 else
2109 Error_Msg_S ("identifier may not start with digit");
2110 Name_Len := 1;
2111 Underline_Found := False;
2112 Name_Buffer (1) := Source (Scan_Ptr);
2113 Accumulate_Checksum (Name_Buffer (1));
2114 Scan_Ptr := Scan_Ptr + 1;
2115 goto Scan_Identifier;
2116 end if;
2117 end;
2118
2119 -- Here we have an OK integer literal
2120
2121 Nlit;
2122
2123 -- Check for proper delimiter, ignoring other format characters
2124
2125 Skip_Other_Format_Characters;
2126
2127 if Identifier_Char (Source (Scan_Ptr)) then
2128 Error_Msg_S
2129 ("delimiter required between literal and identifier");
2130 end if;
2131
2132 Post_Scan;
2133 return;
2134
2135 -- Lower case letters
2136
2137 when 'a' .. 'z' =>
2138 if Source (Scan_Ptr) = 'f'
2139 and then Source (Scan_Ptr + 1) = '"'
2140 then
2141 if Core_Extensions_Allowed then
2142 Scan_Ptr := Scan_Ptr + 1;
2143 Accumulate_Checksum (Source (Scan_Ptr));
2144 Token := Tok_Left_Interpolated_String;
2145 return;
2146 else
2147 Error_Msg_GNAT_Extension
2148 ("interpolated string", Scan_Ptr,
2149 Is_Core_Extension => True);
2150 end if;
2151 end if;
2152
2153 Name_Len := 1;
2154 Underline_Found := False;
2155 Name_Buffer (1) := Source (Scan_Ptr);
2156 Accumulate_Checksum (Name_Buffer (1));
2157 Scan_Ptr := Scan_Ptr + 1;
2158 goto Scan_Identifier;
2159
2160 -- Upper case letters
2161
2162 when 'A' .. 'Z' =>
2163 if Source (Scan_Ptr) = 'F'
2164 and then Source (Scan_Ptr + 1) = '"'
2165 then
2166 if Core_Extensions_Allowed then
2167 Error_Msg_S
2168 ("delimiter of interpolated string must be in lowercase");
2169 Scan_Ptr := Scan_Ptr + 1;
2170 Token := Tok_Left_Interpolated_String;
2171 return;
2172 else
2173 Error_Msg_GNAT_Extension
2174 ("interpolated string", Scan_Ptr,
2175 Is_Core_Extension => True);
2176 end if;
2177 end if;
2178
2179 Token_Contains_Uppercase := True;
2180 Name_Len := 1;
2181 Underline_Found := False;
2182 Name_Buffer (1) :=
2183 Character'Val (Character'Pos (Source (Scan_Ptr)) + 32);
2184 Accumulate_Checksum (Name_Buffer (1));
2185 Scan_Ptr := Scan_Ptr + 1;
2186 goto Scan_Identifier;
2187
2188 -- Underline character
2189
2190 when '_' =>
2191 -- Identifiers with leading underscores are not allowed in Ada.
2192 -- However, we allow them in the run-time library, so we can
2193 -- create names that are hidden from normal Ada code. For an
2194 -- example, search for "Name_uNext", which is "_Next".
2195
2196 if not In_Internal_Unit (Scan_Ptr) then
2197 Error_Msg_S ("identifier cannot start with underline");
2198 end if;
2199
2200 Name_Len := 1;
2201 Name_Buffer (1) := '_';
2202 Scan_Ptr := Scan_Ptr + 1;
2203 Underline_Found := False;
2204 goto Scan_Identifier;
2205
2206 -- Space (not possible, because we scanned past blanks)
2207
2208 when ' ' =>
2209 raise Program_Error;
2210
2211 -- Characters in top half of ASCII 8-bit chart
2212
2213 when Upper_Half_Character =>
2214
2215 -- Wide character case
2216
2217 if Upper_Half_Encoding then
2218 goto Scan_Wide_Character;
2219
2220 -- Otherwise we have OK Latin-1 character
2221
2222 else
2223 -- Upper half characters may possibly be identifier letters
2224 -- but can never be digits, so Identifier_Char can be used to
2225 -- test for a valid start of identifier character.
2226
2227 if Identifier_Char (Source (Scan_Ptr)) then
2228 Name_Len := 0;
2229 Underline_Found := False;
2230 goto Scan_Identifier;
2231 else
2232 Error_Illegal_Character;
2233 end if;
2234 end if;
2235
2236 when ESC =>
2237
2238 -- ESC character, possible start of identifier if wide characters
2239 -- using ESC encoding are allowed in identifiers, which we can
2240 -- tell by looking at the Identifier_Char flag for ESC, which is
2241 -- only true if these conditions are met. In Ada 2005 mode, may
2242 -- also be valid UTF_32 space or line terminator character.
2243
2244 if Identifier_Char (ESC) then
2245 Name_Len := 0;
2246 goto Scan_Wide_Character;
2247 else
2248 Error_Illegal_Character;
2249 end if;
2250
2251 -- Illegal characters
2252
2253 when ACK | ASCII.SO | BEL | BS | CAN | DC1 | DC2 | DC3 | DC4 | DEL
2254 | DLE | EM | ENQ | EOT | ETB | ETX | FS | GS | NAK | NUL | RS | SI
2255 | SOH | STX | SYN | US
2256 | '?' | '`' | '\' | '^' | '~'
2257 =>
2258 Error_Illegal_Character;
2259
2260 -- Special preprocessor characters. If Set_Special_Character has been
2261 -- called, return a Special token. Otherwise give an error.
2262
2263 when Special_Preprocessor_Character =>
2264
2265 declare
2266 function Matches_After_Skipping_White_Space
2267 (S : String) return Boolean;
2268
2269 -- Return True iff after skipping past white space the
2270 -- next Source characters match the given string.
2271
2272 ----------------------------------------
2273 -- Matches_After_Skipping_White_Space --
2274 ----------------------------------------
2275
2276 function Matches_After_Skipping_White_Space
2277 (S : String) return Boolean
2278 is
2279 function To_Lower_Case_String (Buff : Text_Buffer)
2280 return String;
2281 -- Convert a text buffer to a lower-case string.
2282
2283 --------------------------
2284 -- To_Lower_Case_String --
2285 --------------------------
2286
2287 function To_Lower_Case_String (Buff : Text_Buffer)
2288 return String
2289 is
2290 subtype One_Based is Text_Buffer (1 .. Buff'Length);
2291 Result : String := String (One_Based (Buff));
2292 begin
2293 -- The System.Case_Util.To_Lower function (the overload
2294 -- that takes a string parameter) cannot be called
2295 -- here due to bootstrapping problems. That function
2296 -- was added too recently.
2297
2298 System.Case_Util.To_Lower (Result);
2299 return Result;
2300 end To_Lower_Case_String;
2301
2302 pragma Assert (Source (Scan_Ptr) = '#');
2303 Local_Scan_Ptr : Source_Ptr := Scan_Ptr + 1;
2304
2305 -- Start of processing for Matches_After_Skipping_White_Space
2306
2307 begin
2308 while Local_Scan_Ptr in Source'Range
2309 and then Source (Local_Scan_Ptr) in ' ' | HT
2310 loop
2311 Local_Scan_Ptr := Local_Scan_Ptr + 1;
2312 end loop;
2313
2314 return Local_Scan_Ptr in Source'Range
2315 and then Local_Scan_Ptr + (S'Length - 1) in Source'Range
2316 and then S = To_Lower_Case_String (
2317 Source (Local_Scan_Ptr ..
2318 Local_Scan_Ptr + (S'Length - 1)));
2319 end Matches_After_Skipping_White_Space;
2320
2321 begin
2322 -- If Set_Special_Character has been called for this character,
2323 -- set Scans.Special_Character and return a Special token.
2324
2325 if Special_Characters (Source (Scan_Ptr)) then
2326 Token_Ptr := Scan_Ptr;
2327 Token := Tok_Special;
2328 Special_Character := Source (Scan_Ptr);
2329 Scan_Ptr := Scan_Ptr + 1;
2330 return;
2331
2332 -- Check for something looking like a preprocessor directive
2333
2334 elsif Source (Scan_Ptr) = '#'
2335 and then (Matches_After_Skipping_White_Space ("if")
2336 or else
2337 Matches_After_Skipping_White_Space ("elsif")
2338 or else
2339 Matches_After_Skipping_White_Space ("else")
2340 or else
2341 Matches_After_Skipping_White_Space ("end"))
2342 then
2343 Error_Msg_S
2344 ("preprocessor directive ignored" &
2345 ", preprocessor not active");
2346
2347 -- Skip to end of line
2348
2349 loop
2350 if Source (Scan_Ptr) in Graphic_Character
2351 or else
2352 Source (Scan_Ptr) = HT
2353 then
2354 Scan_Ptr := Scan_Ptr + 1;
2355
2356 -- Done if line terminator or EOF
2357
2358 elsif Source (Scan_Ptr) in Line_Terminator
2359 or else
2360 Source (Scan_Ptr) = EOF
2361 then
2362 exit;
2363
2364 -- If we have a wide character, we have to scan it out,
2365 -- because it might be a legitimate line terminator
2366
2367 elsif Start_Of_Wide_Character then
2368 declare
2369 Wptr : constant Source_Ptr := Scan_Ptr;
2370 Code : Char_Code;
2371 Err : Boolean;
2372
2373 begin
2374 Scan_Wide (Source, Scan_Ptr, Code, Err);
2375
2376 -- If not well formed wide character, then just
2377 -- skip past it and ignore it.
2378
2379 if Err then
2380 Scan_Ptr := Wptr + 1;
2381
2382 -- If UTF_32 terminator, terminate comment scan
2383
2384 elsif Is_UTF_32_Line_Terminator (UTF_32 (Code)) then
2385 Scan_Ptr := Wptr;
2386 exit;
2387 end if;
2388 end;
2389
2390 -- Else keep going (don't worry about bad comment chars
2391 -- in this context, we just want to find the end of line.
2392
2393 else
2394 Scan_Ptr := Scan_Ptr + 1;
2395 end if;
2396 end loop;
2397
2398 -- Otherwise, this is an illegal character
2399
2400 else
2401 Error_Illegal_Character;
2402 end if;
2403
2404 end;
2405
2406 -- End switch on non-blank character
2407
2408 end case;
2409
2410 -- End loop past format effectors. The exit from this loop is by
2411 -- executing a return statement following completion of token scan
2412 -- (control never falls out of this loop to the code that follows).
2413
2414 end loop;
2415
2416 pragma Assert (False);
2417
2418 -- Wide_Character scanning routine. On entry we have encountered the
2419 -- initial character of a wide character sequence.
2420
2421 <<Scan_Wide_Character>>
2422 declare
2423 Code : Char_Code;
2424 Cat : Category;
2425 Err : Boolean;
2426
2427 begin
2428 Wptr := Scan_Ptr;
2429 Scan_Wide (Source, Scan_Ptr, Code, Err);
2430
2431 -- If bad wide character, signal error and continue scan
2432
2433 if Err then
2434 Error_Illegal_Wide_Character;
2435 goto Scan_Next_Character;
2436 end if;
2437
2438 Cat := Get_Category (UTF_32 (Code));
2439
2440 -- If OK letter, reset scan ptr and go scan identifier
2441
2442 if Is_UTF_32_Letter (Cat) then
2443 Scan_Ptr := Wptr;
2444 Name_Len := 0;
2445 Underline_Found := False;
2446 goto Scan_Identifier;
2447
2448 -- If OK wide space, ignore and keep scanning (we do not include
2449 -- any ignored spaces in checksum)
2450
2451 elsif Is_UTF_32_Space (Cat) then
2452 goto Scan_Next_Character;
2453
2454 -- If other format character, ignore and keep scanning (again we
2455 -- do not include in the checksum) (this is for AI-0079).
2456
2457 elsif Is_UTF_32_Other (Cat) then
2458 goto Scan_Next_Character;
2459
2460 -- If OK wide line terminator, terminate current line
2461
2462 elsif Is_UTF_32_Line_Terminator (UTF_32 (Code)) then
2463 Scan_Ptr := Wptr;
2464 goto Scan_Line_Terminator;
2465
2466 -- Punctuation is an error (at start of identifier)
2467
2468 elsif Is_UTF_32_Punctuation (Cat) then
2469 Error_Msg ("identifier cannot start with punctuation", Wptr);
2470 Scan_Ptr := Wptr;
2471 Name_Len := 0;
2472 Underline_Found := False;
2473 goto Scan_Identifier;
2474
2475 -- Mark character is an error (at start of identifier)
2476
2477 elsif Is_UTF_32_Mark (Cat) then
2478 Error_Msg ("identifier cannot start with mark character", Wptr);
2479 Scan_Ptr := Wptr;
2480 Name_Len := 0;
2481 Underline_Found := False;
2482 goto Scan_Identifier;
2483
2484 -- Extended digit character is an error. Could be bad start of
2485 -- identifier or bad literal. Not worth doing too much to try to
2486 -- distinguish these cases, but we will do a little bit.
2487
2488 elsif Is_UTF_32_Digit (Cat) then
2489 Error_Msg
2490 ("identifier cannot start with digit character", Wptr);
2491 Scan_Ptr := Wptr;
2492 Name_Len := 0;
2493 Underline_Found := False;
2494 goto Scan_Identifier;
2495
2496 -- All other wide characters are illegal here
2497
2498 else
2499 Error_Illegal_Wide_Character;
2500 goto Scan_Next_Character;
2501 end if;
2502 end;
2503
2504 -- Routine to scan line terminator. On entry Scan_Ptr points to a
2505 -- character which is one of FF,LR,CR,VT, or one of the wide characters
2506 -- that is treated as a line terminator.
2507
2508 <<Scan_Line_Terminator>>
2509
2510 -- Check line too long
2511
2512 Check_End_Of_Line;
2513
2514 -- Set Token_Ptr, if End_Of_Line is a token, for the case when it is
2515 -- a physical line.
2516
2517 if End_Of_Line_Is_Token then
2518 Token_Ptr := Scan_Ptr;
2519 end if;
2520
2521 declare
2522 Physical : Boolean;
2523
2524 begin
2525 Skip_Line_Terminators (Scan_Ptr, Physical);
2526
2527 -- If we are at start of physical line, update scan pointers to
2528 -- reflect the start of the new line.
2529
2530 if Physical then
2531 Current_Line_Start := Scan_Ptr;
2532 Start_Column := Set_Start_Column;
2533 First_Non_Blank_Location := Scan_Ptr;
2534
2535 -- If End_Of_Line is a token, we return it as it is a
2536 -- physical line.
2537
2538 if End_Of_Line_Is_Token then
2539 Token := Tok_End_Of_Line;
2540 return;
2541 end if;
2542 end if;
2543 end;
2544
2545 goto Scan_Next_Character;
2546
2547 -- Identifier scanning routine. On entry, some initial characters of
2548 -- the identifier may have already been stored in Name_Buffer. If so,
2549 -- Name_Len has the number of characters stored, otherwise Name_Len is
2550 -- set to zero on entry. Underline_Found is also set False on entry.
2551
2552 <<Scan_Identifier>>
2553
2554 -- This loop scans as fast as possible past lower half letters and
2555 -- digits, which we expect to be the most common characters.
2556
2557 loop
2558 if Source (Scan_Ptr) in 'a' .. 'z'
2559 or else Source (Scan_Ptr) in '0' .. '9'
2560 then
2561 Name_Buffer (Name_Len + 1) := Source (Scan_Ptr);
2562 Accumulate_Checksum (Source (Scan_Ptr));
2563
2564 elsif Source (Scan_Ptr) in 'A' .. 'Z' then
2565 Token_Contains_Uppercase := True;
2566
2567 Name_Buffer (Name_Len + 1) :=
2568 Character'Val (Character'Pos (Source (Scan_Ptr)) + 32);
2569 Accumulate_Checksum (Name_Buffer (Name_Len + 1));
2570
2571 else
2572 exit;
2573 end if;
2574
2575 Underline_Found := False;
2576 Scan_Ptr := Scan_Ptr + 1;
2577 Name_Len := Name_Len + 1;
2578 end loop;
2579
2580 -- If we fall through, then we have encountered either an underline
2581 -- character, or an extended identifier character (i.e. one from the
2582 -- upper half), or a wide character, or an identifier terminator. The
2583 -- initial test speeds us up in the most common case where we have
2584 -- an identifier terminator. Note that ESC is an identifier character
2585 -- only if a wide character encoding method that uses ESC encoding
2586 -- is active, so if we find an ESC character we know that we have a
2587 -- wide character.
2588
2589 if Identifier_Char (Source (Scan_Ptr))
2590 or else (Source (Scan_Ptr) in Upper_Half_Character
2591 and then Upper_Half_Encoding)
2592 then
2593 -- Case of underline
2594
2595 if Source (Scan_Ptr) = '_' then
2596 Accumulate_Checksum ('_');
2597
2598 if Underline_Found then
2599 Error_No_Double_Underline;
2600 else
2601 Underline_Found := True;
2602 Name_Len := Name_Len + 1;
2603 Name_Buffer (Name_Len) := '_';
2604 end if;
2605
2606 Scan_Ptr := Scan_Ptr + 1;
2607 goto Scan_Identifier;
2608
2609 -- Upper half character
2610
2611 elsif Source (Scan_Ptr) in Upper_Half_Character
2612 and then not Upper_Half_Encoding
2613 then
2614 Accumulate_Checksum (Source (Scan_Ptr));
2615 Store_Encoded_Character
2616 (Get_Char_Code (Fold_Lower (Source (Scan_Ptr))));
2617 Scan_Ptr := Scan_Ptr + 1;
2618 Underline_Found := False;
2619 goto Scan_Identifier;
2620
2621 -- Left bracket not followed by a quote terminates an identifier.
2622 -- This is an error, but we don't want to give a junk error msg
2623 -- about wide characters in this case.
2624
2625 elsif Source (Scan_Ptr) = '['
2626 and then Source (Scan_Ptr + 1) /= '"'
2627 then
2628 null;
2629
2630 -- We know we have a wide character encoding here (the current
2631 -- character is either ESC, left bracket, or an upper half
2632 -- character depending on the encoding method).
2633
2634 else
2635 -- Scan out the wide character and insert the appropriate
2636 -- encoding into the name table entry for the identifier.
2637
2638 declare
2639 Code : Char_Code;
2640 Err : Boolean;
2641 Chr : Character;
2642 Cat : Category;
2643
2644 begin
2645 Wptr := Scan_Ptr;
2646 Scan_Wide (Source, Scan_Ptr, Code, Err);
2647
2648 -- If error, signal error
2649
2650 if Err then
2651 Error_Illegal_Wide_Character;
2652
2653 -- If the character scanned is a normal identifier
2654 -- character, then we treat it that way.
2655
2656 elsif In_Character_Range (Code)
2657 and then Identifier_Char (Get_Character (Code))
2658 then
2659 Chr := Get_Character (Code);
2660 Accumulate_Checksum (Chr);
2661 Store_Encoded_Character
2662 (Get_Char_Code (Fold_Lower (Chr)));
2663 Underline_Found := False;
2664
2665 -- Here if not a normal identifier character
2666
2667 else
2668 Cat := Get_Category (UTF_32 (Code));
2669
2670 -- Wide character in Unicode category "Other, Format"
2671 -- is not accepted in an identifier. This is because it
2672 -- it is considered a security risk (AI-0091).
2673
2674 -- However, it is OK for such a character to appear at
2675 -- the end of an identifier.
2676
2677 if Is_UTF_32_Other (Cat) then
2678 if not Identifier_Char (Source (Scan_Ptr)) then
2679 goto Scan_Identifier_Complete;
2680 else
2681 Error_Msg
2682 ("identifier cannot contain other_format "
2683 & "character", Wptr);
2684 goto Scan_Identifier;
2685 end if;
2686
2687 -- Wide character in category Separator,Space terminates
2688
2689 elsif Is_UTF_32_Space (Cat) then
2690 goto Scan_Identifier_Complete;
2691 end if;
2692
2693 -- Here if wide character is part of the identifier
2694
2695 -- Make sure we are allowing wide characters in
2696 -- identifiers. Note that we allow wide character
2697 -- notation for an OK identifier character. This in
2698 -- particular allows bracket or other notation to be
2699 -- used for upper half letters.
2700
2701 -- Wide characters are always allowed in Ada 2005
2702
2703 if Identifier_Character_Set /= 'w'
2704 and then Ada_Version < Ada_2005
2705 then
2706 Error_Msg
2707 ("wide character not allowed in identifier", Wptr);
2708 end if;
2709
2710 -- AI12-0004: An identifier shall only contain characters
2711 -- that may be present in Normalization Form KC.
2712
2713 if not Is_UTF_32_NFKC (UTF_32 (Code)) then
2714 Error_Msg
2715 ("invalid wide character in identifier", Wptr);
2716
2717 -- If OK letter, store it folding to upper case. Note
2718 -- that we include the folded letter in the checksum.
2719
2720 elsif Is_UTF_32_Letter (Cat) then
2721 Code :=
2722 Char_Code (UTF_32_To_Upper_Case (UTF_32 (Code)));
2723 Accumulate_Checksum (Code);
2724 Store_Encoded_Character (Code);
2725 Underline_Found := False;
2726
2727 -- If OK extended digit or mark, then store it
2728
2729 elsif Is_UTF_32_Digit (Cat)
2730 or else Is_UTF_32_Mark (Cat)
2731 then
2732 Accumulate_Checksum (Code);
2733 Store_Encoded_Character (Code);
2734 Underline_Found := False;
2735
2736 -- Wide punctuation is also stored, but counts as an
2737 -- underline character for error checking purposes.
2738
2739 elsif Is_UTF_32_Punctuation (Cat) then
2740 Accumulate_Checksum (Code);
2741
2742 if Underline_Found then
2743 declare
2744 Cend : constant Source_Ptr := Scan_Ptr;
2745 begin
2746 Scan_Ptr := Wptr;
2747 Error_No_Double_Underline;
2748 Scan_Ptr := Cend;
2749 end;
2750
2751 else
2752 Store_Encoded_Character (Code);
2753 Underline_Found := True;
2754 end if;
2755
2756 -- Any other wide character is not acceptable
2757
2758 else
2759 Error_Msg
2760 ("invalid wide character in identifier", Wptr);
2761 end if;
2762 end if;
2763
2764 goto Scan_Identifier;
2765 end;
2766 end if;
2767 end if;
2768
2769 -- Scan of identifier is complete. The identifier is stored in
2770 -- Name_Buffer, and Scan_Ptr points past the last character.
2771
2772 <<Scan_Identifier_Complete>>
2773 Token_Name := Name_Find;
2774
2775 -- Check for identifier ending with underline or punctuation char
2776
2777 if Underline_Found then
2778 Underline_Found := False;
2779
2780 if Source (Scan_Ptr - 1) = '_' then
2781 Error_Msg
2782 ("identifier cannot end with underline", Scan_Ptr - 1);
2783 else
2784 Error_Msg
2785 ("identifier cannot end with punctuation character", Wptr);
2786 end if;
2787 end if;
2788
2789 -- We will assume it is an identifier, not a keyword, so that the
2790 -- checksum is independent of the Ada version.
2791
2792 Token := Tok_Identifier;
2793
2794 -- Check if it is a keyword
2795
2796 if Is_Keyword_Name (Token_Name) then
2797 Accumulate_Token_Checksum;
2798 Token := Token_Type'Val (Get_Name_Table_Byte (Token_Name));
2799
2800 -- See Exp_Put_Image for documentation of Tagged_Seen
2801
2802 if Token = Tok_Tagged then
2803 Tagged_Seen := True;
2804 end if;
2805
2806 -- Keyword style checks
2807
2808 if Style_Check then
2809
2810 -- Deal with possible style check for non-lower case keyword,
2811 -- but we don't treat ACCESS, DELTA, DIGITS, RANGE as keywords
2812 -- for this purpose if they appear as attribute designators.
2813 -- Actually we only check the first character for speed.
2814
2815 -- Ada 2005 (AI-284): Do not apply the style check in case of
2816 -- "pragma Interface"
2817
2818 -- Ada 2005 (AI-340): Do not apply the style check in case of
2819 -- MOD attribute.
2820
2821 if Token_Contains_Uppercase
2822 and then (Prev_Token /= Tok_Apostrophe
2823 or else
2824 (Token /= Tok_Access and then
2825 Token /= Tok_Delta and then
2826 Token /= Tok_Digits and then
2827 Token /= Tok_Mod and then
2828 Token /= Tok_Range))
2829 and then (Token /= Tok_Interface
2830 or else
2831 (Token = Tok_Interface
2832 and then Prev_Token /= Tok_Pragma))
2833 then
2834 Style.Non_Lower_Case_Keyword;
2835 end if;
2836
2837 -- Check THEN/ELSE style rules. These do not apply to AND THEN
2838 -- or OR ELSE, and do not apply in if expressions.
2839
2840 if (Token = Tok_Then and then Prev_Token /= Tok_And)
2841 or else
2842 (Token = Tok_Else and then Prev_Token /= Tok_Or)
2843 then
2844 if Inside_If_Expression = 0 then
2845 Style.Check_Separate_Stmt_Lines;
2846 end if;
2847 end if;
2848 end if;
2849
2850 -- We must reset Token_Name since this is not an identifier and
2851 -- if we leave Token_Name set, the parser gets confused because
2852 -- it thinks it is dealing with an identifier instead of the
2853 -- corresponding keyword.
2854
2855 Token_Name := No_Name;
2856 return;
2857
2858 -- It is an identifier after all
2859
2860 else
2861 Accumulate_Token_Checksum;
2862 Post_Scan;
2863 end if;
2864 end Scan;
2865
2866 ------------------------------
2867 -- Set_End_Of_Line_As_Token --
2868 ------------------------------
2869
2870 procedure Set_End_Of_Line_As_Token (Value : Boolean) is
2871 begin
2872 End_Of_Line_Is_Token := Value;
2873 end Set_End_Of_Line_As_Token;
2874
2875 ---------------------------
2876 -- Set_Special_Character --
2877 ---------------------------
2878
2879 procedure Set_Special_Character (C : Special_Preprocessor_Character) is
2880 begin
2881 Special_Characters (C) := True;
2882 end Set_Special_Character;
2883
2884 ----------------------
2885 -- Set_Start_Column --
2886 ----------------------
2887
2888 -- Note: it seems at first glance a little expensive to compute this value
2889 -- for every source line (since it is certainly not used for all source
2890 -- lines). On the other hand, it doesn't take much more work to skip past
2891 -- the initial white space on the line counting the columns than it would
2892 -- to scan past the white space using the standard scanning circuits.
2893
2894 function Set_Start_Column return Column_Number is
2895 Start_Column : Column_Number := 0;
2896
2897 begin
2898 -- Outer loop scans past horizontal tab characters
2899
2900 Tabs_Loop : loop
2901
2902 -- Inner loop scans past blanks, bumping Scan_Ptr past the blanks and
2903 -- adjusting Start_Column to account for them.
2904
2905 Blanks_Loop :
2906 while Source (Scan_Ptr) = ' ' loop
2907 Scan_Ptr := Scan_Ptr + 1;
2908 Start_Column := Start_Column + 1;
2909 end loop Blanks_Loop;
2910
2911 -- Outer loop keeps going only if a horizontal tab follows
2912
2913 if Source (Scan_Ptr) = HT then
2914 if Style_Check then
2915 Style.Check_HT;
2916 end if;
2917
2918 Scan_Ptr := Scan_Ptr + 1;
2919 Start_Column := (Start_Column / 8) * 8 + 8;
2920 else
2921 exit Tabs_Loop;
2922 end if;
2923 end loop Tabs_Loop;
2924
2925 return Start_Column;
2926
2927 -- A constraint error can happen only if we have a compiler with checks on
2928 -- and a line with a ludicrous number of tabs or spaces at the start. In
2929 -- such a case, we really don't care if Start_Column is right or not.
2930
2931 exception
2932 when Constraint_Error =>
2933 return Column_Number'Last;
2934 end Set_Start_Column;
2935
2936 end Scng;