1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2000-2002 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 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Exceptions; use Ada.Exceptions;
36 package body SFN_Scan is
39 -- Allow easy access to control character definitions
41 EOF : constant Character := ASCII.SUB;
42 -- The character SUB (16#1A#) is used in DOS and other systems derived
43 -- from DOS (OS/2, NT etc) to signal the end of a text file. If this
44 -- character appears as the last character of a file scanned by a call
45 -- to Scan_SFN_Pragmas, then it is ignored, otherwise it is treated as
46 -- an illegal character.
48 type String_Ptr is access String;
51 -- Points to the gnat.adc input file
54 -- Subscript of next character to process in S
57 -- Current line number
59 Start_Of_Line : Natural;
60 -- Subscript of first character at start of current line
62 ----------------------
63 -- Local Procedures --
64 ----------------------
66 function Acquire_String (B : Natural; E : Natural) return String;
67 -- This function takes a string scanned out by Scan_String, strips
68 -- the enclosing quote characters and any internal doubled quote
69 -- characters, and returns the result as a String. The arguments
70 -- B and E are as returned from a call to Scan_String. The lower
71 -- bound of the string returned is always 1.
73 function Acquire_Unit_Name return String;
74 -- Skips white space, and then scans and returns a unit name. The
75 -- unit name is cased exactly as it appears in the source file.
76 -- The terminating character must be white space, or a comma or
77 -- a right parenthesis or end of file.
79 function At_EOF return Boolean;
80 pragma Inline (At_EOF);
81 -- Returns True if at end of file, False if not. Note that this
82 -- function does NOT skip white space, so P is always unchanged.
84 procedure Check_Not_At_EOF;
85 pragma Inline (Check_Not_At_EOF);
86 -- Skips past white space if any, and then raises Error if at
87 -- end of file. Otherwise returns with P skipped past whitespace.
89 function Check_File_Type return Character;
90 -- Skips white space if any, and then looks for any of the tokens
91 -- Spec_File_Name, Body_File_Name, or Subunit_File_Name. If one
92 -- of these is found then the value returned is 's', 'b' or 'u'
93 -- respectively, and P is bumped past the token. If none of
94 -- these tokens is found, then P is unchanged (except for
95 -- possible skip of white space), and a space is returned.
97 function Check_Token (T : String) return Boolean;
98 -- Skips white space if any, and then checks if the string at the
99 -- current location matches the given string T, and the character
100 -- immediately following is non-alphabetic, non-numeric. If so,
101 -- P is stepped past the token, and True is returned. If not,
102 -- P is unchanged (except for possibly skipping past whitespace),
103 -- and False is returned. S may contain only lower-case letters
106 procedure Error (Err : String);
107 -- Called if an error is detected. Raises Syntax_Error_In_GNAT_ADC
108 -- with a message of the form gnat.adc:line:col: xxx, where xxx is
109 -- the string Err passed as a parameter.
111 procedure Require_Token (T : String);
112 -- Skips white space if any, and then requires the given string
113 -- to be present. If it is, the P is stepped past it, otherwise
114 -- Error is raised, since this is a syntax error. Require_Token
115 -- is used only for sequences of special characters, so there
116 -- is no issue of terminators, or casing of letters.
118 procedure Scan_String (B : out Natural; E : out Natural);
119 -- Skips white space if any, then requires that a double quote
120 -- or percent be present (start of string). Raises error if
121 -- neither of these two characters is found. Otherwise scans
122 -- out the string, and returns with P pointing past the
123 -- closing quote and S (B .. E) contains the characters of the
124 -- string (including the enclosing quotes, with internal quotes
125 -- still doubled). Raises Error if the string is malformed.
128 -- Skips P past any white space characters (end of line
129 -- characters, spaces, comments, horizontal tab characters).
135 function Acquire_String (B : Natural; E : Natural) return String is
136 Str : String (1 .. E - B - 1);
137 Q : constant Character := S (B);
148 if S (Ptr) = Q and then S (Ptr + 1) = Q then
158 -----------------------
159 -- Acquire_Unit_Name --
160 -----------------------
162 function Acquire_Unit_Name return String is
169 while not At_EOF loop
170 exit when S (P) not in '0' .. '9'
171 and then S (P) /= '.'
172 and then S (P) /= '_'
173 and then not (S (P) = '[' and then S (P + 1) = '"')
174 and then not (S (P) = '"' and then S (P - 1) = '[')
175 and then not (S (P) = '"' and then S (P + 1) = ']')
176 and then not (S (P) = ']' and then S (P - 1) = '"')
177 and then S (P) < 'A';
182 Error ("null unit name");
185 return S (B .. P - 1);
186 end Acquire_Unit_Name;
192 function At_EOF return Boolean is
194 -- Immediate return (False) if before last character of file
199 -- Special case: DOS EOF character as last character of file is
200 -- allowed and treated as an end of file.
202 elsif P = S'Last then
205 -- If beyond last character of file, then definitely at EOF
212 ---------------------
213 -- Check_File_Type --
214 ---------------------
216 function Check_File_Type return Character is
218 if Check_Token ("spec_file_name") then
220 elsif Check_Token ("body_file_name") then
222 elsif Check_Token ("subunit_file_name") then
229 ----------------------
230 -- Check_Not_At_EOF --
231 ----------------------
233 procedure Check_Not_At_EOF is
238 Error ("unexpected end of file");
242 end Check_Not_At_EOF;
248 function Check_Token (T : String) return Boolean is
256 for K in T'Range loop
264 if C in 'A' .. 'Z' then
265 C := Character'Val (Character'Pos (C) +
266 (Character'Pos ('a') - Character'Pos ('A')));
284 or else C in 'a' .. 'z'
285 or else C in 'A' .. 'Z'
286 or else C > Character'Val (127)
300 procedure Error (Err : String) is
304 M : String (1 .. 80);
305 -- Buffer used to build resulting error msg
308 -- Pointer to last set location in M
310 procedure Add_Nat (N : Natural);
311 -- Add chars of integer to error msg buffer
313 procedure Add_Nat (N : Natural) is
320 M (LM) := Character'Val (N mod 10 + Character'Pos ('0'));
323 -- Start of processing for Error
326 M (1 .. 9) := "gnat.adc:";
332 -- Determine column number
334 for X in Start_Of_Line .. P loop
338 C := (C + 7) / 8 * 8;
348 M (LM + 1 .. LM + Err'Length) := Err;
349 LM := LM + Err'Length;
351 Raise_Exception (Syntax_Error_In_GNAT_ADC'Identity, M (1 .. LM));
358 procedure Require_Token (T : String) is
365 for J in T'Range loop
367 if At_EOF or else S (P) /= T (J) then
369 S : String (1 .. T'Length + 10);
372 S (1 .. 9) := "missing """;
373 S (10 .. T'Length + 9) := T;
374 S (T'Length + 10) := '"';
385 ----------------------
386 -- Scan_SFN_Pragmas --
387 ----------------------
389 procedure Scan_SFN_Pragmas
391 SFN_Ptr : Set_File_Name_Ptr;
392 SFNP_Ptr : Set_File_Name_Pattern_Ptr)
400 S := Source'Unrestricted_Access;
404 -- Loop through pragmas in file
406 Main_Scan_Loop : loop
408 exit Main_Scan_Loop when At_EOF;
410 -- Error if something other than pragma
412 if not Check_Token ("pragma") then
413 Error ("non pragma encountered");
416 -- Source_File_Name pragma case
418 if Check_Token ("source_file_name") then
421 Typ := Check_File_Type;
423 -- First format, with unit name first
426 if Check_Token ("unit_name") then
427 Require_Token ("=>");
431 U : constant String := Acquire_Unit_Name;
435 Typ := Check_File_Type;
437 if Typ /= 's' and then Typ /= 'b' then
438 Error ("bad pragma");
441 Require_Token ("=>");
445 F : constant String := Acquire_String (B, E);
450 SFN_Ptr.all (Typ, U, F);
454 -- Second format with pattern string
457 Require_Token ("=>");
461 Pat : constant String := Acquire_String (B, E);
465 -- Check exactly one asterisk
467 for J in Pat'Range loop
468 if Pat (J) = '*' then
474 Error ("** not allowed");
481 -- Loop to scan out Casing or Dot_Replacement parameters
485 exit when S (P) = ')';
488 if Check_Token ("casing") then
489 Require_Token ("=>");
492 Error ("duplicate casing argument");
493 elsif Check_Token ("lowercase") then
495 elsif Check_Token ("uppercase") then
497 elsif Check_Token ("mixedcase") then
500 Error ("invalid casing argument");
503 elsif Check_Token ("dot_replacement") then
504 Require_Token ("=>");
507 Error ("duplicate dot_replacement");
513 Error ("invalid argument");
525 SFNP_Ptr.all (Pat, Typ, ".", Cas);
529 Dot : constant String := Acquire_String (B, E);
532 SFNP_Ptr.all (Pat, Typ, Dot, Cas);
538 -- Some other pragma, scan to semicolon at end of pragma
542 exit Main_Scan_Loop when At_EOF;
543 exit Skip_Loop when S (P) = ';';
545 if S (P) = '"' or else S (P) = '%' then
552 -- We successfuly skipped to semicolon, so skip past it
556 end loop Main_Scan_Loop;
560 Cursor := P - S'First + 1;
562 end Scan_SFN_Pragmas;
568 procedure Scan_String (B : out Natural; E : out Natural) is
576 elsif S (P) = '%' then
579 Error ("bad string");
583 -- Scan out the string, B points to first char
589 if At_EOF or else S (P) = LF or else S (P) = CR then
590 Error ("missing string quote");
592 elsif S (P) = HT then
593 Error ("tab character in string");
595 elsif S (P) /= Q then
603 -- Check for doubled quote
605 if not At_EOF and then S (P) = Q then
608 -- Otherwise this is the terminating quote
624 WS_Scan : while not At_EOF loop
627 -- End of physical line
630 Line_Num := Line_Num + 1;
634 and then (S (P) = CR or else S (P) = LF)
636 Line_Num := Line_Num + 1;
642 -- All other cases of white space characters
644 when ' ' | FF | VT | HT =>
653 Error ("bad comment");
655 elsif S (P) = '-' then
658 while not At_EOF loop
660 when CR | LF | FF | VT =>