1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2002-2012, 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
;
37 with GNAT
.Heap_Sort_G
;
43 type Token_Name_Array
is array (Token_Type
) of Name_Id
;
44 Token_Names
: constant Token_Name_Array
:=
45 (Tok_Abort
=> Name_Abort
,
47 Tok_Abstract
=> Name_Abstract
,
48 Tok_Accept
=> Name_Accept
,
49 Tok_Aliased
=> Name_Aliased
,
51 Tok_Array
=> Name_Array
,
54 Tok_Begin
=> Name_Begin
,
55 Tok_Body
=> Name_Body
,
56 Tok_Case
=> Name_Case
,
57 Tok_Constant
=> Name_Constant
,
58 Tok_Declare
=> Name_Declare
,
59 Tok_Delay
=> Name_Delay
,
60 Tok_Delta
=> Name_Delta
,
61 Tok_Digits
=> Name_Digits
,
62 Tok_Else
=> Name_Else
,
63 Tok_Elsif
=> Name_Elsif
,
65 Tok_Entry
=> Name_Entry
,
66 Tok_Exception
=> Name_Exception
,
67 Tok_Exit
=> Name_Exit
,
69 Tok_Function
=> Name_Function
,
70 Tok_Generic
=> Name_Generic
,
71 Tok_Goto
=> Name_Goto
,
74 Tok_Limited
=> Name_Limited
,
75 Tok_Loop
=> Name_Loop
,
78 Tok_Null
=> Name_Null
,
81 Tok_Others
=> Name_Others
,
83 Tok_Package
=> Name_Package
,
84 Tok_Pragma
=> Name_Pragma
,
85 Tok_Private
=> Name_Private
,
86 Tok_Procedure
=> Name_Procedure
,
87 Tok_Protected
=> Name_Protected
,
88 Tok_Raise
=> Name_Raise
,
89 Tok_Range
=> Name_Range
,
90 Tok_Record
=> Name_Record
,
92 Tok_Renames
=> Name_Renames
,
93 Tok_Requeue
=> Name_Requeue
,
94 Tok_Return
=> Name_Return
,
95 Tok_Reverse
=> Name_Reverse
,
96 Tok_Select
=> Name_Select
,
97 Tok_Separate
=> Name_Separate
,
98 Tok_Subtype
=> Name_Subtype
,
99 Tok_Tagged
=> Name_Tagged
,
100 Tok_Task
=> Name_Task
,
101 Tok_Terminate
=> Name_Terminate
,
102 Tok_Then
=> Name_Then
,
103 Tok_Type
=> Name_Type
,
104 Tok_Until
=> Name_Until
,
106 Tok_When
=> Name_When
,
107 Tok_While
=> Name_While
,
108 Tok_With
=> Name_With
,
112 Already_Initialized
: Boolean := False;
113 -- Used to avoid repetition of the part of the initialisation that needs
114 -- to be done only once.
116 Empty_String
: String_Id
;
117 -- "", as a string_id
119 String_False
: String_Id
;
120 -- "false", as a string_id
126 -- Accesses to procedure specified by procedure Initialize
128 Error_Msg
: Error_Msg_Proc
;
134 Set_Ignore_Errors
: Set_Ignore_Errors_Proc
;
135 -- Indicate if error should be taken into account
137 Put_Char
: Put_Char_Proc
;
138 -- Output one character
140 New_EOL
: New_EOL_Proc
;
141 -- Output an end of line indication
143 -------------------------------
144 -- State of the Preprocessor --
145 -------------------------------
147 type Pp_State
is record
149 -- The location of the #if statement.
150 -- Used to flag #if with no corresponding #end if, at the end.
152 Else_Ptr
: Source_Ptr
;
153 -- The location of the #else statement.
154 -- Used to detect multiple #else.
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.
161 -- Also set to True if Deleting at the previous level is True.
162 -- Used to decide if Deleting should be set to True in a following
167 type Pp_Depth
is new Nat
;
169 Ground
: constant Pp_Depth
:= 0;
171 package Pp_States
is new Table
.Table
172 (Table_Component_Type
=> Pp_State
,
173 Table_Index_Type
=> Pp_Depth
,
174 Table_Low_Bound
=> 1,
176 Table_Increment
=> 100,
177 Table_Name
=> "Prep.Pp_States");
178 -- A stack of the states of the preprocessor, for nested #if
180 type Operator
is (None
, Op_Or
, Op_And
);
186 function Deleting
return Boolean;
187 -- Return True if code should be deleted or commented out
190 (Evaluate_It
: Boolean;
191 Complemented
: Boolean := False) return Boolean;
192 -- Evaluate a condition in an #if or an #elsif statement.
193 -- If Evaluate_It is False, the condition is effectively evaluated,
194 -- otherwise, only the syntax is checked.
196 procedure Go_To_End_Of_Line
;
197 -- Advance the scan pointer until we reach an end of line or the end
200 function Matching_Strings
(S1
, S2
: String_Id
) return Boolean;
201 -- Returns True if the two string parameters are equal (case insensitive)
203 ---------------------------------------
204 -- Change_Reserved_Keyword_To_Symbol --
205 ---------------------------------------
207 procedure Change_Reserved_Keyword_To_Symbol
208 (All_Keywords
: Boolean := False)
210 New_Name
: constant Name_Id
:= Token_Names
(Token
);
213 if New_Name
/= No_Name
then
215 when Tok_If | Tok_Else | Tok_Elsif | Tok_End |
216 Tok_And | Tok_Or | Tok_Then
=>
218 Token
:= Tok_Identifier
;
219 Token_Name
:= New_Name
;
223 Token
:= Tok_Identifier
;
224 Token_Name
:= New_Name
;
227 end Change_Reserved_Keyword_To_Symbol
;
229 ------------------------------------------
230 -- Check_Command_Line_Symbol_Definition --
231 ------------------------------------------
233 procedure Check_Command_Line_Symbol_Definition
234 (Definition
: String;
235 Data
: out Symbol_Data
)
237 Index
: Natural := 0;
238 Result
: Symbol_Data
;
241 -- Look for the character '='
243 for J
in Definition
'Range loop
244 if Definition
(J
) = '=' then
250 -- If no character '=', then the value is True
253 -- Put the symbol in the name buffer
255 Name_Len
:= Definition
'Length;
256 Name_Buffer
(1 .. Name_Len
) := Definition
;
257 Result
:= True_Value
;
259 elsif Index
= Definition
'First then
260 Fail
("invalid symbol definition """ & Definition
& """");
263 -- Put the symbol in the name buffer
265 Name_Len
:= Index
- Definition
'First;
266 Name_Buffer
(1 .. Name_Len
) :=
267 String'(Definition (Definition'First .. Index - 1));
269 -- Check the syntax of the value
271 if Definition (Index + 1) /= '"'
272 or else Definition (Definition'Last) /= '"'
274 for J in Index + 1 .. Definition'Last loop
275 case Definition (J) is
276 when '_
' | '.' | '0' .. '9' | 'a
' .. 'z
' | 'A
' .. 'Z
' =>
280 Fail ("illegal value """
281 & Definition (Index + 1 .. Definition'Last)
287 -- And put the value in the result
289 Result.Is_A_String := False;
291 Store_String_Chars (Definition (Index + 1 .. Definition'Last));
292 Result.Value := End_String;
295 -- Now, check the syntax of the symbol (we don't allow accented or
298 if Name_Buffer (1) not in 'a
' .. 'z
'
299 and then Name_Buffer (1) not in 'A
' .. 'Z
'
302 & Name_Buffer (1 .. Name_Len)
303 & """ does not start with a letter");
306 for J in 2 .. Name_Len loop
307 case Name_Buffer (J) is
308 when 'a
' .. 'z
' | 'A
' .. 'Z
' | '0' .. '9' =>
314 & Name_Buffer (1 .. Name_Len)
315 & """ end with a '_
'");
317 elsif Name_Buffer (J + 1) = '_
' then
319 & Name_Buffer (1 .. Name_Len)
320 & """ contains consecutive '_
'");
325 & Name_Buffer (1 .. Name_Len)
326 & """ contains illegal character(s)");
330 Result.On_The_Command_Line := True;
332 -- Put the symbol name in the result
335 Sym : constant String := Name_Buffer (1 .. Name_Len);
338 for Index in 1 .. Name_Len loop
339 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
342 Result.Symbol := Name_Find;
343 Name_Len := Sym'Length;
344 Name_Buffer (1 .. Name_Len) := Sym;
345 Result.Original := Name_Find;
349 end Check_Command_Line_Symbol_Definition;
355 function Deleting return Boolean is
357 -- Always return False when not inside an #if statement
359 if Opt.No_Deletion or else Pp_States.Last = Ground then
362 return Pp_States.Table (Pp_States.Last).Deleting;
371 (Evaluate_It : Boolean;
372 Complemented : Boolean := False) return Boolean
374 Evaluation : Boolean := Evaluate_It;
375 -- Is set to False after an "or else" when left term is True and
376 -- after an "and then" when left term is False.
378 Final_Result : Boolean := False;
380 Current_Result : Boolean := False;
383 Current_Operator : Operator := None;
386 Symbol_Name1 : Name_Id;
387 Symbol_Name2 : Name_Id;
388 Symbol_Pos1 : Source_Ptr;
389 Symbol_Pos2 : Source_Ptr;
390 Symbol_Value1 : String_Id;
391 Symbol_Value2 : String_Id;
394 -- Loop for each term
397 Change_Reserved_Keyword_To_Symbol;
399 Current_Result := False;
403 when Tok_Left_Paren =>
408 Current_Result := Expression (Evaluation);
410 if Token = Tok_Right_Paren then
415 ("`)` expected", Token_Ptr);
424 not Expression (Evaluation, Complemented => True);
426 when Tok_Identifier =>
427 Symbol_Name1 := Token_Name;
428 Symbol_Pos1 := Token_Ptr;
431 if Token = Tok_Apostrophe then
437 if Token = Tok_Identifier
438 and then Token_Name = Name_Defined
443 Error_Msg ("identifier `Defined` expected", Token_Ptr);
447 Current_Result := Index_Of (Symbol_Name1) /= No_Symbol;
450 elsif Token = Tok_Equal then
453 Change_Reserved_Keyword_To_Symbol;
455 if Token = Tok_Identifier then
459 Symbol_Name2 := Token_Name;
460 Symbol_Pos2 := Token_Ptr;
464 Symbol1 := Index_Of (Symbol_Name1);
466 if Symbol1 = No_Symbol then
467 if Undefined_Symbols_Are_False then
468 Symbol_Value1 := String_False;
471 Error_Msg_Name_1 := Symbol_Name1;
472 Error_Msg ("unknown symbol %", Symbol_Pos1);
473 Symbol_Value1 := No_String;
478 Mapping.Table (Symbol1).Value;
481 Symbol2 := Index_Of (Symbol_Name2);
483 if Symbol2 = No_Symbol then
484 if Undefined_Symbols_Are_False then
485 Symbol_Value2 := String_False;
488 Error_Msg_Name_1 := Symbol_Name2;
489 Error_Msg ("unknown symbol %", Symbol_Pos2);
490 Symbol_Value2 := No_String;
494 Symbol_Value2 := Mapping.Table (Symbol2).Value;
497 if Symbol_Value1 /= No_String
498 and then Symbol_Value2 /= No_String
500 Current_Result := Matching_Strings
501 (Symbol_Value1, Symbol_Value2);
505 elsif Token = Tok_String_Literal then
510 Symbol1 := Index_Of (Symbol_Name1);
512 if Symbol1 = No_Symbol then
513 if Undefined_Symbols_Are_False then
514 Symbol_Value1 := String_False;
517 Error_Msg_Name_1 := Symbol_Name1;
518 Error_Msg ("unknown symbol %", Symbol_Pos1);
519 Symbol_Value1 := No_String;
523 Symbol_Value1 := Mapping.Table (Symbol1).Value;
526 if Symbol_Value1 /= No_String then
538 ("symbol or literal string expected", Token_Ptr);
542 -- symbol (True or False)
545 Symbol1 := Index_Of (Symbol_Name1);
547 if Symbol1 = No_Symbol then
548 if Undefined_Symbols_Are_False then
549 Symbol_Value1 := String_False;
552 Error_Msg_Name_1 := Symbol_Name1;
553 Error_Msg ("unknown symbol %", Symbol_Pos1);
554 Symbol_Value1 := No_String;
558 Symbol_Value1 := Mapping.Table (Symbol1).Value;
561 if Symbol_Value1 /= No_String then
562 String_To_Name_Buffer (Symbol_Value1);
564 for Index in 1 .. Name_Len loop
565 Name_Buffer (Index) :=
566 Fold_Lower (Name_Buffer (Index));
569 if Name_Buffer (1 .. Name_Len) = "true" then
570 Current_Result := True;
572 elsif Name_Buffer (1 .. Name_Len) = "false" then
573 Current_Result := False;
576 Error_Msg_Name_1 := Symbol_Name1;
578 ("value of symbol % is not True or False",
586 Error_Msg ("`(`, NOT or symbol expected", Token_Ptr);
589 -- Update the cumulative final result
591 case Current_Operator is
593 Final_Result := Current_Result;
596 Final_Result := Final_Result or Current_Result;
599 Final_Result := Final_Result and Current_Result;
602 -- Check the next operator
604 if Token = Tok_And then
607 ("mixing NOT and AND is not allowed, parentheses are required",
610 elsif Current_Operator = Op_Or then
611 Error_Msg ("mixing OR and AND is not allowed", Token_Ptr);
614 Current_Operator := Op_And;
617 if Token = Tok_Then then
620 if Final_Result = False then
625 elsif Token = Tok_Or then
628 ("mixing NOT and OR is not allowed, parentheses are required",
631 elsif Current_Operator = Op_And then
632 Error_Msg ("mixing AND and OR is not allowed", Token_Ptr);
635 Current_Operator := Op_Or;
638 if Token = Tok_Else then
647 -- No operator: exit the term loop
656 -----------------------
657 -- Go_To_End_Of_Line --
658 -----------------------
660 procedure Go_To_End_Of_Line is
662 -- Scan until we get an end of line or we reach the end of the buffer
664 while Token /= Tok_End_Of_Line
665 and then Token /= Tok_EOF
669 end Go_To_End_Of_Line;
675 function Index_Of (Symbol : Name_Id) return Symbol_Id is
677 if Mapping.Table /= null then
678 for J in Symbol_Id range 1 .. Symbol_Table.Last (Mapping) loop
679 if Mapping.Table (J).Symbol = Symbol then
692 procedure Initialize is
694 if not Already_Initialized then
696 Store_String_Chars ("True");
697 True_Value.Value := End_String;
700 Empty_String := End_String;
703 Store_String_Chars ("False");
704 String_False := End_String;
706 Already_Initialized := True;
714 procedure List_Symbols (Foreword : String) is
715 Order : array (0 .. Integer (Symbol_Table.Last (Mapping)))
717 -- After alphabetical sorting, this array stores the indexes of the
718 -- symbols in the order they are displayed.
720 function Lt (Op1, Op2 : Natural) return Boolean;
721 -- Comparison routine for sort call
723 procedure Move (From : Natural; To : Natural);
724 -- Move routine for sort call
730 function Lt (Op1, Op2 : Natural) return Boolean is
731 S1 : constant String :=
732 Get_Name_String (Mapping.Table (Order (Op1)).Symbol);
733 S2 : constant String :=
734 Get_Name_String (Mapping.Table (Order (Op2)).Symbol);
744 procedure Move (From : Natural; To : Natural) is
746 Order (To) := Order (From);
749 package Sort_Syms is new GNAT.Heap_Sort_G (Move, Lt);
752 -- Maximum length of any symbol
754 -- Start of processing for List_Symbols_Case
757 if Symbol_Table.Last (Mapping) = 0 then
761 if Foreword'Length > 0 then
763 Write_Line (Foreword);
765 for J in Foreword'Range loop
770 -- Initialize the order
772 for J in Order'Range loop
773 Order (J) := Symbol_Id (J);
776 -- Sort alphabetically
778 Sort_Syms.Sort (Order'Last);
782 for J in 1 .. Symbol_Table.Last (Mapping) loop
783 Get_Name_String (Mapping.Table (J).Original);
784 Max_L := Integer'Max (Max_L, Name_Len);
788 Write_Str ("Symbol");
790 for J in 1 .. Max_L - 5 loop
794 Write_Line ("Value");
796 Write_Str ("------");
798 for J in 1 .. Max_L - 5 loop
802 Write_Line ("------");
804 for J in 1 .. Order'Last loop
806 Data : constant Symbol_Data := Mapping.Table (Order (J));
809 Get_Name_String (Data.Original);
810 Write_Str (Name_Buffer (1 .. Name_Len));
812 for K in Name_Len .. Max_L loop
816 String_To_Name_Buffer (Data.Value);
818 if Data.Is_A_String then
821 for J in 1 .. Name_Len loop
822 Write_Char (Name_Buffer (J));
824 if Name_Buffer (J) = '"' then
832 Write_Str (Name_Buffer (1 .. Name_Len));
842 ----------------------
843 -- Matching_Strings --
844 ----------------------
846 function Matching_Strings (S1, S2 : String_Id) return Boolean is
848 String_To_Name_Buffer (S1);
850 for Index in 1 .. Name_Len loop
851 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
855 String1 : constant String := Name_Buffer (1 .. Name_Len);
858 String_To_Name_Buffer (S2);
860 for Index in 1 .. Name_Len loop
861 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
864 return String1 = Name_Buffer (1 .. Name_Len);
866 end Matching_Strings;
872 procedure Parse_Def_File is
874 Symbol_Name : Name_Id;
875 Original_Name : Name_Id;
877 Value_Start : Source_Ptr;
878 Value_End : Source_Ptr;
888 exit Def_Line_Loop when Token = Tok_EOF;
890 if Token /= Tok_End_Of_Line then
891 Change_Reserved_Keyword_To_Symbol;
893 if Token /= Tok_Identifier then
894 Error_Msg ("identifier expected", Token_Ptr);
898 Symbol_Name := Token_Name;
901 for Ptr in Token_Ptr .. Scan_Ptr - 1 loop
902 Name_Len := Name_Len + 1;
903 Name_Buffer (Name_Len) := Sinput.Source (Ptr);
906 Original_Name := Name_Find;
909 if Token /= Tok_Colon_Equal then
911 ("`:=` expected", Token_Ptr);
917 if Token = Tok_String_Literal then
918 Data := (Symbol => Symbol_Name,
919 Original => Original_Name,
920 On_The_Command_Line => False,
922 Value => String_Literal_Id);
926 if Token /= Tok_End_Of_Line and then Token /= Tok_EOF then
927 Error_Msg ("extraneous text in definition", Token_Ptr);
931 elsif Token = Tok_End_Of_Line or else Token = Tok_EOF then
932 Data := (Symbol => Symbol_Name,
933 Original => Original_Name,
934 On_The_Command_Line => False,
935 Is_A_String => False,
936 Value => Empty_String);
939 Value_Start := Token_Ptr;
940 Value_End := Token_Ptr - 1;
941 Scan_Ptr := Token_Ptr;
945 Ch := Sinput.Source (Scan_Ptr);
948 when '_
' | '.' | '0' .. '9' | 'a
' .. 'z
' | 'A
' .. 'Z
' =>
949 Value_End := Scan_Ptr;
950 Scan_Ptr := Scan_Ptr + 1;
952 when ' ' | HT | VT | CR | LF | FF =>
953 exit Value_Chars_Loop;
956 Error_Msg ("illegal character", Scan_Ptr);
959 end loop Value_Chars_Loop;
963 if Token /= Tok_End_Of_Line and then Token /= Tok_EOF then
964 Error_Msg ("extraneous text in definition", Token_Ptr);
970 while Value_Start <= Value_End loop
971 Store_String_Char (Sinput.Source (Value_Start));
972 Value_Start := Value_Start + 1;
975 Data := (Symbol => Symbol_Name,
976 Original => Original_Name,
977 On_The_Command_Line => False,
978 Is_A_String => False,
979 Value => End_String);
982 -- Now that we have the value, get the symbol index
984 Symbol := Index_Of (Symbol_Name);
986 if Symbol /= No_Symbol then
987 -- If we already have an entry for this symbol, replace it
988 -- with the new value, except if the symbol was declared
989 -- on the command line.
991 if Mapping.Table (Symbol).On_The_Command_Line then
996 -- As it is the first time we see this symbol, create a new
997 -- entry in the table.
999 if Mapping.Table = null then
1000 Symbol_Table.Init (Mapping);
1003 Symbol_Table.Increment_Last (Mapping);
1004 Symbol := Symbol_Table.Last (Mapping);
1007 Mapping.Table (Symbol) := Data;
1011 Set_Ignore_Errors (To => True);
1013 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
1017 Set_Ignore_Errors (To => False);
1022 end loop Def_Line_Loop;
1029 procedure Preprocess (Source_Modified : out Boolean) is
1030 Start_Of_Processing : Source_Ptr;
1032 Preprocessor_Line : Boolean := False;
1033 No_Error_Found : Boolean := True;
1034 Modified : Boolean := False;
1036 procedure Output (From, To : Source_Ptr);
1037 -- Output the characters with indexes From .. To in the buffer to the
1040 procedure Output_Line (From, To : Source_Ptr);
1041 -- Output a line or the end of a line from the buffer to the output
1042 -- file, followed by an end of line terminator. Depending on the value
1043 -- of Deleting and the switches, the line may be commented out, blank or
1044 -- not output at all.
1050 procedure Output (From, To : Source_Ptr) is
1052 for J in From .. To loop
1053 Put_Char (Sinput.Source (J));
1061 procedure Output_Line (From, To : Source_Ptr) is
1063 if Deleting or else Preprocessor_Line then
1064 if Blank_Deleted_Lines then
1067 elsif Comment_Deleted_Lines then
1086 -- Start of processing for Preprocess
1089 Start_Of_Processing := Scan_Ptr;
1091 -- We need to call Scan for the first time, because Initialize_Scanner
1092 -- is no longer doing it.
1096 Input_Line_Loop : loop
1097 exit Input_Line_Loop when Token = Tok_EOF;
1099 Preprocessor_Line := False;
1101 if Token /= Tok_End_Of_Line then
1103 -- Preprocessor line
1105 if Token = Tok_Special and then Special_Character = '#
' then
1107 Preprocessor_Line := True;
1116 If_Ptr : constant Source_Ptr := Token_Ptr;
1120 Cond := Expression (not Deleting);
1122 -- Check for an eventual "then"
1124 if Token = Tok_Then then
1128 -- It is an error to have trailing characters after
1129 -- the condition or "then".
1131 if Token /= Tok_End_Of_Line
1132 and then Token /= Tok_EOF
1135 ("extraneous text on preprocessor line",
1137 No_Error_Found := False;
1142 -- Set the initial state of this new "#if". This
1143 -- must be done before incrementing the Last of
1144 -- the table, otherwise function Deleting does
1145 -- not report the correct value.
1147 New_State : constant Pp_State :=
1150 Deleting => Deleting
1152 Match_Seen => Deleting or else Cond);
1155 Pp_States.Increment_Last;
1156 Pp_States.Table (Pp_States.Last) := New_State;
1165 if Pp_States.Last = 0
1166 or else Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1168 Error_Msg ("no IF for this ELSIF", Token_Ptr);
1169 No_Error_Found := False;
1173 not Pp_States.Table (Pp_States.Last).Match_Seen;
1177 Cond := Expression (Cond);
1179 -- Check for an eventual "then"
1181 if Token = Tok_Then then
1185 -- It is an error to have trailing characters after
1186 -- the condition or "then".
1188 if Token /= Tok_End_Of_Line
1189 and then Token /= Tok_EOF
1192 ("extraneous text on preprocessor line",
1194 No_Error_Found := False;
1199 -- Depending on the value of the condition, set the
1200 -- new values of Deleting and Match_Seen.
1201 if Pp_States.Last > 0 then
1202 if Pp_States.Table (Pp_States.Last).Match_Seen then
1203 Pp_States.Table (Pp_States.Last).Deleting := True;
1206 Pp_States.Table (Pp_States.Last).Match_Seen :=
1208 Pp_States.Table (Pp_States.Last).Deleting :=
1217 if Pp_States.Last = 0 then
1218 Error_Msg ("no IF for this ELSE", Token_Ptr);
1219 No_Error_Found := False;
1222 Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1224 Error_Msg -- CODEFIX
1225 ("duplicate ELSE line", Token_Ptr);
1226 No_Error_Found := False;
1229 -- Set the possibly new values of Deleting and
1232 if Pp_States.Last > 0 then
1233 if Pp_States.Table (Pp_States.Last).Match_Seen then
1234 Pp_States.Table (Pp_States.Last).Deleting :=
1238 Pp_States.Table (Pp_States.Last).Match_Seen :=
1240 Pp_States.Table (Pp_States.Last).Deleting :=
1244 -- Set the Else_Ptr to check for illegal #elsif
1247 Pp_States.Table (Pp_States.Last).Else_Ptr :=
1253 -- It is an error to have characters after "#else"
1254 if Token /= Tok_End_Of_Line
1255 and then Token /= Tok_EOF
1258 ("extraneous text on preprocessor line",
1260 No_Error_Found := False;
1267 if Pp_States.Last = 0 then
1268 Error_Msg ("no IF for this END", Token_Ptr);
1269 No_Error_Found := False;
1274 if Token /= Tok_If then
1275 Error_Msg -- CODEFIX
1276 ("IF expected", Token_Ptr);
1277 No_Error_Found := False;
1282 if Token /= Tok_Semicolon then
1283 Error_Msg -- CODEFIX
1284 ("`;` Expected", Token_Ptr);
1285 No_Error_Found := False;
1290 -- It is an error to have character after
1292 if Token /= Tok_End_Of_Line
1293 and then Token /= Tok_EOF
1296 ("extraneous text on preprocessor line",
1298 No_Error_Found := False;
1303 -- In case of one of the errors above, skip the tokens
1304 -- until the end of line is reached.
1308 -- Decrement the depth of the #if stack
1310 if Pp_States.Last > 0 then
1311 Pp_States.Decrement_Last;
1314 -- Illegal preprocessor line
1317 No_Error_Found := False;
1319 if Pp_States.Last = 0 then
1320 Error_Msg -- CODEFIX
1321 ("IF expected", Token_Ptr);
1324 Pp_States.Table (Pp_States.Last).Else_Ptr = 0
1327 ("IF, ELSIF, ELSE, or `END IF` expected",
1331 Error_Msg ("IF or `END IF` expected", Token_Ptr);
1334 -- Skip to the end of this illegal line
1339 -- Not a preprocessor line
1342 -- Do not report errors for those lines, even if there are
1343 -- Ada parsing errors.
1345 Set_Ignore_Errors (To => True);
1351 while Token /= Tok_End_Of_Line
1352 and then Token /= Tok_EOF
1354 if Token = Tok_Special
1355 and then Special_Character = '$
'
1360 Dollar_Ptr : constant Source_Ptr := Token_Ptr;
1365 Change_Reserved_Keyword_To_Symbol;
1367 if Token = Tok_Identifier
1368 and then Token_Ptr = Dollar_Ptr + 1
1372 Symbol := Index_Of (Token_Name);
1374 -- If symbol exists, replace by its value
1376 if Symbol /= No_Symbol then
1377 Output (Start_Of_Processing, Dollar_Ptr - 1);
1378 Start_Of_Processing := Scan_Ptr;
1379 String_To_Name_Buffer
1380 (Mapping.Table (Symbol).Value);
1382 if Mapping.Table (Symbol).Is_A_String then
1384 -- Value is an Ada string
1388 for J in 1 .. Name_Len loop
1389 Put_Char (Name_Buffer (J));
1391 if Name_Buffer (J) = '"' then
1399 -- Value is a sequence of characters, not
1402 for J in 1 .. Name_Len loop
1403 Put_Char (Name_Buffer (J));
1415 Set_Ignore_Errors (To => False);
1419 pragma Assert (Token = Tok_End_Of_Line or else Token = Tok_EOF);
1421 -- At this point, the token is either end of line or EOF.
1422 -- The line to possibly output stops just before the token.
1424 Output_Line (Start_Of_Processing, Token_Ptr - 1);
1426 -- If we are at the end of a line, the scan pointer is at the first
1427 -- non blank character, not necessarily the first character of the
1428 -- line; so, we have to deduct Start_Of_Processing from the token
1431 if Token = Tok_End_Of_Line then
1432 if (Sinput.Source (Token_Ptr) = ASCII.CR
1433 and then Sinput.Source (Token_Ptr + 1) = ASCII.LF)
1435 (Sinput.Source (Token_Ptr) = ASCII.CR
1436 and then Sinput.Source (Token_Ptr + 1) = ASCII.LF)
1438 Start_Of_Processing := Token_Ptr + 2;
1440 Start_Of_Processing := Token_Ptr + 1;
1444 -- Now, scan the first token of the next line. If the token is EOF,
1445 -- the scan pointer will not move, and the token will still be EOF.
1447 Set_Ignore_Errors (To => True);
1449 Set_Ignore_Errors (To => False);
1450 end loop Input_Line_Loop;
1452 -- Report an error for any missing some "#end if;"
1454 for Level in reverse 1 .. Pp_States.Last loop
1455 Error_Msg ("no `END IF` for this IF", Pp_States.Table (Level).If_Ptr);
1456 No_Error_Found := False;
1459 Source_Modified := No_Error_Found and Modified;
1466 procedure Setup_Hooks
1467 (Error_Msg : Error_Msg_Proc;
1469 Set_Ignore_Errors : Set_Ignore_Errors_Proc;
1470 Put_Char : Put_Char_Proc;
1471 New_EOL : New_EOL_Proc)
1474 pragma Assert (Already_Initialized);
1476 Prep.Error_Msg := Error_Msg;
1478 Prep.Set_Ignore_Errors := Set_Ignore_Errors;
1479 Prep.Put_Char := Put_Char;
1480 Prep.New_EOL := New_EOL;