1 /*****************************************************************************/
5 /* The scanner for the ca65 macroassembler */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
41 #include <sys/types.h> /* EMX needs this */
67 /*****************************************************************************/
69 /*****************************************************************************/
73 Token Tok = TOK_NONE; /* Current token */
74 int WS; /* Flag: Whitespace before token */
75 long IVal; /* Integer token attribute */
76 StrBuf SVal = STATIC_STRBUF_INITIALIZER;/* String token attribute */
78 FilePos CurPos = { 0, 0, 0 }; /* Name and position in current file */
82 /* Struct to handle include files. */
83 typedef struct InputFile InputFile;
85 FILE* F; /* Input file descriptor */
86 FilePos Pos; /* Position in file */
87 Token Tok; /* Last token */
88 int C; /* Last character */
89 char Line[256]; /* The current input line */
90 int IncSearchPath; /* True if we've added a search path */
91 int BinSearchPath; /* True if we've added a search path */
92 InputFile* Next; /* Linked list of input files */
95 /* Struct to handle textual input data */
96 typedef struct InputData InputData;
98 char* Text; /* Pointer to the text data */
99 const char* Pos; /* Pointer to current position */
100 int Malloced; /* Memory was malloced */
101 Token Tok; /* Last token */
102 int C; /* Last character */
103 InputData* Next; /* Linked list of input data */
106 /* Input source: Either file or data */
107 typedef struct CharSource CharSource;
109 /* Set of input functions */
110 typedef struct CharSourceFunctions CharSourceFunctions;
111 struct CharSourceFunctions {
112 void (*MarkStart) (CharSource*); /* Mark the start pos of a token */
113 void (*NextChar) (CharSource*); /* Read next char from input */
114 void (*Done) (CharSource*); /* Close input source */
117 /* Input source: Either file or data */
119 CharSource* Next; /* Linked list of char sources */
120 Token Tok; /* Last token */
121 int C; /* Last character */
122 const CharSourceFunctions* Func; /* Pointer to function table */
124 InputFile File; /* File data */
125 InputData Data; /* Textual data */
129 /* Current input variables */
130 static CharSource* Source = 0; /* Current char source */
131 static unsigned FCount = 0; /* Count of input files */
132 static int C = 0; /* Current input character */
134 /* Force end of assembly */
137 /* List of dot keywords with the corresponding tokens */
139 const char* Key; /* MUST be first field */
144 { ".ADDR", TOK_ADDR },
145 { ".ALIGN", TOK_ALIGN },
146 { ".AND", TOK_BOOLAND },
147 { ".ASCIIZ", TOK_ASCIIZ },
148 { ".ASSERT", TOK_ASSERT },
149 { ".AUTOIMPORT", TOK_AUTOIMPORT },
150 { ".BANKBYTE", TOK_BANKBYTE },
151 { ".BANKBYTES", TOK_BANKBYTES },
152 { ".BITAND", TOK_AND },
153 { ".BITNOT", TOK_NOT },
154 { ".BITOR", TOK_OR },
155 { ".BITXOR", TOK_XOR },
156 { ".BLANK", TOK_BLANK },
158 { ".BYT", TOK_BYTE },
159 { ".BYTE", TOK_BYTE },
160 { ".CASE", TOK_CASE },
161 { ".CHARMAP", TOK_CHARMAP },
162 { ".CODE", TOK_CODE },
163 { ".CONCAT", TOK_CONCAT },
164 { ".CONDES", TOK_CONDES },
165 { ".CONST", TOK_CONST },
166 { ".CONSTRUCTOR", TOK_CONSTRUCTOR },
168 { ".DATA", TOK_DATA },
170 { ".DBYT", TOK_DBYT },
171 { ".DEBUGINFO", TOK_DEBUGINFO },
172 { ".DEF", TOK_DEFINED },
173 { ".DEFINE", TOK_DEFINE },
174 { ".DEFINED", TOK_DEFINED },
175 { ".DESTRUCTOR", TOK_DESTRUCTOR },
176 { ".DWORD", TOK_DWORD },
177 { ".ELSE", TOK_ELSE },
178 { ".ELSEIF", TOK_ELSEIF },
180 { ".ENDENUM", TOK_ENDENUM },
181 { ".ENDIF", TOK_ENDIF },
182 { ".ENDMAC", TOK_ENDMACRO },
183 { ".ENDMACRO", TOK_ENDMACRO },
184 { ".ENDPROC", TOK_ENDPROC },
185 { ".ENDREP", TOK_ENDREP },
186 { ".ENDREPEAT", TOK_ENDREP },
187 { ".ENDSCOPE", TOK_ENDSCOPE },
188 { ".ENDSTRUCT", TOK_ENDSTRUCT },
189 { ".ENDUNION", TOK_ENDUNION },
190 { ".ENUM", TOK_ENUM },
191 { ".ERROR", TOK_ERROR },
192 { ".EXITMAC", TOK_EXITMACRO },
193 { ".EXITMACRO", TOK_EXITMACRO },
194 { ".EXPORT", TOK_EXPORT },
195 { ".EXPORTZP", TOK_EXPORTZP },
196 { ".FARADDR", TOK_FARADDR },
197 { ".FEATURE", TOK_FEATURE },
198 { ".FILEOPT", TOK_FILEOPT },
199 { ".FOPT", TOK_FILEOPT },
200 { ".FORCEIMPORT", TOK_FORCEIMPORT },
201 { ".FORCEWORD", TOK_FORCEWORD },
202 { ".GLOBAL", TOK_GLOBAL },
203 { ".GLOBALZP", TOK_GLOBALZP },
204 { ".HIBYTE", TOK_HIBYTE },
205 { ".HIBYTES", TOK_HIBYTES },
206 { ".HIWORD", TOK_HIWORD },
209 { ".IDENT", TOK_MAKEIDENT },
211 { ".IFBLANK", TOK_IFBLANK },
212 { ".IFCONST", TOK_IFCONST },
213 { ".IFDEF", TOK_IFDEF },
214 { ".IFNBLANK", TOK_IFNBLANK },
215 { ".IFNCONST", TOK_IFNCONST },
216 { ".IFNDEF", TOK_IFNDEF },
217 { ".IFNREF", TOK_IFNREF },
218 { ".IFP02", TOK_IFP02 },
219 { ".IFP816", TOK_IFP816 },
220 { ".IFPC02", TOK_IFPC02 },
221 { ".IFPSC02", TOK_IFPSC02 },
222 { ".IFREF", TOK_IFREF },
223 { ".IMPORT", TOK_IMPORT },
224 { ".IMPORTZP", TOK_IMPORTZP },
225 { ".INCBIN", TOK_INCBIN },
226 { ".INCLUDE", TOK_INCLUDE },
227 { ".INTERRUPTOR", TOK_INTERRUPTOR },
228 { ".LEFT", TOK_LEFT },
229 { ".LINECONT", TOK_LINECONT },
230 { ".LIST", TOK_LIST },
231 { ".LISTBYTES", TOK_LISTBYTES },
232 { ".LOBYTE", TOK_LOBYTE },
233 { ".LOBYTES", TOK_LOBYTES },
234 { ".LOCAL", TOK_LOCAL },
235 { ".LOCALCHAR", TOK_LOCALCHAR },
236 { ".LOWORD", TOK_LOWORD },
237 { ".MAC", TOK_MACRO },
238 { ".MACPACK", TOK_MACPACK },
239 { ".MACRO", TOK_MACRO },
240 { ".MATCH", TOK_MATCH },
245 { ".NOT", TOK_BOOLNOT },
246 { ".NULL", TOK_NULL },
247 { ".OR", TOK_BOOLOR },
251 { ".P816", TOK_P816 },
252 { ".PAGELEN", TOK_PAGELENGTH },
253 { ".PAGELENGTH", TOK_PAGELENGTH },
254 { ".PARAMCOUNT", TOK_PARAMCOUNT },
255 { ".PC02", TOK_PC02 },
256 { ".POPCPU", TOK_POPCPU },
257 { ".POPSEG", TOK_POPSEG },
258 { ".PROC", TOK_PROC },
259 { ".PSC02", TOK_PSC02 },
260 { ".PUSHCPU", TOK_PUSHCPU },
261 { ".PUSHSEG", TOK_PUSHSEG },
262 { ".REF", TOK_REFERENCED },
263 { ".REFERENCED", TOK_REFERENCED },
264 { ".RELOC", TOK_RELOC },
265 { ".REPEAT", TOK_REPEAT },
267 { ".RIGHT", TOK_RIGHT },
268 { ".RODATA", TOK_RODATA },
269 { ".SCOPE", TOK_SCOPE },
270 { ".SEGMENT", TOK_SEGMENT },
272 { ".SETCPU", TOK_SETCPU },
275 { ".SIZEOF", TOK_SIZEOF },
276 { ".SMART", TOK_SMART },
277 { ".SPRINTF", TOK_SPRINTF },
278 { ".STRAT", TOK_STRAT },
279 { ".STRING", TOK_STRING },
280 { ".STRLEN", TOK_STRLEN },
281 { ".STRUCT", TOK_STRUCT },
282 { ".SUNPLUS", TOK_SUNPLUS },
284 { ".TCOUNT", TOK_TCOUNT },
285 { ".TIME", TOK_TIME },
286 { ".UNION", TOK_UNION },
287 { ".VERSION", TOK_VERSION },
288 { ".WARNING", TOK_WARNING },
289 { ".WORD", TOK_WORD },
290 { ".XMATCH", TOK_XMATCH },
291 { ".XOR", TOK_BOOLXOR },
292 { ".ZEROPAGE", TOK_ZEROPAGE },
297 /*****************************************************************************/
298 /* CharSource functions */
299 /*****************************************************************************/
303 static void UseCharSource (CharSource* S)
304 /* Initialize a new input source and start to use it. */
306 /* Remember the current input char and token */
310 /* Use the new input source */
314 /* Read the first character from the new file */
315 S->Func->NextChar (S);
317 /* Setup the next token so it will be skipped on the next call to
325 static void DoneCharSource (void)
326 /* Close the top level character source */
330 /* First, call the type specific function */
331 Source->Func->Done (Source);
333 /* Restore the old token */
337 /* Remember the last stacked input source */
340 /* Delete the top level one ... */
343 /* ... and use the one before */
349 /*****************************************************************************/
350 /* InputFile functions */
351 /*****************************************************************************/
355 static void IFMarkStart (CharSource* S)
356 /* Mark the start of the next token */
358 CurPos = S->V.File.Pos;
363 static void IFNextChar (CharSource* S)
364 /* Read the next character from the input file */
366 /* Check for end of line, read the next line if needed */
367 while (S->V.File.Line [S->V.File.Pos.Col] == '\0') {
369 unsigned Len, Removed;
371 /* End of current line reached, read next line */
372 if (fgets (S->V.File.Line, sizeof (S->V.File.Line), S->V.File.F) == 0) {
373 /* End of file. Add an empty line to the listing. This is a
374 * small hack needed to keep the PC output in sync.
376 NewListingLine ("", S->V.File.Pos.Name, FCount);
381 /* For better handling of files with unusual line endings (DOS
382 * files that are accidently translated on Unix for example),
383 * first remove all whitespace at the end, then add a single
386 Len = strlen (S->V.File.Line);
388 while (Len > 0 && IsSpace (S->V.File.Line[Len-1])) {
393 S->V.File.Line[Len+0] = '\n';
394 S->V.File.Line[Len+1] = '\0';
398 S->V.File.Pos.Line++;
399 S->V.File.Pos.Col = 0;
401 /* Remember the new line for the listing */
402 NewListingLine (S->V.File.Line, S->V.File.Pos.Name, FCount);
406 /* Return the next character from the file */
407 C = S->V.File.Line [S->V.File.Pos.Col++];
412 void IFDone (CharSource* S)
413 /* Close the current input file */
415 /* We're at the end of an include file. Check if we have any
416 * open .IFs, or any open token lists in this file. This
417 * enforcement is artificial, using conditionals that start
418 * in one file and end in another are uncommon, and don't
419 * allowing these things will help finding errors.
423 /* If we've added search paths for this file, remove them */
424 if (S->V.File.IncSearchPath) {
425 PopSearchPath (IncSearchPath);
427 if (S->V.File.BinSearchPath) {
428 PopSearchPath (BinSearchPath);
431 /* Close the input file and decrement the file count. We will ignore
432 * errors here, since we were just reading from the file.
434 (void) fclose (S->V.File.F);
440 /* Set of input file handling functions */
441 static const CharSourceFunctions IFFunc = {
449 int NewInputFile (const char* Name)
450 /* Open a new input file. Returns true if the file could be successfully opened
451 * and false otherwise.
454 int RetCode = 0; /* Return code. Assume an error. */
458 StrBuf NameBuf; /* No need to initialize */
459 StrBuf Path = AUTO_STRBUF_INITIALIZER;
464 /* If this is the main file, just try to open it. If it's an include file,
465 * search for it using the include path list.
469 F = fopen (Name, "r");
471 Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
474 /* We are on include level. Search for the file in the include
477 PathName = SearchFile (IncSearchPath, Name);
478 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
479 /* Not found or cannot open, print an error and bail out */
480 Error ("Cannot open include file `%s': %s", Name, strerror (errno));
484 /* Use the path name from now on */
488 /* Stat the file and remember the values. There a race condition here,
489 * since we cannot use fileno() (non standard identifier in standard
490 * header file), and therefore not fstat. When using stat with the
491 * file name, there's a risk that the file was deleted and recreated
492 * while it was open. Since mtime and size are only used to check
493 * if a file has changed in the debugger, we will ignore this problem
496 if (stat (Name, &Buf) != 0) {
497 Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno));
500 /* Add the file to the input file table and remember the index */
501 FileIdx = AddFile (SB_InitFromString (&NameBuf, Name),
502 (FCount == 0)? FT_MAIN : FT_INCLUDE,
503 Buf.st_size, Buf.st_mtime);
505 /* Create a new input source variable and initialize it */
506 S = xmalloc (sizeof (*S));
509 S->V.File.Pos.Line = 0;
510 S->V.File.Pos.Col = 0;
511 S->V.File.Pos.Name = FileIdx;
512 S->V.File.Line[0] = '\0';
514 /* Push the path for this file onto the include search lists */
515 SB_CopyBuf (&Path, Name, FindName (Name) - Name);
516 SB_Terminate (&Path);
517 S->V.File.IncSearchPath = PushSearchPath (IncSearchPath, SB_GetConstBuf (&Path));
518 S->V.File.BinSearchPath = PushSearchPath (BinSearchPath, SB_GetConstBuf (&Path));
521 /* Count active input files */
524 /* Use this input source */
527 /* File successfully opened */
531 /* Free an allocated name buffer */
534 /* Return the success code */
540 /*****************************************************************************/
541 /* InputData functions */
542 /*****************************************************************************/
546 static void IDMarkStart (CharSource* S attribute ((unused)))
547 /* Mark the start of the next token */
549 /* Nothing to do here */
554 static void IDNextChar (CharSource* S)
555 /* Read the next character from the input text */
557 C = *S->V.Data.Pos++;
559 /* End of input data */
567 void IDDone (CharSource* S)
568 /* Close the current input data */
570 /* Cleanup the current stuff */
571 if (S->V.Data.Malloced) {
572 xfree (S->V.Data.Text);
578 /* Set of input data handling functions */
579 static const CharSourceFunctions IDFunc = {
587 void NewInputData (char* Text, int Malloced)
588 /* Add a chunk of input data to the input stream */
592 /* Create a new input source variable and initialize it */
593 S = xmalloc (sizeof (*S));
595 S->V.Data.Text = Text;
596 S->V.Data.Pos = Text;
597 S->V.Data.Malloced = Malloced;
599 /* Use this input source */
605 /*****************************************************************************/
606 /* Character classification functions */
607 /*****************************************************************************/
612 /* Return true if the character is a valid character for an identifier */
614 return IsAlNum (C) ||
616 (C == '@' && AtInIdents) ||
617 (C == '$' && DollarInIdents);
622 int IsIdStart (int C)
623 /* Return true if the character may start an identifier */
625 return IsAlpha (C) || C == '_';
630 /*****************************************************************************/
632 /*****************************************************************************/
636 static unsigned DigitVal (unsigned char C)
637 /* Convert a digit into it's numerical representation */
642 return toupper (C) - 'A' + 10;
648 static void NextChar (void)
649 /* Read the next character from the input file */
651 Source->Func->NextChar (Source);
656 void LocaseSVal (void)
657 /* Make SVal lower case */
664 void UpcaseSVal (void)
665 /* Make SVal upper case */
672 static int CmpDotKeyword (const void* K1, const void* K2)
673 /* Compare function for the dot keyword search */
675 return strcmp (((struct DotKeyword*)K1)->Key, ((struct DotKeyword*)K2)->Key);
680 static unsigned char FindDotKeyword (void)
681 /* Find the dot keyword in SVal. Return the corresponding token if found,
682 * return TOK_NONE if not found.
686 struct DotKeyword* R;
689 K.Key = SB_GetConstBuf (&SVal);
692 /* If we aren't in ignore case mode, we have to uppercase the keyword */
697 /* Search for the keyword */
698 R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]),
699 sizeof (DotKeywords [0]), CmpDotKeyword);
709 static void ReadIdent (void)
710 /* Read an identifier from the current input position into Ident. Filling SVal
711 * starts at the current position with the next character in C. It is assumed
712 * that any characters already filled in are ok, and the character in C is
716 /* Read the identifier */
718 SB_AppendChar (&SVal, C);
720 } while (IsIdChar (C));
721 SB_Terminate (&SVal);
723 /* If we should ignore case, convert the identifier to upper case */
731 static void ReadStringConst (int StringTerm)
732 /* Read a string constant into SVal. */
734 /* Skip the leading string terminator */
737 /* Read the string */
739 if (C == StringTerm) {
742 if (C == '\n' || C == EOF) {
743 Error ("Newline in string constant");
747 /* Append the char to the string */
748 SB_AppendChar (&SVal, C);
750 /* Skip the character */
754 /* Skip the trailing terminator */
757 /* Terminate the string */
758 SB_Terminate (&SVal);
763 static int Sweet16Reg (const StrBuf* Id)
764 /* Check if the given identifier is a sweet16 register. Return -1 if this is
765 * not the case, return the register number otherwise.
771 if (SB_GetLen (Id) < 2) {
774 if (toupper (SB_AtUnchecked (Id, 0)) != 'R') {
777 if (!IsDigit (SB_AtUnchecked (Id, 1))) {
781 if (sscanf (SB_GetConstBuf (Id)+1, "%u%c", &RegNum, &Check) != 1 || RegNum > 15) {
782 /* Invalid register */
786 /* The register number is valid */
792 void NextRawTok (void)
793 /* Read the next raw token from the input stream */
795 /* If we've a forced end of assembly, don't read further */
802 /* Check if we have tokens from another input source */
803 if (InputFromStack ()) {
808 /* Skip whitespace, remember if we had some */
809 if ((WS = IsBlank (C)) != 0) {
812 } while (IsBlank (C));
815 /* Mark the file position of the next token */
816 Source->Func->MarkStart (Source);
818 /* Clear the string attribute */
821 /* Hex number or PC symbol? */
825 /* Hex digit must follow or DollarIsPC must be enabled */
831 Error ("Hexadecimal digit expected");
835 /* Read the number */
837 while (IsXDigit (C)) {
838 if (IVal & 0xF0000000) {
839 Error ("Overflow in hexadecimal number");
842 IVal = (IVal << 4) + DigitVal (C);
846 /* This is an integer constant */
855 /* 0 or 1 must follow */
857 Error ("Binary digit expected");
860 /* Read the number */
862 while (IsBDigit (C)) {
863 if (IVal & 0x80000000) {
864 Error ("Overflow in binary number");
867 IVal = (IVal << 1) + DigitVal (C);
871 /* This is an integer constant */
886 /* Ignore leading zeros */
891 /* Read the number into Buf counting the digits */
893 while (IsXDigit (C)) {
895 /* Buf is big enough to allow any decimal and hex number to
896 * overflow, so ignore excess digits here, they will be detected
897 * when we convert the value.
899 if (Digits < sizeof (Buf)) {
906 /* Allow zilog/intel style hex numbers with a 'h' suffix */
907 if (C == 'h' || C == 'H') {
910 Max = 0xFFFFFFFFUL / 16;
913 Max = 0xFFFFFFFFUL / 10;
916 /* Convert the number using the given base */
918 for (I = 0; I < Digits; ++I) {
920 Error ("Number out of range");
924 DVal = DigitVal (Buf[I]);
926 Error ("Invalid digits in number");
930 IVal = (IVal * Base) + DVal;
933 /* This is an integer constant */
938 /* Control command? */
941 /* Remember and skip the dot */
944 /* Check if it's just a dot */
945 if (!IsIdStart (C)) {
952 /* Read the remainder of the identifier */
953 SB_AppendChar (&SVal, '.');
956 /* Dot keyword, search for it */
957 Tok = FindDotKeyword ();
958 if (Tok == TOK_NONE) {
961 if (!LeadingDotInIdents) {
962 /* Invalid pseudo instruction */
963 Error ("`%m%p' is not a recognized control command", &SVal);
967 /* An identifier with a dot. Check if it's a define style
970 if (IsDefine (&SVal)) {
971 /* This is a define style macro - expand it */
976 /* Just an identifier with a dot */
984 /* Indirect op for sweet16 cpu. Must check this before checking for local
985 * symbols, because these may also use the '@' symbol.
987 if (CPU == CPU_SWEET16 && C == '@') {
994 if (C == LocalStart) {
996 /* Read the identifier. */
999 /* Start character alone is not enough */
1000 if (SB_GetLen (&SVal) == 1) {
1001 Error ("Invalid cheap local symbol");
1005 /* A local identifier */
1006 Tok = TOK_LOCAL_IDENT;
1011 /* Identifier or keyword? */
1012 if (IsIdStart (C)) {
1014 /* Read the identifier */
1017 /* Check for special names. Bail out if we have identified the type of
1018 * the token. Go on if the token is an identifier.
1020 if (SB_GetLen (&SVal) == 1) {
1021 switch (toupper (SB_AtUnchecked (&SVal, 0))) {
1026 Tok = TOK_OVERRIDE_ABS;
1035 Tok = TOK_OVERRIDE_FAR;
1041 if (CPU == CPU_65816) {
1058 Tok = TOK_OVERRIDE_ZP;
1067 } else if (CPU == CPU_SWEET16 && (IVal = Sweet16Reg (&SVal)) >= 0) {
1069 /* A sweet16 register number in sweet16 mode */
1075 /* Check for define style macro */
1076 if (IsDefine (&SVal)) {
1077 /* Macro - expand it */
1087 /* Ok, let's do the switch */
1105 } else if (CComments) {
1106 /* Remember the position, then skip the '*' */
1107 FilePos Pos = CurPos;
1112 PError (&Pos, "Unterminated comment");
1160 Tok = TOK_NAMESPACE;
1199 while (C != '\n' && C != EOF) {
1244 } else if (C == '<') {
1247 } else if (C == '>') {
1270 } else if (C == '>') {
1284 /* Hack: If we allow ' as terminating character for strings, read
1285 * the following stuff as a string, and check for a one character
1288 if (LooseStringTerm) {
1289 ReadStringConst ('\'');
1290 if (SB_GetLen (&SVal) == 1) {
1291 IVal = SB_AtUnchecked (&SVal, 0);
1297 /* Always a character constant */
1299 if (C == EOF || IsControl (C)) {
1300 Error ("Illegal character constant");
1307 if (!MissingCharTerm) {
1308 Error ("Illegal character constant");
1317 ReadStringConst ('\"');
1322 /* Line continuation? */
1326 /* Handle as white space */
1341 /* In case of the main file, do not close it, but return EOF. */
1342 if (Source && Source->Next) {
1351 /* If we go here, we could not identify the current character. Skip it
1354 Error ("Invalid input character: 0x%02X", C & 0xFF);
1361 int GetSubKey (const char** Keys, unsigned Count)
1362 /* Search for a subkey in a table of keywords. The current token must be an
1363 * identifier and all keys must be in upper case. The identifier will be
1364 * uppercased in the process. The function returns the index of the keyword,
1365 * or -1 if the keyword was not found.
1370 /* Must have an identifier */
1371 PRECONDITION (Tok == TOK_IDENT);
1373 /* If we aren't in ignore case mode, we have to uppercase the identifier */
1378 /* Do a linear search (a binary search is not worth the effort) */
1379 for (I = 0; I < Count; ++I) {
1380 if (SB_CompareStr (&SVal, Keys [I]) == 0) {
1392 unsigned char ParseAddrSize (void)
1393 /* Check if the next token is a keyword that denotes an address size specifier.
1394 * If so, return the corresponding address size constant, otherwise output an
1395 * error message and return ADDR_SIZE_DEFAULT.
1398 unsigned char AddrSize;
1400 /* Check for an identifier */
1401 if (Tok != TOK_IDENT) {
1402 Error ("Address size specifier expected");
1403 return ADDR_SIZE_DEFAULT;
1406 /* Convert the attribute */
1407 AddrSize = AddrSizeFromStr (SB_GetConstBuf (&SVal));
1408 if (AddrSize == ADDR_SIZE_INVALID) {
1409 Error ("Address size specifier expected");
1410 AddrSize = ADDR_SIZE_DEFAULT;
1419 void InitScanner (const char* InFile)
1420 /* Initialize the scanner, open the given input file */
1422 /* Open the input file */
1423 NewInputFile (InFile);
1428 void DoneScanner (void)
1429 /* Release scanner resources */