1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2000-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.Exceptions; use Ada.Exceptions;
34 package body SFN_Scan is
37 -- Allow easy access to control character definitions
39 EOF : constant Character := ASCII.SUB;
40 -- The character SUB (16#1A#) is used in DOS and other systems derived
41 -- from DOS (OS/2, NT etc.) to signal the end of a text file. If this
42 -- character appears as the last character of a file scanned by a call
43 -- to Scan_SFN_Pragmas, then it is ignored, otherwise it is treated as
44 -- an illegal character.
46 type String_Ptr is access String;
49 -- Points to the gnat.adc input file
52 -- Subscript of next character to process in S
55 -- Current line number
57 Start_Of_Line : Natural;
58 -- Subscript of first character at start of current line
60 ----------------------
61 -- Local Procedures --
62 ----------------------
64 function Acquire_Integer return Natural;
65 -- This function skips white space, and then scans and returns
66 -- an unsigned integer. Raises Error if no integer is present
67 -- or if the integer is greater than 999.
69 function Acquire_String (B : Natural; E : Natural) return String;
70 -- This function takes a string scanned out by Scan_String, strips
71 -- the enclosing quote characters and any internal doubled quote
72 -- characters, and returns the result as a String. The arguments
73 -- B and E are as returned from a call to Scan_String. The lower
74 -- bound of the string returned is always 1.
76 function Acquire_Unit_Name return String;
77 -- Skips white space, and then scans and returns a unit name. The
78 -- unit name is cased exactly as it appears in the source file.
79 -- The terminating character must be white space, or a comma or
80 -- a right parenthesis or end of file.
82 function At_EOF return Boolean;
83 pragma Inline (At_EOF);
84 -- Returns True if at end of file, False if not. Note that this
85 -- function does NOT skip white space, so P is always unchanged.
87 procedure Check_Not_At_EOF;
88 pragma Inline (Check_Not_At_EOF);
89 -- Skips past white space if any, and then raises Error if at
90 -- end of file. Otherwise returns with P skipped past whitespace.
92 function Check_File_Type return Character;
93 -- Skips white space if any, and then looks for any of the tokens
94 -- Spec_File_Name, Body_File_Name, or Subunit_File_Name. If one
95 -- of these is found then the value returned is 's', 'b' or 'u'
96 -- respectively, and P is bumped past the token. If none of
97 -- these tokens is found, then P is unchanged (except for
98 -- possible skip of white space), and a space is returned.
100 function Check_Token (T : String) return Boolean;
101 -- Skips white space if any, and then checks if the string at the
102 -- current location matches the given string T, and the character
103 -- immediately following is non-alphabetic, non-numeric. If so,
104 -- P is stepped past the token, and True is returned. If not,
105 -- P is unchanged (except for possibly skipping past whitespace),
106 -- and False is returned. S may contain only lower-case letters
109 procedure Error (Err : String);
110 -- Called if an error is detected. Raises Syntax_Error_In_GNAT_ADC
111 -- with a message of the form gnat.adc:line:col: xxx, where xxx is
112 -- the string Err passed as a parameter.
114 procedure Require_Token (T : String);
115 -- Skips white space if any, and then requires the given string
116 -- to be present. If it is, the P is stepped past it, otherwise
117 -- Error is raised, since this is a syntax error. Require_Token
118 -- is used only for sequences of special characters, so there
119 -- is no issue of terminators, or casing of letters.
121 procedure Scan_String (B : out Natural; E : out Natural);
122 -- Skips white space if any, then requires that a double quote
123 -- or percent be present (start of string). Raises error if
124 -- neither of these two characters is found. Otherwise scans
125 -- out the string, and returns with P pointing past the
126 -- closing quote and S (B .. E) contains the characters of the
127 -- string (including the enclosing quotes, with internal quotes
128 -- still doubled). Raises Error if the string is malformed.
131 -- Skips P past any white space characters (end of line
132 -- characters, spaces, comments, horizontal tab characters).
134 ---------------------
135 -- Acquire_Integer --
136 ---------------------
138 function Acquire_Integer return Natural is
144 if S (P) not in '0' .. '9' then
145 Error ("missing index parameter");
148 while S (P) in '0' .. '9' loop
149 N := N * 10 + Character'Pos (S (P)) - Character'Pos ('0');
152 Error ("index value greater than 999");
165 function Acquire_String (B : Natural; E : Natural) return String is
166 Str : String (1 .. E - B - 1);
167 Q : constant Character := S (B);
178 if S (Ptr) = Q and then S (Ptr + 1) = Q then
188 -----------------------
189 -- Acquire_Unit_Name --
190 -----------------------
192 function Acquire_Unit_Name return String is
199 while not At_EOF loop
200 exit when S (P) not in '0' .. '9'
201 and then S (P) /= '.'
202 and then S (P) /= '_'
203 and then not (S (P) = '[' and then S (P + 1) = '"')
204 and then not (S (P) = '"' and then S (P - 1) = '[')
205 and then not (S (P) = '"' and then S (P + 1) = ']')
206 and then not (S (P) = ']' and then S (P - 1) = '"')
207 and then S (P) < 'A';
212 Error ("null unit name");
215 return S (B .. P - 1);
216 end Acquire_Unit_Name;
222 function At_EOF return Boolean is
224 -- Immediate return (False) if before last character of file
229 -- Special case: DOS EOF character as last character of file is
230 -- allowed and treated as an end of file.
232 elsif P = S'Last then
235 -- If beyond last character of file, then definitely at EOF
242 ---------------------
243 -- Check_File_Type --
244 ---------------------
246 function Check_File_Type return Character is
248 if Check_Token ("spec_file_name") then
250 elsif Check_Token ("body_file_name") then
252 elsif Check_Token ("subunit_file_name") then
259 ----------------------
260 -- Check_Not_At_EOF --
261 ----------------------
263 procedure Check_Not_At_EOF is
268 Error ("unexpected end of file");
272 end Check_Not_At_EOF;
278 function Check_Token (T : String) return Boolean is
286 for K in T'Range loop
294 if C in 'A' .. 'Z' then
295 C := Character'Val (Character'Pos (C) +
296 (Character'Pos ('a') - Character'Pos ('A')));
314 or else C in 'a' .. 'z'
315 or else C in 'A' .. 'Z'
316 or else C > Character'Val (127)
330 procedure Error (Err : String) is
334 M : String (1 .. 80);
335 -- Buffer used to build resulting error msg
338 -- Pointer to last set location in M
340 procedure Add_Nat (N : Natural);
341 -- Add chars of integer to error msg buffer
347 procedure Add_Nat (N : Natural) is
354 M (LM) := Character'Val (N mod 10 + Character'Pos ('0'));
357 -- Start of processing for Error
360 M (1 .. 9) := "gnat.adc:";
366 -- Determine column number
368 for X in Start_Of_Line .. P loop
372 C := (C + 7) / 8 * 8;
382 M (LM + 1 .. LM + Err'Length) := Err;
383 LM := LM + Err'Length;
385 Raise_Exception (Syntax_Error_In_GNAT_ADC'Identity, M (1 .. LM));
392 procedure Require_Token (T : String) is
399 for J in T'Range loop
401 if At_EOF or else S (P) /= T (J) then
403 S : String (1 .. T'Length + 10);
406 S (1 .. 9) := "missing """;
407 S (10 .. T'Length + 9) := T;
408 S (T'Length + 10) := '"';
419 ----------------------
420 -- Scan_SFN_Pragmas --
421 ----------------------
423 procedure Scan_SFN_Pragmas
425 SFN_Ptr : Set_File_Name_Ptr;
426 SFNP_Ptr : Set_File_Name_Pattern_Ptr)
434 S := Source'Unrestricted_Access;
438 -- Loop through pragmas in file
440 Main_Scan_Loop : loop
442 exit Main_Scan_Loop when At_EOF;
444 -- Error if something other than pragma
446 if not Check_Token ("pragma") then
447 Error ("non pragma encountered");
450 -- Source_File_Name pragma case
452 if Check_Token ("source_file_name")
454 Check_Token ("source_file_name_project")
458 Typ := Check_File_Type;
460 -- First format, with unit name first
463 if Check_Token ("unit_name") then
464 Require_Token ("=>");
468 U : constant String := Acquire_Unit_Name;
472 Typ := Check_File_Type;
474 if Typ /= 's' and then Typ /= 'b' then
475 Error ("bad pragma");
478 Require_Token ("=>");
482 F : constant String := Acquire_String (B, E);
486 -- Scan Index parameter if present
488 if Check_Token (",") then
489 if Check_Token ("index") then
490 Require_Token ("=>");
493 X := Acquire_Integer;
500 SFN_Ptr.all (Typ, U, F, X);
504 -- Second format with pattern string
507 Require_Token ("=>");
511 Pat : constant String := Acquire_String (B, E);
515 -- Check exactly one asterisk
517 for J in Pat'Range loop
518 if Pat (J) = '*' then
524 Error ("** not allowed");
531 -- Loop to scan out Casing or Dot_Replacement parameters
535 exit when S (P) = ')';
538 if Check_Token ("casing") then
539 Require_Token ("=>");
542 Error ("duplicate casing argument");
543 elsif Check_Token ("lowercase") then
545 elsif Check_Token ("uppercase") then
547 elsif Check_Token ("mixedcase") then
550 Error ("invalid casing argument");
553 elsif Check_Token ("dot_replacement") then
554 Require_Token ("=>");
557 Error ("duplicate dot_replacement");
563 Error ("invalid argument");
575 SFNP_Ptr.all (Pat, Typ, ".", Cas);
579 Dot : constant String := Acquire_String (B, E);
582 SFNP_Ptr.all (Pat, Typ, Dot, Cas);
588 -- Some other pragma, scan to semicolon at end of pragma
592 exit Main_Scan_Loop when At_EOF;
593 exit Skip_Loop when S (P) = ';';
595 if S (P) = '"' or else S (P) = '%' then
602 -- We successfully skipped to semicolon, so skip past it
606 end loop Main_Scan_Loop;
610 Cursor := P - S'First + 1;
612 end Scan_SFN_Pragmas;
618 procedure Scan_String (B : out Natural; E : out Natural) is
626 elsif S (P) = '%' then
629 Error ("bad string");
633 -- Scan out the string, B points to first char
639 if At_EOF or else S (P) = LF or else S (P) = CR then
641 ("missing string quote");
643 elsif S (P) = HT then
644 Error ("tab character in string");
646 elsif S (P) /= Q then
654 -- Check for doubled quote
656 if not At_EOF and then S (P) = Q then
659 -- Otherwise this is the terminating quote
675 WS_Scan : while not At_EOF loop
678 -- End of physical line
681 Line_Num := Line_Num + 1;
685 and then (S (P) = CR or else S (P) = LF)
687 Line_Num := Line_Num + 1;
693 -- All other cases of white space characters
695 when ' ' | FF | VT | HT =>
704 Error ("bad comment");
706 elsif S (P) = '-' then
709 while not At_EOF loop
711 when CR | LF | FF | VT =>