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
|
------------------------------------------------------------------------------
-- --
-- GNATPP COMPONENTS --
-- --
-- G N A T P P . S O U R C E _ L I N E _ B U F F E R --
-- --
-- B o d y --
-- --
-- Copyright (C) 2001-2004, ACT Europe --
-- --
-- GNATPP 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. GNATPP is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI- --
-- LITY 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, --
-- --
-- GNATPP is maintained by ACT Europe (http://www.act-europe.fr). --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Asis.Text; use Asis.Text;
with GNATPP.Common;
with GNATPP.State; use GNATPP.State;
package body GNATPP.Source_Line_Buffer is
Wide_HT : constant Wide_Character := To_Wide_Character (ASCII.HT);
Old_Current_Line : Line_Number_Positive;
Old_Line_Pos : Natural;
-- These variables are used to save and to restore the content of the
-- line buffer
-----------------------
-- Local subprograms --
-----------------------
function Skip_Blanks return Natural;
-- Returns the index of the first non-blank position which follows
-- Line_Pos (ASCII.HT is considered as blank). If the rest of the line
-- in the line buffer contains only blanks, this function returns 0.
function Is_While_Space (W_Ch : Wide_Character) return Boolean;
-- Checks if the argument is the space of HT character.
----------------------
-- Detect_Delimiter --
----------------------
procedure Detect_Delimiter is
begin
case Line_Buf (Line_Pos) is
when '&' =>
Delimiter := Ampersand_Dlm;
when ''' =>
Delimiter := Tick_Dlm;
when '(' =>
Delimiter := Left_Parenthesis_Dlm;
when ')' =>
Delimiter := Right_Parenthesis_Dlm;
when '*' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '*'
then
Delimiter := Double_Star_Dlm;
else
Delimiter := Asterisk_Dlm;
end if;
when '+' =>
Delimiter := Plus_Dlm;
when ',' =>
Delimiter := Comma_Dlm;
when '-' =>
-- This function is not supposed to be called in the situation
-- when Line_Pos points to the beginning of a comment
Delimiter := Minus_Dlm;
when '.' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '.'
then
Delimiter := Double_Dot_Dlm;
else
Delimiter := Dot_Dlm;
end if;
when '/' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '='
then
Delimiter := Inequality_Dlm;
else
Delimiter := Divide_Dlm;
end if;
when ':' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '='
then
Delimiter := Assignment_Dlm;
else
Delimiter := Colon_Dlm;
end if;
when ';' =>
Delimiter := Semicolon_Dlm;
when '<' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '='
then
Delimiter := Less_Or_Equal_Dlm;
elsif Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '>'
then
Delimiter := Box_Dlm;
elsif Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '<'
then
Delimiter := Left_Label_Bracket_Dlm;
else
Delimiter := Less_Than_Dlm;
end if;
when '=' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '>'
then
Delimiter := Arrow_Dlm;
else
Delimiter := Equals_Dlm;
end if;
when '|' =>
Delimiter := Vertical_Line_Dlm;
when '!' =>
Delimiter := Exclamation_Mark_Dlm;
when '>' =>
if Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '='
then
Delimiter := Greater_Or_Equal_Dlm;
elsif Line_Pos < Line_Len and then
Line_Buf (Line_Pos + 1) = '>'
then
Delimiter := Right_Label_Bracket_Dlm;
else
Delimiter := Greater_Than_Dlm;
end if;
when others =>
Delimiter := Not_A_Dlm;
end case;
end Detect_Delimiter;
--------------------
-- Detect_Keyword --
--------------------
procedure Detect_Keyword is
begin
Keyword := Not_A_KW;
Skip_Blanks;
if not (Current_Line = GNATPP.Common.The_Last_Line and then
Line_Pos = 0)
then
Set_Word_End;
if Word_End > 0 and then
Line_Buf (Word_End) = ';'
then
Word_End := Word_End - 1;
end if;
declare
Word : constant Program_Text := Normalized_Word;
begin
if Word = Abort_String then
Keyword := KW_Abort;
elsif Word = Abs_String then
Keyword := KW_Abs;
elsif Word = Abstract_String then
Keyword := KW_Abstract;
elsif Word = Accept_String then
Keyword := KW_Accept;
elsif Word = Access_String then
Keyword := KW_Access;
elsif Word = Aliased_String then
Keyword := KW_Aliased;
elsif Word = All_String then
Keyword := KW_All;
elsif Word = And_String then
Keyword := KW_And;
elsif Word = Array_String then
Keyword := KW_Array;
elsif Word = At_String then
Keyword := KW_At;
elsif Word = Begin_String then
Keyword := KW_Begin;
elsif Word = Body_String then
Keyword := KW_Body;
elsif Word = Case_String then
Keyword := KW_Case;
elsif Word = Constant_String then
Keyword := KW_Constant;
elsif Word = Declare_String then
Keyword := KW_Declare;
elsif Word = Delay_String then
Keyword := KW_Delay;
elsif Word = Delta_String then
Keyword := KW_Delta;
elsif Word = Digits_String then
Keyword := KW_Digits;
elsif Word = Do_String then
Keyword := KW_Do;
elsif Word = Else_String then
Keyword := KW_Else;
elsif Word = Elsif_String then
Keyword := KW_Elsif;
elsif Word = End_String then
Keyword := KW_End;
elsif Word = Entry_String then
Keyword := KW_Entry;
elsif Word = Exception_String then
Keyword := KW_Exception;
elsif Word = Exit_String then
Keyword := KW_Exit;
elsif Word = For_String then
Keyword := KW_For;
elsif Word = Function_String then
Keyword := KW_Function;
elsif Word = Generic_String then
Keyword := KW_Generic;
elsif Word = Goto_String then
Keyword := KW_Goto;
elsif Word = If_String then
Keyword := KW_If;
elsif Word = In_String then
Keyword := KW_In;
elsif Word = KW_Is_String then
Keyword := KW_Is;
elsif Word = Limited_String then
Keyword := KW_Limited;
elsif Word = Loop_String then
Keyword := KW_Loop;
elsif Word = Mod_String then
Keyword := KW_Mod;
elsif Word = New_String then
Keyword := KW_New;
elsif Word = Not_String then
Keyword := KW_Not;
elsif Word = Null_String then
Keyword := KW_Null;
elsif Word = Of_String then
Keyword := KW_Of;
elsif Word = Or_String then
Keyword := KW_Or;
elsif Word = Others_String then
Keyword := KW_Others;
elsif Word = Out_String then
Keyword := KW_Out;
elsif Word = Package_String then
Keyword := KW_Package;
elsif Word = Pragma_String then
Keyword := KW_Pragma;
elsif Word = Private_String then
Keyword := KW_Private;
elsif Word = Procedure_String then
Keyword := KW_Procedure;
elsif Word = Protected_String then
Keyword := KW_Protected;
elsif Word = Raise_String then
Keyword := KW_Raise;
elsif Word = Range_String then
Keyword := KW_Range;
elsif Word = Record_String then
Keyword := KW_Record;
elsif Word = Rem_String then
Keyword := KW_Rem;
elsif Word = Renames_String then
Keyword := KW_Renames;
elsif Word = Requeue_String then
Keyword := KW_Requeue;
elsif Word = Return_String then
Keyword := KW_Return;
elsif Word = Reverse_String then
Keyword := KW_Reverse;
elsif Word = Select_String then
Keyword := KW_Select;
elsif Word = Separate_String then
Keyword := KW_Separate;
elsif Word = Subtype_String then
Keyword := KW_Subtype;
elsif Word = Tagged_String then
Keyword := KW_Tagged;
elsif Word = Task_String then
Keyword := KW_Task;
elsif Word = Terminate_String then
Keyword := KW_Terminate;
elsif Word = Then_String then
Keyword := KW_Then;
elsif Word = Type_String then
Keyword := KW_Type;
elsif Word = Until_String then
Keyword := KW_Until;
elsif Word = Use_String then
Keyword := KW_Use;
elsif Word = When_String then
Keyword := KW_When;
elsif Word = While_String then
Keyword := KW_While;
elsif Word = With_String then
Keyword := KW_With;
elsif Word = Xor_String then
Keyword := KW_Xor;
end if;
end;
end if;
end Detect_Keyword;
-------------------------
-- End_Of_Line_Comment --
-------------------------
function End_Of_Line_Comment return Boolean is
Result : Boolean := False;
Idx : constant Natural := Skip_Blanks;
begin
if not (Idx = 0 or else
Idx = Line_Len)
then
Result := Line_Buf (Idx) = '-' and then
Line_Buf (Idx + 1) = '-';
Line_Pos := Idx;
end if;
return Result;
end End_Of_Line_Comment;
-----------------
-- Get_End_Idx --
-----------------
function Get_End_Idx return Natural is
Result : Natural := 0;
begin
for J in reverse Line_Pos .. Line_Len loop
if not (Line_Buf (J) = ' ' or else
Line_Buf (J) = To_Wide_Character (ASCII.HT))
then
Result := J;
exit;
end if;
end loop;
return Result;
end Get_End_Idx;
---------------
-- In_Buffer --
---------------
procedure In_Buffer (Str_Num : Line_Number_Positive) is
The_line : constant Line := Lines_Table.Table (Str_Num);
begin
GNATPP.State.Current_Line := Str_Num;
Line_Len := Length (The_line);
Line_Buf (1 .. Line_Len) := Line_Image (The_line);
if Line_Len = 0 then
Line_Pos := 0;
else
Line_Pos := 1;
end if;
end In_Buffer;
-------------------
-- Is_Blank_Line --
-------------------
function Is_Blank_Line return Boolean is
begin
return Skip_Blanks = 0;
end Is_Blank_Line;
--------------------
-- Is_While_Space --
--------------------
function Is_While_Space (W_Ch : Wide_Character) return Boolean is
begin
return W_Ch = ' ' or else W_Ch = Wide_HT;
end Is_While_Space;
-------------------------
-- Next_Line_In_Buffer --
-------------------------
procedure Next_Line_In_Buffer is
begin
if Current_Line < GNATPP.Common.The_Last_Line then
In_Buffer (Current_Line + 1);
else
Line_Pos := 0;
end if;
end Next_Line_In_Buffer;
---------------------
-- Normalized_Word --
---------------------
function Normalized_Word return Program_Text is
Result : Program_Text := Line_Buf (Line_Pos .. Word_End);
begin
if Is_String (Result) then
Result := To_Wide_String (To_Lower (To_String (Result)));
end if;
return Result;
end Normalized_Word;
-----------------
-- Restore_Buf --
-----------------
procedure Restore_Buf is
begin
In_Buffer (Old_Current_Line);
Line_Pos := Old_Line_Pos;
end Restore_Buf;
--------------
-- Save_Buf --
--------------
procedure Save_Buf is
begin
Old_Current_Line := Current_Line;
Old_Line_Pos := Line_Pos;
end Save_Buf;
------------------
-- Set_Word_End --
------------------
procedure Set_Word_End is
begin
Word_End := Line_Pos;
if Word_End /= 0 then
while Word_End < Line_Len loop
if not (Is_Alphanumeric (To_Character (Line_Buf (Word_End)))
or else
To_Character (Line_Buf (Word_End)) = '_')
then
exit;
else
Word_End := Word_End + 1;
end if;
end loop;
if not Is_Alphanumeric (To_Character (Line_Buf (Word_End))) then
Word_End := Word_End - 1;
end if;
end if;
end Set_Word_End;
-----------------------------
-- Set_Word_End_In_Comment --
-----------------------------
procedure Set_Word_End_In_Comment is
begin
Word_End := Line_Pos;
if Word_End /= 0 then
while Word_End < Line_Len loop
if Is_While_Space (Line_Buf (Word_End)) then
exit;
else
Word_End := Word_End + 1;
end if;
end loop;
if Is_While_Space (Line_Buf (Word_End)) then
Word_End := Word_End - 1;
end if;
end if;
end Set_Word_End_In_Comment;
-----------------
-- Skip_Blanks --
-----------------
function Skip_Blanks return Natural is
Result : Natural := Line_Pos;
begin
HT_Passed := False;
if Result > Line_Len then
Result := 0;
elsif Result > 0 then
while Line_Buf (Result) = ' ' or else
Line_Buf (Result) = To_Wide_Character (ASCII.HT) or else
Line_Buf (Result) = To_Wide_Character (ASCII.FF) or else
Line_Buf (Result) = To_Wide_Character (ASCII.VT)
loop
Result := Result + 1;
if Result > Line_Len then
Result := 0;
HT_Passed := False;
exit;
end if;
if Line_Buf (Result) = To_Wide_Character (ASCII.HT) then
HT_Passed := True;
end if;
end loop;
end if;
return Result;
end Skip_Blanks;
-----------------
-- Skip_Blanks --
-----------------
procedure Skip_Blanks is
begin
Line_Pos := Skip_Blanks;
end Skip_Blanks;
---------------
-- Skip_Word --
---------------
procedure Skip_Word is
begin
Skip_Blanks;
if Line_Pos > 0 then
-- skipping the word
while not (Line_Buf (Line_Pos) = ' ' or else
Line_Buf (Line_Pos) = To_Wide_Character (ASCII.HT))
and then
Line_Pos <= Line_Len
loop
Line_Pos := Line_Pos + 1;
end loop;
Skip_Blanks;
end if;
end Skip_Word;
end GNATPP.Source_Line_Buffer;
|