1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Csets
; use Csets
;
27 with Err_Vars
; use Err_Vars
;
28 with Hostparm
; use Hostparm
;
29 with Namet
; use Namet
;
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
;
41 with System
.UTF_32
; use System
.UTF_32
;
42 with System
.WCh_Con
; use System
.WCh_Con
;
47 -- Make control characters visible
49 Special_Characters
: array (Character) of Boolean := (others => False);
50 -- For characters that are Special token, the value is True
52 Comment_Is_Token
: Boolean := False;
53 -- True if comments are tokens
55 End_Of_Line_Is_Token
: Boolean := False;
56 -- True if End_Of_Line is a token
58 -----------------------
59 -- Local Subprograms --
60 -----------------------
62 procedure Accumulate_Token_Checksum
;
63 pragma Inline
(Accumulate_Token_Checksum
);
65 procedure Accumulate_Checksum
(C
: Character);
66 pragma Inline
(Accumulate_Checksum
);
67 -- This routine accumulates the checksum given character C. During the
68 -- scanning of a source file, this routine is called with every character
69 -- in the source, excluding blanks, and all control characters (except
70 -- that ESC is included in the checksum). Upper case letters not in string
71 -- literals are folded by the caller. See Sinput spec for the documentation
72 -- of the checksum algorithm. Note: checksum values are only used if we
73 -- generate code, so it is not necessary to worry about making the right
74 -- sequence of calls in any error situation.
76 procedure Accumulate_Checksum
(C
: Char_Code
);
77 pragma Inline
(Accumulate_Checksum
);
78 -- This version is identical, except that the argument, C, is a character
79 -- code value instead of a character. This is used when wide characters
80 -- are scanned. We use the character code rather than the ASCII characters
81 -- so that the checksum is independent of wide character encoding method.
83 procedure Initialize_Checksum
;
84 pragma Inline
(Initialize_Checksum
);
85 -- Initialize checksum value
87 -------------------------
88 -- Accumulate_Checksum --
89 -------------------------
91 procedure Accumulate_Checksum
(C
: Character) is
93 System
.CRC32
.Update
(System
.CRC32
.CRC32
(Checksum
), C
);
94 end Accumulate_Checksum
;
96 procedure Accumulate_Checksum
(C
: Char_Code
) is
99 Accumulate_Checksum
(Character'Val (C
/ 2 ** 24));
100 Accumulate_Checksum
(Character'Val ((C
/ 2 ** 16) mod 256));
101 Accumulate_Checksum
(Character'Val ((C
/ 256) mod 256));
103 Accumulate_Checksum
(Character'Val (C
/ 256));
106 Accumulate_Checksum
(Character'Val (C
mod 256));
107 end Accumulate_Checksum
;
109 -------------------------------
110 -- Accumulate_Token_Checksum --
111 -------------------------------
113 procedure Accumulate_Token_Checksum
is
116 (System
.CRC32
.CRC32
(Checksum
),
117 Character'Val (Token_Type
'Pos (Token
)));
118 end Accumulate_Token_Checksum
;
120 ----------------------------
121 -- Determine_Token_Casing --
122 ----------------------------
124 function Determine_Token_Casing
return Casing_Type
is
126 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
127 end Determine_Token_Casing
;
129 -------------------------
130 -- Initialize_Checksum --
131 -------------------------
133 procedure Initialize_Checksum
is
135 System
.CRC32
.Initialize
(System
.CRC32
.CRC32
(Checksum
));
136 end Initialize_Checksum
;
138 ------------------------
139 -- Initialize_Scanner --
140 ------------------------
142 procedure Initialize_Scanner
(Index
: Source_File_Index
) is
144 -- Establish reserved words
146 Scans
.Initialize_Ada_Keywords
;
148 -- Initialize scan control variables
150 Current_Source_File
:= Index
;
151 Source
:= Source_Text
(Current_Source_File
);
152 Scan_Ptr
:= Source_First
(Current_Source_File
);
154 Token_Ptr
:= Scan_Ptr
;
155 Current_Line_Start
:= Scan_Ptr
;
157 Token_Name
:= No_Name
;
158 Start_Column
:= Set_Start_Column
;
159 First_Non_Blank_Location
:= Scan_Ptr
;
162 Wide_Char_Byte_Count
:= 0;
164 -- Do not call Scan, otherwise the License stuff does not work in Scn
166 end Initialize_Scanner
;
168 ------------------------------
169 -- Reset_Special_Characters --
170 ------------------------------
172 procedure Reset_Special_Characters
is
174 Special_Characters
:= (others => False);
175 end Reset_Special_Characters
;
183 Start_Of_Comment
: Source_Ptr
;
184 -- Record start of comment position
186 Underline_Found
: Boolean;
187 -- During scanning of an identifier, set to True if last character
188 -- scanned was an underline or other punctuation character. This
189 -- is used to flag the error of two underlines/punctuations in a
190 -- row or ending an identifier with a underline/punctuation. Here
191 -- punctuation means any UTF_32 character in the Unicode category
192 -- Punctuation,Connector.
195 -- Used to remember start of last wide character scanned
197 procedure Check_End_Of_Line
;
198 -- Called when end of line encountered. Checks that line is not too
199 -- long, and that other style checks for the end of line are met.
201 function Double_Char_Token
(C
: Character) return Boolean;
202 -- This function is used for double character tokens like := or <>. It
203 -- checks if the character following Source (Scan_Ptr) is C, and if so
204 -- bumps Scan_Ptr past the pair of characters and returns True. A space
205 -- between the two characters is also recognized with an appropriate
206 -- error message being issued. If C is not present, False is returned.
207 -- Note that Double_Char_Token can only be used for tokens defined in
208 -- the Ada syntax (it's use for error cases like && is not appropriate
209 -- since we do not want a junk message for a case like &-space-&).
211 procedure Error_Illegal_Character
;
212 -- Give illegal character error, Scan_Ptr points to character. On
213 -- return, Scan_Ptr is bumped past the illegal character.
215 procedure Error_Illegal_Wide_Character
;
216 -- Give illegal wide character message. On return, Scan_Ptr is bumped
217 -- past the illegal character, which may still leave us pointing to
218 -- junk, not much we can do if the escape sequence is messed up!
220 procedure Error_Long_Line
;
221 -- Signal error of excessively long line
223 procedure Error_No_Double_Underline
;
224 -- Signal error of two underline or punctuation characters in a row.
225 -- Called with Scan_Ptr pointing to second underline/punctuation char.
228 -- This is the procedure for scanning out numeric literals. On entry,
229 -- Scan_Ptr points to the digit that starts the numeric literal (the
230 -- checksum for this character has not been accumulated yet). On return
231 -- Scan_Ptr points past the last character of the numeric literal, Token
232 -- and Token_Node are set appropriately, and the checksum is updated.
235 -- This is the procedure for scanning out string literals. On entry,
236 -- Scan_Ptr points to the opening string quote (the checksum for this
237 -- character has not been accumulated yet). On return Scan_Ptr points
238 -- past the closing quote of the string literal, Token and Token_Node
239 -- are set appropriately, and the checksum is updated.
241 -----------------------
242 -- Check_End_Of_Line --
243 -----------------------
245 procedure Check_End_Of_Line
is
246 Len
: constant Int
:=
248 Int
(Current_Line_Start
) -
249 Wide_Char_Byte_Count
;
253 Style
.Check_Line_Terminator
(Len
);
256 -- Deal with checking maximum line length
258 if Style_Check
and Style_Check_Max_Line_Length
then
259 Style
.Check_Line_Max_Length
(Len
);
261 -- If style checking is inactive, check maximum line length against
264 elsif Len
> Max_Line_Length
then
268 -- Now one more checking circuit. Normally we are only enforcing a
269 -- limit of physical characters, with tabs counting as one character.
270 -- But if after tab expansion we would have a total line length that
271 -- exceeded 32766, that would really cause trouble, because column
272 -- positions would exceed the maximum we allow for a column count.
273 -- Note: the limit is 32766 rather than 32767, since we use a value
274 -- of 32767 for special purposes (see Sinput). Now we really do not
275 -- want to go messing with tabs in the normal case, so what we do is
276 -- to check for a line that has more than 4096 physical characters.
277 -- Any shorter line could not be a problem, even if it was all tabs.
286 Ptr
:= Current_Line_Start
;
288 exit when Ptr
= Scan_Ptr
;
290 if Source
(Ptr
) = ASCII
.HT
then
291 Col
:= (Col
- 1 + 8) / 8 * 8 + 1;
298 ("this line is longer than 32766 characters",
300 raise Unrecoverable_Error
;
308 -- Reset wide character byte count for next line
310 Wide_Char_Byte_Count
:= 0;
311 end Check_End_Of_Line
;
313 -----------------------
314 -- Double_Char_Token --
315 -----------------------
317 function Double_Char_Token
(C
: Character) return Boolean is
319 if Source
(Scan_Ptr
+ 1) = C
then
320 Accumulate_Checksum
(C
);
321 Scan_Ptr
:= Scan_Ptr
+ 2;
324 elsif Source
(Scan_Ptr
+ 1) = ' '
325 and then Source
(Scan_Ptr
+ 2) = C
327 Scan_Ptr
:= Scan_Ptr
+ 1;
328 Error_Msg_S
("no space allowed here");
329 Scan_Ptr
:= Scan_Ptr
+ 2;
335 end Double_Char_Token
;
337 -----------------------------
338 -- Error_Illegal_Character --
339 -----------------------------
341 procedure Error_Illegal_Character
is
343 Error_Msg_S
("illegal character");
344 Scan_Ptr
:= Scan_Ptr
+ 1;
345 end Error_Illegal_Character
;
347 ----------------------------------
348 -- Error_Illegal_Wide_Character --
349 ----------------------------------
351 procedure Error_Illegal_Wide_Character
is
353 Error_Msg
("illegal wide character", Wptr
);
354 end Error_Illegal_Wide_Character
;
356 ---------------------
357 -- Error_Long_Line --
358 ---------------------
360 procedure Error_Long_Line
is
363 ("this line is too long",
364 Current_Line_Start
+ Source_Ptr
(Max_Line_Length
));
367 -------------------------------
368 -- Error_No_Double_Underline --
369 -------------------------------
371 procedure Error_No_Double_Underline
is
373 Underline_Found
:= False;
375 -- There are four cases, and we special case the messages
377 if Source
(Scan_Ptr
) = '_' then
378 if Source
(Scan_Ptr
- 1) = '_' then
380 ("two consecutive underlines not permitted");
383 ("underline cannot follow punctuation character");
387 if Source
(Scan_Ptr
- 1) = '_' then
389 ("punctuation character cannot follow underline");
392 ("two consecutive punctuation characters not permitted");
395 end Error_No_Double_Underline
;
404 -- Current source program character
406 Base_Char
: Character;
407 -- Either # or : (character at start of based number)
413 -- Value of base in Uint format
416 -- Value of integer scanned by Scan_Integer in Uint format
419 -- Value of integer in numeric value being scanned
422 -- Scale value for real literal
425 -- Scale in Uint format
427 Exponent_Is_Negative
: Boolean;
428 -- Set true for negative exponent
430 Extended_Digit_Value
: Int
;
431 -- Extended digit value
433 Point_Scanned
: Boolean;
434 -- Flag for decimal point scanned in numeric literal
436 -----------------------
437 -- Local Subprograms --
438 -----------------------
440 procedure Error_Digit_Expected
;
441 -- Signal error of bad digit, Scan_Ptr points to the location at
442 -- which the digit was expected on input, and is unchanged on return.
444 procedure Scan_Integer
;
445 -- Procedure to scan integer literal. On entry, Scan_Ptr points to a
446 -- digit, on exit Scan_Ptr points past the last character of the
449 -- For each digit encountered, UI_Int_Value is multiplied by 10, and
450 -- the value of the digit added to the result. In addition, the
451 -- value in Scale is decremented by one for each actual digit
454 --------------------------
455 -- Error_Digit_Expected --
456 --------------------------
458 procedure Error_Digit_Expected
is
460 Error_Msg_S
("digit expected");
461 end Error_Digit_Expected
;
467 procedure Scan_Integer
is
469 -- Next character scanned
472 C
:= Source
(Scan_Ptr
);
474 -- Loop through digits (allowing underlines)
477 Accumulate_Checksum
(C
);
479 UI_Int_Value
* 10 + (Character'Pos (C
) - Character'Pos ('0'));
480 Scan_Ptr
:= Scan_Ptr
+ 1;
482 C
:= Source
(Scan_Ptr
);
484 -- Case of underline encountered
488 -- We do not accumulate the '_' in the checksum, so that
489 -- 1_234 is equivalent to 1234, and does not trigger
490 -- compilation for "minimal recompilation" (gnatmake -m).
493 Scan_Ptr
:= Scan_Ptr
+ 1;
494 C
:= Source
(Scan_Ptr
);
496 Error_No_Double_Underline
;
499 if C
not in '0' .. '9' then
500 Error_Digit_Expected
;
505 exit when C
not in '0' .. '9';
510 -- Start of Processing for Nlit
515 UI_Int_Value
:= Uint_0
;
518 Point_Scanned
:= False;
519 UI_Num_Value
:= UI_Int_Value
;
521 -- Various possibilities now for continuing the literal are period,
522 -- E/e (for exponent), or :/# (for based literal).
525 C
:= Source
(Scan_Ptr
);
529 -- Scan out point, but do not scan past .. which is a range
530 -- sequence, and must not be eaten up scanning a numeric literal.
532 while C
= '.' and then Source
(Scan_Ptr
+ 1) /= '.' loop
533 Accumulate_Checksum
('.');
535 if Point_Scanned
then
536 Error_Msg_S
("duplicate point ignored");
539 Point_Scanned
:= True;
540 Scan_Ptr
:= Scan_Ptr
+ 1;
541 C
:= Source
(Scan_Ptr
);
543 if C
not in '0' .. '9' then
545 ("real literal cannot end with point", Scan_Ptr
- 1);
548 UI_Num_Value
:= UI_Int_Value
;
552 -- Based literal case. The base is the value we already scanned.
553 -- In the case of colon, we insist that the following character
554 -- is indeed an extended digit or a period. This catches a number
555 -- of common errors, as well as catching the well known tricky
556 -- bug otherwise arising from "x : integer range 1 .. 10:= 6;"
559 or else (C
= ':' and then
560 (Source
(Scan_Ptr
+ 1) = '.'
562 Source
(Scan_Ptr
+ 1) in '0' .. '9'
564 Source
(Scan_Ptr
+ 1) in 'A' .. 'Z'
566 Source
(Scan_Ptr
+ 1) in 'a' .. 'z'))
569 Obsolescent_Check
(Scan_Ptr
);
571 if Warn_On_Obsolescent_Feature
then
573 ("use of "":"" is an obsolescent feature (RM J.2(3))?");
575 ("\use ""'#"" instead?");
579 Accumulate_Checksum
(C
);
581 UI_Base
:= UI_Int_Value
;
583 if UI_Base
< 2 or else UI_Base
> 16 then
584 Error_Msg_SC
("base not 2-16");
588 Base
:= UI_To_Int
(UI_Base
);
589 Scan_Ptr
:= Scan_Ptr
+ 1;
591 -- Scan out extended integer [. integer]
593 C
:= Source
(Scan_Ptr
);
594 UI_Int_Value
:= Uint_0
;
598 if C
in '0' .. '9' then
599 Accumulate_Checksum
(C
);
600 Extended_Digit_Value
:=
601 Int
'(Character'Pos (C)) - Int'(Character'Pos ('0'));
603 elsif C
in 'A' .. 'F' then
604 Accumulate_Checksum
(Character'Val (Character'Pos (C
) + 32));
605 Extended_Digit_Value
:=
606 Int
'(Character'Pos (C)) - Int'(Character'Pos ('A')) + 10;
608 elsif C
in 'a' .. 'f' then
609 Accumulate_Checksum
(C
);
610 Extended_Digit_Value
:=
611 Int
'(Character'Pos (C)) - Int'(Character'Pos ('a')) + 10;
614 Error_Msg_S
("extended digit expected");
618 if Extended_Digit_Value
>= Base
then
619 Error_Msg_S
("digit '>= base");
622 UI_Int_Value
:= UI_Int_Value
* UI_Base
+ Extended_Digit_Value
;
624 Scan_Ptr
:= Scan_Ptr
+ 1;
625 C
:= Source
(Scan_Ptr
);
629 Accumulate_Checksum
('_');
630 Scan_Ptr
:= Scan_Ptr
+ 1;
631 C
:= Source
(Scan_Ptr
);
633 Error_No_Double_Underline
;
637 Accumulate_Checksum
('.');
639 if Point_Scanned
then
640 Error_Msg_S
("duplicate point ignored");
643 Scan_Ptr
:= Scan_Ptr
+ 1;
644 C
:= Source
(Scan_Ptr
);
645 Point_Scanned
:= True;
648 elsif C
= Base_Char
then
649 Accumulate_Checksum
(C
);
650 Scan_Ptr
:= Scan_Ptr
+ 1;
653 elsif C
= '#' or else C
= ':' then
654 Error_Msg_S
("based number delimiters must match");
655 Scan_Ptr
:= Scan_Ptr
+ 1;
658 elsif not Identifier_Char
(C
) then
659 if Base_Char
= '#' then
660 Error_Msg_S
("missing '#");
662 Error_Msg_S
("missing ':");
670 UI_Num_Value
:= UI_Int_Value
;
675 if not Point_Scanned
then
679 UI_Scale
:= UI_From_Int
(Scale
);
682 if Source
(Scan_Ptr
) = 'e' or else Source
(Scan_Ptr
) = 'E' then
683 Accumulate_Checksum
('e');
684 Scan_Ptr
:= Scan_Ptr
+ 1;
685 Exponent_Is_Negative
:= False;
687 if Source
(Scan_Ptr
) = '+' then
688 Accumulate_Checksum
('+');
689 Scan_Ptr
:= Scan_Ptr
+ 1;
691 elsif Source
(Scan_Ptr
) = '-' then
692 Accumulate_Checksum
('-');
694 if not Point_Scanned
then
696 ("negative exponent not allowed for integer literal");
698 Exponent_Is_Negative
:= True;
701 Scan_Ptr
:= Scan_Ptr
+ 1;
704 UI_Int_Value
:= Uint_0
;
706 if Source
(Scan_Ptr
) in '0' .. '9' then
709 Error_Digit_Expected
;
712 if Exponent_Is_Negative
then
713 UI_Scale
:= UI_Scale
- UI_Int_Value
;
715 UI_Scale
:= UI_Scale
+ UI_Int_Value
;
719 -- Case of real literal to be returned
721 if Point_Scanned
then
722 Token
:= Tok_Real_Literal
;
723 Real_Literal_Value
:=
729 -- Case of integer literal to be returned
732 Token
:= Tok_Integer_Literal
;
735 Int_Literal_Value
:= UI_Num_Value
;
737 -- Avoid doing possibly expensive calculations in cases like
738 -- parsing 163E800_000# when semantics will not be done anyway.
739 -- This is especially useful when parsing garbled input.
741 elsif Operating_Mode
/= Check_Syntax
742 and then (Serious_Errors_Detected
= 0 or else Try_Semantics
)
744 Int_Literal_Value
:= UI_Num_Value
* UI_Base
** UI_Scale
;
747 Int_Literal_Value
:= No_Uint
;
751 Accumulate_Token_Checksum
;
762 Delimiter
: Character;
763 -- Delimiter (first character of string)
766 -- Current source program character
769 -- Current character code value
772 -- Error flag for Scan_Wide call
774 procedure Error_Bad_String_Char
;
775 -- Signal bad character in string/character literal. On entry
776 -- Scan_Ptr points to the improper character encountered during the
777 -- scan. Scan_Ptr is not modified, so it still points to the bad
778 -- character on return.
780 procedure Error_Unterminated_String
;
781 -- Procedure called if a line terminator character is encountered
782 -- during scanning a string, meaning that the string is not properly
785 procedure Set_String
;
786 -- Procedure used to distinguish between string and operator symbol.
787 -- On entry the string has been scanned out, and its characters
788 -- start at Token_Ptr and end one character before Scan_Ptr. On exit
789 -- Token is set to Tok_String_Literal or Tok_Operator_Symbol as
790 -- appropriate, and Token_Node is appropriately initialized. In
791 -- addition, in the operator symbol case, Token_Name is
792 -- appropriately set.
794 ---------------------------
795 -- Error_Bad_String_Char --
796 ---------------------------
798 procedure Error_Bad_String_Char
is
799 C
: constant Character := Source
(Scan_Ptr
);
803 Error_Msg_S
("horizontal tab not allowed in string");
805 elsif C
= VT
or else C
= FF
then
806 Error_Msg_S
("format effector not allowed in string");
808 elsif C
in Upper_Half_Character
then
809 Error_Msg_S
("(Ada 83) upper half character not allowed");
812 Error_Msg_S
("control character not allowed in string");
814 end Error_Bad_String_Char
;
816 -------------------------------
817 -- Error_Unterminated_String --
818 -------------------------------
820 procedure Error_Unterminated_String
is
822 -- An interesting little refinement. Consider the following
825 -- A := "this is an unterminated string;
826 -- A := "this is an unterminated string &
827 -- P(A, "this is a parameter that didn't get terminated);
829 -- We fiddle a little to do slightly better placement in these
830 -- cases also if there is white space at the end of the line we
831 -- place the flag at the start of this white space, not at the
832 -- end. Note that we only have to test for blanks, since tabs
833 -- aren't allowed in strings in the first place and would have
834 -- caused an error message.
836 -- Two more cases that we treat specially are:
838 -- A := "this string uses the wrong terminator'
839 -- A := "this string uses the wrong terminator' &
841 -- In these cases we give a different error message as well
843 -- We actually reposition the scan pointer to the point where we
844 -- place the flag in these cases, since it seems a better bet on
845 -- the original intention.
847 while Source
(Scan_Ptr
- 1) = ' '
848 or else Source
(Scan_Ptr
- 1) = '&'
850 Scan_Ptr
:= Scan_Ptr
- 1;
854 -- Check for case of incorrect string terminator, but single quote
855 -- is not considered incorrect if the opening terminator misused
856 -- a single quote (error message already given).
859 and then Source
(Scan_Ptr
- 1) = '''
863 ("incorrect string terminator character", Scan_Ptr
- 1);
867 if Source
(Scan_Ptr
- 1) = ';' then
868 Scan_Ptr
:= Scan_Ptr
- 1;
871 if Source
(Scan_Ptr
- 1) = ')' then
872 Scan_Ptr
:= Scan_Ptr
- 1;
877 Error_Msg_S
("missing string quote");
878 end Error_Unterminated_String
;
884 procedure Set_String
is
885 Slen
: constant Int
:= Int
(Scan_Ptr
- Token_Ptr
- 2);
891 -- Token_Name is currently set to Error_Name. The following
892 -- section of code resets Token_Name to the proper Name_Op_xx
893 -- value if the string is a valid operator symbol, otherwise it is
894 -- left set to Error_Name.
897 C1
:= Source
(Token_Ptr
+ 1);
901 Token_Name
:= Name_Op_Eq
;
904 Token_Name
:= Name_Op_Gt
;
907 Token_Name
:= Name_Op_Lt
;
910 Token_Name
:= Name_Op_Add
;
913 Token_Name
:= Name_Op_Subtract
;
916 Token_Name
:= Name_Op_Concat
;
919 Token_Name
:= Name_Op_Multiply
;
922 Token_Name
:= Name_Op_Divide
;
929 C1
:= Source
(Token_Ptr
+ 1);
930 C2
:= Source
(Token_Ptr
+ 2);
932 if C1
= '*' and then C2
= '*' then
933 Token_Name
:= Name_Op_Expon
;
938 Token_Name
:= Name_Op_Ne
;
940 Token_Name
:= Name_Op_Le
;
942 Token_Name
:= Name_Op_Ge
;
945 elsif (C1
= 'O' or else C1
= 'o') and then -- OR
946 (C2
= 'R' or else C2
= 'r')
948 Token_Name
:= Name_Op_Or
;
952 C1
:= Source
(Token_Ptr
+ 1);
953 C2
:= Source
(Token_Ptr
+ 2);
954 C3
:= Source
(Token_Ptr
+ 3);
956 if (C1
= 'A' or else C1
= 'a') and then -- AND
957 (C2
= 'N' or else C2
= 'n') and then
958 (C3
= 'D' or else C3
= 'd')
960 Token_Name
:= Name_Op_And
;
962 elsif (C1
= 'A' or else C1
= 'a') and then -- ABS
963 (C2
= 'B' or else C2
= 'b') and then
964 (C3
= 'S' or else C3
= 's')
966 Token_Name
:= Name_Op_Abs
;
968 elsif (C1
= 'M' or else C1
= 'm') and then -- MOD
969 (C2
= 'O' or else C2
= 'o') and then
970 (C3
= 'D' or else C3
= 'd')
972 Token_Name
:= Name_Op_Mod
;
974 elsif (C1
= 'N' or else C1
= 'n') and then -- NOT
975 (C2
= 'O' or else C2
= 'o') and then
976 (C3
= 'T' or else C3
= 't')
978 Token_Name
:= Name_Op_Not
;
980 elsif (C1
= 'R' or else C1
= 'r') and then -- REM
981 (C2
= 'E' or else C2
= 'e') and then
982 (C3
= 'M' or else C3
= 'm')
984 Token_Name
:= Name_Op_Rem
;
986 elsif (C1
= 'X' or else C1
= 'x') and then -- XOR
987 (C2
= 'O' or else C2
= 'o') and then
988 (C3
= 'R' or else C3
= 'r')
990 Token_Name
:= Name_Op_Xor
;
995 -- If it is an operator symbol, then Token_Name is set. If it is
996 -- some other string value, then Token_Name still contains
999 if Token_Name
= Error_Name
then
1000 Token
:= Tok_String_Literal
;
1003 Token
:= Tok_Operator_Symbol
;
1007 -- Start of processing for Slit
1010 -- On entry, Scan_Ptr points to the opening character of the string
1011 -- which is either a percent, double quote, or apostrophe (single
1012 -- quote). The latter case is an error detected by the character
1015 Delimiter
:= Source
(Scan_Ptr
);
1016 Accumulate_Checksum
(Delimiter
);
1018 Scan_Ptr
:= Scan_Ptr
+ 1;
1020 -- Loop to scan out characters of string literal
1023 C
:= Source
(Scan_Ptr
);
1025 if C
= Delimiter
then
1026 Accumulate_Checksum
(C
);
1027 Scan_Ptr
:= Scan_Ptr
+ 1;
1028 exit when Source
(Scan_Ptr
) /= Delimiter
;
1029 Code
:= Get_Char_Code
(C
);
1030 Accumulate_Checksum
(C
);
1031 Scan_Ptr
:= Scan_Ptr
+ 1;
1034 if C
= '"' and then Delimiter
= '%' then
1036 ("quote not allowed in percent delimited string");
1037 Code
:= Get_Char_Code
(C
);
1038 Scan_Ptr
:= Scan_Ptr
+ 1;
1041 and then Wide_Character_Encoding_Method
1042 in WC_ESC_Encoding_Method
)
1043 or else (C
in Upper_Half_Character
1044 and then Upper_Half_Encoding
)
1046 and then Source
(Scan_Ptr
+ 1) = '"'
1047 and then Identifier_Char
(Source
(Scan_Ptr
+ 2)))
1050 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1053 Error_Illegal_Wide_Character
;
1054 Code
:= Get_Char_Code
(' ');
1057 Accumulate_Checksum
(Code
);
1059 -- In Ada 95 mode we allow any wide characters in a string
1060 -- but in Ada 2005, the set of characters allowed has been
1061 -- restricted to graphic characters.
1063 if Ada_Version
>= Ada_05
1064 and then Is_UTF_32_Non_Graphic
(UTF_32
(Code
))
1067 ("(Ada 2005) non-graphic character not permitted " &
1068 "in string literal", Wptr
);
1072 Accumulate_Checksum
(C
);
1074 if C
not in Graphic_Character
then
1075 if C
in Line_Terminator
then
1076 Error_Unterminated_String
;
1079 elsif C
in Upper_Half_Character
then
1080 if Ada_Version
= Ada_83
then
1081 Error_Bad_String_Char
;
1085 Error_Bad_String_Char
;
1089 Code
:= Get_Char_Code
(C
);
1090 Scan_Ptr
:= Scan_Ptr
+ 1;
1094 Store_String_Char
(Code
);
1096 if not In_Character_Range
(Code
) then
1097 Wide_Character_Found
:= True;
1101 String_Literal_Id
:= End_String
;
1106 -- Start of processing for Scan
1109 Prev_Token
:= Token
;
1110 Prev_Token_Ptr
:= Token_Ptr
;
1111 Token_Name
:= Error_Name
;
1113 -- The following loop runs more than once only if a format effector
1114 -- (tab, vertical tab, form feed, line feed, carriage return) is
1115 -- encountered and skipped, or some error situation, such as an
1116 -- illegal character, is encountered.
1118 <<Scan_Next_Character
>>
1121 -- Skip past blanks, loop is opened up for speed
1123 while Source
(Scan_Ptr
) = ' ' loop
1124 if Source
(Scan_Ptr
+ 1) /= ' ' then
1125 Scan_Ptr
:= Scan_Ptr
+ 1;
1129 if Source
(Scan_Ptr
+ 2) /= ' ' then
1130 Scan_Ptr
:= Scan_Ptr
+ 2;
1134 if Source
(Scan_Ptr
+ 3) /= ' ' then
1135 Scan_Ptr
:= Scan_Ptr
+ 3;
1139 if Source
(Scan_Ptr
+ 4) /= ' ' then
1140 Scan_Ptr
:= Scan_Ptr
+ 4;
1144 if Source
(Scan_Ptr
+ 5) /= ' ' then
1145 Scan_Ptr
:= Scan_Ptr
+ 5;
1149 if Source
(Scan_Ptr
+ 6) /= ' ' then
1150 Scan_Ptr
:= Scan_Ptr
+ 6;
1154 if Source
(Scan_Ptr
+ 7) /= ' ' then
1155 Scan_Ptr
:= Scan_Ptr
+ 7;
1159 Scan_Ptr
:= Scan_Ptr
+ 8;
1162 -- We are now at a non-blank character, which is the first character
1163 -- of the token we will scan, and hence the value of Token_Ptr.
1165 Token_Ptr
:= Scan_Ptr
;
1167 -- Here begins the main case statement which transfers control on the
1168 -- basis of the non-blank character we have encountered.
1170 case Source
(Scan_Ptr
) is
1172 -- Line terminator characters
1174 when CR | LF | FF | VT
=>
1175 goto Scan_Line_Terminator
;
1177 -- Horizontal tab, just skip past it
1184 Scan_Ptr
:= Scan_Ptr
+ 1;
1186 -- End of file character, treated as an end of file only if it is
1187 -- the last character in the buffer, otherwise it is ignored.
1190 if Scan_Ptr
= Source_Last
(Current_Source_File
) then
1200 Scan_Ptr
:= Scan_Ptr
+ 1;
1206 Accumulate_Checksum
('&');
1208 if Source
(Scan_Ptr
+ 1) = '&' then
1209 Error_Msg_S
("'&'& should be `AND THEN`");
1210 Scan_Ptr
:= Scan_Ptr
+ 2;
1215 Scan_Ptr
:= Scan_Ptr
+ 1;
1216 Token
:= Tok_Ampersand
;
1220 -- Asterisk (can be multiplication operator or double asterisk which
1221 -- is the exponentiation compound delimiter).
1224 Accumulate_Checksum
('*');
1226 if Source
(Scan_Ptr
+ 1) = '*' then
1227 Accumulate_Checksum
('*');
1228 Scan_Ptr
:= Scan_Ptr
+ 2;
1229 Token
:= Tok_Double_Asterisk
;
1233 Scan_Ptr
:= Scan_Ptr
+ 1;
1234 Token
:= Tok_Asterisk
;
1238 -- Colon, which can either be an isolated colon, or part of an
1239 -- assignment compound delimiter.
1242 Accumulate_Checksum
(':');
1244 if Double_Char_Token
('=') then
1245 Token
:= Tok_Colon_Equal
;
1248 Style
.Check_Colon_Equal
;
1253 elsif Source
(Scan_Ptr
+ 1) = '-'
1254 and then Source
(Scan_Ptr
+ 2) /= '-'
1256 Token
:= Tok_Colon_Equal
;
1257 Error_Msg
(":- should be :=", Scan_Ptr
);
1258 Scan_Ptr
:= Scan_Ptr
+ 2;
1262 Scan_Ptr
:= Scan_Ptr
+ 1;
1275 Accumulate_Checksum
('(');
1276 Scan_Ptr
:= Scan_Ptr
+ 1;
1277 Token
:= Tok_Left_Paren
;
1280 Style
.Check_Left_Paren
;
1288 if Source
(Scan_Ptr
+ 1) = '"' then
1289 goto Scan_Wide_Character
;
1292 Error_Msg_S
("illegal character, replaced by ""(""");
1293 Scan_Ptr
:= Scan_Ptr
+ 1;
1294 Token
:= Tok_Left_Paren
;
1301 Error_Msg_S
("illegal character, replaced by ""(""");
1302 Scan_Ptr
:= Scan_Ptr
+ 1;
1303 Token
:= Tok_Left_Paren
;
1309 Accumulate_Checksum
(',');
1310 Scan_Ptr
:= Scan_Ptr
+ 1;
1319 -- Dot, which is either an isolated period, or part of a double dot
1320 -- compound delimiter sequence. We also check for the case of a
1321 -- digit following the period, to give a better error message.
1324 Accumulate_Checksum
('.');
1326 if Double_Char_Token
('.') then
1327 Token
:= Tok_Dot_Dot
;
1330 Style
.Check_Dot_Dot
;
1335 elsif Source
(Scan_Ptr
+ 1) in '0' .. '9' then
1336 Error_Msg_S
("numeric literal cannot start with point");
1337 Scan_Ptr
:= Scan_Ptr
+ 1;
1340 Scan_Ptr
:= Scan_Ptr
+ 1;
1345 -- Equal, which can either be an equality operator, or part of the
1346 -- arrow (=>) compound delimiter.
1349 Accumulate_Checksum
('=');
1351 if Double_Char_Token
('>') then
1360 elsif Source
(Scan_Ptr
+ 1) = '=' then
1361 Error_Msg_S
("== should be =");
1362 Scan_Ptr
:= Scan_Ptr
+ 1;
1365 Scan_Ptr
:= Scan_Ptr
+ 1;
1369 -- Greater than, which can be a greater than operator, greater than
1370 -- or equal operator, or first character of a right label bracket.
1373 Accumulate_Checksum
('>');
1375 if Double_Char_Token
('=') then
1376 Token
:= Tok_Greater_Equal
;
1379 elsif Double_Char_Token
('>') then
1380 Token
:= Tok_Greater_Greater
;
1384 Scan_Ptr
:= Scan_Ptr
+ 1;
1385 Token
:= Tok_Greater
;
1389 -- Less than, which can be a less than operator, less than or equal
1390 -- operator, or the first character of a left label bracket, or the
1391 -- first character of a box (<>) compound delimiter.
1394 Accumulate_Checksum
('<');
1396 if Double_Char_Token
('=') then
1397 Token
:= Tok_Less_Equal
;
1400 elsif Double_Char_Token
('>') then
1409 elsif Double_Char_Token
('<') then
1410 Token
:= Tok_Less_Less
;
1414 Scan_Ptr
:= Scan_Ptr
+ 1;
1419 -- Minus, which is either a subtraction operator, or the first
1420 -- character of double minus starting a comment
1422 when '-' => Minus_Case
: begin
1423 if Source
(Scan_Ptr
+ 1) = '>' then
1424 Error_Msg_S
("invalid token");
1425 Scan_Ptr
:= Scan_Ptr
+ 2;
1429 elsif Source
(Scan_Ptr
+ 1) /= '-' then
1430 Accumulate_Checksum
('-');
1431 Scan_Ptr
:= Scan_Ptr
+ 1;
1437 else -- Source (Scan_Ptr + 1) = '-' then
1439 Style
.Check_Comment
;
1442 Scan_Ptr
:= Scan_Ptr
+ 2;
1444 -- If we are in preprocessor mode with Replace_In_Comments set,
1445 -- then we return the "--" as a token on its own.
1447 if Replace_In_Comments
then
1448 Token
:= Tok_Comment
;
1452 -- Otherwise scan out the comment
1454 Start_Of_Comment
:= Scan_Ptr
;
1456 -- Loop to scan comment (this loop runs more than once only if
1457 -- a horizontal tab or other non-graphic character is scanned)
1460 -- Scan to non graphic character (opened up for speed)
1462 -- Note that we just eat left brackets, which means that
1463 -- bracket notation cannot be used for end of line
1464 -- characters in comments. This seems a reasonable choice,
1465 -- since no one would ever use brackets notation in a real
1466 -- program in this situation, and if we allow brackets
1467 -- notation, we forbid some valid comments which contain a
1468 -- brackets sequence that happens to match an end of line
1472 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1473 Scan_Ptr
:= Scan_Ptr
+ 1;
1474 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1475 Scan_Ptr
:= Scan_Ptr
+ 1;
1476 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1477 Scan_Ptr
:= Scan_Ptr
+ 1;
1478 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1479 Scan_Ptr
:= Scan_Ptr
+ 1;
1480 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1481 Scan_Ptr
:= Scan_Ptr
+ 1;
1484 -- Keep going if horizontal tab
1486 if Source
(Scan_Ptr
) = HT
then
1491 Scan_Ptr
:= Scan_Ptr
+ 1;
1493 -- Terminate scan of comment if line terminator
1495 elsif Source
(Scan_Ptr
) in Line_Terminator
then
1498 -- Terminate scan of comment if end of file encountered
1499 -- (embedded EOF character or real last character in file)
1501 elsif Source
(Scan_Ptr
) = EOF
then
1504 -- If we have a wide character, we have to scan it out,
1505 -- because it might be a legitimate line terminator
1507 elsif (Source
(Scan_Ptr
) = ESC
1508 and then Identifier_Char
(ESC
))
1510 (Source
(Scan_Ptr
) in Upper_Half_Character
1511 and then Upper_Half_Encoding
)
1514 Wptr
: constant Source_Ptr
:= Scan_Ptr
;
1519 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1521 -- If not well formed wide character, then just skip
1522 -- past it and ignore it.
1525 Scan_Ptr
:= Wptr
+ 1;
1527 -- If UTF_32 terminator, terminate comment scan
1529 elsif Is_UTF_32_Line_Terminator
(UTF_32
(Code
)) then
1535 -- Keep going if character in 80-FF range, or is ESC. These
1536 -- characters are allowed in comments by RM-2.1(1), 2.7(2).
1537 -- They are allowed even in Ada 83 mode according to the
1538 -- approved AI. ESC was added to the AI in June 93.
1540 elsif Source
(Scan_Ptr
) in Upper_Half_Character
1541 or else Source
(Scan_Ptr
) = ESC
1543 Scan_Ptr
:= Scan_Ptr
+ 1;
1545 -- Otherwise we have an illegal comment character
1548 Error_Illegal_Character
;
1552 -- Note that, except when comments are tokens, we do NOT
1553 -- execute a return here, instead we fall through to reexecute
1554 -- the scan loop to look for a token.
1556 if Comment_Is_Token
then
1557 Name_Len
:= Integer (Scan_Ptr
- Start_Of_Comment
);
1558 Name_Buffer
(1 .. Name_Len
) :=
1559 String (Source
(Start_Of_Comment
.. Scan_Ptr
- 1));
1560 Comment_Id
:= Name_Find
;
1561 Token
:= Tok_Comment
;
1567 -- Double quote starting a string literal
1574 -- Percent starting a string literal
1577 Obsolescent_Check
(Token_Ptr
);
1579 if Warn_On_Obsolescent_Feature
then
1581 ("use of ""'%"" is an obsolescent feature (RM J.2(4))?");
1583 ("\use """""" instead?");
1590 -- Apostrophe. This can either be the start of a character literal,
1591 -- or an isolated apostrophe used in a qualified expression or an
1592 -- attribute. We treat it as a character literal if it does not
1593 -- follow a right parenthesis, identifier, the keyword ALL or
1594 -- a literal. This means that we correctly treat constructs like:
1596 -- A := CHARACTER'('A');
1598 -- Note that RM-2.2(7) does not require a separator between
1599 -- "CHARACTER" and "'" in the above.
1601 when ''' => Char_Literal_Case
: declare
1606 Accumulate_Checksum
(''');
1607 Scan_Ptr
:= Scan_Ptr
+ 1;
1609 -- Here is where we make the test to distinguish the cases. Treat
1610 -- as apostrophe if previous token is an identifier, right paren
1611 -- or the reserved word "all" (latter case as in A.all'Address)
1612 -- (or the reserved word "project" in project files). Also treat
1613 -- it as apostrophe after a literal (this catches some legitimate
1614 -- cases, like A."abs"'Address, and also gives better error
1615 -- behavior for impossible cases like 123'xxx).
1617 if Prev_Token
= Tok_Identifier
1618 or else Prev_Token
= Tok_Right_Paren
1619 or else Prev_Token
= Tok_All
1620 or else Prev_Token
= Tok_Project
1621 or else Prev_Token
in Token_Class_Literal
1623 Token
:= Tok_Apostrophe
;
1626 Style
.Check_Apostrophe
;
1631 -- Otherwise the apostrophe starts a character literal
1634 -- Case of wide character literal
1636 if (Source
(Scan_Ptr
) = ESC
1638 Wide_Character_Encoding_Method
in WC_ESC_Encoding_Method
)
1640 (Source
(Scan_Ptr
) in Upper_Half_Character
1642 Upper_Half_Encoding
)
1644 (Source
(Scan_Ptr
) = '['
1646 Source
(Scan_Ptr
+ 1) = '"')
1649 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1650 Accumulate_Checksum
(Code
);
1653 Error_Illegal_Wide_Character
;
1654 Code
:= Character'Pos (' ');
1656 -- In Ada 95 mode we allow any wide character in a character
1657 -- literal, but in Ada 2005, the set of characters allowed
1658 -- is restricted to graphic characters.
1660 elsif Ada_Version
>= Ada_05
1661 and then Is_UTF_32_Non_Graphic
(UTF_32
(Code
))
1664 ("(Ada 2005) non-graphic character not permitted " &
1665 "in character literal", Wptr
);
1668 if Source
(Scan_Ptr
) /= ''' then
1669 Error_Msg_S
("missing apostrophe");
1671 Scan_Ptr
:= Scan_Ptr
+ 1;
1674 -- If we do not find a closing quote in the expected place then
1675 -- assume that we have a misguided attempt at a string literal.
1677 -- However, if previous token is RANGE, then we return an
1678 -- apostrophe instead since this gives better error recovery
1680 elsif Source
(Scan_Ptr
+ 1) /= ''' then
1681 if Prev_Token
= Tok_Range
then
1682 Token
:= Tok_Apostrophe
;
1686 Scan_Ptr
:= Scan_Ptr
- 1;
1688 ("strings are delimited by double quote character");
1694 -- Otherwise we have a (non-wide) character literal
1697 Accumulate_Checksum
(Source
(Scan_Ptr
));
1699 if Source
(Scan_Ptr
) not in Graphic_Character
then
1700 if Source
(Scan_Ptr
) in Upper_Half_Character
then
1701 if Ada_Version
= Ada_83
then
1702 Error_Illegal_Character
;
1706 Error_Illegal_Character
;
1710 Code
:= Get_Char_Code
(Source
(Scan_Ptr
));
1711 Scan_Ptr
:= Scan_Ptr
+ 2;
1714 -- Fall through here with Scan_Ptr updated past the closing
1715 -- quote, and Code set to the Char_Code value for the literal
1717 Accumulate_Checksum
(''');
1718 Token
:= Tok_Char_Literal
;
1719 Set_Character_Literal_Name
(Code
);
1720 Token_Name
:= Name_Find
;
1721 Character_Code
:= Code
;
1725 end Char_Literal_Case
;
1727 -- Right parenthesis
1730 Accumulate_Checksum
(')');
1731 Scan_Ptr
:= Scan_Ptr
+ 1;
1732 Token
:= Tok_Right_Paren
;
1735 Style
.Check_Right_Paren
;
1740 -- Right bracket or right brace, treated as right paren
1743 Error_Msg_S
("illegal character, replaced by "")""");
1744 Scan_Ptr
:= Scan_Ptr
+ 1;
1745 Token
:= Tok_Right_Paren
;
1748 -- Slash (can be division operator or first character of not equal)
1751 Accumulate_Checksum
('/');
1753 if Double_Char_Token
('=') then
1754 Token
:= Tok_Not_Equal
;
1757 Scan_Ptr
:= Scan_Ptr
+ 1;
1765 Accumulate_Checksum
(';');
1766 Scan_Ptr
:= Scan_Ptr
+ 1;
1767 Token
:= Tok_Semicolon
;
1770 Style
.Check_Semicolon
;
1777 when '|' => Vertical_Bar_Case
: begin
1778 Accumulate_Checksum
('|');
1780 -- Special check for || to give nice message
1782 if Source
(Scan_Ptr
+ 1) = '|' then
1783 Error_Msg_S
("""'|'|"" should be `OR ELSE`");
1784 Scan_Ptr
:= Scan_Ptr
+ 2;
1789 Scan_Ptr
:= Scan_Ptr
+ 1;
1790 Token
:= Tok_Vertical_Bar
;
1793 Style
.Check_Vertical_Bar
;
1798 end Vertical_Bar_Case
;
1800 -- Exclamation, replacement character for vertical bar
1802 when '!' => Exclamation_Case
: begin
1803 Accumulate_Checksum
('!');
1804 Obsolescent_Check
(Token_Ptr
);
1806 if Warn_On_Obsolescent_Feature
then
1808 ("use of ""'!"" is an obsolescent feature (RM J.2(2))?");
1810 ("\use ""'|"" instead?");
1813 if Source
(Scan_Ptr
+ 1) = '=' then
1814 Error_Msg_S
("'!= should be /=");
1815 Scan_Ptr
:= Scan_Ptr
+ 2;
1816 Token
:= Tok_Not_Equal
;
1820 Scan_Ptr
:= Scan_Ptr
+ 1;
1821 Token
:= Tok_Vertical_Bar
;
1824 end Exclamation_Case
;
1828 when '+' => Plus_Case
: begin
1829 Accumulate_Checksum
('+');
1830 Scan_Ptr
:= Scan_Ptr
+ 1;
1835 -- Digits starting a numeric literal
1839 -- First a bit of a scan ahead to see if we have a case of an
1840 -- identifier starting with a digit (remembering exponent case).
1843 C
: constant Character := Source
(Scan_Ptr
+ 1);
1846 -- OK literal if digit followed by digit or underscore
1848 if C
in '0' .. '9' or else C
= '_' then
1851 -- OK literal if digit not followed by identifier char
1853 elsif not Identifier_Char
(C
) then
1856 -- OK literal if digit followed by e/E followed by digit/sign.
1857 -- We also allow underscore after the E, which is an error, but
1858 -- better handled by Nlit than deciding this is an identifier.
1860 elsif (C
= 'e' or else C
= 'E')
1861 and then (Source
(Scan_Ptr
+ 2) in '0' .. '9'
1862 or else Source
(Scan_Ptr
+ 2) = '+'
1863 or else Source
(Scan_Ptr
+ 2) = '-'
1864 or else Source
(Scan_Ptr
+ 2) = '_')
1868 -- Here we have what really looks like an identifier that
1869 -- starts with a digit, so give error msg.
1872 Error_Msg_S
("identifier may not start with digit");
1874 Underline_Found
:= False;
1875 Name_Buffer
(1) := Source
(Scan_Ptr
);
1876 Accumulate_Checksum
(Name_Buffer
(1));
1877 Scan_Ptr
:= Scan_Ptr
+ 1;
1878 goto Scan_Identifier
;
1882 -- Here we have an OK integer literal
1886 if Identifier_Char
(Source
(Scan_Ptr
)) then
1888 ("delimiter required between literal and identifier");
1894 -- Lower case letters
1898 Underline_Found
:= False;
1899 Name_Buffer
(1) := Source
(Scan_Ptr
);
1900 Accumulate_Checksum
(Name_Buffer
(1));
1901 Scan_Ptr
:= Scan_Ptr
+ 1;
1902 goto Scan_Identifier
;
1904 -- Upper case letters
1908 Underline_Found
:= False;
1910 Character'Val (Character'Pos (Source
(Scan_Ptr
)) + 32);
1911 Accumulate_Checksum
(Name_Buffer
(1));
1912 Scan_Ptr
:= Scan_Ptr
+ 1;
1913 goto Scan_Identifier
;
1915 -- Underline character
1918 if Special_Characters
('_') then
1919 Token_Ptr
:= Scan_Ptr
;
1920 Scan_Ptr
:= Scan_Ptr
+ 1;
1921 Token
:= Tok_Special
;
1922 Special_Character
:= '_';
1926 Error_Msg_S
("identifier cannot start with underline");
1928 Name_Buffer
(1) := '_';
1929 Scan_Ptr
:= Scan_Ptr
+ 1;
1930 Underline_Found
:= False;
1931 goto Scan_Identifier
;
1933 -- Space (not possible, because we scanned past blanks)
1936 raise Program_Error
;
1938 -- Characters in top half of ASCII 8-bit chart
1940 when Upper_Half_Character
=>
1942 -- Wide character case
1944 if Upper_Half_Encoding
then
1945 goto Scan_Wide_Character
;
1947 -- Otherwise we have OK Latin-1 character
1950 -- Upper half characters may possibly be identifier letters
1951 -- but can never be digits, so Identifier_Char can be used to
1952 -- test for a valid start of identifier character.
1954 if Identifier_Char
(Source
(Scan_Ptr
)) then
1956 Underline_Found
:= False;
1957 goto Scan_Identifier
;
1959 Error_Illegal_Character
;
1965 -- ESC character, possible start of identifier if wide characters
1966 -- using ESC encoding are allowed in identifiers, which we can
1967 -- tell by looking at the Identifier_Char flag for ESC, which is
1968 -- only true if these conditions are met. In Ada 2005 mode, may
1969 -- also be valid UTF_32 space or line terminator character.
1971 if Identifier_Char
(ESC
) then
1973 goto Scan_Wide_Character
;
1975 Error_Illegal_Character
;
1978 -- Invalid control characters
1980 when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | ASCII
.SO |
1981 SI | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
1982 EM | FS | GS | RS | US | DEL
1984 Error_Illegal_Character
;
1986 -- Invalid graphic characters
1988 when '#' |
'$' |
'?' |
'@' |
'`' |
'\' |
'^' |
'~' =>
1990 -- If Set_Special_Character has been called for this character,
1991 -- set Scans.Special_Character and return a Special token.
1993 if Special_Characters
(Source
(Scan_Ptr
)) then
1994 Token_Ptr
:= Scan_Ptr
;
1995 Token
:= Tok_Special
;
1996 Special_Character
:= Source
(Scan_Ptr
);
1997 Scan_Ptr
:= Scan_Ptr
+ 1;
2000 -- Otherwise, this is an illegal character
2003 Error_Illegal_Character
;
2006 -- End switch on non-blank character
2010 -- End loop past format effectors. The exit from this loop is by
2011 -- executing a return statement following completion of token scan
2012 -- (control never falls out of this loop to the code which follows)
2016 -- Wide_Character scanning routine. On entry we have encountered the
2017 -- initial character of a wide character sequence.
2019 <<Scan_Wide_Character
>>
2028 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
2030 -- If bad wide character, signal error and continue scan
2033 Error_Illegal_Wide_Character
;
2034 goto Scan_Next_Character
;
2037 Cat
:= Get_Category
(UTF_32
(Code
));
2039 -- If OK letter, reset scan ptr and go scan identifier
2041 if Is_UTF_32_Letter
(Cat
) then
2044 Underline_Found
:= False;
2045 goto Scan_Identifier
;
2047 -- If OK wide space, ignore and keep scanning (we do not include
2048 -- any ignored spaces in checksum)
2050 elsif Is_UTF_32_Space
(Cat
) then
2051 goto Scan_Next_Character
;
2053 -- If OK wide line terminator, terminate current line
2055 elsif Is_UTF_32_Line_Terminator
(UTF_32
(Code
)) then
2057 goto Scan_Line_Terminator
;
2059 -- Punctuation is an error (at start of identifier)
2061 elsif Is_UTF_32_Punctuation
(Cat
) then
2063 ("identifier cannot start with punctuation", Wptr
);
2066 Underline_Found
:= False;
2067 goto Scan_Identifier
;
2069 -- Mark character is an error (at start of identifier)
2071 elsif Is_UTF_32_Mark
(Cat
) then
2073 ("identifier cannot start with mark character", Wptr
);
2076 Underline_Found
:= False;
2077 goto Scan_Identifier
;
2079 -- Other format character is an error (at start of identifier)
2081 elsif Is_UTF_32_Other
(Cat
) then
2083 ("identifier cannot start with other format character", Wptr
);
2086 Underline_Found
:= False;
2087 goto Scan_Identifier
;
2089 -- Extended digit character is an error. Could be bad start of
2090 -- identifier or bad literal. Not worth doing too much to try to
2091 -- distinguish these cases, but we will do a little bit.
2093 elsif Is_UTF_32_Digit
(Cat
) then
2095 ("identifier cannot start with digit character", Wptr
);
2098 Underline_Found
:= False;
2099 goto Scan_Identifier
;
2101 -- All other wide characters are illegal here
2104 Error_Illegal_Wide_Character
;
2105 goto Scan_Next_Character
;
2109 -- Routine to scan line terminator. On entry Scan_Ptr points to a
2110 -- character which is one of FF,LR,CR,VT, or one of the wide characters
2111 -- that is treated as a line terminator.
2113 <<Scan_Line_Terminator
>>
2115 -- Check line too long
2119 -- Set Token_Ptr, if End_Of_Line is a token, for the case when it is
2122 if End_Of_Line_Is_Token
then
2123 Token_Ptr
:= Scan_Ptr
;
2130 Skip_Line_Terminators
(Scan_Ptr
, Physical
);
2132 -- If we are at start of physical line, update scan pointers to
2133 -- reflect the start of the new line.
2136 Current_Line_Start
:= Scan_Ptr
;
2137 Start_Column
:= Set_Start_Column
;
2138 First_Non_Blank_Location
:= Scan_Ptr
;
2140 -- If End_Of_Line is a token, we return it as it is a
2143 if End_Of_Line_Is_Token
then
2144 Token
:= Tok_End_Of_Line
;
2150 goto Scan_Next_Character
;
2152 -- Identifier scanning routine. On entry, some initial characters of
2153 -- the identifier may have already been stored in Name_Buffer. If so,
2154 -- Name_Len has the number of characters stored, otherwise Name_Len is
2155 -- set to zero on entry. Underline_Found is also set False on entry.
2159 -- This loop scans as fast as possible past lower half letters and
2160 -- digits, which we expect to be the most common characters.
2163 if Source
(Scan_Ptr
) in 'a' .. 'z'
2164 or else Source
(Scan_Ptr
) in '0' .. '9'
2166 Name_Buffer
(Name_Len
+ 1) := Source
(Scan_Ptr
);
2167 Accumulate_Checksum
(Source
(Scan_Ptr
));
2169 elsif Source
(Scan_Ptr
) in 'A' .. 'Z' then
2170 Name_Buffer
(Name_Len
+ 1) :=
2171 Character'Val (Character'Pos (Source
(Scan_Ptr
)) + 32);
2172 Accumulate_Checksum
(Name_Buffer
(Name_Len
+ 1));
2178 Underline_Found
:= False;
2179 Scan_Ptr
:= Scan_Ptr
+ 1;
2180 Name_Len
:= Name_Len
+ 1;
2183 -- If we fall through, then we have encountered either an underline
2184 -- character, or an extended identifier character (i.e. one from the
2185 -- upper half), or a wide character, or an identifier terminator. The
2186 -- initial test speeds us up in the most common case where we have
2187 -- an identifier terminator. Note that ESC is an identifier character
2188 -- only if a wide character encoding method that uses ESC encoding
2189 -- is active, so if we find an ESC character we know that we have a
2192 if Identifier_Char
(Source
(Scan_Ptr
))
2193 or else (Source
(Scan_Ptr
) in Upper_Half_Character
2194 and then Upper_Half_Encoding
)
2196 -- Case of underline
2198 if Source
(Scan_Ptr
) = '_' then
2199 Accumulate_Checksum
('_');
2201 if Underline_Found
then
2202 Error_No_Double_Underline
;
2204 Underline_Found
:= True;
2205 Name_Len
:= Name_Len
+ 1;
2206 Name_Buffer
(Name_Len
) := '_';
2209 Scan_Ptr
:= Scan_Ptr
+ 1;
2210 goto Scan_Identifier
;
2212 -- Upper half character
2214 elsif Source
(Scan_Ptr
) in Upper_Half_Character
2215 and then not Upper_Half_Encoding
2217 Accumulate_Checksum
(Source
(Scan_Ptr
));
2218 Store_Encoded_Character
2219 (Get_Char_Code
(Fold_Lower
(Source
(Scan_Ptr
))));
2220 Scan_Ptr
:= Scan_Ptr
+ 1;
2221 Underline_Found
:= False;
2222 goto Scan_Identifier
;
2224 -- Left bracket not followed by a quote terminates an identifier.
2225 -- This is an error, but we don't want to give a junk error msg
2226 -- about wide characters in this case!
2228 elsif Source
(Scan_Ptr
) = '['
2229 and then Source
(Scan_Ptr
+ 1) /= '"'
2233 -- We know we have a wide character encoding here (the current
2234 -- character is either ESC, left bracket, or an upper half
2235 -- character depending on the encoding method).
2238 -- Scan out the wide character and insert the appropriate
2239 -- encoding into the name table entry for the identifier.
2249 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
2251 -- If error, signal error
2254 Error_Illegal_Wide_Character
;
2256 -- If the character scanned is a normal identifier
2257 -- character, then we treat it that way.
2259 elsif In_Character_Range
(Code
)
2260 and then Identifier_Char
(Get_Character
(Code
))
2262 Chr
:= Get_Character
(Code
);
2263 Accumulate_Checksum
(Chr
);
2264 Store_Encoded_Character
2265 (Get_Char_Code
(Fold_Lower
(Chr
)));
2266 Underline_Found
:= False;
2268 -- Here if not a normal identifier character
2271 -- Make sure we are allowing wide characters in
2272 -- identifiers. Note that we allow wide character
2273 -- notation for an OK identifier character. This in
2274 -- particular allows bracket or other notation to be
2275 -- used for upper half letters.
2277 -- Wide characters are always allowed in Ada 2005
2279 if Identifier_Character_Set
/= 'w'
2280 and then Ada_Version
< Ada_05
2283 ("wide character not allowed in identifier", Wptr
);
2286 Cat
:= Get_Category
(UTF_32
(Code
));
2288 -- If OK letter, store it folding to upper case. Note
2289 -- that we include the folded letter in the checksum.
2291 if Is_UTF_32_Letter
(Cat
) then
2293 Char_Code
(UTF_32_To_Upper_Case
(UTF_32
(Code
)));
2294 Accumulate_Checksum
(Code
);
2295 Store_Encoded_Character
(Code
);
2296 Underline_Found
:= False;
2298 -- If OK extended digit or mark, then store it
2300 elsif Is_UTF_32_Digit
(Cat
)
2301 or else Is_UTF_32_Mark
(Cat
)
2303 Accumulate_Checksum
(Code
);
2304 Store_Encoded_Character
(Code
);
2305 Underline_Found
:= False;
2307 -- Wide punctuation is also stored, but counts as an
2308 -- underline character for error checking purposes.
2310 elsif Is_UTF_32_Punctuation
(Cat
) then
2311 Accumulate_Checksum
(Code
);
2313 if Underline_Found
then
2315 Cend
: constant Source_Ptr
:= Scan_Ptr
;
2318 Error_No_Double_Underline
;
2323 Store_Encoded_Character
(Code
);
2324 Underline_Found
:= True;
2327 -- Wide character in Unicode category "Other, Format"
2328 -- is accepted in an identifier, but is ignored and not
2329 -- stored. It seems reasonable to exclude it from the
2332 -- Note that it is correct (see AI-395) to simply strip
2333 -- other format characters, before testing for double
2334 -- underlines, or for reserved words).
2336 elsif Is_UTF_32_Other
(Cat
) then
2339 -- Wide character in category Separator,Space terminates
2341 elsif Is_UTF_32_Space
(Cat
) then
2342 goto Scan_Identifier_Complete
;
2344 -- Any other wide character is not acceptable
2348 ("invalid wide character in identifier", Wptr
);
2352 goto Scan_Identifier
;
2357 -- Scan of identifier is complete. The identifier is stored in
2358 -- Name_Buffer, and Scan_Ptr points past the last character.
2360 <<Scan_Identifier_Complete
>>
2361 Token_Name
:= Name_Find
;
2363 -- Check for identifier ending with underline or punctuation char
2365 if Underline_Found
then
2366 Underline_Found
:= False;
2368 if Source
(Scan_Ptr
- 1) = '_' then
2370 ("identifier cannot end with underline", Scan_Ptr
- 1);
2373 ("identifier cannot end with punctuation character", Wptr
);
2377 -- Here is where we check if it was a keyword
2379 if Is_Keyword_Name
(Token_Name
) then
2380 Token
:= Token_Type
'Val (Get_Name_Table_Byte
(Token_Name
));
2382 -- Keyword style checks
2386 -- Deal with possible style check for non-lower case keyword,
2387 -- but we don't treat ACCESS, DELTA, DIGITS, RANGE as keywords
2388 -- for this purpose if they appear as attribute designators.
2389 -- Actually we only check the first character for speed.
2391 -- Ada 2005 (AI-284): Do not apply the style check in case of
2392 -- "pragma Interface"
2394 -- Ada 2005 (AI-340): Do not apply the style check in case of
2397 if Source
(Token_Ptr
) <= 'Z'
2398 and then (Prev_Token
/= Tok_Apostrophe
2400 (Token
/= Tok_Access
and then
2401 Token
/= Tok_Delta
and then
2402 Token
/= Tok_Digits
and then
2403 Token
/= Tok_Mod
and then
2404 Token
/= Tok_Range
))
2405 and then (Token
/= Tok_Interface
2407 (Token
= Tok_Interface
2408 and then Prev_Token
/= Tok_Pragma
))
2410 Style
.Non_Lower_Case_Keyword
;
2413 if (Token
= Tok_Then
and then Prev_Token
/= Tok_And
)
2415 (Token
= Tok_Else
and then Prev_Token
/= Tok_Or
)
2417 Style
.Check_Separate_Stmt_Lines
;
2421 -- We must reset Token_Name since this is not an identifier and
2422 -- if we leave Token_Name set, the parser gets confused because
2423 -- it thinks it is dealing with an identifier instead of the
2424 -- corresponding keyword.
2426 Token_Name
:= No_Name
;
2427 Accumulate_Token_Checksum
;
2430 -- It is an identifier after all
2433 Token
:= Tok_Identifier
;
2434 Accumulate_Token_Checksum
;
2440 --------------------------
2441 -- Set_Comment_As_Token --
2442 --------------------------
2444 procedure Set_Comment_As_Token
(Value
: Boolean) is
2446 Comment_Is_Token
:= Value
;
2447 end Set_Comment_As_Token
;
2449 ------------------------------
2450 -- Set_End_Of_Line_As_Token --
2451 ------------------------------
2453 procedure Set_End_Of_Line_As_Token
(Value
: Boolean) is
2455 End_Of_Line_Is_Token
:= Value
;
2456 end Set_End_Of_Line_As_Token
;
2458 ---------------------------
2459 -- Set_Special_Character --
2460 ---------------------------
2462 procedure Set_Special_Character
(C
: Character) is
2465 when '#' |
'$' |
'_' |
'?' |
'@' |
'`' |
'\' |
'^' |
'~' =>
2466 Special_Characters
(C
) := True;
2471 end Set_Special_Character
;
2473 ----------------------
2474 -- Set_Start_Column --
2475 ----------------------
2477 -- Note: it seems at first glance a little expensive to compute this value
2478 -- for every source line (since it is certainly not used for all source
2479 -- lines). On the other hand, it doesn't take much more work to skip past
2480 -- the initial white space on the line counting the columns than it would
2481 -- to scan past the white space using the standard scanning circuits.
2483 function Set_Start_Column
return Column_Number
is
2484 Start_Column
: Column_Number
:= 0;
2487 -- Outer loop scans past horizontal tab characters
2491 -- Inner loop scans past blanks as fast as possible, bumping Scan_Ptr
2492 -- past the blanks and adjusting Start_Column to account for them.
2495 if Source
(Scan_Ptr
) = ' ' then
2496 if Source
(Scan_Ptr
+ 1) = ' ' then
2497 if Source
(Scan_Ptr
+ 2) = ' ' then
2498 if Source
(Scan_Ptr
+ 3) = ' ' then
2499 if Source
(Scan_Ptr
+ 4) = ' ' then
2500 if Source
(Scan_Ptr
+ 5) = ' ' then
2501 if Source
(Scan_Ptr
+ 6) = ' ' then
2502 Scan_Ptr
:= Scan_Ptr
+ 7;
2503 Start_Column
:= Start_Column
+ 7;
2505 Scan_Ptr
:= Scan_Ptr
+ 6;
2506 Start_Column
:= Start_Column
+ 6;
2510 Scan_Ptr
:= Scan_Ptr
+ 5;
2511 Start_Column
:= Start_Column
+ 5;
2515 Scan_Ptr
:= Scan_Ptr
+ 4;
2516 Start_Column
:= Start_Column
+ 4;
2520 Scan_Ptr
:= Scan_Ptr
+ 3;
2521 Start_Column
:= Start_Column
+ 3;
2525 Scan_Ptr
:= Scan_Ptr
+ 2;
2526 Start_Column
:= Start_Column
+ 2;
2530 Scan_Ptr
:= Scan_Ptr
+ 1;
2531 Start_Column
:= Start_Column
+ 1;
2537 end loop Blanks_Loop
;
2539 -- Outer loop keeps going only if a horizontal tab follows
2541 if Source
(Scan_Ptr
) = HT
then
2546 Scan_Ptr
:= Scan_Ptr
+ 1;
2547 Start_Column
:= (Start_Column
/ 8) * 8 + 8;
2554 return Start_Column
;
2556 -- A constraint error can happen only if we have a compiler with checks on
2557 -- and a line with a ludicrous number of tabs or spaces at the start. In
2558 -- such a case, we really don't care if Start_Column is right or not.
2561 when Constraint_Error
=>
2562 return Start_Column
;
2563 end Set_Start_Column
;