1 /*****************************************************************************/
5 /* The scanner for the ca65 macroassembler */
9 /* (C) 1998-2013, 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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 /* Current input token incl. attributes */
73 Token CurTok = STATIC_TOKEN_INITIALIZER;
75 /* Struct to handle include files. */
76 typedef struct InputFile InputFile;
78 FILE* F; /* Input file descriptor */
79 FilePos Pos; /* Position in file */
80 token_t Tok; /* Last token */
81 int C; /* Last character */
82 StrBuf Line; /* The current input line */
83 int IncSearchPath; /* True if we've added a search path */
84 int BinSearchPath; /* True if we've added a search path */
85 InputFile* Next; /* Linked list of input files */
88 /* Struct to handle textual input data */
89 typedef struct InputData InputData;
91 char* Text; /* Pointer to the text data */
92 const char* Pos; /* Pointer to current position */
93 int Malloced; /* Memory was malloced */
94 token_t Tok; /* Last token */
95 int C; /* Last character */
96 InputData* Next; /* Linked list of input data */
99 /* Input source: Either file or data */
100 typedef struct CharSource CharSource;
102 /* Set of input functions */
103 typedef struct CharSourceFunctions CharSourceFunctions;
104 struct CharSourceFunctions {
105 void (*MarkStart) (CharSource*); /* Mark the start pos of a token */
106 void (*NextChar) (CharSource*); /* Read next char from input */
107 void (*Done) (CharSource*); /* Close input source */
110 /* Input source: Either file or data */
112 CharSource* Next; /* Linked list of char sources */
113 token_t Tok; /* Last token */
114 int C; /* Last character */
115 const CharSourceFunctions* Func; /* Pointer to function table */
117 InputFile File; /* File data */
118 InputData Data; /* Textual data */
122 /* Current input variables */
123 static CharSource* Source = 0; /* Current char source */
124 static unsigned FCount = 0; /* Count of input files */
125 static int C = 0; /* Current input character */
127 /* Force end of assembly */
130 /* List of dot keywords with the corresponding tokens */
132 const char* Key; /* MUST be first field */
137 { ".ADDR", TOK_ADDR },
138 { ".ADDRSIZE", TOK_ADDRSIZE },
139 { ".ALIGN", TOK_ALIGN },
140 { ".AND", TOK_BOOLAND },
141 { ".ASCIIZ", TOK_ASCIIZ },
142 { ".ASIZE", TOK_ASIZE },
143 { ".ASSERT", TOK_ASSERT },
144 { ".AUTOIMPORT", TOK_AUTOIMPORT },
145 { ".BANK", TOK_BANK },
146 { ".BANKBYTE", TOK_BANKBYTE },
147 { ".BANKBYTES", TOK_BANKBYTES },
148 { ".BITAND", TOK_AND },
149 { ".BITNOT", TOK_NOT },
150 { ".BITOR", TOK_OR },
151 { ".BITXOR", TOK_XOR },
152 { ".BLANK", TOK_BLANK },
154 { ".BYT", TOK_BYTE },
155 { ".BYTE", TOK_BYTE },
156 { ".CASE", TOK_CASE },
157 { ".CHARMAP", TOK_CHARMAP },
158 { ".CODE", TOK_CODE },
159 { ".CONCAT", TOK_CONCAT },
160 { ".CONDES", TOK_CONDES },
161 { ".CONST", TOK_CONST },
162 { ".CONSTRUCTOR", TOK_CONSTRUCTOR },
164 { ".DATA", TOK_DATA },
166 { ".DBYT", TOK_DBYT },
167 { ".DEBUGINFO", TOK_DEBUGINFO },
168 { ".DEF", TOK_DEFINED },
169 { ".DEFINE", TOK_DEFINE },
170 { ".DEFINED", TOK_DEFINED },
171 { ".DEFINEDMACRO", TOK_DEFINEDMACRO },
172 { ".DELMAC", TOK_DELMAC },
173 { ".DELMACRO", TOK_DELMAC },
174 { ".DESTRUCTOR", TOK_DESTRUCTOR },
175 { ".DWORD", TOK_DWORD },
176 { ".ELSE", TOK_ELSE },
177 { ".ELSEIF", TOK_ELSEIF },
179 { ".ENDENUM", TOK_ENDENUM },
180 { ".ENDIF", TOK_ENDIF },
181 { ".ENDMAC", TOK_ENDMACRO },
182 { ".ENDMACRO", TOK_ENDMACRO },
183 { ".ENDPROC", TOK_ENDPROC },
184 { ".ENDREP", TOK_ENDREP },
185 { ".ENDREPEAT", TOK_ENDREP },
186 { ".ENDSCOPE", TOK_ENDSCOPE },
187 { ".ENDSTRUCT", TOK_ENDSTRUCT },
188 { ".ENDUNION", TOK_ENDUNION },
189 { ".ENUM", TOK_ENUM },
190 { ".ERROR", TOK_ERROR },
191 { ".EXITMAC", TOK_EXITMACRO },
192 { ".EXITMACRO", TOK_EXITMACRO },
193 { ".EXPORT", TOK_EXPORT },
194 { ".EXPORTZP", TOK_EXPORTZP },
195 { ".FARADDR", TOK_FARADDR },
196 { ".FATAL", TOK_FATAL },
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 { ".ISIZE", TOK_ISIZE },
229 { ".ISMNEM", TOK_ISMNEMONIC },
230 { ".ISMNEMONIC", TOK_ISMNEMONIC },
231 { ".LEFT", TOK_LEFT },
232 { ".LINECONT", TOK_LINECONT },
233 { ".LIST", TOK_LIST },
234 { ".LISTBYTES", TOK_LISTBYTES },
235 { ".LOBYTE", TOK_LOBYTE },
236 { ".LOBYTES", TOK_LOBYTES },
237 { ".LOCAL", TOK_LOCAL },
238 { ".LOCALCHAR", TOK_LOCALCHAR },
239 { ".LOWORD", TOK_LOWORD },
240 { ".MAC", TOK_MACRO },
241 { ".MACPACK", TOK_MACPACK },
242 { ".MACRO", TOK_MACRO },
243 { ".MATCH", TOK_MATCH },
248 { ".NOT", TOK_BOOLNOT },
249 { ".NULL", TOK_NULL },
250 { ".OR", TOK_BOOLOR },
254 { ".P816", TOK_P816 },
255 { ".PAGELEN", TOK_PAGELENGTH },
256 { ".PAGELENGTH", TOK_PAGELENGTH },
257 { ".PARAMCOUNT", TOK_PARAMCOUNT },
258 { ".PC02", TOK_PC02 },
259 { ".POPCPU", TOK_POPCPU },
260 { ".POPSEG", TOK_POPSEG },
261 { ".PROC", TOK_PROC },
262 { ".PSC02", TOK_PSC02 },
263 { ".PUSHCPU", TOK_PUSHCPU },
264 { ".PUSHSEG", TOK_PUSHSEG },
265 { ".REF", TOK_REFERENCED },
266 { ".REFERENCED", TOK_REFERENCED },
267 { ".RELOC", TOK_RELOC },
268 { ".REPEAT", TOK_REPEAT },
270 { ".RIGHT", TOK_RIGHT },
271 { ".RODATA", TOK_RODATA },
272 { ".SCOPE", TOK_SCOPE },
273 { ".SEGMENT", TOK_SEGMENT },
275 { ".SETCPU", TOK_SETCPU },
278 { ".SIZEOF", TOK_SIZEOF },
279 { ".SMART", TOK_SMART },
280 { ".SPRINTF", TOK_SPRINTF },
281 { ".STRAT", TOK_STRAT },
282 { ".STRING", TOK_STRING },
283 { ".STRLEN", TOK_STRLEN },
284 { ".STRUCT", TOK_STRUCT },
286 { ".TCOUNT", TOK_TCOUNT },
287 { ".TIME", TOK_TIME },
288 { ".UNDEF", TOK_UNDEF },
289 { ".UNDEFINE", TOK_UNDEF },
290 { ".UNION", TOK_UNION },
291 { ".VERSION", TOK_VERSION },
292 { ".WARNING", TOK_WARNING },
293 { ".WORD", TOK_WORD },
294 { ".XMATCH", TOK_XMATCH },
295 { ".XOR", TOK_BOOLXOR },
296 { ".ZEROPAGE", TOK_ZEROPAGE },
301 /*****************************************************************************/
302 /* CharSource functions */
303 /*****************************************************************************/
307 static void UseCharSource (CharSource* S)
308 /* Initialize a new input source and start to use it. */
310 /* Remember the current input char and token */
314 /* Use the new input source */
318 /* Read the first character from the new file */
319 S->Func->NextChar (S);
321 /* Setup the next token so it will be skipped on the next call to
324 CurTok.Tok = TOK_SEP;
329 static void DoneCharSource (void)
330 /* Close the top level character source */
334 /* First, call the type specific function */
335 Source->Func->Done (Source);
337 /* Restore the old token */
338 CurTok.Tok = Source->Tok;
341 /* Remember the last stacked input source */
344 /* Delete the top level one ... */
347 /* ... and use the one before */
353 /*****************************************************************************/
354 /* InputFile functions */
355 /*****************************************************************************/
359 static void IFMarkStart (CharSource* S)
360 /* Mark the start of the next token */
362 CurTok.Pos = S->V.File.Pos;
367 static void IFNextChar (CharSource* S)
368 /* Read the next character from the input file */
370 /* Check for end of line, read the next line if needed */
371 while (SB_GetIndex (&S->V.File.Line) >= SB_GetLen (&S->V.File.Line)) {
375 /* End of current line reached, read next line */
376 SB_Clear (&S->V.File.Line);
379 int N = fgetc (S->V.File.F);
381 /* End of file. Accept files without a newline at the end */
382 if (SB_NotEmpty (&S->V.File.Line)) {
386 /* No more data - add an empty line to the listing. This
387 ** is a small hack needed to keep the PC output in sync.
389 NewListingLine (&EmptyStrBuf, S->V.File.Pos.Name, FCount);
393 /* Check for end of line */
394 } else if (N == '\n') {
399 /* Collect other stuff */
402 /* Append data to line */
403 SB_AppendChar (&S->V.File.Line, N);
409 /* If we come here, we have a new input line. To avoid problems
410 ** with strange line terminators, remove all whitespace from the
411 ** end of the line, then add a single newline.
413 Len = SB_GetLen (&S->V.File.Line);
414 while (Len > 0 && IsSpace (SB_AtUnchecked (&S->V.File.Line, Len-1))) {
417 SB_Drop (&S->V.File.Line, SB_GetLen (&S->V.File.Line) - Len);
418 SB_AppendChar (&S->V.File.Line, '\n');
420 /* Terminate the string buffer */
421 SB_Terminate (&S->V.File.Line);
424 S->V.File.Pos.Line++;
426 /* Remember the new line for the listing */
427 NewListingLine (&S->V.File.Line, S->V.File.Pos.Name, FCount);
431 /* Set the column pointer */
432 S->V.File.Pos.Col = SB_GetIndex (&S->V.File.Line);
434 /* Return the next character from the buffer */
435 C = SB_Get (&S->V.File.Line);
440 void IFDone (CharSource* S)
441 /* Close the current input file */
443 /* We're at the end of an include file. Check if we have any
444 ** open .IFs, or any open token lists in this file. This
445 ** enforcement is artificial, using conditionals that start
446 ** in one file and end in another are uncommon, and don't
447 ** allowing these things will help finding errors.
451 /* If we've added search paths for this file, remove them */
452 if (S->V.File.IncSearchPath) {
453 PopSearchPath (IncSearchPath);
455 if (S->V.File.BinSearchPath) {
456 PopSearchPath (BinSearchPath);
459 /* Free the line buffer */
460 SB_Done (&S->V.File.Line);
462 /* Close the input file and decrement the file count. We will ignore
463 ** errors here, since we were just reading from the file.
465 (void) fclose (S->V.File.F);
471 /* Set of input file handling functions */
472 static const CharSourceFunctions IFFunc = {
480 int NewInputFile (const char* Name)
481 /* Open a new input file. Returns true if the file could be successfully opened
482 ** and false otherwise.
485 int RetCode = 0; /* Return code. Assume an error. */
489 StrBuf NameBuf; /* No need to initialize */
490 StrBuf Path = AUTO_STRBUF_INITIALIZER;
495 /* If this is the main file, just try to open it. If it's an include file,
496 ** search for it using the include path list.
500 F = fopen (Name, "r");
502 Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
505 /* We are on include level. Search for the file in the include
508 PathName = SearchFile (IncSearchPath, Name);
509 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
510 /* Not found or cannot open, print an error and bail out */
511 Error ("Cannot open include file `%s': %s", Name, strerror (errno));
515 /* Use the path name from now on */
519 /* Stat the file and remember the values. There's a race condition here,
520 ** since we cannot use fileno() (non-standard identifier in standard
521 ** header file), and therefore not fstat. When using stat with the
522 ** file name, there's a risk that the file was deleted and recreated
523 ** while it was open. Since mtime and size are only used to check
524 ** if a file has changed in the debugger, we will ignore this problem
527 if (FileStat (Name, &Buf) != 0) {
528 Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno));
531 /* Add the file to the input file table and remember the index */
532 FileIdx = AddFile (SB_InitFromString (&NameBuf, Name),
533 (FCount == 0)? FT_MAIN : FT_INCLUDE,
534 Buf.st_size, (unsigned long) Buf.st_mtime);
536 /* Create a new input source variable and initialize it */
537 S = xmalloc (sizeof (*S));
540 S->V.File.Pos.Line = 0;
541 S->V.File.Pos.Col = 0;
542 S->V.File.Pos.Name = FileIdx;
543 SB_Init (&S->V.File.Line);
545 /* Push the path for this file onto the include search lists */
546 SB_CopyBuf (&Path, Name, FindName (Name) - Name);
547 SB_Terminate (&Path);
548 S->V.File.IncSearchPath = PushSearchPath (IncSearchPath, SB_GetConstBuf (&Path));
549 S->V.File.BinSearchPath = PushSearchPath (BinSearchPath, SB_GetConstBuf (&Path));
552 /* Count active input files */
555 /* Use this input source */
558 /* File successfully opened */
562 /* Free an allocated name buffer */
565 /* Return the success code */
571 /*****************************************************************************/
572 /* InputData functions */
573 /*****************************************************************************/
577 static void IDMarkStart (CharSource* S attribute ((unused)))
578 /* Mark the start of the next token */
580 /* Nothing to do here */
585 static void IDNextChar (CharSource* S)
586 /* Read the next character from the input text */
588 C = *S->V.Data.Pos++;
590 /* End of input data */
598 void IDDone (CharSource* S)
599 /* Close the current input data */
601 /* Cleanup the current stuff */
602 if (S->V.Data.Malloced) {
603 xfree (S->V.Data.Text);
609 /* Set of input data handling functions */
610 static const CharSourceFunctions IDFunc = {
618 void NewInputData (char* Text, int Malloced)
619 /* Add a chunk of input data to the input stream */
623 /* Create a new input source variable and initialize it */
624 S = xmalloc (sizeof (*S));
626 S->V.Data.Text = Text;
627 S->V.Data.Pos = Text;
628 S->V.Data.Malloced = Malloced;
630 /* Use this input source */
636 /*****************************************************************************/
637 /* Character classification functions */
638 /*****************************************************************************/
643 /* Return true if the character is a valid character for an identifier */
645 return IsAlNum (C) ||
647 (C == '@' && AtInIdents) ||
648 (C == '$' && DollarInIdents);
653 int IsIdStart (int C)
654 /* Return true if the character may start an identifier */
656 return IsAlpha (C) || C == '_';
661 /*****************************************************************************/
663 /*****************************************************************************/
667 static unsigned DigitVal (unsigned char C)
668 /* Convert a digit into it's numerical representation */
673 return toupper (C) - 'A' + 10;
679 static void NextChar (void)
680 /* Read the next character from the input file */
682 Source->Func->NextChar (Source);
687 void LocaseSVal (void)
688 /* Make SVal lower case */
690 SB_ToLower (&CurTok.SVal);
695 void UpcaseSVal (void)
696 /* Make SVal upper case */
698 SB_ToUpper (&CurTok.SVal);
703 static int CmpDotKeyword (const void* K1, const void* K2)
704 /* Compare function for the dot keyword search */
706 return strcmp (((struct DotKeyword*)K1)->Key, ((struct DotKeyword*)K2)->Key);
711 static token_t FindDotKeyword (void)
712 /* Find the dot keyword in SVal. Return the corresponding token if found,
713 ** return TOK_NONE if not found.
717 struct DotKeyword* R;
720 K.Key = SB_GetConstBuf (&CurTok.SVal);
723 /* If we aren't in ignore case mode, we have to uppercase the keyword */
728 /* Search for the keyword */
729 R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]),
730 sizeof (DotKeywords [0]), CmpDotKeyword);
733 /* By default, disable any somewhat experiemental DotKeyword. */
738 /* Disallow .ADDRSIZE function by default */
757 static void ReadIdent (void)
758 /* Read an identifier from the current input position into Ident. Filling SVal
759 ** starts at the current position with the next character in C. It is assumed
760 ** that any characters already filled in are ok, and the character in C is
764 /* Read the identifier */
766 SB_AppendChar (&CurTok.SVal, C);
768 } while (IsIdChar (C));
769 SB_Terminate (&CurTok.SVal);
771 /* If we should ignore case, convert the identifier to upper case */
779 static void ReadStringConst (int StringTerm)
780 /* Read a string constant into SVal. */
782 /* Skip the leading string terminator */
785 /* Read the string */
787 if (C == StringTerm) {
790 if (C == '\n' || C == EOF) {
791 Error ("Newline in string constant");
795 /* Append the char to the string */
796 SB_AppendChar (&CurTok.SVal, C);
798 /* Skip the character */
802 /* Skip the trailing terminator */
805 /* Terminate the string */
806 SB_Terminate (&CurTok.SVal);
811 static int Sweet16Reg (const StrBuf* Id)
812 /* Check if the given identifier is a sweet16 register. Return -1 if this is
813 ** not the case, return the register number otherwise.
819 if (SB_GetLen (Id) < 2) {
822 if (toupper (SB_AtUnchecked (Id, 0)) != 'R') {
825 if (!IsDigit (SB_AtUnchecked (Id, 1))) {
829 if (sscanf (SB_GetConstBuf (Id)+1, "%u%c", &RegNum, &Check) != 1 || RegNum > 15) {
830 /* Invalid register */
834 /* The register number is valid */
840 void NextRawTok (void)
841 /* Read the next raw token from the input stream */
845 /* If we've a forced end of assembly, don't read further */
847 CurTok.Tok = TOK_EOF;
852 /* Check if we have tokens from another input source */
853 if (InputFromStack ()) {
854 if (CurTok.Tok == TOK_IDENT && (M = FindDefine (&CurTok.SVal)) != 0) {
855 /* This is a define style macro - expand it */
863 /* Skip whitespace, remember if we had some */
864 if ((CurTok.WS = IsBlank (C)) != 0) {
867 } while (IsBlank (C));
870 /* Mark the file position of the next token */
871 Source->Func->MarkStart (Source);
873 /* Clear the string attribute */
874 SB_Clear (&CurTok.SVal);
876 /* Generate line info for the current token */
879 /* Hex number or PC symbol? */
883 /* Hex digit must follow or DollarIsPC must be enabled */
889 Error ("Hexadecimal digit expected");
893 /* Read the number */
896 if (UnderlineInNumbers && C == '_') {
901 Error ("Number may not end with underline");
905 if (CurTok.IVal & 0xF0000000) {
906 Error ("Overflow in hexadecimal number");
909 CurTok.IVal = (CurTok.IVal << 4) + DigitVal (C);
916 /* This is an integer constant */
917 CurTok.Tok = TOK_INTCON;
925 /* 0 or 1 must follow */
927 Error ("Binary digit expected");
930 /* Read the number */
933 if (UnderlineInNumbers && C == '_') {
938 Error ("Number may not end with underline");
942 if (CurTok.IVal & 0x80000000) {
943 Error ("Overflow in binary number");
946 CurTok.IVal = (CurTok.IVal << 1) + DigitVal (C);
953 /* This is an integer constant */
954 CurTok.Tok = TOK_INTCON;
968 /* Ignore leading zeros */
973 /* Read the number into Buf counting the digits */
976 if (UnderlineInNumbers && C == '_') {
981 Error ("Number may not end with underline");
985 /* Buf is big enough to allow any decimal and hex number to
986 ** overflow, so ignore excess digits here, they will be detected
987 ** when we convert the value.
989 if (Digits < sizeof (Buf)) {
998 /* Allow zilog/intel style hex numbers with a 'h' suffix */
999 if (C == 'h' || C == 'H') {
1002 Max = 0xFFFFFFFFUL / 16;
1005 Max = 0xFFFFFFFFUL / 10;
1008 /* Convert the number using the given base */
1010 for (I = 0; I < Digits; ++I) {
1011 if (CurTok.IVal > Max) {
1012 Error ("Number out of range");
1016 DVal = DigitVal (Buf[I]);
1018 Error ("Invalid digits in number");
1022 CurTok.IVal = (CurTok.IVal * Base) + DVal;
1025 /* This is an integer constant */
1026 CurTok.Tok = TOK_INTCON;
1030 /* Control command? */
1033 /* Remember and skip the dot */
1036 /* Check if it's just a dot */
1037 if (!IsIdStart (C)) {
1040 CurTok.Tok = TOK_DOT;
1044 /* Read the remainder of the identifier */
1045 SB_AppendChar (&CurTok.SVal, '.');
1048 /* Dot keyword, search for it */
1049 CurTok.Tok = FindDotKeyword ();
1050 if (CurTok.Tok == TOK_NONE) {
1053 if (!LeadingDotInIdents) {
1054 /* Invalid pseudo instruction */
1055 Error ("`%m%p' is not a recognized control command", &CurTok.SVal);
1059 /* An identifier with a dot. Check if it's a define style
1062 if ((M = FindDefine (&CurTok.SVal)) != 0) {
1063 /* This is a define style macro - expand it */
1068 /* Just an identifier with a dot */
1069 CurTok.Tok = TOK_IDENT;
1076 /* Indirect op for sweet16 cpu. Must check this before checking for local
1077 ** symbols, because these may also use the '@' symbol.
1079 if (CPU == CPU_SWEET16 && C == '@') {
1081 CurTok.Tok = TOK_AT;
1086 if (C == LocalStart) {
1088 /* Read the identifier. */
1091 /* Start character alone is not enough */
1092 if (SB_GetLen (&CurTok.SVal) == 1) {
1093 Error ("Invalid cheap local symbol");
1097 /* A local identifier */
1098 CurTok.Tok = TOK_LOCAL_IDENT;
1103 /* Identifier or keyword? */
1104 if (IsIdStart (C)) {
1106 /* Read the identifier */
1109 /* Check for special names. Bail out if we have identified the type of
1110 ** the token. Go on if the token is an identifier.
1112 switch (SB_GetLen (&CurTok.SVal)) {
1114 switch (toupper (SB_AtUnchecked (&CurTok.SVal, 0))) {
1119 CurTok.Tok = TOK_OVERRIDE_ABS;
1128 CurTok.Tok = TOK_OVERRIDE_FAR;
1134 if ((CPU == CPU_4510) || (CPU == CPU_65816)) {
1151 CurTok.Tok = TOK_OVERRIDE_ZP;
1154 if (CPU == CPU_4510) {
1166 if ((CPU == CPU_4510) &&
1167 (toupper (SB_AtUnchecked (&CurTok.SVal, 0)) == 'S') &&
1168 (toupper (SB_AtUnchecked (&CurTok.SVal, 1)) == 'P')) {
1175 if (CPU == CPU_SWEET16 &&
1176 (CurTok.IVal = Sweet16Reg (&CurTok.SVal)) >= 0) {
1178 /* A sweet16 register number in sweet16 mode */
1179 CurTok.Tok = TOK_REG;
1184 /* Check for define style macro */
1185 if ((M = FindDefine (&CurTok.SVal)) != 0) {
1186 /* Macro - expand it */
1191 CurTok.Tok = TOK_IDENT;
1196 /* Ok, let's do the switch */
1202 CurTok.Tok = TOK_PLUS;
1207 CurTok.Tok = TOK_MINUS;
1213 CurTok.Tok = TOK_DIV;
1214 } else if (CComments) {
1215 /* Remember the position, then skip the '*' */
1216 Collection LineInfos = STATIC_COLLECTION_INITIALIZER;
1217 GetFullLineInfo (&LineInfos);
1222 LIError (&LineInfos, "Unterminated comment");
1223 ReleaseFullLineInfo (&LineInfos);
1224 DoneCollection (&LineInfos);
1232 ReleaseFullLineInfo (&LineInfos);
1233 DoneCollection (&LineInfos);
1240 CurTok.Tok = TOK_MUL;
1245 CurTok.Tok = TOK_XOR;
1252 CurTok.Tok = TOK_BOOLAND;
1254 CurTok.Tok = TOK_AND;
1262 CurTok.Tok = TOK_BOOLOR;
1264 CurTok.Tok = TOK_OR;
1274 CurTok.Tok = TOK_NAMESPACE;
1283 CurTok.Tok = TOK_ULABEL;
1292 CurTok.Tok = TOK_ULABEL;
1297 CurTok.Tok = TOK_ASSIGN;
1301 CurTok.Tok = TOK_COLON;
1308 CurTok.Tok = TOK_COMMA;
1313 while (C != '\n' && C != EOF) {
1320 CurTok.Tok = TOK_HASH;
1325 CurTok.Tok = TOK_LPAREN;
1330 CurTok.Tok = TOK_RPAREN;
1335 CurTok.Tok = TOK_LBRACK;
1340 CurTok.Tok = TOK_RBRACK;
1345 CurTok.Tok = TOK_LCURLY;
1350 CurTok.Tok = TOK_RCURLY;
1357 CurTok.Tok = TOK_LE;
1358 } else if (C == '<') {
1360 CurTok.Tok = TOK_SHL;
1361 } else if (C == '>') {
1363 CurTok.Tok = TOK_NE;
1365 CurTok.Tok = TOK_LT;
1371 CurTok.Tok = TOK_EQ;
1376 CurTok.Tok = TOK_BOOLNOT;
1383 CurTok.Tok = TOK_GE;
1384 } else if (C == '>') {
1386 CurTok.Tok = TOK_SHR;
1388 CurTok.Tok = TOK_GT;
1394 CurTok.Tok = TOK_NOT;
1398 /* Hack: If we allow ' as terminating character for strings, read
1399 ** the following stuff as a string, and check for a one character
1402 if (LooseStringTerm) {
1403 ReadStringConst ('\'');
1404 if (SB_GetLen (&CurTok.SVal) == 1) {
1405 CurTok.IVal = SB_AtUnchecked (&CurTok.SVal, 0);
1406 CurTok.Tok = TOK_CHARCON;
1408 CurTok.Tok = TOK_STRCON;
1411 /* Always a character constant */
1413 if (C == EOF || IsControl (C)) {
1414 Error ("Illegal character constant");
1418 CurTok.Tok = TOK_CHARCON;
1421 if (!MissingCharTerm) {
1422 Error ("Illegal character constant");
1431 ReadStringConst ('\"');
1432 CurTok.Tok = TOK_STRCON;
1436 /* Line continuation? */
1439 /* Next char should be a LF, if not, will result in an error later */
1441 /* Ignore the '\n' */
1445 /* Make it clear what the problem is: */
1446 Error ("EOL expected.");
1453 CurTok.Tok = TOK_SEP;
1458 /* In case of the main file, do not close it, but return EOF. */
1459 if (Source && Source->Next) {
1463 CurTok.Tok = TOK_EOF;
1468 /* If we go here, we could not identify the current character. Skip it
1471 Error ("Invalid input character: 0x%02X", C & 0xFF);
1478 int GetSubKey (const char** Keys, unsigned Count)
1479 /* Search for a subkey in a table of keywords. The current token must be an
1480 ** identifier and all keys must be in upper case. The identifier will be
1481 ** uppercased in the process. The function returns the index of the keyword,
1482 ** or -1 if the keyword was not found.
1487 /* Must have an identifier */
1488 PRECONDITION (CurTok.Tok == TOK_IDENT);
1490 /* If we aren't in ignore case mode, we have to uppercase the identifier */
1495 /* Do a linear search (a binary search is not worth the effort) */
1496 for (I = 0; I < Count; ++I) {
1497 if (SB_CompareStr (&CurTok.SVal, Keys [I]) == 0) {
1509 unsigned char ParseAddrSize (void)
1510 /* Check if the next token is a keyword that denotes an address size specifier.
1511 ** If so, return the corresponding address size constant, otherwise output an
1512 ** error message and return ADDR_SIZE_DEFAULT.
1515 unsigned char AddrSize;
1517 /* Check for an identifier */
1518 if (CurTok.Tok != TOK_IDENT) {
1519 Error ("Address size specifier expected");
1520 return ADDR_SIZE_DEFAULT;
1523 /* Convert the attribute */
1524 AddrSize = AddrSizeFromStr (SB_GetConstBuf (&CurTok.SVal));
1525 if (AddrSize == ADDR_SIZE_INVALID) {
1526 Error ("Address size specifier expected");
1527 AddrSize = ADDR_SIZE_DEFAULT;
1536 void InitScanner (const char* InFile)
1537 /* Initialize the scanner, open the given input file */
1539 /* Open the input file */
1540 NewInputFile (InFile);
1545 void DoneScanner (void)
1546 /* Release scanner resources */