1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 2000-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Ada
.Exceptions
; use Ada
.Exceptions
;
37 package body SFN_Scan
is
40 -- Allow easy access to control character definitions
42 type String_Ptr
is access String;
45 -- Points to the gnat.adc input file
48 -- Subscript of next character to process in S
51 -- Current line number
53 Start_Of_Line
: Natural;
54 -- Subscript of first character at start of current line
56 ----------------------
57 -- Local Procedures --
58 ----------------------
60 function Acquire_String
(B
: Natural; E
: Natural) return String;
61 -- This function takes a string scanned out by Scan_String, strips
62 -- the enclosing quote characters and any internal doubled quote
63 -- characters, and returns the result as a String. The arguments
64 -- B and E are as returned from a call to Scan_String. The lower
65 -- bound of the string returned is always 1.
67 function Acquire_Unit_Name
return String;
68 -- Skips white space, and then scans and returns a unit name. The
69 -- unit name is cased exactly as it appears in the source file.
70 -- The terminating character must be white space, or a comma or
71 -- a right parenthesis or end of file.
73 function At_EOF
return Boolean;
74 pragma Inline
(At_EOF
);
75 -- Returns True if at end of file, False if not. Note that this
76 -- function does NOT skip white space, so P is always unchanged.
78 procedure Check_Not_At_EOF
;
79 pragma Inline
(Check_Not_At_EOF
);
80 -- Skips past white space if any, and then raises Error if at
81 -- end of file. Otherwise returns with P skipped past whitespace.
83 function Check_File_Type
return Character;
84 -- Skips white space if any, and then looks for any of the tokens
85 -- Spec_File_Name, Body_File_Name, or Subunit_File_Name. If one
86 -- of these is found then the value returned is 's', 'b' or 'u'
87 -- respectively, and P is bumped past the token. If none of
88 -- these tokens is found, then P is unchanged (except for
89 -- possible skip of white space), and a space is returned.
91 function Check_Token
(T
: String) return Boolean;
92 -- Skips white space if any, and then checks if the string at the
93 -- current location matches the given string T, and the character
94 -- immediately following is non-alphabetic, non-numeric. If so,
95 -- P is stepped past the token, and True is returned. If not,
96 -- P is unchanged (except for possibly skipping past whitespace),
97 -- and False is returned. S may contain only lower-case letters
100 procedure Error
(Err
: String);
101 -- Called if an error is detected. Raises Syntax_Error_In_GNAT_ADC
102 -- with a message of the form gnat.adc:line:col: xxx, where xxx is
103 -- the string Err passed as a parameter.
105 procedure Require_Token
(T
: String);
106 -- Skips white space if any, and then requires the given string
107 -- to be present. If it is, the P is stepped past it, otherwise
108 -- Error is raised, since this is a syntax error. Require_Token
109 -- is used only for sequences of special characters, so there
110 -- is no issue of terminators, or casing of letters.
112 procedure Scan_String
(B
: out Natural; E
: out Natural);
113 -- Skips white space if any, then requires that a double quote
114 -- or percent be present (start of string). Raises error if
115 -- neither of these two characters is found. Otherwise scans
116 -- out the string, and returns with P pointing past the
117 -- closing quote and S (B .. E) contains the characters of the
118 -- string (including the enclosing quotes, with internal quotes
119 -- still doubled). Raises Error if the string is malformed.
122 -- Skips P past any white space characters (end of line
123 -- characters, spaces, comments, horizontal tab characters).
129 function Acquire_String
(B
: Natural; E
: Natural) return String is
130 Str
: String (1 .. E
- B
- 1);
131 Q
: constant Character := S
(B
);
142 if S
(Ptr
) = Q
and then S
(Ptr
+ 1) = Q
then
152 -----------------------
153 -- Acquire_Unit_Name --
154 -----------------------
156 function Acquire_Unit_Name
return String is
163 while not At_EOF
loop
164 exit when S
(P
) not in '0' .. '9'
165 and then S
(P
) /= '.'
166 and then S
(P
) /= '_'
167 and then not (S
(P
) = '[' and then S
(P
+ 1) = '"')
168 and then not (S
(P
) = '"' and then S
(P
- 1) = '[')
169 and then not (S
(P
) = '"' and then S
(P
+ 1) = ']')
170 and then not (S
(P
) = ']' and then S
(P
- 1) = '"')
171 and then S
(P
) < 'A';
176 Error
("null unit name");
179 return S
(B
.. P
- 1);
180 end Acquire_Unit_Name
;
186 function At_EOF
return Boolean is
191 ---------------------
192 -- Check_File_Type --
193 ---------------------
195 function Check_File_Type
return Character is
197 if Check_Token
("spec_file_name") then
199 elsif Check_Token
("body_file_name") then
201 elsif Check_Token
("subunit_file_name") then
208 ----------------------
209 -- Check_Not_At_EOF --
210 ----------------------
212 procedure Check_Not_At_EOF
is
217 Error
("unexpected end of file");
221 end Check_Not_At_EOF
;
227 function Check_Token
(T
: String) return Boolean is
235 for K
in T
'Range loop
243 if C
in 'A' .. 'Z' then
244 C
:= Character'Val (Character'Pos (C
) +
245 (Character'Pos ('a') - Character'Pos ('A')));
263 or else C
in 'a' .. 'z'
264 or else C
in 'A' .. 'Z'
265 or else C
> Character'Val (127)
279 procedure Error
(Err
: String) is
283 M
: String (1 .. 80);
284 -- Buffer used to build resulting error msg
287 -- Pointer to last set location in M
289 procedure Add_Nat
(N
: Natural);
290 -- Add chars of integer to error msg buffer
292 procedure Add_Nat
(N
: Natural) is
299 M
(LM
) := Character'Val (N
mod 10 + Character'Pos ('0'));
302 -- Start of processing for Error
305 M
(1 .. 9) := "gnat.adc:";
311 -- Determine column number
313 for X
in Start_Of_Line
.. P
loop
317 C
:= (C
+ 7) / 8 * 8;
327 M
(LM
+ 1 .. LM
+ Err
'Length) := Err
;
328 LM
:= LM
+ Err
'Length;
330 Raise_Exception
(Syntax_Error_In_GNAT_ADC
'Identity, M
(1 .. LM
));
337 procedure Require_Token
(T
: String) is
344 for J
in T
'Range loop
346 if At_EOF
or else S
(P
) /= T
(J
) then
348 S
: String (1 .. T
'Length + 10);
351 S
(1 .. 9) := "missing """;
352 S
(10 .. T
'Length + 9) := T
;
353 S
(T
'Length + 10) := '"';
364 ----------------------
365 -- Scan_SFN_Pragmas --
366 ----------------------
368 procedure Scan_SFN_Pragmas
370 SFN_Ptr
: Set_File_Name_Ptr
;
371 SFNP_Ptr
: Set_File_Name_Pattern_Ptr
)
379 S
:= Source
'Unrestricted_Access;
383 -- Loop through pragmas in file
385 Main_Scan_Loop
: loop
387 exit Main_Scan_Loop
when At_EOF
;
389 -- Error if something other than pragma
391 if not Check_Token
("pragma") then
392 Error
("non pragma encountered");
395 -- Source_File_Name pragma case
397 if Check_Token
("source_file_name") then
400 Typ
:= Check_File_Type
;
402 -- First format, with unit name first
405 if Check_Token
("unit_name") then
406 Require_Token
("=>");
410 U
: constant String := Acquire_Unit_Name
;
414 Typ
:= Check_File_Type
;
416 if Typ
/= 's' and then Typ
/= 'b' then
417 Error
("bad pragma");
420 Require_Token
("=>");
424 F
: constant String := Acquire_String
(B
, E
);
429 SFN_Ptr
.all (Typ
, U
, F
);
433 -- Second format with pattern string
436 Require_Token
("=>");
440 Pat
: constant String := Acquire_String
(B
, E
);
444 -- Check exactly one asterisk
446 for J
in Pat
'Range loop
447 if Pat
(J
) = '*' then
453 Error
("** not allowed");
460 -- Loop to scan out Casing or Dot_Replacement parameters
464 exit when S
(P
) = ')';
467 if Check_Token
("casing") then
468 Require_Token
("=>");
471 Error
("duplicate casing argument");
472 elsif Check_Token
("lowercase") then
474 elsif Check_Token
("uppercase") then
476 elsif Check_Token
("mixedcase") then
479 Error
("invalid casing argument");
482 elsif Check_Token
("dot_replacement") then
483 Require_Token
("=>");
486 Error
("duplicate dot_replacement");
492 Error
("invalid argument");
504 SFNP_Ptr
.all (Pat
, Typ
, ".", Cas
);
508 Dot
: constant String := Acquire_String
(B
, E
);
511 SFNP_Ptr
.all (Pat
, Typ
, Dot
, Cas
);
517 -- Some other pragma, scan to semicolon at end of pragma
521 exit Main_Scan_Loop
when At_EOF
;
522 exit Skip_Loop
when S
(P
) = ';';
524 if S
(P
) = '"' or else S
(P
) = '%' then
531 -- We successfuly skipped to semicolon, so skip past it
535 end loop Main_Scan_Loop
;
539 Cursor
:= P
- S
'First + 1;
541 end Scan_SFN_Pragmas
;
547 procedure Scan_String
(B
: out Natural; E
: out Natural) is
555 elsif S
(P
) = '%' then
558 Error
("bad string");
562 -- Scan out the string, B points to first char
568 if At_EOF
or else S
(P
) = LF
or else S
(P
) = CR
then
569 Error
("missing string quote");
571 elsif S
(P
) = HT
then
572 Error
("tab character in string");
574 elsif S
(P
) /= Q
then
582 -- Check for doubled quote
584 if not At_EOF
and then S
(P
) = Q
then
587 -- Otherwise this is the terminating quote
603 WS_Scan
: while not At_EOF
loop
606 -- End of physical line
609 Line_Num
:= Line_Num
+ 1;
613 and then (S
(P
) = CR
or else S
(P
) = LF
)
615 Line_Num
:= Line_Num
+ 1;
621 -- All other cases of white space characters
623 when ' ' | FF | VT | HT
=>
632 Error
("bad comment");
634 elsif S
(P
) = '-' then
637 while not At_EOF
loop
639 when CR | LF | FF | VT
=>