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