1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-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 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 with Uintp
; use Uintp
;
30 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
39 function Bad_Spelling_Of
(T
: Token_Type
) return Boolean is
40 Tname
: constant String := Token_Type
'Image (T
);
41 -- Characters of token name
43 S
: String (1 .. Tname
'Last - 4);
44 -- Characters of token name folded to lower case, omitting TOK_ at start
46 M1
: String (1 .. 42) := "incorrect spelling of keyword ************";
47 M2
: String (1 .. 44) := "illegal abbreviation of keyword ************";
48 -- Buffers used to construct error message
52 -- Starting subscripts in M1, M2 for keyword name
54 SL
: constant Natural := S
'Length;
55 -- Length of expected token name excluding TOK_ at start
58 if Token
/= Tok_Identifier
then
63 S
(J
) := Fold_Lower
(Tname
(Integer (J
) + 4));
66 Get_Name_String
(Token_Name
);
68 -- A special check for case of PROGRAM used for PROCEDURE
72 and then Name_Buffer
(1 .. 7) = "program"
74 Error_Msg_SC
("PROCEDURE expected");
78 -- A special check for an illegal abbrevation
80 elsif Name_Len
< S
'Length
81 and then Name_Len
>= 4
82 and then Name_Buffer
(1 .. Name_Len
) = S
(1 .. Name_Len
)
84 for J
in 1 .. S
'Last loop
85 M2
(P2
+ J
- 1) := Fold_Upper
(S
(J
));
88 Error_Msg_SC
(M2
(1 .. P2
- 1 + S
'Last));
93 -- Now we go into the full circuit to check for a misspelling
95 -- Never consider something a misspelling if either the actual or
96 -- expected string is less than 3 characters (before this check we
97 -- used to consider i to be a misspelled if in some cases!)
99 if SL
< 3 or else Name_Len
< 3 then
102 -- Special case: prefix matches, i.e. the leading characters of the
103 -- token that we have exactly match the required keyword. If there
104 -- are at least two characters left over, assume that we have a case
105 -- of two keywords joined together which should not be joined.
107 elsif Name_Len
> SL
+ 1
108 and then S
= Name_Buffer
(1 .. SL
)
110 Scan_Ptr
:= Token_Ptr
+ S
'Length;
111 Error_Msg_S
("missing space");
116 if Is_Bad_Spelling_Of
(Name_Buffer
(1 .. Name_Len
), S
) then
118 for J
in 1 .. S
'Last loop
119 M1
(P1
+ J
- 1) := Fold_Upper
(S
(J
));
122 Error_Msg_SC
(M1
(1 .. P1
- 1 + S
'Last));
132 ----------------------
133 -- Check_95_Keyword --
134 ----------------------
136 -- On entry, the caller has checked that current token is an identifier
137 -- whose name matches the name of the 95 keyword New_Tok.
139 procedure Check_95_Keyword
(Token_95
, Next
: Token_Type
) is
140 Scan_State
: Saved_Scan_State
;
143 Save_Scan_State
(Scan_State
); -- at identifier/keyword
144 Scan
; -- past identifier/keyword
147 Restore_Scan_State
(Scan_State
); -- to identifier
148 Error_Msg_Name_1
:= Token_Name
;
149 Error_Msg_SC
("(Ada 83) keyword* cannot be used!");
152 Restore_Scan_State
(Scan_State
); -- to identifier
154 end Check_95_Keyword
;
156 ----------------------
157 -- Check_Bad_Layout --
158 ----------------------
160 procedure Check_Bad_Layout
is
162 if Style
.RM_Column_Check
and then Token_Is_At_Start_Of_Line
163 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
165 Error_Msg_BC
("(style) incorrect layout");
167 end Check_Bad_Layout
;
169 --------------------------
170 -- Check_Misspelling_Of --
171 --------------------------
173 procedure Check_Misspelling_Of
(T
: Token_Type
) is
175 if Bad_Spelling_Of
(T
) then
178 end Check_Misspelling_Of
;
180 -----------------------------
181 -- Check_Simple_Expression --
182 -----------------------------
184 procedure Check_Simple_Expression
(E
: Node_Id
) is
186 if Expr_Form
= EF_Non_Simple
then
187 Error_Msg_N
("this expression must be parenthesized", E
);
189 end Check_Simple_Expression
;
191 ---------------------------------------
192 -- Check_Simple_Expression_In_Ada_83 --
193 ---------------------------------------
195 procedure Check_Simple_Expression_In_Ada_83
(E
: Node_Id
) is
197 if Expr_Form
= EF_Non_Simple
then
199 Error_Msg_N
("(Ada 83) this expression must be parenthesized!", E
);
202 end Check_Simple_Expression_In_Ada_83
;
204 ------------------------
205 -- Check_Subtype_Mark --
206 ------------------------
208 function Check_Subtype_Mark
(Mark
: Node_Id
) return Node_Id
is
210 if Nkind
(Mark
) = N_Identifier
211 or else Nkind
(Mark
) = N_Selected_Component
212 or else (Nkind
(Mark
) = N_Attribute_Reference
213 and then Is_Type_Attribute_Name
(Attribute_Name
(Mark
)))
218 Error_Msg
("subtype mark expected", Sloc
(Mark
));
221 end Check_Subtype_Mark
;
227 function Comma_Present
return Boolean is
228 Scan_State
: Saved_Scan_State
;
232 -- First check, if a comma is present, then a comma is present!
234 if Token
= Tok_Comma
then
238 -- If we have a right paren, then that is taken as ending the list
239 -- i.e. no comma is present.
241 elsif Token
= Tok_Right_Paren
then
244 -- If pragmas, then get rid of them and make a recursive call
245 -- to process what follows these pragmas.
247 elsif Token
= Tok_Pragma
then
249 return Comma_Present
;
251 -- At this stage we have an error, and the goal is to decide on whether
252 -- or not we should diagnose an error and report a (non-existent)
253 -- comma as being present, or simply to report no comma is present
255 -- If we are a semicolon, then the question is whether we have a missing
256 -- right paren, or whether the semicolon should have been a comma. To
257 -- guess the right answer, we scan ahead keeping track of the paren
258 -- level, looking for a clue that helps us make the right decision.
260 -- This approach is highly accurate in the single error case, and does
261 -- not make bad mistakes in the multiple error case (indeed we can't
262 -- really make a very bad decision at this point in any case).
264 elsif Token
= Tok_Semicolon
then
265 Save_Scan_State
(Scan_State
);
266 Scan
; -- past semicolon
268 -- Check for being followed by identifier => which almost certainly
269 -- means we are still in a parameter list and the comma should have
270 -- been a semicolon (such a sequence could not follow a semicolon)
272 if Token
= Tok_Identifier
then
275 if Token
= Tok_Arrow
then
280 -- If that test didn't work, loop ahead looking for a comma or
281 -- semicolon at the same parenthesis level. Always remember that
282 -- we can't go badly wrong in an error situation like this!
286 -- Here is the look ahead loop, Paren_Count tells us whether the
287 -- token we are looking at is at the same paren level as the
288 -- suspicious semicolon that we are trying to figure out.
292 -- If we hit another semicolon or an end of file, and we have
293 -- not seen a right paren or another comma on the way, then
294 -- probably the semicolon did end the list. Indeed that is
295 -- certainly the only single error correction possible here.
297 if Token
= Tok_Semicolon
or else Token
= Tok_EOF
then
298 Restore_Scan_State
(Scan_State
);
301 -- A comma at the same paren level as the semicolon is a strong
302 -- indicator that the semicolon should have been a comma, indeed
303 -- again this is the only possible single error correction.
305 elsif Token
= Tok_Comma
then
306 exit when Paren_Count
= 0;
308 -- A left paren just bumps the paren count
310 elsif Token
= Tok_Left_Paren
then
311 Paren_Count
:= Paren_Count
+ 1;
313 -- A right paren that is at the same paren level as the semicolon
314 -- also means that the only possible single error correction is
315 -- to assume that the semicolon should have been a comma. If we
316 -- are not at the same paren level, then adjust the paren level.
318 elsif Token
= Tok_Right_Paren
then
319 exit when Paren_Count
= 0;
320 Paren_Count
:= Paren_Count
- 1;
323 -- Keep going, we haven't made a decision yet
328 -- If we fall through the loop, it means that we found a terminating
329 -- right paren or another comma. In either case it is reasonable to
330 -- assume that the semicolon was really intended to be a comma. Also
331 -- come here for the identifier arrow case.
334 Restore_Scan_State
(Scan_State
);
335 Error_Msg_SC
(""";"" illegal here, replaced by "",""");
336 Scan
; -- past the semicolon
339 -- If we are not at semicolon or a right paren, then we base the
340 -- decision on whether or not the next token can be part of an
341 -- expression. If not, then decide that no comma is present (the
342 -- caller will eventually generate a missing right parent message)
344 elsif Token
in Token_Class_Eterm
then
347 -- Otherwise we assume a comma is present, even if none is present,
348 -- since the next token must be part of an expression, so if we were
349 -- at the end of the list, then there is more than one error present.
352 T_Comma
; -- to give error
357 -----------------------
358 -- Discard_Junk_List --
359 -----------------------
361 procedure Discard_Junk_List
(L
: List_Id
) is
362 pragma Warnings
(Off
, L
);
366 end Discard_Junk_List
;
368 -----------------------
369 -- Discard_Junk_Node --
370 -----------------------
372 procedure Discard_Junk_Node
(N
: Node_Id
) is
373 pragma Warnings
(Off
, N
);
377 end Discard_Junk_Node
;
383 procedure Ignore
(T
: Token_Type
) is
386 if T
= Tok_Comma
then
387 Error_Msg_SC
("unexpected "","" ignored");
389 elsif T
= Tok_Left_Paren
then
390 Error_Msg_SC
("unexpected ""("" ignored");
392 elsif T
= Tok_Right_Paren
then
393 Error_Msg_SC
("unexpected "")"" ignored");
395 elsif T
= Tok_Semicolon
then
396 Error_Msg_SC
("unexpected "";"" ignored");
400 Tname
: constant String := Token_Type
'Image (Token
);
401 Msg
: String := "unexpected keyword ????????????????????????";
404 -- Loop to copy characters of keyword name (ignoring Tok_)
406 for J
in 5 .. Tname
'Last loop
407 Msg
(J
+ 14) := Fold_Upper
(Tname
(J
));
410 Msg
(Tname
'Last + 15 .. Tname
'Last + 22) := " ignored";
411 Error_Msg_SC
(Msg
(1 .. Tname
'Last + 22));
415 Scan
; -- Scan past ignored token
419 ----------------------------
420 -- Is_Reserved_Identifier --
421 ----------------------------
423 function Is_Reserved_Identifier
return Boolean is
425 if not Is_Reserved_Keyword
(Token
) then
430 Ident_Casing
: constant Casing_Type
:=
431 Identifier_Casing
(Current_Source_File
);
433 Key_Casing
: constant Casing_Type
:=
434 Keyword_Casing
(Current_Source_File
);
437 -- If the casing of identifiers and keywords is different in
438 -- this source file, and the casing of this token matches the
439 -- keyword casing, then we return False, since it is pretty
440 -- clearly intended to be a keyword.
442 if Ident_Casing
/= Unknown
443 and then Key_Casing
/= Unknown
444 and then Ident_Casing
/= Key_Casing
445 and then Determine_Token_Casing
= Key_Casing
449 -- Otherwise assume that an identifier was intended
456 end Is_Reserved_Identifier
;
458 ----------------------
459 -- Merge_Identifier --
460 ----------------------
462 procedure Merge_Identifier
(Prev
: Node_Id
; Nxt
: Token_Type
) is
464 if Token
/= Tok_Identifier
then
469 S
: Saved_Scan_State
;
476 Restore_Scan_State
(S
);
483 -- Check exactly one space between identifiers
485 if Source
(Token_Ptr
- 1) /= ' '
486 or else Int
(Token_Ptr
) /=
487 Int
(Prev_Token_Ptr
) + Length_Of_Name
(Chars
(Prev
)) + 1
494 Get_Name_String
(Chars
(Token_Node
));
497 Buf
: String (1 .. Name_Len
) := Name_Buffer
(1 .. Name_Len
);
500 Get_Name_String
(Chars
(Prev
));
501 Add_Char_To_Name_Buffer
('_');
502 Add_Str_To_Name_Buffer
(Buf
);
503 Set_Chars
(Prev
, Name_Find
);
506 Error_Msg_Node_1
:= Prev
;
508 ("unexpected identifier, possibly & was meant here");
510 end Merge_Identifier
;
516 procedure No_Constraint
is
518 if Token
in Token_Class_Consk
then
519 Error_Msg_SC
("constraint not allowed here");
520 Discard_Junk_Node
(P_Constraint_Opt
);
528 function No_Right_Paren
(Expr
: Node_Id
) return Node_Id
is
530 if Token
= Tok_Right_Paren
then
531 Error_Msg_SC
("unexpected right parenthesis");
539 ---------------------
540 -- Pop_Scope_Stack --
541 ---------------------
543 procedure Pop_Scope_Stack
is
545 pragma Assert
(Scope
.Last
> 0);
546 Scope
.Decrement_Last
;
549 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
550 Error_Msg_SC
("decrement scope stack ptr, new value = ^!");
554 ----------------------
555 -- Push_Scope_Stack --
556 ----------------------
558 procedure Push_Scope_Stack
is
560 Scope
.Increment_Last
;
561 Scope
.Table
(Scope
.Last
).Junk
:= False;
562 Scope
.Table
(Scope
.Last
).Node
:= Empty
;
565 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
566 Error_Msg_SC
("increment scope stack ptr, new value = ^!");
568 end Push_Scope_Stack
;
570 ----------------------
571 -- Separate_Present --
572 ----------------------
574 function Separate_Present
return Boolean is
575 Scan_State
: Saved_Scan_State
;
578 if Token
= Tok_Separate
then
581 elsif Token
/= Tok_Identifier
then
585 Save_Scan_State
(Scan_State
);
586 Scan
; -- past identifier
588 if Token
= Tok_Semicolon
then
589 Restore_Scan_State
(Scan_State
);
590 return Bad_Spelling_Of
(Tok_Separate
);
593 Restore_Scan_State
(Scan_State
);
597 end Separate_Present
;
599 --------------------------
600 -- Signal_Bad_Attribute --
601 --------------------------
603 procedure Signal_Bad_Attribute
is
605 Error_Msg_N
("unrecognized attribute&", Token_Node
);
607 -- Check for possible misspelling
609 Get_Name_String
(Token_Name
);
612 AN
: constant String := Name_Buffer
(1 .. Name_Len
);
615 Error_Msg_Name_1
:= First_Attribute_Name
;
616 while Error_Msg_Name_1
<= Last_Attribute_Name
loop
617 Get_Name_String
(Error_Msg_Name_1
);
619 if Is_Bad_Spelling_Of
620 (AN
, Name_Buffer
(1 .. Name_Len
))
623 ("\possible misspelling of %", Token_Node
);
627 Error_Msg_Name_1
:= Error_Msg_Name_1
+ 1;
630 end Signal_Bad_Attribute
;
632 -------------------------------
633 -- Token_Is_At_Start_Of_Line --
634 -------------------------------
636 function Token_Is_At_Start_Of_Line
return Boolean is
638 return (Token_Ptr
= First_Non_Blank_Location
or else Token
= Tok_EOF
);
639 end Token_Is_At_Start_Of_Line
;