1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 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.WCh_Con; use System.WCh_Con;
46 -- Make control characters visible
48 Special_Characters : array (Character) of Boolean := (others => False);
49 -- For characters that are Special token, the value is True
51 Comment_Is_Token : Boolean := False;
52 -- True if comments are tokens
54 End_Of_Line_Is_Token : Boolean := False;
55 -- True if End_Of_Line is a token
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Accumulate_Token_Checksum;
62 pragma Inline (Accumulate_Token_Checksum);
64 procedure Accumulate_Checksum (C : Character);
65 pragma Inline (Accumulate_Checksum);
66 -- This routine accumulates the checksum given character C. During the
67 -- scanning of a source file, this routine is called with every character
68 -- in the source, excluding blanks, and all control characters (except
69 -- that ESC is included in the checksum). Upper case letters not in string
70 -- literals are folded by the caller. See Sinput spec for the documentation
71 -- of the checksum algorithm. Note: checksum values are only used if we
72 -- generate code, so it is not necessary to worry about making the right
73 -- sequence of calls in any error situation.
75 procedure Accumulate_Checksum (C : Char_Code);
76 pragma Inline (Accumulate_Checksum);
77 -- This version is identical, except that the argument, C, is a character
78 -- code value instead of a character. This is used when wide characters
79 -- are scanned. We use the character code rather than the ASCII characters
80 -- so that the checksum is independent of wide character encoding method.
82 procedure Initialize_Checksum;
83 pragma Inline (Initialize_Checksum);
84 -- Initialize checksum value
86 -------------------------
87 -- Accumulate_Checksum --
88 -------------------------
90 procedure Accumulate_Checksum (C : Character) is
92 System.CRC32.Update (System.CRC32.CRC32 (Checksum), C);
93 end Accumulate_Checksum;
95 procedure Accumulate_Checksum (C : Char_Code) is
97 Accumulate_Checksum (Character'Val (C / 256));
98 Accumulate_Checksum (Character'Val (C mod 256));
99 end Accumulate_Checksum;
101 -------------------------------
102 -- Accumulate_Token_Checksum --
103 -------------------------------
105 procedure Accumulate_Token_Checksum is
108 (System.CRC32.CRC32 (Checksum),
109 Character'Val (Token_Type'Pos (Token)));
110 end Accumulate_Token_Checksum;
112 ----------------------------
113 -- Determine_Token_Casing --
114 ----------------------------
116 function Determine_Token_Casing return Casing_Type is
118 return Determine_Casing (Source (Token_Ptr .. Scan_Ptr - 1));
119 end Determine_Token_Casing;
121 -------------------------
122 -- Initialize_Checksum --
123 -------------------------
125 procedure Initialize_Checksum is
127 System.CRC32.Initialize (System.CRC32.CRC32 (Checksum));
128 end Initialize_Checksum;
130 ------------------------
131 -- Initialize_Scanner --
132 ------------------------
134 procedure Initialize_Scanner
135 (Unit : Unit_Number_Type;
136 Index : Source_File_Index)
139 -- Set up Token_Type values in Names Table entries for reserved keywords
140 -- We use the Pos value of the Token_Type value. Note we are relying on
141 -- the fact that Token_Type'Val (0) is not a reserved word!
143 Set_Name_Table_Byte (Name_Abort, Token_Type'Pos (Tok_Abort));
144 Set_Name_Table_Byte (Name_Abs, Token_Type'Pos (Tok_Abs));
145 Set_Name_Table_Byte (Name_Abstract, Token_Type'Pos (Tok_Abstract));
146 Set_Name_Table_Byte (Name_Accept, Token_Type'Pos (Tok_Accept));
147 Set_Name_Table_Byte (Name_Access, Token_Type'Pos (Tok_Access));
148 Set_Name_Table_Byte (Name_And, Token_Type'Pos (Tok_And));
149 Set_Name_Table_Byte (Name_Aliased, Token_Type'Pos (Tok_Aliased));
150 Set_Name_Table_Byte (Name_All, Token_Type'Pos (Tok_All));
151 Set_Name_Table_Byte (Name_Array, Token_Type'Pos (Tok_Array));
152 Set_Name_Table_Byte (Name_At, Token_Type'Pos (Tok_At));
153 Set_Name_Table_Byte (Name_Begin, Token_Type'Pos (Tok_Begin));
154 Set_Name_Table_Byte (Name_Body, Token_Type'Pos (Tok_Body));
155 Set_Name_Table_Byte (Name_Case, Token_Type'Pos (Tok_Case));
156 Set_Name_Table_Byte (Name_Constant, Token_Type'Pos (Tok_Constant));
157 Set_Name_Table_Byte (Name_Declare, Token_Type'Pos (Tok_Declare));
158 Set_Name_Table_Byte (Name_Delay, Token_Type'Pos (Tok_Delay));
159 Set_Name_Table_Byte (Name_Delta, Token_Type'Pos (Tok_Delta));
160 Set_Name_Table_Byte (Name_Digits, Token_Type'Pos (Tok_Digits));
161 Set_Name_Table_Byte (Name_Do, Token_Type'Pos (Tok_Do));
162 Set_Name_Table_Byte (Name_Else, Token_Type'Pos (Tok_Else));
163 Set_Name_Table_Byte (Name_Elsif, Token_Type'Pos (Tok_Elsif));
164 Set_Name_Table_Byte (Name_End, Token_Type'Pos (Tok_End));
165 Set_Name_Table_Byte (Name_Entry, Token_Type'Pos (Tok_Entry));
166 Set_Name_Table_Byte (Name_Exception, Token_Type'Pos (Tok_Exception));
167 Set_Name_Table_Byte (Name_Exit, Token_Type'Pos (Tok_Exit));
168 Set_Name_Table_Byte (Name_For, Token_Type'Pos (Tok_For));
169 Set_Name_Table_Byte (Name_Function, Token_Type'Pos (Tok_Function));
170 Set_Name_Table_Byte (Name_Generic, Token_Type'Pos (Tok_Generic));
171 Set_Name_Table_Byte (Name_Goto, Token_Type'Pos (Tok_Goto));
172 Set_Name_Table_Byte (Name_If, Token_Type'Pos (Tok_If));
173 Set_Name_Table_Byte (Name_In, Token_Type'Pos (Tok_In));
174 Set_Name_Table_Byte (Name_Is, Token_Type'Pos (Tok_Is));
175 Set_Name_Table_Byte (Name_Limited, Token_Type'Pos (Tok_Limited));
176 Set_Name_Table_Byte (Name_Loop, Token_Type'Pos (Tok_Loop));
177 Set_Name_Table_Byte (Name_Mod, Token_Type'Pos (Tok_Mod));
178 Set_Name_Table_Byte (Name_New, Token_Type'Pos (Tok_New));
179 Set_Name_Table_Byte (Name_Not, Token_Type'Pos (Tok_Not));
180 Set_Name_Table_Byte (Name_Null, Token_Type'Pos (Tok_Null));
181 Set_Name_Table_Byte (Name_Of, Token_Type'Pos (Tok_Of));
182 Set_Name_Table_Byte (Name_Or, Token_Type'Pos (Tok_Or));
183 Set_Name_Table_Byte (Name_Others, Token_Type'Pos (Tok_Others));
184 Set_Name_Table_Byte (Name_Out, Token_Type'Pos (Tok_Out));
185 Set_Name_Table_Byte (Name_Package, Token_Type'Pos (Tok_Package));
186 Set_Name_Table_Byte (Name_Pragma, Token_Type'Pos (Tok_Pragma));
187 Set_Name_Table_Byte (Name_Private, Token_Type'Pos (Tok_Private));
188 Set_Name_Table_Byte (Name_Procedure, Token_Type'Pos (Tok_Procedure));
189 Set_Name_Table_Byte (Name_Protected, Token_Type'Pos (Tok_Protected));
190 Set_Name_Table_Byte (Name_Raise, Token_Type'Pos (Tok_Raise));
191 Set_Name_Table_Byte (Name_Range, Token_Type'Pos (Tok_Range));
192 Set_Name_Table_Byte (Name_Record, Token_Type'Pos (Tok_Record));
193 Set_Name_Table_Byte (Name_Rem, Token_Type'Pos (Tok_Rem));
194 Set_Name_Table_Byte (Name_Renames, Token_Type'Pos (Tok_Renames));
195 Set_Name_Table_Byte (Name_Requeue, Token_Type'Pos (Tok_Requeue));
196 Set_Name_Table_Byte (Name_Return, Token_Type'Pos (Tok_Return));
197 Set_Name_Table_Byte (Name_Reverse, Token_Type'Pos (Tok_Reverse));
198 Set_Name_Table_Byte (Name_Select, Token_Type'Pos (Tok_Select));
199 Set_Name_Table_Byte (Name_Separate, Token_Type'Pos (Tok_Separate));
200 Set_Name_Table_Byte (Name_Subtype, Token_Type'Pos (Tok_Subtype));
201 Set_Name_Table_Byte (Name_Tagged, Token_Type'Pos (Tok_Tagged));
202 Set_Name_Table_Byte (Name_Task, Token_Type'Pos (Tok_Task));
203 Set_Name_Table_Byte (Name_Terminate, Token_Type'Pos (Tok_Terminate));
204 Set_Name_Table_Byte (Name_Then, Token_Type'Pos (Tok_Then));
205 Set_Name_Table_Byte (Name_Type, Token_Type'Pos (Tok_Type));
206 Set_Name_Table_Byte (Name_Until, Token_Type'Pos (Tok_Until));
207 Set_Name_Table_Byte (Name_Use, Token_Type'Pos (Tok_Use));
208 Set_Name_Table_Byte (Name_When, Token_Type'Pos (Tok_When));
209 Set_Name_Table_Byte (Name_While, Token_Type'Pos (Tok_While));
210 Set_Name_Table_Byte (Name_With, Token_Type'Pos (Tok_With));
211 Set_Name_Table_Byte (Name_Xor, Token_Type'Pos (Tok_Xor));
213 -- Initialize scan control variables
215 Current_Source_File := Index;
216 Source := Source_Text (Current_Source_File);
217 Current_Source_Unit := Unit;
218 Scan_Ptr := Source_First (Current_Source_File);
220 Token_Ptr := Scan_Ptr;
221 Current_Line_Start := Scan_Ptr;
223 Token_Name := No_Name;
224 Start_Column := Set_Start_Column;
225 First_Non_Blank_Location := Scan_Ptr;
229 -- Do not call Scan, otherwise the License stuff does not work in Scn.
231 end Initialize_Scanner;
233 ------------------------------
234 -- Reset_Special_Characters --
235 ------------------------------
237 procedure Reset_Special_Characters is
239 Special_Characters := (others => False);
240 end Reset_Special_Characters;
248 Start_Of_Comment : Source_Ptr;
250 procedure Check_End_Of_Line;
251 -- Called when end of line encountered. Checks that line is not
252 -- too long, and that other style checks for the end of line are met.
254 function Double_Char_Token (C : Character) return Boolean;
255 -- This function is used for double character tokens like := or <>. It
256 -- checks if the character following Source (Scan_Ptr) is C, and if so
257 -- bumps Scan_Ptr past the pair of characters and returns True. A space
258 -- between the two characters is also recognized with an appropriate
259 -- error message being issued. If C is not present, False is returned.
260 -- Note that Double_Char_Token can only be used for tokens defined in
261 -- the Ada syntax (it's use for error cases like && is not appropriate
262 -- since we do not want a junk message for a case like &-space-&).
264 procedure Error_Illegal_Character;
265 -- Give illegal character error, Scan_Ptr points to character.
266 -- On return, Scan_Ptr is bumped past the illegal character.
268 procedure Error_Illegal_Wide_Character;
269 -- Give illegal wide character message. On return, Scan_Ptr is bumped
270 -- past the illegal character, which may still leave us pointing to
271 -- junk, not much we can do if the escape sequence is messed up!
273 procedure Error_Long_Line;
274 -- Signal error of excessively long line
276 procedure Error_No_Double_Underline;
277 -- Signal error of double underline character
280 -- This is the procedure for scanning out numeric literals. On entry,
281 -- Scan_Ptr points to the digit that starts the numeric literal (the
282 -- checksum for this character has not been accumulated yet). On return
283 -- Scan_Ptr points past the last character of the numeric literal, Token
284 -- and Token_Node are set appropriately, and the checksum is updated.
287 -- This is the procedure for scanning out string literals. On entry,
288 -- Scan_Ptr points to the opening string quote (the checksum for this
289 -- character has not been accumulated yet). On return Scan_Ptr points
290 -- past the closing quote of the string literal, Token and Token_Node
291 -- are set appropriately, and the checksum is upated.
293 -----------------------
294 -- Check_End_Of_Line --
295 -----------------------
297 procedure Check_End_Of_Line is
298 Len : constant Int := Int (Scan_Ptr) - Int (Current_Line_Start);
301 if Style_Check and Style_Check_Max_Line_Length then
302 Style.Check_Line_Terminator (Len);
304 -- If style checking is inactive, check maximum line length against
305 -- standard value. Note that we take this from Opt.Max_Line_Length
306 -- rather than Hostparm.Max_Line_Length because we do not want to
307 -- impose any limit during scanning of configuration pragma files,
308 -- and Opt.Max_Line_Length (normally set to Hostparm.Max_Line_Length)
309 -- is reset to Column_Number'Max during scanning of such files.
311 elsif Len > Opt.Max_Line_Length then
314 end Check_End_Of_Line;
316 -----------------------
317 -- Double_Char_Token --
318 -----------------------
320 function Double_Char_Token (C : Character) return Boolean is
322 if Source (Scan_Ptr + 1) = C then
323 Accumulate_Checksum (C);
324 Scan_Ptr := Scan_Ptr + 2;
327 elsif Source (Scan_Ptr + 1) = ' '
328 and then Source (Scan_Ptr + 2) = C
330 Scan_Ptr := Scan_Ptr + 1;
331 Error_Msg_S ("no space allowed here");
332 Scan_Ptr := Scan_Ptr + 2;
338 end Double_Char_Token;
340 -----------------------------
341 -- Error_Illegal_Character --
342 -----------------------------
344 procedure Error_Illegal_Character is
346 Error_Msg_S ("illegal character");
347 Scan_Ptr := Scan_Ptr + 1;
348 end Error_Illegal_Character;
350 ----------------------------------
351 -- Error_Illegal_Wide_Character --
352 ----------------------------------
354 procedure Error_Illegal_Wide_Character is
356 Error_Msg_S ("illegal wide character, check -gnatW switch");
357 Scan_Ptr := Scan_Ptr + 1;
358 end Error_Illegal_Wide_Character;
360 ---------------------
361 -- Error_Long_Line --
362 ---------------------
364 procedure Error_Long_Line is
367 ("this line is too long",
368 Current_Line_Start + Source_Ptr (Opt.Max_Line_Length));
371 -------------------------------
372 -- Error_No_Double_Underline --
373 -------------------------------
375 procedure Error_No_Double_Underline is
377 Error_Msg_S ("two consecutive underlines not permitted");
378 end Error_No_Double_Underline;
387 -- Current source program character
389 Base_Char : Character;
390 -- Either # or : (character at start of based number)
396 -- Value of base in Uint format
399 -- Value of integer scanned by Scan_Integer in Uint format
402 -- Value of integer in numeric value being scanned
405 -- Scale value for real literal
408 -- Scale in Uint format
410 Exponent_Is_Negative : Boolean;
411 -- Set true for negative exponent
413 Extended_Digit_Value : Int;
414 -- Extended digit value
416 Point_Scanned : Boolean;
417 -- Flag for decimal point scanned in numeric literal
419 -----------------------
420 -- Local Subprograms --
421 -----------------------
423 procedure Error_Digit_Expected;
424 -- Signal error of bad digit, Scan_Ptr points to the location at
425 -- which the digit was expected on input, and is unchanged on return.
427 procedure Scan_Integer;
428 -- Procedure to scan integer literal. On entry, Scan_Ptr points to
429 -- a digit, on exit Scan_Ptr points past the last character of
432 -- For each digit encountered, UI_Int_Value is multiplied by 10,
433 -- and the value of the digit added to the result. In addition,
434 -- the value in Scale is decremented by one for each actual digit
437 --------------------------
438 -- Error_Digit_Expected --
439 --------------------------
441 procedure Error_Digit_Expected is
443 Error_Msg_S ("digit expected");
444 end Error_Digit_Expected;
450 procedure Scan_Integer is
452 -- Next character scanned
455 C := Source (Scan_Ptr);
457 -- Loop through digits (allowing underlines)
460 Accumulate_Checksum (C);
462 UI_Int_Value * 10 + (Character'Pos (C) - Character'Pos ('0'));
463 Scan_Ptr := Scan_Ptr + 1;
465 C := Source (Scan_Ptr);
469 -- We do not accumulate the '_' in the checksum, so that
470 -- 1_234 is equivalent to 1234, and does not trigger
471 -- compilation for "minimal recompilation" (gnatmake -m).
474 Scan_Ptr := Scan_Ptr + 1;
475 C := Source (Scan_Ptr);
477 Error_No_Double_Underline;
480 if C not in '0' .. '9' then
481 Error_Digit_Expected;
486 exit when C not in '0' .. '9';
492 ----------------------------------
493 -- Start of Processing for Nlit --
494 ----------------------------------
499 UI_Int_Value := Uint_0;
503 Point_Scanned := False;
504 UI_Num_Value := UI_Int_Value;
506 -- Various possibilities now for continuing the literal are
507 -- period, E/e (for exponent), or :/# (for based literal).
510 C := Source (Scan_Ptr);
514 -- Scan out point, but do not scan past .. which is a range
515 -- sequence, and must not be eaten up scanning a numeric literal.
517 while C = '.' and then Source (Scan_Ptr + 1) /= '.' loop
518 Accumulate_Checksum ('.');
520 if Point_Scanned then
521 Error_Msg_S ("duplicate point ignored");
524 Point_Scanned := True;
525 Scan_Ptr := Scan_Ptr + 1;
526 C := Source (Scan_Ptr);
528 if C not in '0' .. '9' then
530 ("real literal cannot end with point", Scan_Ptr - 1);
533 UI_Num_Value := UI_Int_Value;
537 -- Based literal case. The base is the value we already scanned.
538 -- In the case of colon, we insist that the following character
539 -- is indeed an extended digit or a period. This catches a number
540 -- of common errors, as well as catching the well known tricky
541 -- bug otherwise arising from "x : integer range 1 .. 10:= 6;"
544 or else (C = ':' and then
545 (Source (Scan_Ptr + 1) = '.'
547 Source (Scan_Ptr + 1) in '0' .. '9'
549 Source (Scan_Ptr + 1) in 'A' .. 'Z'
551 Source (Scan_Ptr + 1) in 'a' .. 'z'))
553 if C = ':' and then Warn_On_Obsolescent_Feature then
555 ("use of "":"" is an obsolescent feature ('R'M 'J.2(3))?");
557 ("\use ""'#"" instead?");
560 Accumulate_Checksum (C);
562 UI_Base := UI_Int_Value;
564 if UI_Base < 2 or else UI_Base > 16 then
565 Error_Msg_SC ("base not 2-16");
569 Base := UI_To_Int (UI_Base);
570 Scan_Ptr := Scan_Ptr + 1;
572 -- Scan out extended integer [. integer]
574 C := Source (Scan_Ptr);
575 UI_Int_Value := Uint_0;
579 if C in '0' .. '9' then
580 Accumulate_Checksum (C);
581 Extended_Digit_Value :=
582 Int'(Character'Pos (C)) - Int'(Character'Pos ('0'));
584 elsif C in 'A' .. 'F' then
585 Accumulate_Checksum (Character'Val (Character'Pos (C) + 32));
586 Extended_Digit_Value :=
587 Int'(Character'Pos (C)) - Int'(Character'Pos ('A')) + 10;
589 elsif C in 'a' .. 'f' then
590 Accumulate_Checksum (C);
591 Extended_Digit_Value :=
592 Int'(Character'Pos (C)) - Int'(Character'Pos ('a')) + 10;
595 Error_Msg_S ("extended digit expected");
599 if Extended_Digit_Value >= Base then
600 Error_Msg_S ("digit '>= base");
603 UI_Int_Value := UI_Int_Value * UI_Base + Extended_Digit_Value;
605 Scan_Ptr := Scan_Ptr + 1;
606 C := Source (Scan_Ptr);
610 Accumulate_Checksum ('_');
611 Scan_Ptr := Scan_Ptr + 1;
612 C := Source (Scan_Ptr);
614 Error_No_Double_Underline;
618 Accumulate_Checksum ('.');
620 if Point_Scanned then
621 Error_Msg_S ("duplicate point ignored");
624 Scan_Ptr := Scan_Ptr + 1;
625 C := Source (Scan_Ptr);
626 Point_Scanned := True;
629 elsif C = Base_Char then
630 Accumulate_Checksum (C);
631 Scan_Ptr := Scan_Ptr + 1;
634 elsif C = '#' or else C = ':' then
635 Error_Msg_S ("based number delimiters must match");
636 Scan_Ptr := Scan_Ptr + 1;
639 elsif not Identifier_Char (C) then
640 if Base_Char = '#' then
641 Error_Msg_S ("missing '#");
643 Error_Msg_S ("missing ':");
651 UI_Num_Value := UI_Int_Value;
656 if not Point_Scanned then
660 UI_Scale := UI_From_Int (Scale);
663 if Source (Scan_Ptr) = 'e' or else Source (Scan_Ptr) = 'E' then
664 Accumulate_Checksum ('e');
665 Scan_Ptr := Scan_Ptr + 1;
666 Exponent_Is_Negative := False;
668 if Source (Scan_Ptr) = '+' then
669 Accumulate_Checksum ('+');
670 Scan_Ptr := Scan_Ptr + 1;
672 elsif Source (Scan_Ptr) = '-' then
673 Accumulate_Checksum ('-');
675 if not Point_Scanned then
677 ("negative exponent not allowed for integer literal");
679 Exponent_Is_Negative := True;
682 Scan_Ptr := Scan_Ptr + 1;
685 UI_Int_Value := Uint_0;
687 if Source (Scan_Ptr) in '0' .. '9' then
690 Error_Digit_Expected;
693 if Exponent_Is_Negative then
694 UI_Scale := UI_Scale - UI_Int_Value;
696 UI_Scale := UI_Scale + UI_Int_Value;
700 -- Case of real literal to be returned
702 if Point_Scanned then
703 Token := Tok_Real_Literal;
704 Real_Literal_Value :=
710 -- Case of integer literal to be returned
713 Token := Tok_Integer_Literal;
716 Int_Literal_Value := UI_Num_Value;
718 -- Avoid doing possibly expensive calculations in cases like
719 -- parsing 163E800_000# when semantics will not be done anyway.
720 -- This is especially useful when parsing garbled input.
722 elsif Operating_Mode /= Check_Syntax
723 and then (Serious_Errors_Detected = 0 or else Try_Semantics)
725 Int_Literal_Value := UI_Num_Value * UI_Base ** UI_Scale;
728 Int_Literal_Value := No_Uint;
734 Accumulate_Token_Checksum;
746 Delimiter : Character;
747 -- Delimiter (first character of string)
750 -- Current source program character
753 -- Current character code value
756 -- Error flag for Scan_Wide call
758 procedure Error_Bad_String_Char;
759 -- Signal bad character in string/character literal. On entry
760 -- Scan_Ptr points to the improper character encountered during
761 -- the scan. Scan_Ptr is not modified, so it still points to the bad
762 -- character on return.
764 procedure Error_Unterminated_String;
765 -- Procedure called if a line terminator character is encountered
766 -- during scanning a string, meaning that the string is not properly
769 procedure Set_String;
770 -- Procedure used to distinguish between string and operator symbol.
771 -- On entry the string has been scanned out, and its characters start
772 -- at Token_Ptr and end one character before Scan_Ptr. On exit Token
773 -- is set to Tok_String_Literal or Tok_Operator_Symbol as
774 -- appropriate, and Token_Node is appropriately initialized.
775 -- In addition, in the operator symbol case, Token_Name is
776 -- appropriately set.
778 ---------------------------
779 -- Error_Bad_String_Char --
780 ---------------------------
782 procedure Error_Bad_String_Char is
783 C : constant Character := Source (Scan_Ptr);
787 Error_Msg_S ("horizontal tab not allowed in string");
789 elsif C = VT or else C = FF then
790 Error_Msg_S ("format effector not allowed in string");
792 elsif C in Upper_Half_Character then
793 Error_Msg_S ("(Ada 83) upper half character not allowed");
796 Error_Msg_S ("control character not allowed in string");
798 end Error_Bad_String_Char;
800 -------------------------------
801 -- Error_Unterminated_String --
802 -------------------------------
804 procedure Error_Unterminated_String is
806 -- An interesting little refinement. Consider the following
809 -- A := "this is an unterminated string;
810 -- A := "this is an unterminated string &
811 -- P(A, "this is a parameter that didn't get terminated);
813 -- We fiddle a little to do slightly better placement in these
814 -- cases also if there is white space at the end of the line we
815 -- place the flag at the start of this white space, not at the
816 -- end. Note that we only have to test for blanks, since tabs
817 -- aren't allowed in strings in the first place and would have
818 -- caused an error message.
820 -- Two more cases that we treat specially are:
822 -- A := "this string uses the wrong terminator'
823 -- A := "this string uses the wrong terminator' &
825 -- In these cases we give a different error message as well
827 -- We actually reposition the scan pointer to the point where we
828 -- place the flag in these cases, since it seems a better bet on
829 -- the original intention.
831 while Source (Scan_Ptr - 1) = ' '
832 or else Source (Scan_Ptr - 1) = '&'
834 Scan_Ptr := Scan_Ptr - 1;
838 -- Check for case of incorrect string terminator, but single quote
839 -- is not considered incorrect if the opening terminator misused
840 -- a single quote (error message already given).
843 and then Source (Scan_Ptr - 1) = '''
847 ("incorrect string terminator character", Scan_Ptr - 1);
851 if Source (Scan_Ptr - 1) = ';' then
852 Scan_Ptr := Scan_Ptr - 1;
855 if Source (Scan_Ptr - 1) = ')' then
856 Scan_Ptr := Scan_Ptr - 1;
861 Error_Msg_S ("missing string quote");
862 end Error_Unterminated_String;
868 procedure Set_String is
869 Slen : constant Int := Int (Scan_Ptr - Token_Ptr - 2);
875 -- Token_Name is currently set to Error_Name. The following
876 -- section of code resets Token_Name to the proper Name_Op_xx
877 -- value if the string is a valid operator symbol, otherwise it is
878 -- left set to Error_Name.
881 C1 := Source (Token_Ptr + 1);
885 Token_Name := Name_Op_Eq;
888 Token_Name := Name_Op_Gt;
891 Token_Name := Name_Op_Lt;
894 Token_Name := Name_Op_Add;
897 Token_Name := Name_Op_Subtract;
900 Token_Name := Name_Op_Concat;
903 Token_Name := Name_Op_Multiply;
906 Token_Name := Name_Op_Divide;
913 C1 := Source (Token_Ptr + 1);
914 C2 := Source (Token_Ptr + 2);
916 if C1 = '*' and then C2 = '*' then
917 Token_Name := Name_Op_Expon;
922 Token_Name := Name_Op_Ne;
924 Token_Name := Name_Op_Le;
926 Token_Name := Name_Op_Ge;
929 elsif (C1 = 'O' or else C1 = 'o') and then -- OR
930 (C2 = 'R' or else C2 = 'r')
932 Token_Name := Name_Op_Or;
936 C1 := Source (Token_Ptr + 1);
937 C2 := Source (Token_Ptr + 2);
938 C3 := Source (Token_Ptr + 3);
940 if (C1 = 'A' or else C1 = 'a') and then -- AND
941 (C2 = 'N' or else C2 = 'n') and then
942 (C3 = 'D' or else C3 = 'd')
944 Token_Name := Name_Op_And;
946 elsif (C1 = 'A' or else C1 = 'a') and then -- ABS
947 (C2 = 'B' or else C2 = 'b') and then
948 (C3 = 'S' or else C3 = 's')
950 Token_Name := Name_Op_Abs;
952 elsif (C1 = 'M' or else C1 = 'm') and then -- MOD
953 (C2 = 'O' or else C2 = 'o') and then
954 (C3 = 'D' or else C3 = 'd')
956 Token_Name := Name_Op_Mod;
958 elsif (C1 = 'N' or else C1 = 'n') and then -- NOT
959 (C2 = 'O' or else C2 = 'o') and then
960 (C3 = 'T' or else C3 = 't')
962 Token_Name := Name_Op_Not;
964 elsif (C1 = 'R' or else C1 = 'r') and then -- REM
965 (C2 = 'E' or else C2 = 'e') and then
966 (C3 = 'M' or else C3 = 'm')
968 Token_Name := Name_Op_Rem;
970 elsif (C1 = 'X' or else C1 = 'x') and then -- XOR
971 (C2 = 'O' or else C2 = 'o') and then
972 (C3 = 'R' or else C3 = 'r')
974 Token_Name := Name_Op_Xor;
979 -- If it is an operator symbol, then Token_Name is set.
980 -- If it is some other string value, then Token_Name still
981 -- contains Error_Name.
983 if Token_Name = Error_Name then
984 Token := Tok_String_Literal;
987 Token := Tok_Operator_Symbol;
997 -- On entry, Scan_Ptr points to the opening character of the string
998 -- which is either a percent, double quote, or apostrophe
999 -- (single quote). The latter case is an error detected by
1000 -- the character literal circuit.
1002 Delimiter := Source (Scan_Ptr);
1003 Accumulate_Checksum (Delimiter);
1005 Scan_Ptr := Scan_Ptr + 1;
1007 -- Loop to scan out characters of string literal
1010 C := Source (Scan_Ptr);
1012 if C = Delimiter then
1013 Accumulate_Checksum (C);
1014 Scan_Ptr := Scan_Ptr + 1;
1015 exit when Source (Scan_Ptr) /= Delimiter;
1016 Code := Get_Char_Code (C);
1017 Accumulate_Checksum (C);
1018 Scan_Ptr := Scan_Ptr + 1;
1021 if C = '"' and then Delimiter = '%' then
1023 ("quote not allowed in percent delimited string");
1024 Code := Get_Char_Code (C);
1025 Scan_Ptr := Scan_Ptr + 1;
1029 Wide_Character_Encoding_Method
1030 in WC_ESC_Encoding_Method)
1032 (C in Upper_Half_Character
1034 Upper_Half_Encoding)
1038 Source (Scan_Ptr + 1) = '"'
1040 Identifier_Char (Source (Scan_Ptr + 2)))
1042 Scan_Wide (Source, Scan_Ptr, Code, Err);
1043 Accumulate_Checksum (Code);
1046 Error_Illegal_Wide_Character;
1047 Code := Get_Char_Code (' ');
1051 Accumulate_Checksum (C);
1053 if C not in Graphic_Character then
1054 if C in Line_Terminator then
1055 Error_Unterminated_String;
1058 elsif C in Upper_Half_Character then
1059 if Ada_Version = Ada_83 then
1060 Error_Bad_String_Char;
1064 Error_Bad_String_Char;
1068 Code := Get_Char_Code (C);
1069 Scan_Ptr := Scan_Ptr + 1;
1073 Store_String_Char (Code);
1075 if not In_Character_Range (Code) then
1076 Wide_Character_Found := True;
1080 String_Literal_Id := End_String;
1086 -- Start of body of Scan
1089 Prev_Token := Token;
1090 Prev_Token_Ptr := Token_Ptr;
1091 Token_Name := Error_Name;
1093 -- The following loop runs more than once only if a format effector
1094 -- (tab, vertical tab, form feed, line feed, carriage return) is
1095 -- encountered and skipped, or some error situation, such as an
1096 -- illegal character, is encountered.
1099 -- Skip past blanks, loop is opened up for speed
1101 while Source (Scan_Ptr) = ' ' loop
1103 if Source (Scan_Ptr + 1) /= ' ' then
1104 Scan_Ptr := Scan_Ptr + 1;
1108 if Source (Scan_Ptr + 2) /= ' ' then
1109 Scan_Ptr := Scan_Ptr + 2;
1113 if Source (Scan_Ptr + 3) /= ' ' then
1114 Scan_Ptr := Scan_Ptr + 3;
1118 if Source (Scan_Ptr + 4) /= ' ' then
1119 Scan_Ptr := Scan_Ptr + 4;
1123 if Source (Scan_Ptr + 5) /= ' ' then
1124 Scan_Ptr := Scan_Ptr + 5;
1128 if Source (Scan_Ptr + 6) /= ' ' then
1129 Scan_Ptr := Scan_Ptr + 6;
1133 if Source (Scan_Ptr + 7) /= ' ' then
1134 Scan_Ptr := Scan_Ptr + 7;
1138 Scan_Ptr := Scan_Ptr + 8;
1141 -- We are now at a non-blank character, which is the first character
1142 -- of the token we will scan, and hence the value of Token_Ptr.
1144 Token_Ptr := Scan_Ptr;
1146 -- Here begins the main case statement which transfers control on
1147 -- the basis of the non-blank character we have encountered.
1149 case Source (Scan_Ptr) is
1151 -- Line terminator characters
1153 when CR | LF | FF | VT => Line_Terminator_Case : begin
1155 -- Check line too long
1159 -- Set Token_Ptr, if End_Of_Line is a token, for the case when
1160 -- it is a physical line.
1162 if End_Of_Line_Is_Token then
1163 Token_Ptr := Scan_Ptr;
1170 Skip_Line_Terminators (Scan_Ptr, Physical);
1172 -- If we are at start of physical line, update scan pointers
1173 -- to reflect the start of the new line.
1176 Current_Line_Start := Scan_Ptr;
1177 Start_Column := Set_Start_Column;
1178 First_Non_Blank_Location := Scan_Ptr;
1180 -- If End_Of_Line is a token, we return it as it is
1183 if End_Of_Line_Is_Token then
1184 Token := Tok_End_Of_Line;
1189 end Line_Terminator_Case;
1191 -- Horizontal tab, just skip past it
1194 if Style_Check then Style.Check_HT; end if;
1195 Scan_Ptr := Scan_Ptr + 1;
1197 -- End of file character, treated as an end of file only if it
1198 -- is the last character in the buffer, otherwise it is ignored.
1201 if Scan_Ptr = Source_Last (Current_Source_File) then
1207 Scan_Ptr := Scan_Ptr + 1;
1213 Accumulate_Checksum ('&');
1215 if Source (Scan_Ptr + 1) = '&' then
1216 Error_Msg_S ("'&'& should be `AND THEN`");
1217 Scan_Ptr := Scan_Ptr + 2;
1222 Scan_Ptr := Scan_Ptr + 1;
1223 Token := Tok_Ampersand;
1227 -- Asterisk (can be multiplication operator or double asterisk
1228 -- which is the exponentiation compound delimiter).
1231 Accumulate_Checksum ('*');
1233 if Source (Scan_Ptr + 1) = '*' then
1234 Accumulate_Checksum ('*');
1235 Scan_Ptr := Scan_Ptr + 2;
1236 Token := Tok_Double_Asterisk;
1240 Scan_Ptr := Scan_Ptr + 1;
1241 Token := Tok_Asterisk;
1245 -- Colon, which can either be an isolated colon, or part of an
1246 -- assignment compound delimiter.
1249 Accumulate_Checksum (':');
1251 if Double_Char_Token ('=') then
1252 Token := Tok_Colon_Equal;
1253 if Style_Check then Style.Check_Colon_Equal; end if;
1256 elsif Source (Scan_Ptr + 1) = '-'
1257 and then Source (Scan_Ptr + 2) /= '-'
1259 Token := Tok_Colon_Equal;
1260 Error_Msg (":- should be :=", Scan_Ptr);
1261 Scan_Ptr := Scan_Ptr + 2;
1265 Scan_Ptr := Scan_Ptr + 1;
1267 if Style_Check then Style.Check_Colon; end if;
1274 Accumulate_Checksum ('(');
1275 Scan_Ptr := Scan_Ptr + 1;
1276 Token := Tok_Left_Paren;
1277 if Style_Check then Style.Check_Left_Paren; end if;
1283 if Source (Scan_Ptr + 1) = '"' then
1285 goto Scan_Identifier;
1288 Error_Msg_S ("illegal character, replaced by ""(""");
1289 Scan_Ptr := Scan_Ptr + 1;
1290 Token := Tok_Left_Paren;
1297 Error_Msg_S ("illegal character, replaced by ""(""");
1298 Scan_Ptr := Scan_Ptr + 1;
1299 Token := Tok_Left_Paren;
1305 Accumulate_Checksum (',');
1306 Scan_Ptr := Scan_Ptr + 1;
1308 if Style_Check then Style.Check_Comma; end if;
1311 -- Dot, which is either an isolated period, or part of a double
1312 -- dot compound delimiter sequence. We also check for the case of
1313 -- a digit following the period, to give a better error message.
1316 Accumulate_Checksum ('.');
1318 if Double_Char_Token ('.') then
1319 Token := Tok_Dot_Dot;
1320 if Style_Check then Style.Check_Dot_Dot; end if;
1323 elsif Source (Scan_Ptr + 1) in '0' .. '9' then
1324 Error_Msg_S ("numeric literal cannot start with point");
1325 Scan_Ptr := Scan_Ptr + 1;
1328 Scan_Ptr := Scan_Ptr + 1;
1333 -- Equal, which can either be an equality operator, or part of the
1334 -- arrow (=>) compound delimiter.
1337 Accumulate_Checksum ('=');
1339 if Double_Char_Token ('>') then
1341 if Style_Check then Style.Check_Arrow; end if;
1344 elsif Source (Scan_Ptr + 1) = '=' then
1345 Error_Msg_S ("== should be =");
1346 Scan_Ptr := Scan_Ptr + 1;
1349 Scan_Ptr := Scan_Ptr + 1;
1353 -- Greater than, which can be a greater than operator, greater than
1354 -- or equal operator, or first character of a right label bracket.
1357 Accumulate_Checksum ('>');
1359 if Double_Char_Token ('=') then
1360 Token := Tok_Greater_Equal;
1363 elsif Double_Char_Token ('>') then
1364 Token := Tok_Greater_Greater;
1368 Scan_Ptr := Scan_Ptr + 1;
1369 Token := Tok_Greater;
1373 -- Less than, which can be a less than operator, less than or equal
1374 -- operator, or the first character of a left label bracket, or the
1375 -- first character of a box (<>) compound delimiter.
1378 Accumulate_Checksum ('<');
1380 if Double_Char_Token ('=') then
1381 Token := Tok_Less_Equal;
1384 elsif Double_Char_Token ('>') then
1386 if Style_Check then Style.Check_Box; end if;
1389 elsif Double_Char_Token ('<') then
1390 Token := Tok_Less_Less;
1394 Scan_Ptr := Scan_Ptr + 1;
1399 -- Minus, which is either a subtraction operator, or the first
1400 -- character of double minus starting a comment
1402 when '-' => Minus_Case : begin
1403 if Source (Scan_Ptr + 1) = '>' then
1404 Error_Msg_S ("invalid token");
1405 Scan_Ptr := Scan_Ptr + 2;
1409 elsif Source (Scan_Ptr + 1) /= '-' then
1410 Accumulate_Checksum ('-');
1411 Scan_Ptr := Scan_Ptr + 1;
1417 else -- Source (Scan_Ptr + 1) = '-' then
1418 if Style_Check then Style.Check_Comment; end if;
1419 Scan_Ptr := Scan_Ptr + 2;
1420 Start_Of_Comment := Scan_Ptr;
1422 -- Loop to scan comment (this loop runs more than once only if
1423 -- a horizontal tab or other non-graphic character is scanned)
1426 -- Scan to non graphic character (opened up for speed)
1429 exit when Source (Scan_Ptr) not in Graphic_Character;
1430 Scan_Ptr := Scan_Ptr + 1;
1431 exit when Source (Scan_Ptr) not in Graphic_Character;
1432 Scan_Ptr := Scan_Ptr + 1;
1433 exit when Source (Scan_Ptr) not in Graphic_Character;
1434 Scan_Ptr := Scan_Ptr + 1;
1435 exit when Source (Scan_Ptr) not in Graphic_Character;
1436 Scan_Ptr := Scan_Ptr + 1;
1437 exit when Source (Scan_Ptr) not in Graphic_Character;
1438 Scan_Ptr := Scan_Ptr + 1;
1441 -- Keep going if horizontal tab
1443 if Source (Scan_Ptr) = HT then
1444 if Style_Check then Style.Check_HT; end if;
1445 Scan_Ptr := Scan_Ptr + 1;
1447 -- Terminate scan of comment if line terminator
1449 elsif Source (Scan_Ptr) in Line_Terminator then
1452 -- Terminate scan of comment if end of file encountered
1453 -- (embedded EOF character or real last character in file)
1455 elsif Source (Scan_Ptr) = EOF then
1458 -- Keep going if character in 80-FF range, or is ESC. These
1459 -- characters are allowed in comments by RM-2.1(1), 2.7(2).
1460 -- They are allowed even in Ada 83 mode according to the
1461 -- approved AI. ESC was added to the AI in June 93.
1463 elsif Source (Scan_Ptr) in Upper_Half_Character
1464 or else Source (Scan_Ptr) = ESC
1466 Scan_Ptr := Scan_Ptr + 1;
1468 -- Otherwise we have an illegal comment character
1471 Error_Illegal_Character;
1476 -- Note that, except when comments are tokens, we do NOT
1477 -- execute a return here, instead we fall through to reexecute
1478 -- the scan loop to look for a token.
1480 if Comment_Is_Token then
1481 Name_Len := Integer (Scan_Ptr - Start_Of_Comment);
1482 Name_Buffer (1 .. Name_Len) :=
1483 String (Source (Start_Of_Comment .. Scan_Ptr - 1));
1484 Comment_Id := Name_Find;
1485 Token := Tok_Comment;
1491 -- Double quote starting a string literal
1498 -- Percent starting a string literal
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).
1535 -- Also treat it as apostrophe after a literal (this catches
1536 -- some legitimate cases, like A."abs"'Address, and also gives
1537 -- better error 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 with ESC or [ encoding
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) = '"')
1566 Scan_Wide (Source, Scan_Ptr, Code, Err);
1567 Accumulate_Checksum (Code);
1570 Error_Illegal_Wide_Character;
1573 if Source (Scan_Ptr) /= ''' then
1574 Error_Msg_S ("missing apostrophe");
1576 Scan_Ptr := Scan_Ptr + 1;
1579 -- If we do not find a closing quote in the expected place then
1580 -- assume that we have a misguided attempt at a string literal.
1582 -- However, if previous token is RANGE, then we return an
1583 -- apostrophe instead since this gives better error recovery
1585 elsif Source (Scan_Ptr + 1) /= ''' then
1587 if Prev_Token = Tok_Range then
1588 Token := Tok_Apostrophe;
1592 Scan_Ptr := Scan_Ptr - 1;
1594 ("strings are delimited by double quote character");
1600 -- Otherwise we have a (non-wide) character literal
1603 Accumulate_Checksum (Source (Scan_Ptr));
1605 if Source (Scan_Ptr) not in Graphic_Character then
1606 if Source (Scan_Ptr) in Upper_Half_Character then
1607 if Ada_Version = Ada_83 then
1608 Error_Illegal_Character;
1612 Error_Illegal_Character;
1616 Code := Get_Char_Code (Source (Scan_Ptr));
1617 Scan_Ptr := Scan_Ptr + 2;
1620 -- Fall through here with Scan_Ptr updated past the closing
1621 -- quote, and Code set to the Char_Code value for the literal
1623 Accumulate_Checksum (''');
1624 Token := Tok_Char_Literal;
1625 Set_Character_Literal_Name (Code);
1626 Token_Name := Name_Find;
1627 Character_Code := Code;
1631 end Char_Literal_Case;
1633 -- Right parenthesis
1636 Accumulate_Checksum (')');
1637 Scan_Ptr := Scan_Ptr + 1;
1638 Token := Tok_Right_Paren;
1639 if Style_Check then Style.Check_Right_Paren; end if;
1642 -- Right bracket or right brace, treated as right paren
1645 Error_Msg_S ("illegal character, replaced by "")""");
1646 Scan_Ptr := Scan_Ptr + 1;
1647 Token := Tok_Right_Paren;
1650 -- Slash (can be division operator or first character of not equal)
1653 Accumulate_Checksum ('/');
1655 if Double_Char_Token ('=') then
1656 Token := Tok_Not_Equal;
1659 Scan_Ptr := Scan_Ptr + 1;
1667 Accumulate_Checksum (';');
1668 Scan_Ptr := Scan_Ptr + 1;
1669 Token := Tok_Semicolon;
1670 if Style_Check then Style.Check_Semicolon; end if;
1675 when '|' => Vertical_Bar_Case : begin
1676 Accumulate_Checksum ('|');
1678 -- Special check for || to give nice message
1680 if Source (Scan_Ptr + 1) = '|' then
1681 Error_Msg_S ("""'|'|"" should be `OR ELSE`");
1682 Scan_Ptr := Scan_Ptr + 2;
1687 Scan_Ptr := Scan_Ptr + 1;
1688 Token := Tok_Vertical_Bar;
1689 if Style_Check then Style.Check_Vertical_Bar; end if;
1692 end Vertical_Bar_Case;
1694 -- Exclamation, replacement character for vertical bar
1696 when '!' => Exclamation_Case : begin
1697 Accumulate_Checksum ('!');
1699 if Warn_On_Obsolescent_Feature then
1701 ("use of ""'!"" is an obsolescent feature ('R'M 'J.2(2))?");
1703 ("\use ""'|"" instead?");
1706 if Source (Scan_Ptr + 1) = '=' then
1707 Error_Msg_S ("'!= should be /=");
1708 Scan_Ptr := Scan_Ptr + 2;
1709 Token := Tok_Not_Equal;
1713 Scan_Ptr := Scan_Ptr + 1;
1714 Token := Tok_Vertical_Bar;
1718 end Exclamation_Case;
1722 when '+' => Plus_Case : begin
1723 Accumulate_Checksum ('+');
1724 Scan_Ptr := Scan_Ptr + 1;
1729 -- Digits starting a numeric literal
1734 if Identifier_Char (Source (Scan_Ptr)) then
1736 ("delimiter required between literal and identifier");
1741 -- Lower case letters
1745 Name_Buffer (1) := Source (Scan_Ptr);
1746 Accumulate_Checksum (Name_Buffer (1));
1747 Scan_Ptr := Scan_Ptr + 1;
1748 goto Scan_Identifier;
1750 -- Upper case letters
1755 Character'Val (Character'Pos (Source (Scan_Ptr)) + 32);
1756 Accumulate_Checksum (Name_Buffer (1));
1757 Scan_Ptr := Scan_Ptr + 1;
1758 goto Scan_Identifier;
1760 -- Underline character
1763 if Special_Characters ('_') then
1764 Token_Ptr := Scan_Ptr;
1765 Scan_Ptr := Scan_Ptr + 1;
1766 Token := Tok_Special;
1767 Special_Character := '_';
1771 Error_Msg_S ("identifier cannot start with underline");
1773 Name_Buffer (1) := '_';
1774 Scan_Ptr := Scan_Ptr + 1;
1775 goto Scan_Identifier;
1777 -- Space (not possible, because we scanned past blanks)
1780 raise Program_Error;
1782 -- Characters in top half of ASCII 8-bit chart
1784 when Upper_Half_Character =>
1786 -- Wide character case. Note that Scan_Identifier will issue
1787 -- an appropriate message if wide characters are not allowed
1790 if Upper_Half_Encoding then
1792 goto Scan_Identifier;
1794 -- Otherwise we have OK Latin-1 character
1797 -- Upper half characters may possibly be identifier letters
1798 -- but can never be digits, so Identifier_Char can be used
1799 -- to test for a valid start of identifier character.
1801 if Identifier_Char (Source (Scan_Ptr)) then
1803 goto Scan_Identifier;
1805 Error_Illegal_Character;
1811 -- ESC character, possible start of identifier if wide characters
1812 -- using ESC encoding are allowed in identifiers, which we can
1813 -- tell by looking at the Identifier_Char flag for ESC, which is
1814 -- only true if these conditions are met.
1816 if Identifier_Char (ESC) then
1818 goto Scan_Identifier;
1820 Error_Illegal_Wide_Character;
1823 -- Invalid control characters
1825 when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | SO |
1826 SI | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
1827 EM | FS | GS | RS | US | DEL
1829 Error_Illegal_Character;
1831 -- Invalid graphic characters
1833 when '#' | '$' | '?' | '@' | '`' | '\' | '^' | '~' =>
1834 -- If Set_Special_Character has been called for this character,
1835 -- set Scans.Special_Character and return a Special token.
1837 if Special_Characters (Source (Scan_Ptr)) then
1838 Token_Ptr := Scan_Ptr;
1839 Token := Tok_Special;
1840 Special_Character := Source (Scan_Ptr);
1841 Scan_Ptr := Scan_Ptr + 1;
1844 -- otherwise, this is an illegal character
1847 Error_Illegal_Character;
1850 -- End switch on non-blank character
1854 -- End loop past format effectors. The exit from this loop is by
1855 -- executing a return statement following completion of token scan
1856 -- (control never falls out of this loop to the code which follows)
1860 -- Identifier scanning routine. On entry, some initial characters
1861 -- of the identifier may have already been stored in Name_Buffer.
1862 -- If so, Name_Len has the number of characters stored. otherwise
1863 -- Name_Len is set to zero on entry.
1867 -- This loop scans as fast as possible past lower half letters
1868 -- and digits, which we expect to be the most common characters.
1871 if Source (Scan_Ptr) in 'a' .. 'z'
1872 or else Source (Scan_Ptr) in '0' .. '9'
1874 Name_Buffer (Name_Len + 1) := Source (Scan_Ptr);
1875 Accumulate_Checksum (Source (Scan_Ptr));
1877 elsif Source (Scan_Ptr) in 'A' .. 'Z' then
1878 Name_Buffer (Name_Len + 1) :=
1879 Character'Val (Character'Pos (Source (Scan_Ptr)) + 32);
1880 Accumulate_Checksum (Name_Buffer (Name_Len + 1));
1885 -- Open out the loop a couple of times for speed
1887 if Source (Scan_Ptr + 1) in 'a' .. 'z'
1888 or else Source (Scan_Ptr + 1) in '0' .. '9'
1890 Name_Buffer (Name_Len + 2) := Source (Scan_Ptr + 1);
1891 Accumulate_Checksum (Source (Scan_Ptr + 1));
1893 elsif Source (Scan_Ptr + 1) in 'A' .. 'Z' then
1894 Name_Buffer (Name_Len + 2) :=
1895 Character'Val (Character'Pos (Source (Scan_Ptr + 1)) + 32);
1896 Accumulate_Checksum (Name_Buffer (Name_Len + 2));
1899 Scan_Ptr := Scan_Ptr + 1;
1900 Name_Len := Name_Len + 1;
1904 if Source (Scan_Ptr + 2) in 'a' .. 'z'
1905 or else Source (Scan_Ptr + 2) in '0' .. '9'
1907 Name_Buffer (Name_Len + 3) := Source (Scan_Ptr + 2);
1908 Accumulate_Checksum (Source (Scan_Ptr + 2));
1910 elsif Source (Scan_Ptr + 2) in 'A' .. 'Z' then
1911 Name_Buffer (Name_Len + 3) :=
1912 Character'Val (Character'Pos (Source (Scan_Ptr + 2)) + 32);
1913 Accumulate_Checksum (Name_Buffer (Name_Len + 3));
1915 Scan_Ptr := Scan_Ptr + 2;
1916 Name_Len := Name_Len + 2;
1920 if Source (Scan_Ptr + 3) in 'a' .. 'z'
1921 or else Source (Scan_Ptr + 3) in '0' .. '9'
1923 Name_Buffer (Name_Len + 4) := Source (Scan_Ptr + 3);
1924 Accumulate_Checksum (Source (Scan_Ptr + 3));
1926 elsif Source (Scan_Ptr + 3) in 'A' .. 'Z' then
1927 Name_Buffer (Name_Len + 4) :=
1928 Character'Val (Character'Pos (Source (Scan_Ptr + 3)) + 32);
1929 Accumulate_Checksum (Name_Buffer (Name_Len + 4));
1932 Scan_Ptr := Scan_Ptr + 3;
1933 Name_Len := Name_Len + 3;
1937 Scan_Ptr := Scan_Ptr + 4;
1938 Name_Len := Name_Len + 4;
1941 -- If we fall through, then we have encountered either an underline
1942 -- character, or an extended identifier character (i.e. one from the
1943 -- upper half), or a wide character, or an identifier terminator.
1944 -- The initial test speeds us up in the most common case where we
1945 -- have an identifier terminator. Note that ESC is an identifier
1946 -- character only if a wide character encoding method that uses
1947 -- ESC encoding is active, so if we find an ESC character we know
1948 -- that we have a wide character.
1950 if Identifier_Char (Source (Scan_Ptr)) then
1952 -- Case of underline
1954 if Source (Scan_Ptr) = '_' then
1955 Accumulate_Checksum ('_');
1957 -- Check error case of identifier ending with underscore
1958 -- In this case we ignore the underscore and do not store it.
1960 if not Identifier_Char (Source (Scan_Ptr + 1)) then
1961 Error_Msg_S ("identifier cannot end with underline");
1962 Scan_Ptr := Scan_Ptr + 1;
1964 -- Check error case of two underscores. In this case we do
1965 -- not store the first underscore (we will store the second)
1967 elsif Source (Scan_Ptr + 1) = '_' then
1968 Error_No_Double_Underline;
1970 -- Normal case of legal underscore
1973 Name_Len := Name_Len + 1;
1974 Name_Buffer (Name_Len) := '_';
1977 Scan_Ptr := Scan_Ptr + 1;
1978 goto Scan_Identifier;
1980 -- Upper half character
1982 elsif Source (Scan_Ptr) in Upper_Half_Character
1983 and then not Upper_Half_Encoding
1985 Accumulate_Checksum (Source (Scan_Ptr));
1986 Store_Encoded_Character
1987 (Get_Char_Code (Fold_Lower (Source (Scan_Ptr))));
1988 Scan_Ptr := Scan_Ptr + 1;
1989 goto Scan_Identifier;
1991 -- Left bracket not followed by a quote terminates an identifier.
1992 -- This is an error, but we don't want to give a junk error msg
1993 -- about wide characters in this case!
1995 elsif Source (Scan_Ptr) = '['
1996 and then Source (Scan_Ptr + 1) /= '"'
2000 -- We know we have a wide character encoding here (the current
2001 -- character is either ESC, left bracket, or an upper half
2002 -- character depending on the encoding method).
2005 -- Scan out the wide character and insert the appropriate
2006 -- encoding into the name table entry for the identifier.
2009 Sptr : constant Source_Ptr := Scan_Ptr;
2015 Scan_Wide (Source, Scan_Ptr, Code, Err);
2017 -- If error, signal error
2020 Error_Illegal_Wide_Character;
2022 -- If the character scanned is a normal identifier
2023 -- character, then we treat it that way.
2025 elsif In_Character_Range (Code)
2026 and then Identifier_Char (Get_Character (Code))
2028 Chr := Get_Character (Code);
2029 Accumulate_Checksum (Chr);
2030 Store_Encoded_Character
2031 (Get_Char_Code (Fold_Lower (Chr)));
2033 -- Character is not normal identifier character, store
2034 -- it in encoded form.
2037 Accumulate_Checksum (Code);
2038 Store_Encoded_Character (Code);
2040 -- Make sure we are allowing wide characters in
2041 -- identifiers. Note that we allow wide character
2042 -- notation for an OK identifier character. This
2043 -- in particular allows bracket or other notation
2044 -- to be used for upper half letters.
2046 if Identifier_Character_Set /= 'w' then
2048 ("wide character not allowed in identifier", Sptr);
2053 goto Scan_Identifier;
2057 -- Scan of identifier is complete. The identifier is stored in
2058 -- Name_Buffer, and Scan_Ptr points past the last character.
2060 Token_Name := Name_Find;
2062 -- Here is where we check if it was a keyword
2064 if Get_Name_Table_Byte (Token_Name) /= 0
2065 and then (Ada_Version >= Ada_95
2066 or else Token_Name not in Ada_95_Reserved_Words)
2068 Token := Token_Type'Val (Get_Name_Table_Byte (Token_Name));
2070 -- Deal with possible style check for non-lower case keyword,
2071 -- but we don't treat ACCESS, DELTA, DIGITS, RANGE as keywords
2072 -- for this purpose if they appear as attribute designators.
2073 -- Actually we only check the first character for speed.
2076 and then Source (Token_Ptr) <= 'Z'
2077 and then (Prev_Token /= Tok_Apostrophe
2079 (Token /= Tok_Access
2080 and then Token /= Tok_Delta
2081 and then Token /= Tok_Digits
2082 and then Token /= Tok_Range))
2084 Style.Non_Lower_Case_Keyword;
2087 -- We must reset Token_Name since this is not an identifier
2088 -- and if we leave Token_Name set, the parser gets confused
2089 -- because it thinks it is dealing with an identifier instead
2090 -- of the corresponding keyword.
2092 Token_Name := No_Name;
2093 Accumulate_Token_Checksum;
2096 -- It is an identifier after all
2099 Token := Tok_Identifier;
2100 Accumulate_Token_Checksum;
2106 --------------------------
2107 -- Set_Comment_As_Token --
2108 --------------------------
2110 procedure Set_Comment_As_Token (Value : Boolean) is
2112 Comment_Is_Token := Value;
2113 end Set_Comment_As_Token;
2115 ------------------------------
2116 -- Set_End_Of_Line_As_Token --
2117 ------------------------------
2119 procedure Set_End_Of_Line_As_Token (Value : Boolean) is
2121 End_Of_Line_Is_Token := Value;
2122 end Set_End_Of_Line_As_Token;
2124 ---------------------------
2125 -- Set_Special_Character --
2126 ---------------------------
2128 procedure Set_Special_Character (C : Character) is
2131 when '#' | '$' | '_' | '?' | '@' | '`' | '\' | '^' | '~' =>
2132 Special_Characters (C) := True;
2137 end Set_Special_Character;
2139 ----------------------
2140 -- Set_Start_Column --
2141 ----------------------
2143 -- Note: it seems at first glance a little expensive to compute this value
2144 -- for every source line (since it is certainly not used for all source
2145 -- lines). On the other hand, it doesn't take much more work to skip past
2146 -- the initial white space on the line counting the columns than it would
2147 -- to scan past the white space using the standard scanning circuits.
2149 function Set_Start_Column return Column_Number is
2150 Start_Column : Column_Number := 0;
2153 -- Outer loop scans past horizontal tab characters
2157 -- Inner loop scans past blanks as fast as possible, bumping Scan_Ptr
2158 -- past the blanks and adjusting Start_Column to account for them.
2161 if Source (Scan_Ptr) = ' ' then
2162 if Source (Scan_Ptr + 1) = ' ' then
2163 if Source (Scan_Ptr + 2) = ' ' then
2164 if Source (Scan_Ptr + 3) = ' ' then
2165 if Source (Scan_Ptr + 4) = ' ' then
2166 if Source (Scan_Ptr + 5) = ' ' then
2167 if Source (Scan_Ptr + 6) = ' ' then
2168 Scan_Ptr := Scan_Ptr + 7;
2169 Start_Column := Start_Column + 7;
2171 Scan_Ptr := Scan_Ptr + 6;
2172 Start_Column := Start_Column + 6;
2176 Scan_Ptr := Scan_Ptr + 5;
2177 Start_Column := Start_Column + 5;
2181 Scan_Ptr := Scan_Ptr + 4;
2182 Start_Column := Start_Column + 4;
2186 Scan_Ptr := Scan_Ptr + 3;
2187 Start_Column := Start_Column + 3;
2191 Scan_Ptr := Scan_Ptr + 2;
2192 Start_Column := Start_Column + 2;
2196 Scan_Ptr := Scan_Ptr + 1;
2197 Start_Column := Start_Column + 1;
2203 end loop Blanks_Loop;
2205 -- Outer loop keeps going only if a horizontal tab follows
2207 if Source (Scan_Ptr) = HT then
2208 if Style_Check then Style.Check_HT; end if;
2209 Scan_Ptr := Scan_Ptr + 1;
2210 Start_Column := (Start_Column / 8) * 8 + 8;
2217 return Start_Column;
2218 end Set_Start_Column;