OSDN Git Service

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