1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2002-2016, 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. 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Csets
; use Csets
;
27 with Err_Vars
; use Err_Vars
;
29 with Osint
; use Osint
;
30 with Output
; use Output
;
31 with Scans
; use Scans
;
32 with Snames
; use Snames
;
34 with Stringt
; use Stringt
;
36 with Uintp
; use Uintp
;
38 with GNAT
.Heap_Sort_G
;
44 type Token_Name_Array
is array (Token_Type
) of Name_Id
;
45 Token_Names
: constant Token_Name_Array
:=
46 (Tok_Abort
=> Name_Abort
,
48 Tok_Abstract
=> Name_Abstract
,
49 Tok_Accept
=> Name_Accept
,
50 Tok_Aliased
=> Name_Aliased
,
52 Tok_Array
=> Name_Array
,
55 Tok_Begin
=> Name_Begin
,
56 Tok_Body
=> Name_Body
,
57 Tok_Case
=> Name_Case
,
58 Tok_Constant
=> Name_Constant
,
59 Tok_Declare
=> Name_Declare
,
60 Tok_Delay
=> Name_Delay
,
61 Tok_Delta
=> Name_Delta
,
62 Tok_Digits
=> Name_Digits
,
63 Tok_Else
=> Name_Else
,
64 Tok_Elsif
=> Name_Elsif
,
66 Tok_Entry
=> Name_Entry
,
67 Tok_Exception
=> Name_Exception
,
68 Tok_Exit
=> Name_Exit
,
70 Tok_Function
=> Name_Function
,
71 Tok_Generic
=> Name_Generic
,
72 Tok_Goto
=> Name_Goto
,
75 Tok_Limited
=> Name_Limited
,
76 Tok_Loop
=> Name_Loop
,
79 Tok_Null
=> Name_Null
,
82 Tok_Others
=> Name_Others
,
84 Tok_Package
=> Name_Package
,
85 Tok_Pragma
=> Name_Pragma
,
86 Tok_Private
=> Name_Private
,
87 Tok_Procedure
=> Name_Procedure
,
88 Tok_Protected
=> Name_Protected
,
89 Tok_Raise
=> Name_Raise
,
90 Tok_Range
=> Name_Range
,
91 Tok_Record
=> Name_Record
,
93 Tok_Renames
=> Name_Renames
,
94 Tok_Requeue
=> Name_Requeue
,
95 Tok_Return
=> Name_Return
,
96 Tok_Reverse
=> Name_Reverse
,
97 Tok_Select
=> Name_Select
,
98 Tok_Separate
=> Name_Separate
,
99 Tok_Subtype
=> Name_Subtype
,
100 Tok_Tagged
=> Name_Tagged
,
101 Tok_Task
=> Name_Task
,
102 Tok_Terminate
=> Name_Terminate
,
103 Tok_Then
=> Name_Then
,
104 Tok_Type
=> Name_Type
,
105 Tok_Until
=> Name_Until
,
107 Tok_When
=> Name_When
,
108 Tok_While
=> Name_While
,
109 Tok_With
=> Name_With
,
113 Already_Initialized
: Boolean := False;
114 -- Used to avoid repetition of the part of the initialisation that needs
115 -- to be done only once.
117 Empty_String
: String_Id
;
118 -- "", as a string_id
120 String_False
: String_Id
;
121 -- "false", as a string_id
127 -- Accesses to procedure specified by procedure Initialize
129 Error_Msg
: Error_Msg_Proc
;
135 Set_Ignore_Errors
: Set_Ignore_Errors_Proc
;
136 -- Indicate if error should be taken into account
138 Put_Char
: Put_Char_Proc
;
139 -- Output one character
141 New_EOL
: New_EOL_Proc
;
142 -- Output an end of line indication
144 -------------------------------
145 -- State of the Preprocessor --
146 -------------------------------
148 type Pp_State
is record
150 -- The location of the #if statement (used to flag #if with no
151 -- corresponding #end if, at the end).
153 Else_Ptr
: Source_Ptr
;
154 -- The location of the #else statement (used to detect multiple #else's)
157 -- Set to True when the code should be deleted or commented out
159 Match_Seen
: Boolean;
160 -- Set to True when a condition in an #if or an #elsif is True. Also set
161 -- to True if Deleting at the previous level is True. Used to decide if
162 -- Deleting should be set to True in a following #elsif or #else.
166 type Pp_Depth
is new Nat
;
168 Ground
: constant Pp_Depth
:= 0;
170 package Pp_States
is new Table
.Table
171 (Table_Component_Type
=> Pp_State
,
172 Table_Index_Type
=> Pp_Depth
,
173 Table_Low_Bound
=> 1,
175 Table_Increment
=> 100,
176 Table_Name
=> "Prep.Pp_States");
177 -- A stack of the states of the preprocessor, for nested #if
179 type Operator
is (None
, Op_Or
, Op_And
);
185 function Deleting
return Boolean;
186 -- Return True if code should be deleted or commented out
189 (Evaluate_It
: Boolean;
190 Complemented
: Boolean := False) return Boolean;
191 -- Evaluate a condition in an #if or an #elsif statement. If Evaluate_It
192 -- is False, the condition is effectively evaluated, otherwise, only the
193 -- syntax is checked.
195 procedure Go_To_End_Of_Line
;
196 -- Advance the scan pointer until we reach an end of line or the end of the
199 function Matching_Strings
(S1
, S2
: String_Id
) return Boolean;
200 -- Returns True if the two string parameters are equal (case insensitive)
202 ---------------------------------------
203 -- Change_Reserved_Keyword_To_Symbol --
204 ---------------------------------------
206 procedure Change_Reserved_Keyword_To_Symbol
207 (All_Keywords
: Boolean := False)
209 New_Name
: constant Name_Id
:= Token_Names
(Token
);
212 if New_Name
/= No_Name
then
223 Token
:= Tok_Identifier
;
224 Token_Name
:= New_Name
;
228 Token
:= Tok_Identifier
;
229 Token_Name
:= New_Name
;
232 end Change_Reserved_Keyword_To_Symbol
;
234 ------------------------------------------
235 -- Check_Command_Line_Symbol_Definition --
236 ------------------------------------------
238 procedure Check_Command_Line_Symbol_Definition
239 (Definition
: String;
240 Data
: out Symbol_Data
)
242 Index
: Natural := 0;
243 Result
: Symbol_Data
;
246 -- Look for the character '='
248 for J
in Definition
'Range loop
249 if Definition
(J
) = '=' then
255 -- If no character '=', then the value is True
259 -- Put the symbol in the name buffer
261 Name_Len
:= Definition
'Length;
262 Name_Buffer
(1 .. Name_Len
) := Definition
;
263 Result
:= True_Value
;
265 elsif Index
= Definition
'First then
266 Fail
("invalid symbol definition """ & Definition
& """");
269 -- Put the symbol in the name buffer
271 Name_Len
:= Index
- Definition
'First;
272 Name_Buffer
(1 .. Name_Len
) :=
273 String'(Definition (Definition'First .. Index - 1));
275 -- Check the syntax of the value
277 if Definition (Index + 1) /= '"'
278 or else Definition (Definition'Last) /= '"'
280 for J in Index + 1 .. Definition'Last loop
281 case Definition (J) is
282 when '_
' | '.' | '0' .. '9' | 'a
' .. 'z
' | 'A
' .. 'Z
' =>
286 Fail ("illegal value """
287 & Definition (Index + 1 .. Definition'Last)
293 -- Even if the value is a string, we still set Is_A_String to False,
294 -- to avoid adding additional quotes in the preprocessed sources when
295 -- replacing $<symbol>.
297 Result.Is_A_String := False;
299 -- Put the value in the result
302 Store_String_Chars (Definition (Index + 1 .. Definition'Last));
303 Result.Value := End_String;
306 -- Now, check the syntax of the symbol (we don't allow accented or
309 if Name_Buffer (1) not in 'a
' .. 'z
'
310 and then Name_Buffer (1) not in 'A
' .. 'Z
'
313 & Name_Buffer (1 .. Name_Len)
314 & """ does not start with a letter");
317 for J in 2 .. Name_Len loop
318 case Name_Buffer (J) is
319 when 'a
' .. 'z
' | 'A
' .. 'Z
' | '0' .. '9' =>
325 & Name_Buffer (1 .. Name_Len)
326 & """ end with a '_
'");
328 elsif Name_Buffer (J + 1) = '_
' then
330 & Name_Buffer (1 .. Name_Len)
331 & """ contains consecutive '_
'");
336 & Name_Buffer (1 .. Name_Len)
337 & """ contains illegal character(s)");
341 Result.On_The_Command_Line := True;
343 -- Put the symbol name in the result
346 Sym : constant String := Name_Buffer (1 .. Name_Len);
349 for Index in 1 .. Name_Len loop
350 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
353 Result.Symbol := Name_Find;
354 Name_Len := Sym'Length;
355 Name_Buffer (1 .. Name_Len) := Sym;
356 Result.Original := Name_Find;
360 end Check_Command_Line_Symbol_Definition;
366 function Deleting return Boolean is
368 -- Always return False when not inside an #if statement
370 if Opt.No_Deletion or else Pp_States.Last = Ground then
373 return Pp_States.Table (Pp_States.Last).Deleting;
382 (Evaluate_It : Boolean;
383 Complemented : Boolean := False) return Boolean
385 Evaluation : Boolean := Evaluate_It;
386 -- Is set to False after an "or else" when left term is True and after
387 -- an "and then" when left term is False.
389 Final_Result : Boolean := False;
391 Current_Result : Boolean := False;
394 Current_Operator : Operator := None;
397 Symbol_Name1 : Name_Id;
398 Symbol_Name2 : Name_Id;
399 Symbol_Pos1 : Source_Ptr;
400 Symbol_Pos2 : Source_Ptr;
401 Symbol_Value1 : String_Id;
402 Symbol_Value2 : String_Id;
407 -- Loop for each term
410 Change_Reserved_Keyword_To_Symbol;
412 Current_Result := False;
414 -- Scan current term, starting with Token
418 -- Handle parenthesized expression
420 when Tok_Left_Paren =>
422 Current_Result := Expression (Evaluation);
424 if Token = Tok_Right_Paren then
429 ("`)` expected", Token_Ptr);
432 -- Handle not expression
437 not Expression (Evaluation, Complemented => True);
439 -- Handle sequence starting with identifier
441 when Tok_Identifier =>
442 Symbol_Name1 := Token_Name;
443 Symbol_Pos1 := Token_Ptr;
446 if Token = Tok_Apostrophe then
452 if Token = Tok_Identifier
453 and then Token_Name = Name_Defined
458 Error_Msg ("identifier `Defined` expected", Token_Ptr);
462 Current_Result := Index_Of (Symbol_Name1) /= No_Symbol;
465 -- Handle relational operator
467 elsif Token = Tok_Equal
468 or else Token = Tok_Less
469 or else Token = Tok_Less_Equal
470 or else Token = Tok_Greater
471 or else Token = Tok_Greater_Equal
475 Change_Reserved_Keyword_To_Symbol;
477 if Token = Tok_Integer_Literal then
486 Value : constant Int := UI_To_Int (Int_Literal_Value);
490 -- Value of symbol as Int
494 Symbol1 := Index_Of (Symbol_Name1);
496 if Symbol1 = No_Symbol then
497 Error_Msg_Name_1 := Symbol_Name1;
498 Error_Msg ("unknown symbol %", Symbol_Pos1);
499 Symbol_Value1 := No_String;
502 Data := Mapping.Table (Symbol1);
504 if Data.Is_A_String then
505 Error_Msg_Name_1 := Symbol_Name1;
507 ("symbol % value is not integer",
512 String_To_Name_Buffer (Data.Value);
514 Int'Value (Name_Buffer (1 .. Name_Len));
519 Symbol_Value = Value;
523 Symbol_Value < Value;
525 when Tok_Less_Equal =>
527 Symbol_Value <= Value;
531 Symbol_Value > Value;
533 when Tok_Greater_Equal =>
535 Symbol_Value >= Value;
542 when Constraint_Error =>
543 Error_Msg_Name_1 := Symbol_Name1;
545 ("symbol % value is not an integer",
555 -- Error if relational operator other than = if not numbers
557 elsif Relop /= Tok_Equal then
558 Error_Msg ("number expected", Token_Ptr);
560 -- Equality comparison of two strings
562 elsif Token = Tok_Identifier then
566 Symbol_Name2 := Token_Name;
567 Symbol_Pos2 := Token_Ptr;
571 Symbol1 := Index_Of (Symbol_Name1);
573 if Symbol1 = No_Symbol then
574 if Undefined_Symbols_Are_False then
575 Symbol_Value1 := String_False;
578 Error_Msg_Name_1 := Symbol_Name1;
579 Error_Msg ("unknown symbol %", Symbol_Pos1);
580 Symbol_Value1 := No_String;
585 Mapping.Table (Symbol1).Value;
588 Symbol2 := Index_Of (Symbol_Name2);
590 if Symbol2 = No_Symbol then
591 if Undefined_Symbols_Are_False then
592 Symbol_Value2 := String_False;
595 Error_Msg_Name_1 := Symbol_Name2;
596 Error_Msg ("unknown symbol %", Symbol_Pos2);
597 Symbol_Value2 := No_String;
601 Symbol_Value2 := Mapping.Table (Symbol2).Value;
604 if Symbol_Value1 /= No_String
606 Symbol_Value2 /= No_String
609 Matching_Strings (Symbol_Value1, Symbol_Value2);
613 elsif Token = Tok_String_Literal then
618 Symbol1 := Index_Of (Symbol_Name1);
620 if Symbol1 = No_Symbol then
621 if Undefined_Symbols_Are_False then
622 Symbol_Value1 := String_False;
625 Error_Msg_Name_1 := Symbol_Name1;
626 Error_Msg ("unknown symbol %", Symbol_Pos1);
627 Symbol_Value1 := No_String;
631 Symbol_Value1 := Mapping.Table (Symbol1).Value;
634 if Symbol_Value1 /= No_String then
646 ("literal integer, symbol or literal string expected",
650 -- Handle True or False
654 Symbol1 := Index_Of (Symbol_Name1);
656 if Symbol1 = No_Symbol then
657 if Undefined_Symbols_Are_False then
658 Symbol_Value1 := String_False;
661 Error_Msg_Name_1 := Symbol_Name1;
662 Error_Msg ("unknown symbol %", Symbol_Pos1);
663 Symbol_Value1 := No_String;
667 Symbol_Value1 := Mapping.Table (Symbol1).Value;
670 if Symbol_Value1 /= No_String then
671 String_To_Name_Buffer (Symbol_Value1);
673 for Index in 1 .. Name_Len loop
674 Name_Buffer (Index) :=
675 Fold_Lower (Name_Buffer (Index));
678 if Name_Buffer (1 .. Name_Len) = "true" then
679 Current_Result := True;
681 elsif Name_Buffer (1 .. Name_Len) = "false" then
682 Current_Result := False;
685 Error_Msg_Name_1 := Symbol_Name1;
687 ("value of symbol % is not True or False",
694 -- Unrecognized sequence
697 Error_Msg ("`(`, NOT or symbol expected", Token_Ptr);
700 -- Update the cumulative final result
702 case Current_Operator is
704 Final_Result := Current_Result;
707 Final_Result := Final_Result or Current_Result;
710 Final_Result := Final_Result and Current_Result;
715 if Token = Tok_And then
718 ("mixing NOT and AND is not allowed, parentheses are required",
721 elsif Current_Operator = Op_Or then
722 Error_Msg ("mixing OR and AND is not allowed", Token_Ptr);
725 Current_Operator := Op_And;
728 if Token = Tok_Then then
731 if Final_Result = False then
738 elsif Token = Tok_Or then
741 ("mixing NOT and OR is not allowed, parentheses are required",
744 elsif Current_Operator = Op_And then
745 Error_Msg ("mixing AND and OR is not allowed", Token_Ptr);
748 Current_Operator := Op_Or;
751 if Token = Tok_Else then
759 -- No AND/OR operator, so exit from the loop through terms
769 -----------------------
770 -- Go_To_End_Of_Line --
771 -----------------------
773 procedure Go_To_End_Of_Line is
775 -- Scan until we get an end of line or we reach the end of the buffer
777 while Token /= Tok_End_Of_Line
778 and then Token /= Tok_EOF
782 end Go_To_End_Of_Line;
788 function Index_Of (Symbol : Name_Id) return Symbol_Id is
790 if Mapping.Table /= null then
791 for J in Symbol_Id range 1 .. Symbol_Table.Last (Mapping) loop
792 if Mapping.Table (J).Symbol = Symbol then
805 procedure Initialize is
807 if not Already_Initialized then
809 Store_String_Chars ("True");
810 True_Value.Value := End_String;
813 Empty_String := End_String;
816 Store_String_Chars ("False");
817 String_False := End_String;
819 Already_Initialized := True;
827 procedure List_Symbols (Foreword : String) is
828 Order : array (0 .. Integer (Symbol_Table.Last (Mapping)))
830 -- After alphabetical sorting, this array stores the indexes of the
831 -- symbols in the order they are displayed.
833 function Lt (Op1, Op2 : Natural) return Boolean;
834 -- Comparison routine for sort call
836 procedure Move (From : Natural; To : Natural);
837 -- Move routine for sort call
843 function Lt (Op1, Op2 : Natural) return Boolean is
844 S1 : constant String :=
845 Get_Name_String (Mapping.Table (Order (Op1)).Symbol);
846 S2 : constant String :=
847 Get_Name_String (Mapping.Table (Order (Op2)).Symbol);
856 procedure Move (From : Natural; To : Natural) is
858 Order (To) := Order (From);
861 package Sort_Syms is new GNAT.Heap_Sort_G (Move, Lt);
864 -- Maximum length of any symbol
866 -- Start of processing for List_Symbols_Case
869 if Symbol_Table.Last (Mapping) = 0 then
873 if Foreword'Length > 0 then
875 Write_Line (Foreword);
877 for J in Foreword'Range loop
882 -- Initialize the order
884 for J in Order'Range loop
885 Order (J) := Symbol_Id (J);
888 -- Sort alphabetically
890 Sort_Syms.Sort (Order'Last);
894 for J in 1 .. Symbol_Table.Last (Mapping) loop
895 Get_Name_String (Mapping.Table (J).Original);
896 Max_L := Integer'Max (Max_L, Name_Len);
900 Write_Str ("Symbol");
902 for J in 1 .. Max_L - 5 loop
906 Write_Line ("Value");
908 Write_Str ("------");
910 for J in 1 .. Max_L - 5 loop
914 Write_Line ("------");
916 for J in 1 .. Order'Last loop
918 Data : constant Symbol_Data := Mapping.Table (Order (J));
921 Get_Name_String (Data.Original);
922 Write_Str (Name_Buffer (1 .. Name_Len));
924 for K in Name_Len .. Max_L loop
928 String_To_Name_Buffer (Data.Value);
930 if Data.Is_A_String then
933 for J in 1 .. Name_Len loop
934 Write_Char (Name_Buffer (J));
936 if Name_Buffer (J) = '"' then
944 Write_Str (Name_Buffer (1 .. Name_Len));
954 ----------------------
955 -- Matching_Strings --
956 ----------------------
958 function Matching_Strings (S1, S2 : String_Id) return Boolean is
960 String_To_Name_Buffer (S1);
962 for Index in 1 .. Name_Len loop
963 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
967 String1 : constant String := Name_Buffer (1 .. Name_Len);
970 String_To_Name_Buffer (S2);
972 for Index in 1 .. Name_Len loop
973 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
976 return String1 = Name_Buffer (1 .. Name_Len);
978 end Matching_Strings;
984 -- This procedure REALLY needs some more comments ???
986 procedure Parse_Def_File is
988 Symbol_Name : Name_Id;
989 Original_Name : Name_Id;
991 Value_Start : Source_Ptr;
992 Value_End : Source_Ptr;
1002 exit Def_Line_Loop when Token = Tok_EOF;
1004 if Token /= Tok_End_Of_Line then
1005 Change_Reserved_Keyword_To_Symbol;
1007 if Token /= Tok_Identifier then
1008 Error_Msg ("identifier expected", Token_Ptr);
1012 Symbol_Name := Token_Name;
1015 for Ptr in Token_Ptr .. Scan_Ptr - 1 loop
1016 Name_Len := Name_Len + 1;
1017 Name_Buffer (Name_Len) := Sinput.Source (Ptr);
1020 Original_Name := Name_Find;
1023 if Token /= Tok_Colon_Equal then
1024 Error_Msg -- CODEFIX
1025 ("`:=` expected", Token_Ptr);
1031 if Token = Tok_Integer_Literal then
1033 Ptr : Source_Ptr := Token_Ptr;
1037 while Ptr < Scan_Ptr loop
1038 Store_String_Char (Sinput.Source (Ptr));
1042 Data := (Symbol => Symbol_Name,
1043 Original => Original_Name,
1044 On_The_Command_Line => False,
1045 Is_A_String => False,
1046 Value => End_String);
1051 if Token /= Tok_End_Of_Line and then Token /= Tok_EOF then
1052 Error_Msg ("extraneous text in definition", Token_Ptr);
1056 elsif Token = Tok_String_Literal then
1057 Data := (Symbol => Symbol_Name,
1058 Original => Original_Name,
1059 On_The_Command_Line => False,
1060 Is_A_String => True,
1061 Value => String_Literal_Id);
1065 if Token /= Tok_End_Of_Line and then Token /= Tok_EOF then
1066 Error_Msg ("extraneous text in definition", Token_Ptr);
1070 elsif Token = Tok_End_Of_Line or else Token = Tok_EOF then
1071 Data := (Symbol => Symbol_Name,
1072 Original => Original_Name,
1073 On_The_Command_Line => False,
1074 Is_A_String => False,
1075 Value => Empty_String);
1078 Value_Start := Token_Ptr;
1079 Value_End := Token_Ptr - 1;
1080 Scan_Ptr := Token_Ptr;
1084 Ch := Sinput.Source (Scan_Ptr);
1087 when '_
' | '.' | '0' .. '9' | 'a
' .. 'z
' | 'A
' .. 'Z
' =>
1088 Value_End := Scan_Ptr;
1089 Scan_Ptr := Scan_Ptr + 1;
1091 when ' ' | HT | VT | CR | LF | FF =>
1092 exit Value_Chars_Loop;
1095 Error_Msg ("illegal character", Scan_Ptr);
1098 end loop Value_Chars_Loop;
1102 if Token /= Tok_End_Of_Line and then Token /= Tok_EOF then
1103 Error_Msg ("extraneous text in definition", Token_Ptr);
1109 while Value_Start <= Value_End loop
1110 Store_String_Char (Sinput.Source (Value_Start));
1111 Value_Start := Value_Start + 1;
1114 Data := (Symbol => Symbol_Name,
1115 Original => Original_Name,
1116 On_The_Command_Line => False,
1117 Is_A_String => False,
1118 Value => End_String);
1121 -- Now that we have the value, get the symbol index
1123 Symbol := Index_Of (Symbol_Name);
1125 if Symbol /= No_Symbol then
1127 -- If we already have an entry for this symbol, replace it
1128 -- with the new value, except if the symbol was declared on
1129 -- the command line.
1131 if Mapping.Table (Symbol).On_The_Command_Line then
1136 -- As it is the first time we see this symbol, create a new
1137 -- entry in the table.
1139 if Mapping.Table = null then
1140 Symbol_Table.Init (Mapping);
1143 Symbol_Table.Increment_Last (Mapping);
1144 Symbol := Symbol_Table.Last (Mapping);
1147 Mapping.Table (Symbol) := Data;
1151 Set_Ignore_Errors (To => True);
1153 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
1157 Set_Ignore_Errors (To => False);
1162 end loop Def_Line_Loop;
1169 procedure Preprocess (Source_Modified : out Boolean) is
1170 Start_Of_Processing : Source_Ptr;
1172 Preprocessor_Line : Boolean := False;
1173 No_Error_Found : Boolean := True;
1174 Modified : Boolean := False;
1176 procedure Output (From, To : Source_Ptr);
1177 -- Output the characters with indexes From .. To in the buffer to the
1180 procedure Output_Line (From, To : Source_Ptr);
1181 -- Output a line or the end of a line from the buffer to the output
1182 -- file, followed by an end of line terminator. Depending on the value
1183 -- of Deleting and the switches, the line may be commented out, blank or
1184 -- not output at all.
1190 procedure Output (From, To : Source_Ptr) is
1192 for J in From .. To loop
1193 Put_Char (Sinput.Source (J));
1201 procedure Output_Line (From, To : Source_Ptr) is
1203 if Deleting or else Preprocessor_Line then
1204 if Blank_Deleted_Lines then
1207 elsif Comment_Deleted_Lines then
1226 -- Start of processing for Preprocess
1229 Start_Of_Processing := Scan_Ptr;
1231 -- First a call to Scan, because Initialize_Scanner is not doing it
1235 Input_Line_Loop : loop
1236 exit Input_Line_Loop when Token = Tok_EOF;
1238 Preprocessor_Line := False;
1240 if Token /= Tok_End_Of_Line then
1242 -- Preprocessor line
1244 if Token = Tok_Special and then Special_Character = '#
' then
1246 Preprocessor_Line := True;
1255 If_Ptr : constant Source_Ptr := Token_Ptr;
1259 Cond := Expression (not Deleting);
1261 -- Check for an eventual "then"
1263 if Token = Tok_Then then
1267 -- It is an error to have trailing characters after
1268 -- the condition or "then".
1270 if Token /= Tok_End_Of_Line
1271 and then Token /= Tok_EOF
1274 ("extraneous text on preprocessor line",
1276 No_Error_Found := False;
1281 -- Set the initial state of this new "#if". This
1282 -- must be done before incrementing the Last of
1283 -- the table, otherwise function Deleting does
1284 -- not report the correct value.
1286 New_State : constant Pp_State :=
1289 Deleting => Deleting
1291 Match_Seen => Deleting or else Cond);
1294 Pp_States.Increment_Last;
1295 Pp_States.Table (Pp_States.Last) := New_State;
1304 if Pp_States.Last = 0
1305 or else Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1307 Error_Msg ("no IF for this ELSIF", Token_Ptr);
1308 No_Error_Found := False;
1312 not Pp_States.Table (Pp_States.Last).Match_Seen;
1316 Cond := Expression (Cond);
1318 -- Check for an eventual "then"
1320 if Token = Tok_Then then
1324 -- It is an error to have trailing characters after the
1325 -- condition or "then".
1327 if Token /= Tok_End_Of_Line
1328 and then Token /= Tok_EOF
1331 ("extraneous text on preprocessor line",
1333 No_Error_Found := False;
1338 -- Depending on the value of the condition, set the new
1339 -- values of Deleting and Match_Seen.
1341 if Pp_States.Last > 0 then
1342 if Pp_States.Table (Pp_States.Last).Match_Seen then
1343 Pp_States.Table (Pp_States.Last).Deleting := True;
1346 Pp_States.Table (Pp_States.Last).Match_Seen :=
1348 Pp_States.Table (Pp_States.Last).Deleting :=
1357 if Pp_States.Last = 0 then
1358 Error_Msg ("no IF for this ELSE", Token_Ptr);
1359 No_Error_Found := False;
1362 Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1364 Error_Msg -- CODEFIX
1365 ("duplicate ELSE line", Token_Ptr);
1366 No_Error_Found := False;
1369 -- Set the possibly new values of Deleting and Match_Seen
1371 if Pp_States.Last > 0 then
1372 if Pp_States.Table (Pp_States.Last).Match_Seen then
1373 Pp_States.Table (Pp_States.Last).Deleting :=
1377 Pp_States.Table (Pp_States.Last).Match_Seen :=
1379 Pp_States.Table (Pp_States.Last).Deleting :=
1383 -- Set the Else_Ptr to check for illegal #elsif later
1385 Pp_States.Table (Pp_States.Last).Else_Ptr :=
1391 -- Error of character present after "#else"
1393 if Token /= Tok_End_Of_Line
1394 and then Token /= Tok_EOF
1397 ("extraneous text on preprocessor line",
1399 No_Error_Found := False;
1406 if Pp_States.Last = 0 then
1407 Error_Msg ("no IF for this END", Token_Ptr);
1408 No_Error_Found := False;
1413 if Token /= Tok_If then
1414 Error_Msg -- CODEFIX
1415 ("IF expected", Token_Ptr);
1416 No_Error_Found := False;
1421 if Token /= Tok_Semicolon then
1422 Error_Msg -- CODEFIX
1423 ("`;` Expected", Token_Ptr);
1424 No_Error_Found := False;
1429 -- Error of character present after "#end if;"
1431 if Token /= Tok_End_Of_Line
1432 and then Token /= Tok_EOF
1435 ("extraneous text on preprocessor line",
1437 No_Error_Found := False;
1442 -- In case of one of the errors above, skip the tokens
1443 -- until the end of line is reached.
1447 -- Decrement the depth of the #if stack
1449 if Pp_States.Last > 0 then
1450 Pp_States.Decrement_Last;
1453 -- Illegal preprocessor line
1456 No_Error_Found := False;
1458 if Pp_States.Last = 0 then
1459 Error_Msg -- CODEFIX
1460 ("IF expected", Token_Ptr);
1463 Pp_States.Table (Pp_States.Last).Else_Ptr = 0
1466 ("IF, ELSIF, ELSE, or `END IF` expected",
1470 Error_Msg ("IF or `END IF` expected", Token_Ptr);
1473 -- Skip to the end of this illegal line
1478 -- Not a preprocessor line
1481 -- Do not report errors for those lines, even if there are
1482 -- Ada parsing errors.
1484 Set_Ignore_Errors (To => True);
1490 while Token /= Tok_End_Of_Line
1491 and then Token /= Tok_EOF
1493 if Token = Tok_Special
1494 and then Special_Character = '$
'
1499 Dollar_Ptr : constant Source_Ptr := Token_Ptr;
1504 Change_Reserved_Keyword_To_Symbol;
1506 if Token = Tok_Identifier
1507 and then Token_Ptr = Dollar_Ptr + 1
1511 Symbol := Index_Of (Token_Name);
1513 -- If symbol exists, replace by its value
1515 if Symbol /= No_Symbol then
1516 Output (Start_Of_Processing, Dollar_Ptr - 1);
1517 Start_Of_Processing := Scan_Ptr;
1518 String_To_Name_Buffer
1519 (Mapping.Table (Symbol).Value);
1521 if Mapping.Table (Symbol).Is_A_String then
1523 -- Value is an Ada string
1527 for J in 1 .. Name_Len loop
1528 Put_Char (Name_Buffer (J));
1530 if Name_Buffer (J) = '"' then
1538 -- Value is a sequence of characters, not
1541 for J in 1 .. Name_Len loop
1542 Put_Char (Name_Buffer (J));
1554 Set_Ignore_Errors (To => False);
1558 pragma Assert (Token = Tok_End_Of_Line or else Token = Tok_EOF);
1560 -- At this point, the token is either end of line or EOF. The line to
1561 -- possibly output stops just before the token.
1563 Output_Line (Start_Of_Processing, Token_Ptr - 1);
1565 -- If we are at the end of a line, the scan pointer is at the first
1566 -- non-blank character (may not be the first character of the line),
1567 -- so we have to deduct Start_Of_Processing from the token pointer.
1569 if Token = Tok_End_Of_Line then
1570 if Sinput.Source (Token_Ptr) = ASCII.CR
1571 and then Sinput.Source (Token_Ptr + 1) = ASCII.LF
1573 Start_Of_Processing := Token_Ptr + 2;
1575 Start_Of_Processing := Token_Ptr + 1;
1579 -- Now, scan the first token of the next line. If the token is EOF,
1580 -- the scan pointer will not move, and the token will still be EOF.
1582 Set_Ignore_Errors (To => True);
1584 Set_Ignore_Errors (To => False);
1585 end loop Input_Line_Loop;
1587 -- Report an error for any missing some "#end if;"
1589 for Level in reverse 1 .. Pp_States.Last loop
1590 Error_Msg ("no `END IF` for this IF", Pp_States.Table (Level).If_Ptr);
1591 No_Error_Found := False;
1594 Source_Modified := No_Error_Found and Modified;
1601 procedure Setup_Hooks
1602 (Error_Msg : Error_Msg_Proc;
1604 Set_Ignore_Errors : Set_Ignore_Errors_Proc;
1605 Put_Char : Put_Char_Proc;
1606 New_EOL : New_EOL_Proc)
1609 pragma Assert (Already_Initialized);
1611 Prep.Error_Msg := Error_Msg;
1613 Prep.Set_Ignore_Errors := Set_Ignore_Errors;
1614 Prep.Put_Char := Put_Char;
1615 Prep.New_EOL := New_EOL;