1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- P A R . U T I L --
-- --
-- B o d y --
-- --
-- $Revision: 1.57 $ --
-- --
-- Copyright (C) 1992-1997, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
with Uintp; use Uintp;
separate (Par)
package body Util is
---------------------
-- Bad_Spelling_Of --
---------------------
function Bad_Spelling_Of (T : Token_Type) return Boolean is
Tname : constant String := Token_Type'Image (T);
-- Characters of token name
S : String (1 .. Tname'Last - 4);
-- Characters of token name folded to lower case, omitting TOK_ at start
M1 : String (1 .. 42) := "incorrect spelling of keyword ************";
M2 : String (1 .. 44) := "illegal abbreviation of keyword ************";
-- Buffers used to construct error message
P1 : constant := 30;
P2 : constant := 32;
-- Starting subscripts in M1, M2 for keyword name
SL : constant Natural := S'Length;
-- Length of token name excluding TOK_ at start
begin
if Token /= Tok_Identifier then
return False;
end if;
for J in S'Range loop
S (J) := Fold_Lower (Tname (Integer (J) + 4));
end loop;
Get_Name_String (Token_Name);
-- A special check for case of PROGRAM used for PROCEDURE
if T = Tok_Procedure
and then Name_Len = 7
and then Name_Buffer (1 .. 7) = "program"
then
Error_Msg_SC ("PROCEDURE expected");
Token := T;
return True;
-- A special check for an illegal abbrevation
elsif Name_Len < S'Length
and then Name_Len >= 4
and then Name_Buffer (1 .. Name_Len) = S (1 .. Name_Len)
then
for J in 1 .. S'Last loop
M2 (P2 + J - 1) := Fold_Upper (S (J));
end loop;
Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last));
Token := T;
return True;
end if;
-- Now we go into the full circuit to check for a misspelling
-- Never consider something a misspelling if either the actual or
-- expected string is less than 3 characters (before this check we
-- used to consider i to be a misspelled if in some cases!)
if SL < 3 or else Name_Len < 3 then
return False;
-- Special case: prefix matches, i.e. the leading characters of the
-- token that we have exactly match the required keyword. If there
-- are at least two characters left over, assume that we have a case
-- of two keywords joined together which should not be joined.
elsif Name_Len > SL + 1
and then S = Name_Buffer (1 .. SL)
then
Scan_Ptr := Token_Ptr + S'Length;
Error_Msg_S ("missing space");
Token := T;
return True;
end if;
if Is_Bad_Spelling (S) then
for J in 1 .. S'Last loop
M1 (P1 + J - 1) := Fold_Upper (S (J));
end loop;
Error_Msg_SC (M1 (1 .. P1 - 1 + S'Last));
Token := T;
return True;
else
return False;
end if;
end Bad_Spelling_Of;
----------------------
-- Check_95_Keyword --
----------------------
-- On entry, the caller has checked that current token is an identifier
-- whose name matches the name of the 95 keyword New_Tok.
procedure Check_95_Keyword (Token_95, Next : Token_Type) is
Scan_State : Saved_Scan_State;
begin
Save_Scan_State (Scan_State); -- at identifier/keyword
Scan; -- past identifier/keyword
if Token = Next then
Restore_Scan_State (Scan_State); -- to identifier
Error_Msg_Name_1 := Token_Name;
Error_Msg_SC ("(Ada 83) keyword* cannot be used!");
Token := Token_95;
else
Restore_Scan_State (Scan_State); -- to identifier
end if;
end Check_95_Keyword;
--------------------------
-- Check_Misspelling_Of --
--------------------------
procedure Check_Misspelling_Of (T : Token_Type) is
begin
if Bad_Spelling_Of (T) then
null;
end if;
end Check_Misspelling_Of;
-----------------------------
-- Check_Simple_Expression --
-----------------------------
procedure Check_Simple_Expression (E : Node_Id) is
begin
if Expr_Form = EF_Non_Simple then
Error_Msg_N ("this expression must be parenthesized", E);
end if;
end Check_Simple_Expression;
---------------------------------------
-- Check_Simple_Expression_In_Ada_83 --
---------------------------------------
procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id) is
begin
if Expr_Form = EF_Non_Simple then
Note_Feature (Non_Simple_Expressions, Sloc (E));
if Ada_83 then
Error_Msg_N ("(Ada 83) this expression must be parenthesized!", E);
end if;
end if;
end Check_Simple_Expression_In_Ada_83;
------------------------
-- Check_Subtype_Mark --
------------------------
function Check_Subtype_Mark (Mark : Node_Id) return Node_Id is
begin
if Nkind (Mark) = N_Identifier
or else Nkind (Mark) = N_Selected_Component
or else (Nkind (Mark) = N_Attribute_Reference
and then Is_Type_Attribute_Name (Attribute_Name (Mark)))
or else Mark = Error
then
return Mark;
else
Error_Msg ("subtype mark expected", Sloc (Mark));
return Error;
end if;
end Check_Subtype_Mark;
-------------------
-- Comma_Present --
-------------------
function Comma_Present return Boolean is
Scan_State : Saved_Scan_State;
Paren_Count : Nat;
begin
-- First check, if a comma is present, then a comma is present!
if Token = Tok_Comma then
T_Comma;
return True;
-- If we have a right paren, then that is taken as ending the list
-- i.e. no comma is present.
elsif Token = Tok_Right_Paren then
return False;
-- If pragmas, then get rid of them and make a recursive call
-- to process what follows these pragmas.
elsif Token = Tok_Pragma then
P_Pragmas_Misplaced;
return Comma_Present;
-- At this stage we have an error, and the goal is to decide on whether
-- or not we should diagnose an error and report a (non-existent)
-- comma as being present, or simply to report no comma is present
-- If we are a semicolon, then the question is whether we have a missing
-- right paren, or whether the semicolon should have been a comma. To
-- guess the right answer, we scan ahead keeping track of the paren
-- level, looking for a clue that helps us make the right decision.
-- This approach is highly accurate in the single error case, and does
-- not make bad mistakes in the multiple error case (indeed we can't
-- really make a very bad decision at this point in any case).
elsif Token = Tok_Semicolon then
Save_Scan_State (Scan_State);
Scan; -- past semicolon
-- Check for being followed by identifier => which almost certainly
-- means we are still in a parameter list and the comma should have
-- been a semicolon (such a sequence could not follow a semicolon)
if Token = Tok_Identifier then
Scan;
if Token = Tok_Arrow then
goto Assume_Comma;
end if;
end if;
-- If that test didn't work, loop ahead looking for a comma or
-- semicolon at the same parenthesis level. Always remember that
-- we can't go badly wrong in an error situation like this!
Paren_Count := 0;
-- Here is the look ahead loop, Paren_Count tells us whether the
-- token we are looking at is at the same paren level as the
-- suspicious semicolon that we are trying to figure out.
loop
-- If we hit another semicolon or an end of file, and we have
-- not seen a right paren or another comma on the way, then
-- probably the semicolon did end the list. Indeed that is
-- certainly the only single error correction possible here.
if Token = Tok_Semicolon or else Token = Tok_EOF then
Restore_Scan_State (Scan_State);
return False;
-- A comma at the same paren level as the semicolon is a strong
-- indicator that the semicolon should have been a comma, indeed
-- again this is the only possible single error correction.
elsif Token = Tok_Comma then
exit when Paren_Count = 0;
-- A left paren just bumps the paren count
elsif Token = Tok_Left_Paren then
Paren_Count := Paren_Count + 1;
-- A right paren that is at the same paren level as the semicolon
-- also means that the only possible single error correction is
-- to assume that the semicolon should have been a comma. If we
-- are not at the same paren level, then adjust the paren level.
elsif Token = Tok_Right_Paren then
exit when Paren_Count = 0;
Paren_Count := Paren_Count - 1;
end if;
-- Keep going, we haven't made a decision yet
Scan;
end loop;
-- If we fall through the loop, it means that we found a terminating
-- right paren or another comma. In either case it is reasonable to
-- assume that the semicolon was really intended to be a comma. Also
-- come here for the identifier arrow case.
<<Assume_Comma>>
Restore_Scan_State (Scan_State);
Error_Msg_SC (""";"" illegal here, replaced by "",""");
Scan; -- past the semicolon
return True;
-- If we are not at semicolon or a right paren, then we base the
-- decision on whether or not the next token can be part of an
-- expression. If not, then decide that no comma is present (the
-- caller will eventually generate a missing right parent message)
elsif Token in Token_Class_Eterm then
return False;
-- Otherwise we assume a comma is present, even if none is present,
-- since the next token must be part of an expression, so if we were
-- at the end of the list, then there is more than one error present.
else
T_Comma; -- to give error
return True;
end if;
end Comma_Present;
-----------------------
-- Discard_Junk_List --
-----------------------
procedure Discard_Junk_List (L : List_Id) is
begin
null;
end Discard_Junk_List;
-----------------------
-- Discard_Junk_Node --
-----------------------
procedure Discard_Junk_Node (N : Node_Id) is
begin
null;
end Discard_Junk_Node;
------------
-- Ignore --
------------
procedure Ignore (T : Token_Type) is
begin
if Token = T then
if T = Tok_Comma then
Error_Msg_SC ("unexpected "","" ignored");
elsif T = Tok_Left_Paren then
Error_Msg_SC ("unexpected ""("" ignored");
elsif T = Tok_Right_Paren then
Error_Msg_SC ("unexpected "")"" ignored");
elsif T = Tok_Left_Paren then
Error_Msg_SC ("unexpected "";"" ignored");
else
declare
Tname : constant String := Token_Type'Image (Token);
Msg : String := "unexpected keyword ????????????????????????";
begin
-- Loop to copy characters of keyword name (ignoring Tok_)
for J in 5 .. Tname'Last loop
Msg (J + 14) := Fold_Upper (Tname (J));
end loop;
Msg (Tname'Last + 15 .. Tname'Last + 22) := " ignored";
Error_Msg_SC (Msg (1 .. Tname'Last + 22));
end;
end if;
Scan; -- Scan past ignored token
end if;
end Ignore;
---------------------
-- Is_Bad_Spelling --
---------------------
function Is_Bad_Spelling (S : String) return Boolean is
SL : constant Natural := S'Length;
begin
-- If first character does not match, then definitely not misspelling
if S (1) /= Name_Buffer (1) then
return False;
-- Not a bad spelling if both strings are 1-2 characters long
elsif SL < 3 and then Name_Len < 3 then
return False;
-- Lengths match. Execute loop to check for a single error, single
-- transposition or exact match (we only fall through this loop if
-- one of these three conditions is found).
elsif Name_Len = SL then
for J in 2 .. Name_Len - 1 loop
if Name_Buffer (J) /= S (J) then
-- If either mismatched characters is a digit, then we
-- don't consider it a misspelling (e.g. B345 is not a
-- misspelling of B346, it is something quite different)
if Name_Buffer (J) in '0' .. '9'
or else S (J) in '0' .. '9'
then
return False;
elsif Name_Buffer (J + 1) = S (J + 1)
and then Name_Buffer (J + 2 .. Name_Len) = S (J + 2 .. SL)
then
return True;
elsif Name_Buffer (J) = S (J + 1)
and then Name_Buffer (J + 1) = S (J)
and then Name_Buffer (J + 2 .. Name_Len) = S (J + 2 .. SL)
then
return True;
else
return False;
end if;
end if;
end loop;
-- At last character
if Name_Buffer (Name_Len) in '0' .. '9'
and then S (SL) in '0' .. '9'
then
return False;
else
return True;
end if;
-- Length is 1 too short. Execute loop to check for single deletion
-- (we only fall through this loop if a single insertion is found)
elsif Name_Len = S'Last - 1 then
for J in 2 .. Name_Len loop
if Name_Buffer (J) /= S (J) then
exit when Name_Buffer (J .. Name_Len) = S (J + 1 .. SL);
return False;
end if;
end loop;
return True;
-- Length is 1 too long. Execute loop to check for single insertion
-- (we only fall through this loop if a single insertion is found)
elsif Name_Len = S'Last + 1 then
for J in 2 .. S'Last loop
if Name_Buffer (J) /= S (J) then
exit when Name_Buffer (J + 1 .. Name_Len) = S (J .. SL);
return False;
end if;
end loop;
return True;
-- Length is completely wrong
else
return False;
end if;
end Is_Bad_Spelling;
----------------------------
-- Is_Reserved_Identifier --
----------------------------
function Is_Reserved_Identifier return Boolean is
begin
if not Is_Reserved_Keyword (Token) then
return False;
else
declare
Ident_Casing : constant Casing_Type :=
Identifier_Casing (Current_Source_File);
Key_Casing : constant Casing_Type :=
Keyword_Casing (Current_Source_File);
begin
-- If the casing of identifiers and keywords is different in
-- this source file, and the casing of this token matches the
-- keyword casing, then we return False, since it is pretty
-- clearly intended to be a keyword.
if Ident_Casing /= Unknown
and then Key_Casing /= Unknown
and then Ident_Casing /= Key_Casing
and then Determine_Token_Casing = Key_Casing
then
return False;
-- Otherwise assume that an identifier was intended
else
return True;
end if;
end;
end if;
end Is_Reserved_Identifier;
-------------------
-- No_Constraint --
-------------------
procedure No_Constraint is
begin
if Token in Token_Class_Consk then
Error_Msg_SC ("constraint not allowed here");
Discard_Junk_Node (P_Constraint_Opt);
end if;
end No_Constraint;
--------------------
-- No_Right_Paren --
--------------------
function No_Right_Paren (Expr : Node_Id) return Node_Id is
begin
if Token = Tok_Right_Paren then
Error_Msg_SC ("unexpected right parenthesis");
Resync_Expression;
return Error;
else
return Expr;
end if;
end No_Right_Paren;
---------------------
-- Pop_Scope_Stack --
---------------------
procedure Pop_Scope_Stack is
begin
pragma Assert (Scope.Last > 0);
Scope.Decrement_Last;
if Debug_Flag_P then
Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
Error_Msg_SC ("decrement scope stack ptr, new value = ^!");
end if;
end Pop_Scope_Stack;
----------------------
-- Push_Scope_Stack --
----------------------
procedure Push_Scope_Stack is
begin
Scope.Increment_Last;
if Debug_Flag_P then
Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
Error_Msg_SC ("increment scope stack ptr, new value = ^!");
end if;
end Push_Scope_Stack;
----------------------
-- Separate_Present --
----------------------
function Separate_Present return Boolean is
Scan_State : Saved_Scan_State;
begin
if Token = Tok_Separate then
return True;
elsif Token /= Tok_Identifier then
return False;
else
Save_Scan_State (Scan_State);
Scan; -- past identifier
if Token = Tok_Semicolon then
Restore_Scan_State (Scan_State);
return Bad_Spelling_Of (Tok_Separate);
else
Restore_Scan_State (Scan_State);
return False;
end if;
end if;
end Separate_Present;
-------------------------------
-- Token_Is_At_Start_Of_Line --
-------------------------------
function Token_Is_At_Start_Of_Line return Boolean is
begin
return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF);
end Token_Is_At_Start_Of_Line;
end Util;
|