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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
|
/*
*
* Copyright (c) International Business Machines Corp., 2000
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module: scanner.c
*/
/*
* Change History:
*
*/
/*
*
*/
/* This module implements the scanner, which is the front end of the syntactic
analyzer used by the LVM command. The scanner is an FSA whose states and
actions are as indicated in the table below:
Token Type
State Characters that trigger a State to Output To
Name Transition Transition to Screener
----------------------------------------------------------------------------
Start --
' ' -> SingleSpace
'\t' -> SingleTab
',',':','{','}' , '=' , '(' , ')' -> Start => "Separator"
'-' -> OptionCheck
'+' -> PositiveNumberCheck
'0' .. '9' -> IsNumber
'"' -> IsString
'/' -> IsPathName
'A' .. 'Z' , 'a' .. 'z' -> IsKeyWord
EOF -> EndState
-> Error;
OptionCheck --
'-' -> Start => "Separator"
-> IsNegativeNumber;
IsNegativeNumber --
'0' .. '9' -> IsNumber
-> Start => "Separator";
IsNumber --
'0' .. '9' -> IsNumber
'.' -> IsRealNumber
'A' .. 'Z', 'a' .. 'z' -> IsKeyWord => "Number"
-> SeparatorOrError => "Number";
IsRealNumber --
'0' .. '9' -> IsRealNumber
'A' .. 'Z', 'a' .. 'z' -> IsKeyWord => "RealNumber"
-> SeparatorOrError => "RealNumber";
SeparatorOrError --
',',':','}','(',')' -> Start => "Separator"
' ' -> SingleSpace
'\t' -> SingleTab
EOF -> EndState
-> Error;
PositiveNumberCheck --
'0' .. '9' -> IsNumber
-> EndState => "Invalid Character";
IsPathName --
'/' , 'A' .. 'Z' , 'a' .. 'z' -> IsPathName
'0' .. '9' -> IsPathName
'_' , '-' -> IsPathName
-> SeparatorOrError => "String";
IsKeyWord --
'A' .. 'Z' , 'a' .. 'z' -> IsKeyWord
'0' .. '9' -> IsKeyWord
'_' , '-' -> IsKeyWord
'/' -> IsPathName
-> SeparatorOrError => "Keyword";
EndOfString --
'"' -> IsString
-> Start => "String";
IsString --
'"' -> EndOfString
accept any character other than
the single quote mark as being
part of the string -> IsString;
SingleSpace --
' ' -> IsMultiSpace
-> Start => "Space";
IsMultiSpace --
' ' -> IsMultiSpace
-> Start => "MultiSpace";
SingleTab --
'\t' -> IsMultiTab
-> Start => "Tab";
IsMultiTab --
' ' -> IsMultiTab
-> Start => "MultiTab";
Error --
-> EndState => "ERROR";
EndState --
=> "EOF";
----------------------------------------------------------------------------
The scanner maintains a buffer. Each time a character is used in a
transition, it is placed into the buffer. The buffer is cleared each
time a transition to the Start state is made. When the scanner reaches
a state where it outputs a value (as indicated in the table), the output
consists of two parts: the contents of the buffer, and a characterization
of the contents of the buffer. In the table above, only the characterization
is shown in the output column. In those cases where output occurs on a
transition to the start state, the output takes place before the transition
to the start state. Each of the items "output" by the scanner is appended
to a linked list, which is returned to the caller when scanning has been
completed. Thus, the scanner returns a linked list of tokens. */
/* Identify this file. */
#define SCANNER_C
/*--------------------------------------------------
* Necessary include files
--------------------------------------------------*/
#include <ctype.h> /* toupper */
#include <stdlib.h> /* malloc */
#include <stdio.h>
#include <string.h>
#include "token.h" /* TokenType, TokenCharacterizations, MaxIdentifierLength */
#include "error.h" /* Scanner_Errors, Report_Scanner_Error */
#include "scanner.h" /* GetToken, SetInput prototypes */
/*--------------------------------------------------
* Private constants
--------------------------------------------------*/
/*--------------------------------------------------
* Private Type definitions
--------------------------------------------------*/
/* The following enumeration has one entry for each state in the
state table. A variable of this type will be used to keep
track of which state the FSA is in at any given time. */
typedef enum
{
Start,
OptionCheck,
IsNegativeNumber,
IsNumber,
IsRealNumber,
SeparatorOrError,
PositiveNumberCheck,
IsKeyWord,
IsPathName,
EndOfString,
IsString,
SingleSpace,
IsMultiSpace,
SingleTab,
IsMultiTab,
EndState,
ErrorState
} State;
/*--------------------------------------------------
Private global variables.
--------------------------------------------------*/
static char * CommandLine; /* Original command line passed to us. */
static char * CurrentCommandLine; /* Starting point within the command line passed to us. */
static uint CharactersConsumed; /* Count of the number of characters we have used from the command line. */
static uint CurrentRow; /* If the command line is actually a command file, this will indicate which line of the command file is being processed. */
static uint CurrentColumn; /* The current position (0 based) within the current line/command line expressed as an offset. */
static FILE * InputFile; /* The command file being processed, if there is one. */
static char Buffer[MaxIdentifierLength]; /* Our buffer for creating tokens. */
static uint PositionInBuffer; /* Used to keep track of where to put characters in the Buffer. */
static uint Flush_Count = 1; /* Used by the GetCharacter function to advance past the end of comments. */
static BOOLEAN ExamineCurrentCharacter = FALSE; /* Causes a state to not load a new character but to examine the existing one. */
static char CharacterToExamine = ' '; /* The current character to examine. Examination of a character will
result in the FSA transitioning to a new state in accordance with
the state table at the beginning of this file. */
static char NextCharacter = ' '; /* The next character that will be examined. */
static BOOLEAN Init_Characters = TRUE; /* Controls the initialization of NextCharacter and CharacterToExamine. */
static BOOLEAN Detect_Comments = TRUE; /* Control comment detection and elimination. If FALSE, then comment detection is disabled. */
static State CurrentState = Start; /* The current state within the FSA. */
static char * Error_Source = NULL; /* Used for error reporting purposes. It points to either CommandLine or the name of the input file. */
/*--------------------------------------------------
Local Function Prototypes
--------------------------------------------------*/
/* The following function creates a token using the the characters in the Buffer. */
static TokenType * MakeToken( TokenCharacterizations Characterization);
/* The following function gets a character from the command line or the input file.
It adjusts the CharactersConsumed and CurrentColumn variables everytime it gets
a character, and it adjusts both the CurrentColumn and CurrentRow variables whenever
it finds a carriage return or carriage return/linefeed pair.
NOTE: If GetCharacter encounters a NULL in a file or command line, it will treat
that as EOF. In fact, GetCharacter will return NULL to represent EOF.
NOTE: If there is an I/O error during input from a command file, this will be
treated as EOF also and NULL will be returned for the character value.
NOTE: Whenever EOF is returned by this function, if the source of input is a command
file, the command file will be closed. */
static void GetCharacter( void );
/* KeepCharacter - This puts the character passed to it into Buffer if Buffer is not yet full. */
static void KeepCharacter( void );
/*--------------------------------------------------
There are no public global variables.
--------------------------------------------------*/
/*--------------------------------------------------
* Public Functions Available
--------------------------------------------------*/
/*********************************************************************/
/* */
/* Function Name: SetInput */
/* */
/* Descriptive Name: Sets the input source for drawing characters */
/* used to build tokens. */
/* */
/* Input: BOOLEAN IsFile - if TRUE, then the following parameter */
/* is interpreted as the name of a file to */
/* be used for input. */
/* char * FilenameOrString - If IsFile is TRUE, then this */
/* is the name of the file to use for */
/* input. If IsFile is FALSE, then this */
/* is a pointer to a buffer containing a */
/* NULL terminated string which will be */
/* used as input for building tokens. */
/* */
/* Output: The function returns TRUE if it succeeded, FALSE */
/* otherwise. */
/* */
/* Error Handling: */
/* */
/* Side Effects: */
/* */
/* Notes: */
/* */
/*********************************************************************/
BOOLEAN SetInput(BOOLEAN IsFile, char * FilenameOrString)
{
uint Index; /* Used to walk Buffer. */
/* Initialize our global variables. */
CommandLine = NULL;
CurrentCommandLine = NULL;
CharactersConsumed = 0;
CurrentRow = 1;
CurrentColumn = 0;
InputFile = NULL;
CurrentState = Start;
Error_Source = NULL;
PositionInBuffer = 0;
ExamineCurrentCharacter = FALSE;
Flush_Count = 1;
CharacterToExamine = ' ';
NextCharacter = ' ';
/* Null out Buffer. */
for (Index = 0; Index < MaxIdentifierLength; Index++)
Buffer[Index] = 0;
if (!IsFile)
{
/* Since we have a command line, lets save it accordingly. */
CommandLine = FilenameOrString;
CurrentCommandLine = CommandLine;
Error_Source = CommandLine;
/* Indicate success. */
return TRUE;
}
else
{
/* Since we have a command file, we must open it and prepare it for use. */
/* Open the file. */
InputFile = fopen(FilenameOrString, "rt");
/* Did we succeed? */
if ( InputFile )
{
Error_Source = strdup(FilenameOrString);
if ( Error_Source == NULL )
{
/* Report the error. */
Report_Scanner_Error(Scanner_Out_Of_Memory,NULL,NULL,0,0);
/* Indicate failure. */
return FALSE;
}
return TRUE; /* Indicate success */
}
else
{
/* Report the error. */
Report_Scanner_Error(Bad_Command_File, FilenameOrString,NULL,0,0);
/* Indicate failure. */
return FALSE;
}
}
}
/*********************************************************************/
/* */
/* Function Name: GetToken */
/* */
/* Descriptive Name: Returns a token derived from the source set */
/* by the SetInput function. */
/* */
/* Input: None. */
/* */
/* Output: The function return value is a token. */
/* */
/* Error Handling: */
/* */
/* Side Effects: May alter the following static global variables, */
/* either directly or by calling other functions: */
/* CurrentCommandLine */
/* CharactersConsumed */
/* CurrentRow */
/* CurrentColumn */
/* InputFile */
/* Buffer */
/* PositionInBuffer */
/* ExamineCurrentCharacter */
/* CharacterToExamine */
/* CurrentState */
/* */
/* Notes: */
/* */
/*********************************************************************/
TokenType * GetToken(void)
{
/* The FSA depicted in the state table at the beginning of this file is
simulated using a switch statement. Each case in the switch statement
corresponds to a state in the state table. The CurrentState variable
is used to indicate which state the FSA is in. */
for (;;)
{
switch (CurrentState)
{
case Start : /* The START state */
/* Initialize PositionInBuffer to 0 since we are beginning a new token. */
PositionInBuffer = 0;
/* Enable comment detection and elimination. */
Detect_Comments = TRUE;
/* Get a character to examine.*/
GetCharacter();
/* Are we at EOF? */
if (CharacterToExamine == 0 )
{
/* We are out of input. Go to the EndState. */
CurrentState = EndState;
break;
}
/* If the first character is a quote, then we will not be keeping it. */
if ( CharacterToExamine == 0x22 )
{
/* Comments can not appear in strings, so disable comment detection. */
Detect_Comments = FALSE;
/* Go to the IsString state. */
CurrentState = IsString;
break;
}
/* Since we will be keeping this character, put it in the buffer. */
KeepCharacter();
/* Process the current character according to the state table. */
/* Check for a space. */
if (CharacterToExamine == ' ')
{
CurrentState = SingleSpace; /* Transition to the SingleSpace state. */
break;
}
/* Check for a tab. */
if (CharacterToExamine == '\t')
{
CurrentState = SingleTab; /* Transition to the SingleTab state. */
break;
}
/* Check for a minus sign. */
if ( CharacterToExamine == '-' )
{
/* Transition to the OptionCheck state. */
CurrentState = OptionCheck;
break;
}
/* Check for a plus sign. */
if ( CharacterToExamine == '+' )
{
/* Transition to the PositiveNumberCheck state. */
CurrentState = PositiveNumberCheck;
break;
}
/* Check for a number. */
if ( ( CharacterToExamine >= '0' ) &&
( CharacterToExamine <= '9' )
)
{
/* Transition to the IsNumber state. */
CurrentState = IsNumber;
break;
}
/* Check for a path name. */
if ( CharacterToExamine == '/' )
{
/* Transition to the IsPathName state. */
CurrentState = IsPathName;
break;
}
/* Is the character a letter of the alphabet? */
if ( (
( CharacterToExamine >= 'A' ) &&
( CharacterToExamine <= 'Z' )
) ||
(
( CharacterToExamine >= 'a' ) &&
( CharacterToExamine <= 'z' )
)
)
{
/* Transition to the IsKeyword state. */
CurrentState = IsKeyWord;
break;
}
/* Check for a separator. */
if ( (CharacterToExamine == ',')
||
(CharacterToExamine == ':')
||
(CharacterToExamine == '{')
||
(CharacterToExamine == '}')
||
(CharacterToExamine == '(')
||
(CharacterToExamine == ')')
||
(CharacterToExamine == '=')
)
{
/* We must create and return a separator token. */
return MakeToken(Separator);
}
/* Since we did not recognize the character, go to the error state! */
CurrentState = ErrorState;
/* Report the error. */
Report_Scanner_Error(Invalid_Character,Error_Source,&CharacterToExamine, CurrentColumn, CurrentRow);
break;
case OptionCheck:
/* Get a character to examine. */
GetCharacter();
/* Is the character part of a number or something else? */
if ( CharacterToExamine == '-' )
{
/* Keep the character. */
KeepCharacter();
/* Set the state to resume the FSA at the next time this function is called. */
CurrentState = Start;
/* We will treat this character as a separator since it is not part of a number. */
return MakeToken(Separator);
}
/* Since we did not recognize the character, we will give the IsNegativeNumber state a chance. */
ExamineCurrentCharacter = TRUE;
CurrentState = IsNegativeNumber;
break;
case IsNegativeNumber:
/* Get the next character to examine. */
GetCharacter();
/* Is the current character being examined a number? */
if ( ( CharacterToExamine >= '0' ) && ( CharacterToExamine <= '9' ) )
{
/* We have a negative number! Keep the character and transition to the IsNumber state. */
KeepCharacter();
CurrentState = IsNumber;
break;
}
/* Since we did not recognize this character, it can't be part of a number. We should only
have a '-' in the buffer, so we will save the current character for the Start state
to work on while we output a Separator token for the '-' in the buffer. */
ExamineCurrentCharacter = TRUE;
CurrentState = Start;
return MakeToken(Separator);
break; /* Keep the compiler happy. */
case IsNumber: /* The IsNumber state. */
/* Get the next character to examine. */
GetCharacter();
/* Is the character to examine part of a number? */
if ( (CharacterToExamine >= '0') && (CharacterToExamine <= '9') )
{
/* Lets keep the character. */
KeepCharacter();
break;
}
else
{
/* Is the character a period? If so, we may have a real number. */
if ( CharacterToExamine == '.' )
{
/* We will keep the character and change state. */
KeepCharacter();
CurrentState = IsRealNumber;
break;
}
else
{
/* Is the character a letter of the alphabet? */
if ( (
( CharacterToExamine >= 'A' ) &&
( CharacterToExamine <= 'Z' )
) ||
(
( CharacterToExamine >= 'a' ) &&
( CharacterToExamine <= 'z' )
)
)
{
/* Transition to the IsKeyword state. */
CurrentState = IsKeyWord;
}
}
}
/* If CurrentState is still IsNumber, then we did not recognize the character.
Since we did not recognize the character, transition to the SeparatorOrError state. */
if ( CurrentState == IsNumber )
CurrentState = SeparatorOrError;
/* We want the next state to examine this token. */
ExamineCurrentCharacter = TRUE;
/* According to the FSA table at the beginning of this file, we must output a token. */
return MakeToken(Number);
break;
case IsRealNumber:
/* Get the next character to examine. */
GetCharacter();
/* Is the character to examine part of a number? */
if ( (CharacterToExamine >= '0') && (CharacterToExamine <= '9') )
{
/* Lets keep the character. */
KeepCharacter();
break;
}
/* Is the character a letter of the alphabet? */
if ( (
( CharacterToExamine >= 'A' ) &&
( CharacterToExamine <= 'Z' )
) ||
(
( CharacterToExamine >= 'a' ) &&
( CharacterToExamine <= 'z' )
)
)
{
/* Transition to the IsKeyword state. */
CurrentState = IsKeyWord;
}
else
/* Since we did not recognize the character, transition to the SeparatorOrError state. */
CurrentState = SeparatorOrError;
/* We want the next state to examine this token. */
ExamineCurrentCharacter = TRUE;
/* According to the FSA table at the beginning of this file, we must output a token. */
return MakeToken(RealNumber);
break;
case SeparatorOrError:
/* Get the next character to examine. */
GetCharacter();
/* Check for a separator. */
if ( (CharacterToExamine == ',')
||
(CharacterToExamine == ':')
||
(CharacterToExamine == '{')
||
(CharacterToExamine == '}')
||
(CharacterToExamine == '(')
||
(CharacterToExamine == ')')
||
(CharacterToExamine == '=')
)
{
/* We will keep the character and return to the start state after creating a token. */
KeepCharacter();
CurrentState = Start;
/* We must create and return a separator token. */
return MakeToken(Separator);
}
/* Check for a space. */
if ( CharacterToExamine == ' ' )
{
/* Go to the SingleSpace state. */
CurrentState = SingleSpace;
/* Keep the current character. */
KeepCharacter();
break;
}
/* Check for a tab character. */
if ( CharacterToExamine == '\t' )
{
/* Go to the SingleTab state. */
CurrentState = SingleTab;
/* Keep the current character. */
KeepCharacter();
break;
}
/* Check for EOF */
if ( CharacterToExamine == 0 )
{
/* Go to the end state. */
CurrentState = EndState;
break;
}
/* Since we did not recognize the character, report the error and go to the error state! */
KeepCharacter();
Report_Scanner_Error(Invalid_Character, Error_Source, &CharacterToExamine, CurrentColumn, CurrentRow);
CurrentState = ErrorState;
break;
case PositiveNumberCheck:
/* Get the next character to examine. */
GetCharacter();
/* Is the character to examine part of a number? */
if ( (CharacterToExamine >= '0') && (CharacterToExamine <= '9') )
{
/* Lets keep the character. */
KeepCharacter();
/* Lets go to the IsNumber state. */
CurrentState = IsNumber;
break;
}
/* Since we did not recognize the character, report the error and go to the error state! */
KeepCharacter();
Report_Scanner_Error(Invalid_Character, Error_Source, &CharacterToExamine, CurrentColumn, CurrentRow);
CurrentState = ErrorState;
break;
case IsPathName:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a letter of the alphabet, a number, or '_' or '-'? */
if ( ( CharacterToExamine == '/' ) ||
(
( CharacterToExamine >= 'A' ) &&
( CharacterToExamine <= 'Z' )
) ||
(
( CharacterToExamine >= 'a' ) &&
( CharacterToExamine <= 'z' )
) ||
(
( CharacterToExamine >= '0' ) &&
( CharacterToExamine <= '9' )
) ||
( CharacterToExamine == '_' ) ||
( CharacterToExamine == '-' )
)
{
/* Lets keep it. */
KeepCharacter();
break;
}
/* Since we did not recognize the character, transition to the SeparatorOrError state. */
CurrentState = SeparatorOrError;
/* We want the SeparatorOrError state to examine this token. */
ExamineCurrentCharacter = TRUE;
/* According to the FSA table at the beginning of this file, we must output a token. */
return MakeToken(String);
break;
case IsKeyWord:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a letter of the alphabet, a number, or '_' or '-'? */
if (
(
( CharacterToExamine >= 'A' ) &&
( CharacterToExamine <= 'Z' )
) ||
(
( CharacterToExamine >= 'a' ) &&
( CharacterToExamine <= 'z' )
) ||
(
( CharacterToExamine >= '0' ) &&
( CharacterToExamine <= '9' )
) ||
( CharacterToExamine == '_' ) ||
( CharacterToExamine == '-' )
)
{
/* Lets keep it. */
KeepCharacter();
break;
}
/* Is the character a '/'? If so, transition to the IsPathName state. */
if ( CharacterToExamine == '/' )
{
/* Let the IsPathName state handle this character. */
ExamineCurrentCharacter = TRUE;
CurrentState = IsPathName;
break;
}
/* Since we did not recognize the character, transition to the SeparatorOrError state. */
CurrentState = SeparatorOrError;
/* We want the SeparatorOrError state to examine this token. */
ExamineCurrentCharacter = TRUE;
/* According to the FSA table at the beginning of this file, we must output a token. */
return MakeToken(KeyWord);
break;
case EndOfString :
/* Get the next character to examine. */
GetCharacter();
/* Is the character the ending quote mark for the string? If it is, then the Quote that
caused this state to be invoked is an embedded quote. We will keep this quote and
return to the IsString state to continue looking for the end of the string. */
if ( CharacterToExamine == 0x22 )
{
KeepCharacter();
CurrentState = IsString;
}
else
{
/* We have found the end of the string. The next state is the start state. */
CurrentState = Start;
/* We want the current character to be returned on the next GetCharacter call so that it
can be examined by the Start state. */
ExamineCurrentCharacter = TRUE;
/* Output the token. */
return MakeToken(String);
}
break;
case IsString:
/* Get the next character to examine. */
GetCharacter();
/* Is the character the ending quote mark for the string? Are we at EOF? */
if ( ( CharacterToExamine == 0x22 ) ||
( CharacterToExamine == 0 )
)
{
/* We may have found the end of the string. */
CurrentState = EndOfString;
}
else
{
/* Keep the character and return to this state. */
KeepCharacter();
}
break;
case SingleSpace:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a space? */
if ( CharacterToExamine == ' ' )
{
/* Keep the character and go to the multi-space state. */
KeepCharacter();
CurrentState = IsMultiSpace;
break;
}
/* Since the following character was not a space, set up to output a token and
resume the FSA at the start state with the current character. */
ExamineCurrentCharacter = TRUE;
CurrentState = Start;
return MakeToken(Space);
break; /* Keep the compiler happy. */
case IsMultiSpace:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a space? */
if ( CharacterToExamine == ' ' )
{
/* Keep the character and stay in this state. */
KeepCharacter();
break;
}
/* Since the following character was not a space, set up to output a token and
resume the FSA at the start state with the current character. */
ExamineCurrentCharacter = TRUE;
CurrentState = Start;
return MakeToken(MultiSpace);
break; /* Keep the compiler happy. */
case SingleTab:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a tab? */
if ( CharacterToExamine == '\t' )
{
/* Keep the character and go to the IsMultiTab state. */
KeepCharacter();
CurrentState = IsMultiTab;
break;
}
/* Since the following character was not a tab, set up to output a token and
resume the FSA at the start state with the current character. */
ExamineCurrentCharacter = TRUE;
CurrentState = Start;
return MakeToken(Tab);
break; /* Keep the compiler happy. */
case IsMultiTab:
/* Get the next character to examine. */
GetCharacter();
/* Is the character a tab? */
if ( CharacterToExamine == '\t' )
{
/* Keep the character and stay in this state. */
KeepCharacter();
break;
}
/* Since the following character was not a tab, set up to output a token and
resume the FSA at the start state with the current character. */
ExamineCurrentCharacter = TRUE;
CurrentState = Start;
return MakeToken( MultiTab );
break; /* Keep the compiler happy. */
case EndState : /* The "END" state. */
/* Cleanup */
if ( InputFile != NULL )
{
/* Since we were getting our input from a command file, close it. */
fclose(InputFile);
InputFile = NULL;
}
/* Make an EOF token. */
return MakeToken(EofToken);
case ErrorState : /* The "ERROR" state. */
/* Set up to go to the end state. */
CurrentState = EndState;
/* Now lets make an error token. */
return MakeToken(InvalidCharacter);
break;
} /* switch */
} /* for */
}
/*--------------------------------------------------
* Local Functions Available
--------------------------------------------------*/
/*********************************************************************/
/* */
/* Function Name: MakeToken */
/* */
/* Descriptive Name: Creates a token from the contents of the scan */
/* buffer and returns it as the function result. */
/* */
/* Input: TokenTypes Characterization - The characterization to */
/* assign to the token being */
/* made. */
/* */
/* Output: If Success : The function return value will be non-NULL,*/
/* and it will be a pointer to a new token. */
/* */
/* If Failure : The function return value will be NULL. */
/* */
/* Error Handling: If an error occurs, all memory allocated by this*/
/* function is freed, and NULL is returned as */
/* the function return value. */
/* */
/* Side Effects: Each position in buffer that contained a */
/* character used in the token is set to NULL. */
/* */
/* Notes: None. */
/* */
/*********************************************************************/
static TokenType * MakeToken(TokenCharacterizations Characterization)
{
uint Index; /* Used as an index when stepping through the Buffer. */
TokenType * New_Token; /* The return value. */
/* Do we have the memory for our return value? */
New_Token = (TokenType *) malloc(sizeof(TokenType) );
if ( New_Token == NULL)
{
Report_Scanner_Error(Scanner_Out_Of_Memory, Error_Source, &CharacterToExamine, CurrentColumn, CurrentRow);
return NULL;
}
/* To make a token, we must allocate memory for the contents of
Buffer and then copy the contents of Buffer. Once this has been
done, we need to set the Characterization, TokenRow, TokenColumn,
and TokenLength fields. */
if (PositionInBuffer > 0)
{
/* Allocate memory. */
New_Token->TokenText = (char *) malloc(PositionInBuffer + 1);
if (New_Token->TokenText == NULL)
{
/* Malloc failed! We must be out of memory. */
Report_Scanner_Error(Scanner_Out_Of_Memory, Error_Source, &CharacterToExamine, CurrentColumn, CurrentRow);
free(New_Token);
return NULL;
}
/* Copy the contents of Buffer. */
for (Index = 0; Index < PositionInBuffer; Index++)
{
New_Token->TokenText[Index] = Buffer[Index];
Buffer[Index] = 0;
}
/* Make sure that the string we copied from Buffer is NULL terminated. */
New_Token->TokenText[PositionInBuffer] = 0;
}
else /* Buffer is empty */
New_Token->TokenText = NULL;
/* Characterize the token. */
New_Token->Characterization = Characterization;
/* Save the token length and position. */
New_Token->TokenRow = CurrentRow;
New_Token->TokenColumn = CurrentColumn;
New_Token->TokenLength = PositionInBuffer;
New_Token->In_Lookahead_Queue = FALSE;
/* Reset the position at which characters will be added to the buffer. */
PositionInBuffer = 0;
/* Indicate success! */
return New_Token;
}
/* The following function gets a character from the command line or the input file.
It adjusts the CharactersConsumed and CurrentColumn variables everytime it gets
a character, and it adjusts both the CurrentColumn and CurrentRow variables whenever
it finds a carriage return or carriage return/linefeed pair.
NOTE: If GetCharacter encounters a NULL in a file or command line, it will treat
that as EOF. In fact, GetCharacter will return NULL to represent EOF.
NOTE: If there is an I/O error during input from a command file, this will be
treated as EOF also and NULL will be returned for the character value.
NOTE: Whenever EOF is returned by this function, if the source of input is a command
file, the command file will be closed. */
static void GetCharacter( void )
{
BOOLEAN In_Comment = FALSE;
BOOLEAN Do_Flush;
uint Nesting_Level = 0; /* Used to track nested comments. */
/* Should we return the currently loaded character or get a new one? */
if ( ExamineCurrentCharacter )
{
ExamineCurrentCharacter = FALSE;
if ( Detect_Comments )
{
/* Check for the start of a comment. */
if ( ( CharacterToExamine == '/' ) && ( NextCharacter == '*' ) )
{
Nesting_Level++;
In_Comment = TRUE;
}
}
if ( ! In_Comment )
return;
}
/* We will be in a loop until we get an acceptable character to return. */
do
{
/* Initialize CharacterToExamine to NextCharacter. Initialize NextCharacter to 0, which is our EOF indicator. */
CharacterToExamine = NextCharacter;
NextCharacter = 0;
/* Are we reading from a file or from the command line?*/
if (CommandLine != NULL )
{
/* We must get the next character from the command line. */
NextCharacter = *CurrentCommandLine;
/* If the character we are returning is NULL, we must NOT advance the CurrentCommandLine pointer! */
if ( *CurrentCommandLine != 0 )
CurrentCommandLine++;
}
else
{
/* Is there an open file for us to deal with? */
if ( InputFile != NULL )
{
/* We are reading from a file. Get the next character from the file. */
if ( fread(&NextCharacter,1,1,InputFile) != 1 )
{
/* There was a problem! We have reached the end of the input file or an I/O error perhaps. */
fclose(InputFile);
/* Clear out InputFile so that future calls to this function will return NULL. */
InputFile = NULL;
/* Set NextCharacter to EOF. */
NextCharacter = 0;
}
}
}
/* Have we been initialized yet? If not, then CharacterToExamine will be EOF and NextCharacter will hold the first character. */
if ( ! Init_Characters )
{
/* Before we return a character, we adjust the CurrentRow and CurrentColumn variables. If CharacterToExamine is a line feed
or carriage return/line feed pair, then we must adjust CurrentRow and CurrentColumn accordingly. If it is anything else,
then we just adjust CurrentColumn. */
if ( CharacterToExamine == '\n' )
{
CurrentColumn = 0;
CurrentRow++;
}
else
CurrentColumn++;
/* If the character is not NULL, then we must adjust the CharactersConsumed variable. */
if ( CharacterToExamine != 0 )
CharactersConsumed++;
}
if ( Detect_Comments )
{
if ( In_Comment )
{
/* Check for the end of the comment. */
if ( ( CharacterToExamine == 0 ) ||
( ( CharacterToExamine == '*' ) && ( NextCharacter == '/' ) )
)
{
Nesting_Level--;
if ( Nesting_Level == 0 )
{
In_Comment = FALSE;
Flush_Count = 2;
}
}
}
/* Check for the start of a comment. */
if ( ( CharacterToExamine == '/' ) && ( NextCharacter == '*' ) )
{
Nesting_Level++;
In_Comment = TRUE;
Init_Characters = FALSE; /* We need this here in case a file begins with a comment. Without this, our CurrentRow value will be off by the number of lines used by that initial comment. */
}
}
if ( Flush_Count > 0 )
{
Do_Flush = TRUE;
Flush_Count--;
}
else
Do_Flush = FALSE;
} while ( ( CharacterToExamine == '\r' ) || ( CharacterToExamine == '\n' ) || ( In_Comment ) || ( Do_Flush ) );
Init_Characters = FALSE;
// if ( Init_Characters )
// {
//
// Init_Characters = FALSE;
// GetCharacter();
//
// }
/* Return whatever character we have. */
return;
}
/* KeepCharacter - This puts the character passed to it into Buffer if Buffer is not yet full. */
static void KeepCharacter( void )
{
/* Do we have room in the Buffer? */
if ( PositionInBuffer < MaxIdentifierLength )
{
/* Place the character into the buffer. */
Buffer[PositionInBuffer] = CharacterToExamine;
PositionInBuffer++;
}
return;
}
|