1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Csets
; use Csets
;
28 with Err_Vars
; use Err_Vars
;
29 with Hostparm
; use Hostparm
;
30 with Namet
; use Namet
;
32 with Scans
; use Scans
;
33 with Sinput
; use Sinput
;
34 with Snames
; use Snames
;
35 with Stringt
; use Stringt
;
36 with Stylesw
; use Stylesw
;
37 with Uintp
; use Uintp
;
38 with Urealp
; use Urealp
;
39 with Widechar
; use Widechar
;
42 with System
.WCh_Con
; use System
.WCh_Con
;
44 with GNAT
.UTF_32
; use GNAT
.UTF_32
;
49 -- Make control characters visible
51 Special_Characters
: array (Character) of Boolean := (others => False);
52 -- For characters that are Special token, the value is True
54 Comment_Is_Token
: Boolean := False;
55 -- True if comments are tokens
57 End_Of_Line_Is_Token
: Boolean := False;
58 -- True if End_Of_Line is a token
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 procedure Accumulate_Token_Checksum
;
65 pragma Inline
(Accumulate_Token_Checksum
);
67 procedure Accumulate_Checksum
(C
: Character);
68 pragma Inline
(Accumulate_Checksum
);
69 -- This routine accumulates the checksum given character C. During the
70 -- scanning of a source file, this routine is called with every character
71 -- in the source, excluding blanks, and all control characters (except
72 -- that ESC is included in the checksum). Upper case letters not in string
73 -- literals are folded by the caller. See Sinput spec for the documentation
74 -- of the checksum algorithm. Note: checksum values are only used if we
75 -- generate code, so it is not necessary to worry about making the right
76 -- sequence of calls in any error situation.
78 procedure Accumulate_Checksum
(C
: Char_Code
);
79 pragma Inline
(Accumulate_Checksum
);
80 -- This version is identical, except that the argument, C, is a character
81 -- code value instead of a character. This is used when wide characters
82 -- are scanned. We use the character code rather than the ASCII characters
83 -- so that the checksum is independent of wide character encoding method.
85 procedure Initialize_Checksum
;
86 pragma Inline
(Initialize_Checksum
);
87 -- Initialize checksum value
89 -------------------------
90 -- Accumulate_Checksum --
91 -------------------------
93 procedure Accumulate_Checksum
(C
: Character) is
95 System
.CRC32
.Update
(System
.CRC32
.CRC32
(Checksum
), C
);
96 end Accumulate_Checksum
;
98 procedure Accumulate_Checksum
(C
: Char_Code
) is
101 Accumulate_Checksum
(Character'Val (C
/ 2 ** 24));
102 Accumulate_Checksum
(Character'Val ((C
/ 2 ** 16) mod 256));
103 Accumulate_Checksum
(Character'Val ((C
/ 256) mod 256));
105 Accumulate_Checksum
(Character'Val (C
/ 256));
108 Accumulate_Checksum
(Character'Val (C
mod 256));
109 end Accumulate_Checksum
;
111 -------------------------------
112 -- Accumulate_Token_Checksum --
113 -------------------------------
115 procedure Accumulate_Token_Checksum
is
118 (System
.CRC32
.CRC32
(Checksum
),
119 Character'Val (Token_Type
'Pos (Token
)));
120 end Accumulate_Token_Checksum
;
122 ----------------------------
123 -- Determine_Token_Casing --
124 ----------------------------
126 function Determine_Token_Casing
return Casing_Type
is
128 return Determine_Casing
(Source
(Token_Ptr
.. Scan_Ptr
- 1));
129 end Determine_Token_Casing
;
131 -------------------------
132 -- Initialize_Checksum --
133 -------------------------
135 procedure Initialize_Checksum
is
137 System
.CRC32
.Initialize
(System
.CRC32
.CRC32
(Checksum
));
138 end Initialize_Checksum
;
140 ------------------------
141 -- Initialize_Scanner --
142 ------------------------
144 procedure Initialize_Scanner
(Index
: Source_File_Index
) is
146 -- Establish reserved words
148 Scans
.Initialize_Ada_Keywords
;
150 -- Initialize scan control variables
152 Current_Source_File
:= Index
;
153 Source
:= Source_Text
(Current_Source_File
);
154 Scan_Ptr
:= Source_First
(Current_Source_File
);
156 Token_Ptr
:= Scan_Ptr
;
157 Current_Line_Start
:= Scan_Ptr
;
159 Token_Name
:= No_Name
;
160 Start_Column
:= Set_Start_Column
;
161 First_Non_Blank_Location
:= Scan_Ptr
;
164 Wide_Char_Byte_Count
:= 0;
166 -- Do not call Scan, otherwise the License stuff does not work in Scn
168 end Initialize_Scanner
;
170 ------------------------------
171 -- Reset_Special_Characters --
172 ------------------------------
174 procedure Reset_Special_Characters
is
176 Special_Characters
:= (others => False);
177 end Reset_Special_Characters
;
185 Start_Of_Comment
: Source_Ptr
;
186 -- Record start of comment position
188 Underline_Found
: Boolean;
189 -- During scanning of an identifier, set to True if last character
190 -- scanned was an underline or other punctuation character. This
191 -- is used to flag the error of two underlines/punctuations in a
192 -- row or ending an identifier with a underline/punctuation. Here
193 -- punctuation means any UTF_32 character in the Unicode category
194 -- Punctuation,Connector.
197 -- Used to remember start of last wide character scanned
199 procedure Check_End_Of_Line
;
200 -- Called when end of line encountered. Checks that line is not too
201 -- long, and that other style checks for the end of line are met.
203 function Double_Char_Token
(C
: Character) return Boolean;
204 -- This function is used for double character tokens like := or <>. It
205 -- checks if the character following Source (Scan_Ptr) is C, and if so
206 -- bumps Scan_Ptr past the pair of characters and returns True. A space
207 -- between the two characters is also recognized with an appropriate
208 -- error message being issued. If C is not present, False is returned.
209 -- Note that Double_Char_Token can only be used for tokens defined in
210 -- the Ada syntax (it's use for error cases like && is not appropriate
211 -- since we do not want a junk message for a case like &-space-&).
213 procedure Error_Illegal_Character
;
214 -- Give illegal character error, Scan_Ptr points to character. On
215 -- return, Scan_Ptr is bumped past the illegal character.
217 procedure Error_Illegal_Wide_Character
;
218 -- Give illegal wide character message. On return, Scan_Ptr is bumped
219 -- past the illegal character, which may still leave us pointing to
220 -- junk, not much we can do if the escape sequence is messed up!
222 procedure Error_Long_Line
;
223 -- Signal error of excessively long line
225 procedure Error_No_Double_Underline
;
226 -- Signal error of two underline or punctuation characters in a row.
227 -- Called with Scan_Ptr pointing to second underline/punctuation char.
230 -- This is the procedure for scanning out numeric literals. On entry,
231 -- Scan_Ptr points to the digit that starts the numeric literal (the
232 -- checksum for this character has not been accumulated yet). On return
233 -- Scan_Ptr points past the last character of the numeric literal, Token
234 -- and Token_Node are set appropriately, and the checksum is updated.
237 -- This is the procedure for scanning out string literals. On entry,
238 -- Scan_Ptr points to the opening string quote (the checksum for this
239 -- character has not been accumulated yet). On return Scan_Ptr points
240 -- past the closing quote of the string literal, Token and Token_Node
241 -- are set appropriately, and the checksum is upated.
243 -----------------------
244 -- Check_End_Of_Line --
245 -----------------------
247 procedure Check_End_Of_Line
is
248 Len
: constant Int
:=
250 Int
(Current_Line_Start
) -
251 Wide_Char_Byte_Count
;
255 Style
.Check_Line_Terminator
(Len
);
258 -- Deal with checking maximum line length
260 if Style_Check
and Style_Check_Max_Line_Length
then
261 Style
.Check_Line_Max_Length
(Len
);
263 -- If style checking is inactive, check maximum line length against
266 elsif Len
> Max_Line_Length
then
270 -- Reset wide character byte count for next line
272 Wide_Char_Byte_Count
:= 0;
273 end Check_End_Of_Line
;
275 -----------------------
276 -- Double_Char_Token --
277 -----------------------
279 function Double_Char_Token
(C
: Character) return Boolean is
281 if Source
(Scan_Ptr
+ 1) = C
then
282 Accumulate_Checksum
(C
);
283 Scan_Ptr
:= Scan_Ptr
+ 2;
286 elsif Source
(Scan_Ptr
+ 1) = ' '
287 and then Source
(Scan_Ptr
+ 2) = C
289 Scan_Ptr
:= Scan_Ptr
+ 1;
290 Error_Msg_S
("no space allowed here");
291 Scan_Ptr
:= Scan_Ptr
+ 2;
297 end Double_Char_Token
;
299 -----------------------------
300 -- Error_Illegal_Character --
301 -----------------------------
303 procedure Error_Illegal_Character
is
305 Error_Msg_S
("illegal character");
306 Scan_Ptr
:= Scan_Ptr
+ 1;
307 end Error_Illegal_Character
;
309 ----------------------------------
310 -- Error_Illegal_Wide_Character --
311 ----------------------------------
313 procedure Error_Illegal_Wide_Character
is
315 Error_Msg
("illegal wide character", Wptr
);
316 end Error_Illegal_Wide_Character
;
318 ---------------------
319 -- Error_Long_Line --
320 ---------------------
322 procedure Error_Long_Line
is
325 ("this line is too long",
326 Current_Line_Start
+ Source_Ptr
(Max_Line_Length
));
329 -------------------------------
330 -- Error_No_Double_Underline --
331 -------------------------------
333 procedure Error_No_Double_Underline
is
335 Underline_Found
:= False;
337 -- There are four cases, and we special case the messages
339 if Source
(Scan_Ptr
) = '_' then
340 if Source
(Scan_Ptr
- 1) = '_' then
342 ("two consecutive underlines not permitted");
345 ("underline cannot follow punctuation character");
349 if Source
(Scan_Ptr
- 1) = '_' then
351 ("punctuation character cannot follow underline");
354 ("two consecutive punctuation characters not permitted");
357 end Error_No_Double_Underline
;
366 -- Current source program character
368 Base_Char
: Character;
369 -- Either # or : (character at start of based number)
375 -- Value of base in Uint format
378 -- Value of integer scanned by Scan_Integer in Uint format
381 -- Value of integer in numeric value being scanned
384 -- Scale value for real literal
387 -- Scale in Uint format
389 Exponent_Is_Negative
: Boolean;
390 -- Set true for negative exponent
392 Extended_Digit_Value
: Int
;
393 -- Extended digit value
395 Point_Scanned
: Boolean;
396 -- Flag for decimal point scanned in numeric literal
398 -----------------------
399 -- Local Subprograms --
400 -----------------------
402 procedure Error_Digit_Expected
;
403 -- Signal error of bad digit, Scan_Ptr points to the location at
404 -- which the digit was expected on input, and is unchanged on return.
406 procedure Scan_Integer
;
407 -- Procedure to scan integer literal. On entry, Scan_Ptr points to a
408 -- digit, on exit Scan_Ptr points past the last character of the
411 -- For each digit encountered, UI_Int_Value is multiplied by 10, and
412 -- the value of the digit added to the result. In addition, the
413 -- value in Scale is decremented by one for each actual digit
416 --------------------------
417 -- Error_Digit_Expected --
418 --------------------------
420 procedure Error_Digit_Expected
is
422 Error_Msg_S
("digit expected");
423 end Error_Digit_Expected
;
429 procedure Scan_Integer
is
431 -- Next character scanned
434 C
:= Source
(Scan_Ptr
);
436 -- Loop through digits (allowing underlines)
439 Accumulate_Checksum
(C
);
441 UI_Int_Value
* 10 + (Character'Pos (C
) - Character'Pos ('0'));
442 Scan_Ptr
:= Scan_Ptr
+ 1;
444 C
:= Source
(Scan_Ptr
);
446 -- Case of underline encountered
450 -- We do not accumulate the '_' in the checksum, so that
451 -- 1_234 is equivalent to 1234, and does not trigger
452 -- compilation for "minimal recompilation" (gnatmake -m).
455 Scan_Ptr
:= Scan_Ptr
+ 1;
456 C
:= Source
(Scan_Ptr
);
458 Error_No_Double_Underline
;
461 if C
not in '0' .. '9' then
462 Error_Digit_Expected
;
467 exit when C
not in '0' .. '9';
472 -- Start of Processing for Nlit
477 UI_Int_Value
:= Uint_0
;
481 Point_Scanned
:= False;
482 UI_Num_Value
:= UI_Int_Value
;
484 -- Various possibilities now for continuing the literal are period,
485 -- E/e (for exponent), or :/# (for based literal).
488 C
:= Source
(Scan_Ptr
);
492 -- Scan out point, but do not scan past .. which is a range
493 -- sequence, and must not be eaten up scanning a numeric literal.
495 while C
= '.' and then Source
(Scan_Ptr
+ 1) /= '.' loop
496 Accumulate_Checksum
('.');
498 if Point_Scanned
then
499 Error_Msg_S
("duplicate point ignored");
502 Point_Scanned
:= True;
503 Scan_Ptr
:= Scan_Ptr
+ 1;
504 C
:= Source
(Scan_Ptr
);
506 if C
not in '0' .. '9' then
508 ("real literal cannot end with point", Scan_Ptr
- 1);
511 UI_Num_Value
:= UI_Int_Value
;
515 -- Based literal case. The base is the value we already scanned.
516 -- In the case of colon, we insist that the following character
517 -- is indeed an extended digit or a period. This catches a number
518 -- of common errors, as well as catching the well known tricky
519 -- bug otherwise arising from "x : integer range 1 .. 10:= 6;"
522 or else (C
= ':' and then
523 (Source
(Scan_Ptr
+ 1) = '.'
525 Source
(Scan_Ptr
+ 1) in '0' .. '9'
527 Source
(Scan_Ptr
+ 1) in 'A' .. 'Z'
529 Source
(Scan_Ptr
+ 1) in 'a' .. 'z'))
532 Obsolescent_Check
(Scan_Ptr
);
534 if Warn_On_Obsolescent_Feature
then
536 ("use of "":"" is an obsolescent feature ('R'M 'J.2(3))?");
538 ("\use ""'#"" instead?");
542 Accumulate_Checksum
(C
);
544 UI_Base
:= UI_Int_Value
;
546 if UI_Base
< 2 or else UI_Base
> 16 then
547 Error_Msg_SC
("base not 2-16");
551 Base
:= UI_To_Int
(UI_Base
);
552 Scan_Ptr
:= Scan_Ptr
+ 1;
554 -- Scan out extended integer [. integer]
556 C
:= Source
(Scan_Ptr
);
557 UI_Int_Value
:= Uint_0
;
561 if C
in '0' .. '9' then
562 Accumulate_Checksum
(C
);
563 Extended_Digit_Value
:=
564 Int
'(Character'Pos (C)) - Int'(Character'Pos ('0'));
566 elsif C
in 'A' .. 'F' then
567 Accumulate_Checksum
(Character'Val (Character'Pos (C
) + 32));
568 Extended_Digit_Value
:=
569 Int
'(Character'Pos (C)) - Int'(Character'Pos ('A')) + 10;
571 elsif C
in 'a' .. 'f' then
572 Accumulate_Checksum
(C
);
573 Extended_Digit_Value
:=
574 Int
'(Character'Pos (C)) - Int'(Character'Pos ('a')) + 10;
577 Error_Msg_S
("extended digit expected");
581 if Extended_Digit_Value
>= Base
then
582 Error_Msg_S
("digit '>= base");
585 UI_Int_Value
:= UI_Int_Value
* UI_Base
+ Extended_Digit_Value
;
587 Scan_Ptr
:= Scan_Ptr
+ 1;
588 C
:= Source
(Scan_Ptr
);
592 Accumulate_Checksum
('_');
593 Scan_Ptr
:= Scan_Ptr
+ 1;
594 C
:= Source
(Scan_Ptr
);
596 Error_No_Double_Underline
;
600 Accumulate_Checksum
('.');
602 if Point_Scanned
then
603 Error_Msg_S
("duplicate point ignored");
606 Scan_Ptr
:= Scan_Ptr
+ 1;
607 C
:= Source
(Scan_Ptr
);
608 Point_Scanned
:= True;
611 elsif C
= Base_Char
then
612 Accumulate_Checksum
(C
);
613 Scan_Ptr
:= Scan_Ptr
+ 1;
616 elsif C
= '#' or else C
= ':' then
617 Error_Msg_S
("based number delimiters must match");
618 Scan_Ptr
:= Scan_Ptr
+ 1;
621 elsif not Identifier_Char
(C
) then
622 if Base_Char
= '#' then
623 Error_Msg_S
("missing '#");
625 Error_Msg_S
("missing ':");
633 UI_Num_Value
:= UI_Int_Value
;
638 if not Point_Scanned
then
642 UI_Scale
:= UI_From_Int
(Scale
);
645 if Source
(Scan_Ptr
) = 'e' or else Source
(Scan_Ptr
) = 'E' then
646 Accumulate_Checksum
('e');
647 Scan_Ptr
:= Scan_Ptr
+ 1;
648 Exponent_Is_Negative
:= False;
650 if Source
(Scan_Ptr
) = '+' then
651 Accumulate_Checksum
('+');
652 Scan_Ptr
:= Scan_Ptr
+ 1;
654 elsif Source
(Scan_Ptr
) = '-' then
655 Accumulate_Checksum
('-');
657 if not Point_Scanned
then
659 ("negative exponent not allowed for integer literal");
661 Exponent_Is_Negative
:= True;
664 Scan_Ptr
:= Scan_Ptr
+ 1;
667 UI_Int_Value
:= Uint_0
;
669 if Source
(Scan_Ptr
) in '0' .. '9' then
672 Error_Digit_Expected
;
675 if Exponent_Is_Negative
then
676 UI_Scale
:= UI_Scale
- UI_Int_Value
;
678 UI_Scale
:= UI_Scale
+ UI_Int_Value
;
682 -- Case of real literal to be returned
684 if Point_Scanned
then
685 Token
:= Tok_Real_Literal
;
686 Real_Literal_Value
:=
692 -- Case of integer literal to be returned
695 Token
:= Tok_Integer_Literal
;
698 Int_Literal_Value
:= UI_Num_Value
;
700 -- Avoid doing possibly expensive calculations in cases like
701 -- parsing 163E800_000# when semantics will not be done anyway.
702 -- This is especially useful when parsing garbled input.
704 elsif Operating_Mode
/= Check_Syntax
705 and then (Serious_Errors_Detected
= 0 or else Try_Semantics
)
707 Int_Literal_Value
:= UI_Num_Value
* UI_Base
** UI_Scale
;
710 Int_Literal_Value
:= No_Uint
;
714 Accumulate_Token_Checksum
;
725 Delimiter
: Character;
726 -- Delimiter (first character of string)
729 -- Current source program character
732 -- Current character code value
735 -- Error flag for Scan_Wide call
737 procedure Error_Bad_String_Char
;
738 -- Signal bad character in string/character literal. On entry
739 -- Scan_Ptr points to the improper character encountered during the
740 -- scan. Scan_Ptr is not modified, so it still points to the bad
741 -- character on return.
743 procedure Error_Unterminated_String
;
744 -- Procedure called if a line terminator character is encountered
745 -- during scanning a string, meaning that the string is not properly
748 procedure Set_String
;
749 -- Procedure used to distinguish between string and operator symbol.
750 -- On entry the string has been scanned out, and its characters
751 -- start at Token_Ptr and end one character before Scan_Ptr. On exit
752 -- Token is set to Tok_String_Literal or Tok_Operator_Symbol as
753 -- appropriate, and Token_Node is appropriately initialized. In
754 -- addition, in the operator symbol case, Token_Name is
755 -- appropriately set.
757 ---------------------------
758 -- Error_Bad_String_Char --
759 ---------------------------
761 procedure Error_Bad_String_Char
is
762 C
: constant Character := Source
(Scan_Ptr
);
766 Error_Msg_S
("horizontal tab not allowed in string");
768 elsif C
= VT
or else C
= FF
then
769 Error_Msg_S
("format effector not allowed in string");
771 elsif C
in Upper_Half_Character
then
772 Error_Msg_S
("(Ada 83) upper half character not allowed");
775 Error_Msg_S
("control character not allowed in string");
777 end Error_Bad_String_Char
;
779 -------------------------------
780 -- Error_Unterminated_String --
781 -------------------------------
783 procedure Error_Unterminated_String
is
785 -- An interesting little refinement. Consider the following
788 -- A := "this is an unterminated string;
789 -- A := "this is an unterminated string &
790 -- P(A, "this is a parameter that didn't get terminated);
792 -- We fiddle a little to do slightly better placement in these
793 -- cases also if there is white space at the end of the line we
794 -- place the flag at the start of this white space, not at the
795 -- end. Note that we only have to test for blanks, since tabs
796 -- aren't allowed in strings in the first place and would have
797 -- caused an error message.
799 -- Two more cases that we treat specially are:
801 -- A := "this string uses the wrong terminator'
802 -- A := "this string uses the wrong terminator' &
804 -- In these cases we give a different error message as well
806 -- We actually reposition the scan pointer to the point where we
807 -- place the flag in these cases, since it seems a better bet on
808 -- the original intention.
810 while Source
(Scan_Ptr
- 1) = ' '
811 or else Source
(Scan_Ptr
- 1) = '&'
813 Scan_Ptr
:= Scan_Ptr
- 1;
817 -- Check for case of incorrect string terminator, but single quote
818 -- is not considered incorrect if the opening terminator misused
819 -- a single quote (error message already given).
822 and then Source
(Scan_Ptr
- 1) = '''
826 ("incorrect string terminator character", Scan_Ptr
- 1);
830 if Source
(Scan_Ptr
- 1) = ';' then
831 Scan_Ptr
:= Scan_Ptr
- 1;
834 if Source
(Scan_Ptr
- 1) = ')' then
835 Scan_Ptr
:= Scan_Ptr
- 1;
840 Error_Msg_S
("missing string quote");
841 end Error_Unterminated_String
;
847 procedure Set_String
is
848 Slen
: constant Int
:= Int
(Scan_Ptr
- Token_Ptr
- 2);
854 -- Token_Name is currently set to Error_Name. The following
855 -- section of code resets Token_Name to the proper Name_Op_xx
856 -- value if the string is a valid operator symbol, otherwise it is
857 -- left set to Error_Name.
860 C1
:= Source
(Token_Ptr
+ 1);
864 Token_Name
:= Name_Op_Eq
;
867 Token_Name
:= Name_Op_Gt
;
870 Token_Name
:= Name_Op_Lt
;
873 Token_Name
:= Name_Op_Add
;
876 Token_Name
:= Name_Op_Subtract
;
879 Token_Name
:= Name_Op_Concat
;
882 Token_Name
:= Name_Op_Multiply
;
885 Token_Name
:= Name_Op_Divide
;
892 C1
:= Source
(Token_Ptr
+ 1);
893 C2
:= Source
(Token_Ptr
+ 2);
895 if C1
= '*' and then C2
= '*' then
896 Token_Name
:= Name_Op_Expon
;
901 Token_Name
:= Name_Op_Ne
;
903 Token_Name
:= Name_Op_Le
;
905 Token_Name
:= Name_Op_Ge
;
908 elsif (C1
= 'O' or else C1
= 'o') and then -- OR
909 (C2
= 'R' or else C2
= 'r')
911 Token_Name
:= Name_Op_Or
;
915 C1
:= Source
(Token_Ptr
+ 1);
916 C2
:= Source
(Token_Ptr
+ 2);
917 C3
:= Source
(Token_Ptr
+ 3);
919 if (C1
= 'A' or else C1
= 'a') and then -- AND
920 (C2
= 'N' or else C2
= 'n') and then
921 (C3
= 'D' or else C3
= 'd')
923 Token_Name
:= Name_Op_And
;
925 elsif (C1
= 'A' or else C1
= 'a') and then -- ABS
926 (C2
= 'B' or else C2
= 'b') and then
927 (C3
= 'S' or else C3
= 's')
929 Token_Name
:= Name_Op_Abs
;
931 elsif (C1
= 'M' or else C1
= 'm') and then -- MOD
932 (C2
= 'O' or else C2
= 'o') and then
933 (C3
= 'D' or else C3
= 'd')
935 Token_Name
:= Name_Op_Mod
;
937 elsif (C1
= 'N' or else C1
= 'n') and then -- NOT
938 (C2
= 'O' or else C2
= 'o') and then
939 (C3
= 'T' or else C3
= 't')
941 Token_Name
:= Name_Op_Not
;
943 elsif (C1
= 'R' or else C1
= 'r') and then -- REM
944 (C2
= 'E' or else C2
= 'e') and then
945 (C3
= 'M' or else C3
= 'm')
947 Token_Name
:= Name_Op_Rem
;
949 elsif (C1
= 'X' or else C1
= 'x') and then -- XOR
950 (C2
= 'O' or else C2
= 'o') and then
951 (C3
= 'R' or else C3
= 'r')
953 Token_Name
:= Name_Op_Xor
;
958 -- If it is an operator symbol, then Token_Name is set. If it is
959 -- some other string value, then Token_Name still contains
962 if Token_Name
= Error_Name
then
963 Token
:= Tok_String_Literal
;
966 Token
:= Tok_Operator_Symbol
;
970 -- Start of processing for Slit
973 -- On entry, Scan_Ptr points to the opening character of the string
974 -- which is either a percent, double quote, or apostrophe (single
975 -- quote). The latter case is an error detected by the character
978 Delimiter
:= Source
(Scan_Ptr
);
979 Accumulate_Checksum
(Delimiter
);
981 Scan_Ptr
:= Scan_Ptr
+ 1;
983 -- Loop to scan out characters of string literal
986 C
:= Source
(Scan_Ptr
);
988 if C
= Delimiter
then
989 Accumulate_Checksum
(C
);
990 Scan_Ptr
:= Scan_Ptr
+ 1;
991 exit when Source
(Scan_Ptr
) /= Delimiter
;
992 Code
:= Get_Char_Code
(C
);
993 Accumulate_Checksum
(C
);
994 Scan_Ptr
:= Scan_Ptr
+ 1;
997 if C
= '"' and then Delimiter
= '%' then
999 ("quote not allowed in percent delimited string");
1000 Code
:= Get_Char_Code
(C
);
1001 Scan_Ptr
:= Scan_Ptr
+ 1;
1004 and then Wide_Character_Encoding_Method
1005 in WC_ESC_Encoding_Method
)
1006 or else (C
in Upper_Half_Character
1007 and then Upper_Half_Encoding
)
1009 and then Source
(Scan_Ptr
+ 1) = '"'
1010 and then Identifier_Char
(Source
(Scan_Ptr
+ 2)))
1013 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1016 Error_Illegal_Wide_Character
;
1017 Code
:= Get_Char_Code
(' ');
1020 Accumulate_Checksum
(Code
);
1022 -- In Ada 95 mode we allow any wide characters in a string
1023 -- but in Ada 2005, the set of characters allowed has been
1024 -- restricted to graphic characters.
1026 if Ada_Version
>= Ada_05
1027 and then Is_UTF_32_Non_Graphic
(UTF_32
(Code
))
1030 ("(Ada 2005) non-graphic character not permitted " &
1031 "in string literal", Wptr
);
1035 Accumulate_Checksum
(C
);
1037 if C
not in Graphic_Character
then
1038 if C
in Line_Terminator
then
1039 Error_Unterminated_String
;
1042 elsif C
in Upper_Half_Character
then
1043 if Ada_Version
= Ada_83
then
1044 Error_Bad_String_Char
;
1048 Error_Bad_String_Char
;
1052 Code
:= Get_Char_Code
(C
);
1053 Scan_Ptr
:= Scan_Ptr
+ 1;
1057 Store_String_Char
(Code
);
1059 if not In_Character_Range
(Code
) then
1060 Wide_Character_Found
:= True;
1064 String_Literal_Id
:= End_String
;
1069 -- Start of processing for Scan
1072 Prev_Token
:= Token
;
1073 Prev_Token_Ptr
:= Token_Ptr
;
1074 Token_Name
:= Error_Name
;
1076 -- The following loop runs more than once only if a format effector
1077 -- (tab, vertical tab, form feed, line feed, carriage return) is
1078 -- encountered and skipped, or some error situation, such as an
1079 -- illegal character, is encountered.
1081 <<Scan_Next_Character
>>
1084 -- Skip past blanks, loop is opened up for speed
1086 while Source
(Scan_Ptr
) = ' ' loop
1087 if Source
(Scan_Ptr
+ 1) /= ' ' then
1088 Scan_Ptr
:= Scan_Ptr
+ 1;
1092 if Source
(Scan_Ptr
+ 2) /= ' ' then
1093 Scan_Ptr
:= Scan_Ptr
+ 2;
1097 if Source
(Scan_Ptr
+ 3) /= ' ' then
1098 Scan_Ptr
:= Scan_Ptr
+ 3;
1102 if Source
(Scan_Ptr
+ 4) /= ' ' then
1103 Scan_Ptr
:= Scan_Ptr
+ 4;
1107 if Source
(Scan_Ptr
+ 5) /= ' ' then
1108 Scan_Ptr
:= Scan_Ptr
+ 5;
1112 if Source
(Scan_Ptr
+ 6) /= ' ' then
1113 Scan_Ptr
:= Scan_Ptr
+ 6;
1117 if Source
(Scan_Ptr
+ 7) /= ' ' then
1118 Scan_Ptr
:= Scan_Ptr
+ 7;
1122 Scan_Ptr
:= Scan_Ptr
+ 8;
1125 -- We are now at a non-blank character, which is the first character
1126 -- of the token we will scan, and hence the value of Token_Ptr.
1128 Token_Ptr
:= Scan_Ptr
;
1130 -- Here begins the main case statement which transfers control on the
1131 -- basis of the non-blank character we have encountered.
1133 case Source
(Scan_Ptr
) is
1135 -- Line terminator characters
1137 when CR | LF | FF | VT
=>
1138 goto Scan_Line_Terminator
;
1140 -- Horizontal tab, just skip past it
1143 if Style_Check
then Style
.Check_HT
; end if;
1144 Scan_Ptr
:= Scan_Ptr
+ 1;
1146 -- End of file character, treated as an end of file only if it is
1147 -- the last character in the buffer, otherwise it is ignored.
1150 if Scan_Ptr
= Source_Last
(Current_Source_File
) then
1152 if Style_Check
then Style
.Check_EOF
; end if;
1156 Scan_Ptr
:= Scan_Ptr
+ 1;
1162 Accumulate_Checksum
('&');
1164 if Source
(Scan_Ptr
+ 1) = '&' then
1165 Error_Msg_S
("'&'& should be `AND THEN`");
1166 Scan_Ptr
:= Scan_Ptr
+ 2;
1171 Scan_Ptr
:= Scan_Ptr
+ 1;
1172 Token
:= Tok_Ampersand
;
1176 -- Asterisk (can be multiplication operator or double asterisk which
1177 -- is the exponentiation compound delimiter).
1180 Accumulate_Checksum
('*');
1182 if Source
(Scan_Ptr
+ 1) = '*' then
1183 Accumulate_Checksum
('*');
1184 Scan_Ptr
:= Scan_Ptr
+ 2;
1185 Token
:= Tok_Double_Asterisk
;
1189 Scan_Ptr
:= Scan_Ptr
+ 1;
1190 Token
:= Tok_Asterisk
;
1194 -- Colon, which can either be an isolated colon, or part of an
1195 -- assignment compound delimiter.
1198 Accumulate_Checksum
(':');
1200 if Double_Char_Token
('=') then
1201 Token
:= Tok_Colon_Equal
;
1202 if Style_Check
then Style
.Check_Colon_Equal
; end if;
1205 elsif Source
(Scan_Ptr
+ 1) = '-'
1206 and then Source
(Scan_Ptr
+ 2) /= '-'
1208 Token
:= Tok_Colon_Equal
;
1209 Error_Msg
(":- should be :=", Scan_Ptr
);
1210 Scan_Ptr
:= Scan_Ptr
+ 2;
1214 Scan_Ptr
:= Scan_Ptr
+ 1;
1216 if Style_Check
then Style
.Check_Colon
; end if;
1223 Accumulate_Checksum
('(');
1224 Scan_Ptr
:= Scan_Ptr
+ 1;
1225 Token
:= Tok_Left_Paren
;
1226 if Style_Check
then Style
.Check_Left_Paren
; end if;
1232 if Source
(Scan_Ptr
+ 1) = '"' then
1233 goto Scan_Wide_Character
;
1236 Error_Msg_S
("illegal character, replaced by ""(""");
1237 Scan_Ptr
:= Scan_Ptr
+ 1;
1238 Token
:= Tok_Left_Paren
;
1245 Error_Msg_S
("illegal character, replaced by ""(""");
1246 Scan_Ptr
:= Scan_Ptr
+ 1;
1247 Token
:= Tok_Left_Paren
;
1253 Accumulate_Checksum
(',');
1254 Scan_Ptr
:= Scan_Ptr
+ 1;
1256 if Style_Check
then Style
.Check_Comma
; end if;
1259 -- Dot, which is either an isolated period, or part of a double dot
1260 -- compound delimiter sequence. We also check for the case of a
1261 -- digit following the period, to give a better error message.
1264 Accumulate_Checksum
('.');
1266 if Double_Char_Token
('.') then
1267 Token
:= Tok_Dot_Dot
;
1268 if Style_Check
then Style
.Check_Dot_Dot
; end if;
1271 elsif Source
(Scan_Ptr
+ 1) in '0' .. '9' then
1272 Error_Msg_S
("numeric literal cannot start with point");
1273 Scan_Ptr
:= Scan_Ptr
+ 1;
1276 Scan_Ptr
:= Scan_Ptr
+ 1;
1281 -- Equal, which can either be an equality operator, or part of the
1282 -- arrow (=>) compound delimiter.
1285 Accumulate_Checksum
('=');
1287 if Double_Char_Token
('>') then
1289 if Style_Check
then Style
.Check_Arrow
; end if;
1292 elsif Source
(Scan_Ptr
+ 1) = '=' then
1293 Error_Msg_S
("== should be =");
1294 Scan_Ptr
:= Scan_Ptr
+ 1;
1297 Scan_Ptr
:= Scan_Ptr
+ 1;
1301 -- Greater than, which can be a greater than operator, greater than
1302 -- or equal operator, or first character of a right label bracket.
1305 Accumulate_Checksum
('>');
1307 if Double_Char_Token
('=') then
1308 Token
:= Tok_Greater_Equal
;
1311 elsif Double_Char_Token
('>') then
1312 Token
:= Tok_Greater_Greater
;
1316 Scan_Ptr
:= Scan_Ptr
+ 1;
1317 Token
:= Tok_Greater
;
1321 -- Less than, which can be a less than operator, less than or equal
1322 -- operator, or the first character of a left label bracket, or the
1323 -- first character of a box (<>) compound delimiter.
1326 Accumulate_Checksum
('<');
1328 if Double_Char_Token
('=') then
1329 Token
:= Tok_Less_Equal
;
1332 elsif Double_Char_Token
('>') then
1334 if Style_Check
then Style
.Check_Box
; end if;
1337 elsif Double_Char_Token
('<') then
1338 Token
:= Tok_Less_Less
;
1342 Scan_Ptr
:= Scan_Ptr
+ 1;
1347 -- Minus, which is either a subtraction operator, or the first
1348 -- character of double minus starting a comment
1350 when '-' => Minus_Case
: begin
1351 if Source
(Scan_Ptr
+ 1) = '>' then
1352 Error_Msg_S
("invalid token");
1353 Scan_Ptr
:= Scan_Ptr
+ 2;
1357 elsif Source
(Scan_Ptr
+ 1) /= '-' then
1358 Accumulate_Checksum
('-');
1359 Scan_Ptr
:= Scan_Ptr
+ 1;
1365 else -- Source (Scan_Ptr + 1) = '-' then
1366 if Style_Check
then Style
.Check_Comment
; end if;
1367 Scan_Ptr
:= Scan_Ptr
+ 2;
1369 -- If we are in preprocessor mode with Replace_In_Comments set,
1370 -- then we return the "--" as a token on its own.
1372 if Replace_In_Comments
then
1373 Token
:= Tok_Comment
;
1377 -- Otherwise scan out the comment
1379 Start_Of_Comment
:= Scan_Ptr
;
1381 -- Loop to scan comment (this loop runs more than once only if
1382 -- a horizontal tab or other non-graphic character is scanned)
1385 -- Scan to non graphic character (opened up for speed)
1387 -- Note that we just eat left brackets, which means that
1388 -- bracket notation cannot be used for end of line
1389 -- characters in comments. This seems a reasonable choice,
1390 -- since no one would ever use brackets notation in a real
1391 -- program in this situation, and if we allow brackets
1392 -- notation, we forbid some valid comments which contain a
1393 -- brackets sequence that happens to match an end of line
1397 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1398 Scan_Ptr
:= Scan_Ptr
+ 1;
1399 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1400 Scan_Ptr
:= Scan_Ptr
+ 1;
1401 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1402 Scan_Ptr
:= Scan_Ptr
+ 1;
1403 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1404 Scan_Ptr
:= Scan_Ptr
+ 1;
1405 exit when Source
(Scan_Ptr
) not in Graphic_Character
;
1406 Scan_Ptr
:= Scan_Ptr
+ 1;
1409 -- Keep going if horizontal tab
1411 if Source
(Scan_Ptr
) = HT
then
1412 if Style_Check
then Style
.Check_HT
; end if;
1413 Scan_Ptr
:= Scan_Ptr
+ 1;
1415 -- Terminate scan of comment if line terminator
1417 elsif Source
(Scan_Ptr
) in Line_Terminator
then
1420 -- Terminate scan of comment if end of file encountered
1421 -- (embedded EOF character or real last character in file)
1423 elsif Source
(Scan_Ptr
) = EOF
then
1426 -- If we have a wide character, we have to scan it out,
1427 -- because it might be a legitimate line terminator
1429 elsif (Source
(Scan_Ptr
) = ESC
1430 and then Identifier_Char
(ESC
))
1432 (Source
(Scan_Ptr
) in Upper_Half_Character
1433 and then Upper_Half_Encoding
)
1436 Wptr
: constant Source_Ptr
:= Scan_Ptr
;
1441 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1443 -- If not well formed wide character, then just skip
1444 -- past it and ignore it.
1447 Scan_Ptr
:= Wptr
+ 1;
1449 -- If UTF_32 terminator, terminate comment scan
1451 elsif Is_UTF_32_Line_Terminator
(UTF_32
(Code
)) then
1457 -- Keep going if character in 80-FF range, or is ESC. These
1458 -- characters are allowed in comments by RM-2.1(1), 2.7(2).
1459 -- They are allowed even in Ada 83 mode according to the
1460 -- approved AI. ESC was added to the AI in June 93.
1462 elsif Source
(Scan_Ptr
) in Upper_Half_Character
1463 or else Source
(Scan_Ptr
) = ESC
1465 Scan_Ptr
:= Scan_Ptr
+ 1;
1467 -- Otherwise we have an illegal comment character
1470 Error_Illegal_Character
;
1474 -- Note that, except when comments are tokens, we do NOT
1475 -- execute a return here, instead we fall through to reexecute
1476 -- the scan loop to look for a token.
1478 if Comment_Is_Token
then
1479 Name_Len
:= Integer (Scan_Ptr
- Start_Of_Comment
);
1480 Name_Buffer
(1 .. Name_Len
) :=
1481 String (Source
(Start_Of_Comment
.. Scan_Ptr
- 1));
1482 Comment_Id
:= Name_Find
;
1483 Token
:= Tok_Comment
;
1489 -- Double quote starting a string literal
1496 -- Percent starting a string literal
1499 Obsolescent_Check
(Token_Ptr
);
1501 if Warn_On_Obsolescent_Feature
then
1503 ("use of ""'%"" is an obsolescent feature ('R'M 'J.2(4))?");
1505 ("\use """""" instead?");
1512 -- Apostrophe. This can either be the start of a character literal,
1513 -- or an isolated apostrophe used in a qualified expression or an
1514 -- attribute. We treat it as a character literal if it does not
1515 -- follow a right parenthesis, identifier, the keyword ALL or
1516 -- a literal. This means that we correctly treat constructs like:
1518 -- A := CHARACTER'('A');
1520 -- Note that RM-2.2(7) does not require a separator between
1521 -- "CHARACTER" and "'" in the above.
1523 when ''' => Char_Literal_Case
: declare
1528 Accumulate_Checksum
(''');
1529 Scan_Ptr
:= Scan_Ptr
+ 1;
1531 -- Here is where we make the test to distinguish the cases. Treat
1532 -- as apostrophe if previous token is an identifier, right paren
1533 -- or the reserved word "all" (latter case as in A.all'Address)
1534 -- (or the reserved word "project" in project files). Also treat
1535 -- it as apostrophe after a literal (this catches some legitimate
1536 -- cases, like A."abs"'Address, and also gives better error
1537 -- behavior for impossible cases like 123'xxx).
1539 if Prev_Token
= Tok_Identifier
1540 or else Prev_Token
= Tok_Right_Paren
1541 or else Prev_Token
= Tok_All
1542 or else Prev_Token
= Tok_Project
1543 or else Prev_Token
in Token_Class_Literal
1545 Token
:= Tok_Apostrophe
;
1546 if Style_Check
then Style
.Check_Apostrophe
; end if;
1549 -- Otherwise the apostrophe starts a character literal
1552 -- Case of wide character literal
1554 if (Source
(Scan_Ptr
) = ESC
1556 Wide_Character_Encoding_Method
in WC_ESC_Encoding_Method
)
1558 (Source
(Scan_Ptr
) in Upper_Half_Character
1560 Upper_Half_Encoding
)
1562 (Source
(Scan_Ptr
) = '['
1564 Source
(Scan_Ptr
+ 1) = '"')
1567 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1568 Accumulate_Checksum
(Code
);
1571 Error_Illegal_Wide_Character
;
1572 Code
:= Character'Pos (' ');
1574 -- In Ada 95 mode we allow any wide character in a character
1575 -- literal, but in Ada 2005, the set of characters allowed
1576 -- is restricted to graphic characters.
1578 elsif Ada_Version
>= Ada_05
1579 and then Is_UTF_32_Non_Graphic
(UTF_32
(Code
))
1582 ("(Ada 2005) non-graphic character not permitted " &
1583 "in character literal", Wptr
);
1586 if Source
(Scan_Ptr
) /= ''' then
1587 Error_Msg_S
("missing apostrophe");
1589 Scan_Ptr
:= Scan_Ptr
+ 1;
1592 -- If we do not find a closing quote in the expected place then
1593 -- assume that we have a misguided attempt at a string literal.
1595 -- However, if previous token is RANGE, then we return an
1596 -- apostrophe instead since this gives better error recovery
1598 elsif Source
(Scan_Ptr
+ 1) /= ''' then
1599 if Prev_Token
= Tok_Range
then
1600 Token
:= Tok_Apostrophe
;
1604 Scan_Ptr
:= Scan_Ptr
- 1;
1606 ("strings are delimited by double quote character");
1612 -- Otherwise we have a (non-wide) character literal
1615 Accumulate_Checksum
(Source
(Scan_Ptr
));
1617 if Source
(Scan_Ptr
) not in Graphic_Character
then
1618 if Source
(Scan_Ptr
) in Upper_Half_Character
then
1619 if Ada_Version
= Ada_83
then
1620 Error_Illegal_Character
;
1624 Error_Illegal_Character
;
1628 Code
:= Get_Char_Code
(Source
(Scan_Ptr
));
1629 Scan_Ptr
:= Scan_Ptr
+ 2;
1632 -- Fall through here with Scan_Ptr updated past the closing
1633 -- quote, and Code set to the Char_Code value for the literal
1635 Accumulate_Checksum
(''');
1636 Token
:= Tok_Char_Literal
;
1637 Set_Character_Literal_Name
(Code
);
1638 Token_Name
:= Name_Find
;
1639 Character_Code
:= Code
;
1643 end Char_Literal_Case
;
1645 -- Right parenthesis
1648 Accumulate_Checksum
(')');
1649 Scan_Ptr
:= Scan_Ptr
+ 1;
1650 Token
:= Tok_Right_Paren
;
1651 if Style_Check
then Style
.Check_Right_Paren
; end if;
1654 -- Right bracket or right brace, treated as right paren
1657 Error_Msg_S
("illegal character, replaced by "")""");
1658 Scan_Ptr
:= Scan_Ptr
+ 1;
1659 Token
:= Tok_Right_Paren
;
1662 -- Slash (can be division operator or first character of not equal)
1665 Accumulate_Checksum
('/');
1667 if Double_Char_Token
('=') then
1668 Token
:= Tok_Not_Equal
;
1671 Scan_Ptr
:= Scan_Ptr
+ 1;
1679 Accumulate_Checksum
(';');
1680 Scan_Ptr
:= Scan_Ptr
+ 1;
1681 Token
:= Tok_Semicolon
;
1682 if Style_Check
then Style
.Check_Semicolon
; end if;
1687 when '|' => Vertical_Bar_Case
: begin
1688 Accumulate_Checksum
('|');
1690 -- Special check for || to give nice message
1692 if Source
(Scan_Ptr
+ 1) = '|' then
1693 Error_Msg_S
("""'|'|"" should be `OR ELSE`");
1694 Scan_Ptr
:= Scan_Ptr
+ 2;
1699 Scan_Ptr
:= Scan_Ptr
+ 1;
1700 Token
:= Tok_Vertical_Bar
;
1701 if Style_Check
then Style
.Check_Vertical_Bar
; end if;
1704 end Vertical_Bar_Case
;
1706 -- Exclamation, replacement character for vertical bar
1708 when '!' => Exclamation_Case
: begin
1709 Accumulate_Checksum
('!');
1710 Obsolescent_Check
(Token_Ptr
);
1712 if Warn_On_Obsolescent_Feature
then
1714 ("use of ""'!"" is an obsolescent feature ('R'M 'J.2(2))?");
1716 ("\use ""'|"" instead?");
1719 if Source
(Scan_Ptr
+ 1) = '=' then
1720 Error_Msg_S
("'!= should be /=");
1721 Scan_Ptr
:= Scan_Ptr
+ 2;
1722 Token
:= Tok_Not_Equal
;
1726 Scan_Ptr
:= Scan_Ptr
+ 1;
1727 Token
:= Tok_Vertical_Bar
;
1730 end Exclamation_Case
;
1734 when '+' => Plus_Case
: begin
1735 Accumulate_Checksum
('+');
1736 Scan_Ptr
:= Scan_Ptr
+ 1;
1741 -- Digits starting a numeric literal
1746 if Identifier_Char
(Source
(Scan_Ptr
)) then
1748 ("delimiter required between literal and identifier");
1753 -- Lower case letters
1757 Underline_Found
:= False;
1758 Name_Buffer
(1) := Source
(Scan_Ptr
);
1759 Accumulate_Checksum
(Name_Buffer
(1));
1760 Scan_Ptr
:= Scan_Ptr
+ 1;
1761 goto Scan_Identifier
;
1763 -- Upper case letters
1767 Underline_Found
:= False;
1769 Character'Val (Character'Pos (Source
(Scan_Ptr
)) + 32);
1770 Accumulate_Checksum
(Name_Buffer
(1));
1771 Scan_Ptr
:= Scan_Ptr
+ 1;
1772 goto Scan_Identifier
;
1774 -- Underline character
1777 if Special_Characters
('_') then
1778 Token_Ptr
:= Scan_Ptr
;
1779 Scan_Ptr
:= Scan_Ptr
+ 1;
1780 Token
:= Tok_Special
;
1781 Special_Character
:= '_';
1785 Error_Msg_S
("identifier cannot start with underline");
1787 Name_Buffer
(1) := '_';
1788 Scan_Ptr
:= Scan_Ptr
+ 1;
1789 Underline_Found
:= False;
1790 goto Scan_Identifier
;
1792 -- Space (not possible, because we scanned past blanks)
1795 raise Program_Error
;
1797 -- Characters in top half of ASCII 8-bit chart
1799 when Upper_Half_Character
=>
1801 -- Wide character case
1803 if Upper_Half_Encoding
then
1804 goto Scan_Wide_Character
;
1806 -- Otherwise we have OK Latin-1 character
1809 -- Upper half characters may possibly be identifier letters
1810 -- but can never be digits, so Identifier_Char can be used to
1811 -- test for a valid start of identifier character.
1813 if Identifier_Char
(Source
(Scan_Ptr
)) then
1815 Underline_Found
:= False;
1816 goto Scan_Identifier
;
1818 Error_Illegal_Character
;
1824 -- ESC character, possible start of identifier if wide characters
1825 -- using ESC encoding are allowed in identifiers, which we can
1826 -- tell by looking at the Identifier_Char flag for ESC, which is
1827 -- only true if these conditions are met. In Ada 2005 mode, may
1828 -- also be valid UTF_32 space or line terminator character.
1830 if Identifier_Char
(ESC
) then
1832 goto Scan_Wide_Character
;
1834 Error_Illegal_Character
;
1837 -- Invalid control characters
1839 when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | ASCII
.SO |
1840 SI | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
1841 EM | FS | GS | RS | US | DEL
1843 Error_Illegal_Character
;
1845 -- Invalid graphic characters
1847 when '#' |
'$' |
'?' |
'@' |
'`' |
'\' |
'^' |
'~' =>
1849 -- If Set_Special_Character has been called for this character,
1850 -- set Scans.Special_Character and return a Special token.
1852 if Special_Characters
(Source
(Scan_Ptr
)) then
1853 Token_Ptr
:= Scan_Ptr
;
1854 Token
:= Tok_Special
;
1855 Special_Character
:= Source
(Scan_Ptr
);
1856 Scan_Ptr
:= Scan_Ptr
+ 1;
1859 -- Otherwise, this is an illegal character
1862 Error_Illegal_Character
;
1865 -- End switch on non-blank character
1869 -- End loop past format effectors. The exit from this loop is by
1870 -- executing a return statement following completion of token scan
1871 -- (control never falls out of this loop to the code which follows)
1875 -- Wide_Character scanning routine. On entry we have encountered the
1876 -- initial character of a wide character sequence.
1878 <<Scan_Wide_Character
>>
1887 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
1889 -- If bad wide character, signal error and continue scan
1892 Error_Illegal_Wide_Character
;
1893 goto Scan_Next_Character
;
1896 Cat
:= Get_Category
(UTF_32
(Code
));
1898 -- If OK letter, reset scan ptr and go scan identifier
1900 if Is_UTF_32_Letter
(Cat
) then
1903 Underline_Found
:= False;
1904 goto Scan_Identifier
;
1906 -- If OK wide space, ignore and keep scanning (we do not include
1907 -- any ignored spaces in checksum)
1909 elsif Is_UTF_32_Space
(Cat
) then
1910 goto Scan_Next_Character
;
1912 -- If OK wide line terminator, terminate current line
1914 elsif Is_UTF_32_Line_Terminator
(UTF_32
(Code
)) then
1916 goto Scan_Line_Terminator
;
1918 -- Punctuation is an error (at start of identifier)
1920 elsif Is_UTF_32_Punctuation
(Cat
) then
1922 ("identifier cannot start with punctuation", Wptr
);
1925 Underline_Found
:= False;
1926 goto Scan_Identifier
;
1928 -- Mark character is an error (at start of identifer)
1930 elsif Is_UTF_32_Mark
(Cat
) then
1932 ("identifier cannot start with mark character", Wptr
);
1935 Underline_Found
:= False;
1936 goto Scan_Identifier
;
1938 -- Other format character is an error (at start of identifer)
1940 elsif Is_UTF_32_Other
(Cat
) then
1942 ("identifier cannot start with other format character", Wptr
);
1945 Underline_Found
:= False;
1946 goto Scan_Identifier
;
1948 -- Extended digit character is an error. Could be bad start of
1949 -- identifier or bad literal. Not worth doing too much to try to
1950 -- distinguish these cases, but we will do a little bit.
1952 elsif Is_UTF_32_Digit
(Cat
) then
1954 ("identifier cannot start with digit character", Wptr
);
1957 Underline_Found
:= False;
1958 goto Scan_Identifier
;
1960 -- All other wide characters are illegal here
1963 Error_Illegal_Wide_Character
;
1964 goto Scan_Next_Character
;
1968 -- Routine to scan line terminator. On entry Scan_Ptr points to a
1969 -- character which is one of FF,LR,CR,VT, or one of the wide characters
1970 -- that is treated as a line termiantor.
1972 <<Scan_Line_Terminator
>>
1974 -- Check line too long
1978 -- Set Token_Ptr, if End_Of_Line is a token, for the case when it is
1981 if End_Of_Line_Is_Token
then
1982 Token_Ptr
:= Scan_Ptr
;
1989 Skip_Line_Terminators
(Scan_Ptr
, Physical
);
1991 -- If we are at start of physical line, update scan pointers to
1992 -- reflect the start of the new line.
1995 Current_Line_Start
:= Scan_Ptr
;
1996 Start_Column
:= Set_Start_Column
;
1997 First_Non_Blank_Location
:= Scan_Ptr
;
1999 -- If End_Of_Line is a token, we return it as it is a
2002 if End_Of_Line_Is_Token
then
2003 Token
:= Tok_End_Of_Line
;
2009 goto Scan_Next_Character
;
2011 -- Identifier scanning routine. On entry, some initial characters of
2012 -- the identifier may have already been stored in Name_Buffer. If so,
2013 -- Name_Len has the number of characters stored. otherwise Name_Len is
2014 -- set to zero on entry. Underline_Found is also set False on entry.
2018 -- This loop scans as fast as possible past lower half letters and
2019 -- digits, which we expect to be the most common characters.
2022 if Source
(Scan_Ptr
) in 'a' .. 'z'
2023 or else Source
(Scan_Ptr
) in '0' .. '9'
2025 Name_Buffer
(Name_Len
+ 1) := Source
(Scan_Ptr
);
2026 Accumulate_Checksum
(Source
(Scan_Ptr
));
2028 elsif Source
(Scan_Ptr
) in 'A' .. 'Z' then
2029 Name_Buffer
(Name_Len
+ 1) :=
2030 Character'Val (Character'Pos (Source
(Scan_Ptr
)) + 32);
2031 Accumulate_Checksum
(Name_Buffer
(Name_Len
+ 1));
2037 Underline_Found
:= False;
2038 Scan_Ptr
:= Scan_Ptr
+ 1;
2039 Name_Len
:= Name_Len
+ 1;
2042 -- If we fall through, then we have encountered either an underline
2043 -- character, or an extended identifier character (i.e. one from the
2044 -- upper half), or a wide character, or an identifier terminator. The
2045 -- initial test speeds us up in the most common case where we have
2046 -- an identifier terminator. Note that ESC is an identifier character
2047 -- only if a wide character encoding method that uses ESC encoding
2048 -- is active, so if we find an ESC character we know that we have a
2051 if Identifier_Char
(Source
(Scan_Ptr
)) then
2053 -- Case of underline
2055 if Source
(Scan_Ptr
) = '_' then
2056 Accumulate_Checksum
('_');
2058 if Underline_Found
then
2059 Error_No_Double_Underline
;
2061 Underline_Found
:= True;
2062 Name_Len
:= Name_Len
+ 1;
2063 Name_Buffer
(Name_Len
) := '_';
2066 Scan_Ptr
:= Scan_Ptr
+ 1;
2067 goto Scan_Identifier
;
2069 -- Upper half character
2071 elsif Source
(Scan_Ptr
) in Upper_Half_Character
2072 and then not Upper_Half_Encoding
2074 Accumulate_Checksum
(Source
(Scan_Ptr
));
2075 Store_Encoded_Character
2076 (Get_Char_Code
(Fold_Lower
(Source
(Scan_Ptr
))));
2077 Scan_Ptr
:= Scan_Ptr
+ 1;
2078 Underline_Found
:= False;
2079 goto Scan_Identifier
;
2081 -- Left bracket not followed by a quote terminates an identifier.
2082 -- This is an error, but we don't want to give a junk error msg
2083 -- about wide characters in this case!
2085 elsif Source
(Scan_Ptr
) = '['
2086 and then Source
(Scan_Ptr
+ 1) /= '"'
2090 -- We know we have a wide character encoding here (the current
2091 -- character is either ESC, left bracket, or an upper half
2092 -- character depending on the encoding method).
2095 -- Scan out the wide character and insert the appropriate
2096 -- encoding into the name table entry for the identifier.
2106 Scan_Wide
(Source
, Scan_Ptr
, Code
, Err
);
2108 -- If error, signal error
2111 Error_Illegal_Wide_Character
;
2113 -- If the character scanned is a normal identifier
2114 -- character, then we treat it that way.
2116 elsif In_Character_Range
(Code
)
2117 and then Identifier_Char
(Get_Character
(Code
))
2119 Chr
:= Get_Character
(Code
);
2120 Accumulate_Checksum
(Chr
);
2121 Store_Encoded_Character
2122 (Get_Char_Code
(Fold_Lower
(Chr
)));
2123 Underline_Found
:= False;
2125 -- Here if not a normal identifier character
2128 -- Make sure we are allowing wide characters in
2129 -- identifiers. Note that we allow wide character
2130 -- notation for an OK identifier character. This in
2131 -- particular allows bracket or other notation to be
2132 -- used for upper half letters.
2134 -- Wide characters are always allowed in Ada 2005
2136 if Identifier_Character_Set
/= 'w'
2137 and then Ada_Version
< Ada_05
2140 ("wide character not allowed in identifier", Wptr
);
2143 Cat
:= Get_Category
(UTF_32
(Code
));
2145 -- If OK letter, store it folding to upper case. Note
2146 -- that we include the folded letter in the checksum.
2148 if Is_UTF_32_Letter
(Cat
) then
2150 Char_Code
(UTF_32_To_Upper_Case
(UTF_32
(Code
)));
2151 Accumulate_Checksum
(Code
);
2152 Store_Encoded_Character
(Code
);
2153 Underline_Found
:= False;
2155 -- If OK extended digit or mark, then store it
2157 elsif Is_UTF_32_Digit
(Cat
)
2158 or else Is_UTF_32_Mark
(Cat
)
2160 Accumulate_Checksum
(Code
);
2161 Store_Encoded_Character
(Code
);
2162 Underline_Found
:= False;
2164 -- Wide punctuation is also stored, but counts as an
2165 -- underline character for error checking purposes.
2167 elsif Is_UTF_32_Punctuation
(Cat
) then
2168 Accumulate_Checksum
(Code
);
2170 if Underline_Found
then
2172 Cend
: constant Source_Ptr
:= Scan_Ptr
;
2175 Error_No_Double_Underline
;
2180 Store_Encoded_Character
(Code
);
2181 Underline_Found
:= True;
2184 -- Wide character in Unicode cateogory "Other, Format"
2185 -- is accepted in an identifier, but is ignored and not
2186 -- stored. It seems reasonable to exclude it from the
2189 -- Note that it is correct (see AI-395) to simply strip
2190 -- other format characters, before testing for double
2191 -- underlines, or for reserved words).
2193 elsif Is_UTF_32_Other
(Cat
) then
2196 -- Wide character in category Separator,Space terminates
2198 elsif Is_UTF_32_Space
(Cat
) then
2199 goto Scan_Identifier_Complete
;
2201 -- Any other wide character is not acceptable
2205 ("invalid wide character in identifier", Wptr
);
2209 goto Scan_Identifier
;
2214 -- Scan of identifier is complete. The identifier is stored in
2215 -- Name_Buffer, and Scan_Ptr points past the last character.
2217 <<Scan_Identifier_Complete
>>
2218 Token_Name
:= Name_Find
;
2220 -- Check for identifier ending with underline or punctuation char
2222 if Underline_Found
then
2223 Underline_Found
:= False;
2225 if Source
(Scan_Ptr
- 1) = '_' then
2227 ("identifier cannot end with underline", Scan_Ptr
- 1);
2230 ("identifier cannot end with punctuation character", Wptr
);
2234 -- Here is where we check if it was a keyword
2236 if Is_Keyword_Name
(Token_Name
) then
2237 Token
:= Token_Type
'Val (Get_Name_Table_Byte
(Token_Name
));
2239 -- Deal with possible style check for non-lower case keyword, but
2240 -- we don't treat ACCESS, DELTA, DIGITS, RANGE as keywords for
2241 -- this purpose if they appear as attribute designators. Actually
2242 -- we only check the first character for speed.
2244 -- Ada 2005 (AI-284): Do not apply the style check in case of
2245 -- "pragma Interface"
2247 -- Ada 2005 (AI-340): Do not apply the style check in case of
2251 and then Source
(Token_Ptr
) <= 'Z'
2252 and then (Prev_Token
/= Tok_Apostrophe
2254 (Token
/= Tok_Access
and then
2255 Token
/= Tok_Delta
and then
2256 Token
/= Tok_Digits
and then
2257 Token
/= Tok_Mod
and then
2258 Token
/= Tok_Range
))
2259 and then (Token
/= Tok_Interface
2261 (Token
= Tok_Interface
2262 and then Prev_Token
/= Tok_Pragma
))
2264 Style
.Non_Lower_Case_Keyword
;
2267 -- We must reset Token_Name since this is not an identifier and
2268 -- if we leave Token_Name set, the parser gets confused because
2269 -- it thinks it is dealing with an identifier instead of the
2270 -- corresponding keyword.
2272 Token_Name
:= No_Name
;
2273 Accumulate_Token_Checksum
;
2276 -- It is an identifier after all
2279 Token
:= Tok_Identifier
;
2280 Accumulate_Token_Checksum
;
2286 --------------------------
2287 -- Set_Comment_As_Token --
2288 --------------------------
2290 procedure Set_Comment_As_Token
(Value
: Boolean) is
2292 Comment_Is_Token
:= Value
;
2293 end Set_Comment_As_Token
;
2295 ------------------------------
2296 -- Set_End_Of_Line_As_Token --
2297 ------------------------------
2299 procedure Set_End_Of_Line_As_Token
(Value
: Boolean) is
2301 End_Of_Line_Is_Token
:= Value
;
2302 end Set_End_Of_Line_As_Token
;
2304 ---------------------------
2305 -- Set_Special_Character --
2306 ---------------------------
2308 procedure Set_Special_Character
(C
: Character) is
2311 when '#' |
'$' |
'_' |
'?' |
'@' |
'`' |
'\' |
'^' |
'~' =>
2312 Special_Characters
(C
) := True;
2317 end Set_Special_Character
;
2319 ----------------------
2320 -- Set_Start_Column --
2321 ----------------------
2323 -- Note: it seems at first glance a little expensive to compute this value
2324 -- for every source line (since it is certainly not used for all source
2325 -- lines). On the other hand, it doesn't take much more work to skip past
2326 -- the initial white space on the line counting the columns than it would
2327 -- to scan past the white space using the standard scanning circuits.
2329 function Set_Start_Column
return Column_Number
is
2330 Start_Column
: Column_Number
:= 0;
2333 -- Outer loop scans past horizontal tab characters
2337 -- Inner loop scans past blanks as fast as possible, bumping Scan_Ptr
2338 -- past the blanks and adjusting Start_Column to account for them.
2341 if Source
(Scan_Ptr
) = ' ' then
2342 if Source
(Scan_Ptr
+ 1) = ' ' then
2343 if Source
(Scan_Ptr
+ 2) = ' ' then
2344 if Source
(Scan_Ptr
+ 3) = ' ' then
2345 if Source
(Scan_Ptr
+ 4) = ' ' then
2346 if Source
(Scan_Ptr
+ 5) = ' ' then
2347 if Source
(Scan_Ptr
+ 6) = ' ' then
2348 Scan_Ptr
:= Scan_Ptr
+ 7;
2349 Start_Column
:= Start_Column
+ 7;
2351 Scan_Ptr
:= Scan_Ptr
+ 6;
2352 Start_Column
:= Start_Column
+ 6;
2356 Scan_Ptr
:= Scan_Ptr
+ 5;
2357 Start_Column
:= Start_Column
+ 5;
2361 Scan_Ptr
:= Scan_Ptr
+ 4;
2362 Start_Column
:= Start_Column
+ 4;
2366 Scan_Ptr
:= Scan_Ptr
+ 3;
2367 Start_Column
:= Start_Column
+ 3;
2371 Scan_Ptr
:= Scan_Ptr
+ 2;
2372 Start_Column
:= Start_Column
+ 2;
2376 Scan_Ptr
:= Scan_Ptr
+ 1;
2377 Start_Column
:= Start_Column
+ 1;
2383 end loop Blanks_Loop
;
2385 -- Outer loop keeps going only if a horizontal tab follows
2387 if Source
(Scan_Ptr
) = HT
then
2388 if Style_Check
then Style
.Check_HT
; end if;
2389 Scan_Ptr
:= Scan_Ptr
+ 1;
2390 Start_Column
:= (Start_Column
/ 8) * 8 + 8;
2397 return Start_Column
;
2398 end Set_Start_Column
;