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 { ".ASSERT", TOK_ASSERT },
143 { ".AUTOIMPORT", TOK_AUTOIMPORT },
144 { ".BANK", TOK_BANK },
145 { ".BANKBYTE", TOK_BANKBYTE },
146 { ".BANKBYTES", TOK_BANKBYTES },
147 { ".BITAND", TOK_AND },
148 { ".BITNOT", TOK_NOT },
149 { ".BITOR", TOK_OR },
150 { ".BITXOR", TOK_XOR },
151 { ".BLANK", TOK_BLANK },
153 { ".BYT", TOK_BYTE },
154 { ".BYTE", TOK_BYTE },
155 { ".CASE", TOK_CASE },
156 { ".CHARMAP", TOK_CHARMAP },
157 { ".CODE", TOK_CODE },
158 { ".CONCAT", TOK_CONCAT },
159 { ".CONDES", TOK_CONDES },
160 { ".CONST", TOK_CONST },
161 { ".CONSTRUCTOR", TOK_CONSTRUCTOR },
163 { ".DATA", TOK_DATA },
165 { ".DBYT", TOK_DBYT },
166 { ".DEBUGINFO", TOK_DEBUGINFO },
167 { ".DEF", TOK_DEFINED },
168 { ".DEFINE", TOK_DEFINE },
169 { ".DEFINED", TOK_DEFINED },
170 { ".DEFINEDMACRO", TOK_DEFINEDMACRO },
171 { ".DELMAC", TOK_DELMAC },
172 { ".DELMACRO", TOK_DELMAC },
173 { ".DESTRUCTOR", TOK_DESTRUCTOR },
174 { ".DWORD", TOK_DWORD },
175 { ".ELSE", TOK_ELSE },
176 { ".ELSEIF", TOK_ELSEIF },
178 { ".ENDENUM", TOK_ENDENUM },
179 { ".ENDIF", TOK_ENDIF },
180 { ".ENDMAC", TOK_ENDMACRO },
181 { ".ENDMACRO", TOK_ENDMACRO },
182 { ".ENDPROC", TOK_ENDPROC },
183 { ".ENDREP", TOK_ENDREP },
184 { ".ENDREPEAT", TOK_ENDREP },
185 { ".ENDSCOPE", TOK_ENDSCOPE },
186 { ".ENDSTRUCT", TOK_ENDSTRUCT },
187 { ".ENDUNION", TOK_ENDUNION },
188 { ".ENUM", TOK_ENUM },
189 { ".ERROR", TOK_ERROR },
190 { ".EXITMAC", TOK_EXITMACRO },
191 { ".EXITMACRO", TOK_EXITMACRO },
192 { ".EXPORT", TOK_EXPORT },
193 { ".EXPORTZP", TOK_EXPORTZP },
194 { ".FARADDR", TOK_FARADDR },
195 { ".FATAL", TOK_FATAL },
196 { ".FEATURE", TOK_FEATURE },
197 { ".FILEOPT", TOK_FILEOPT },
198 { ".FOPT", TOK_FILEOPT },
199 { ".FORCEIMPORT", TOK_FORCEIMPORT },
200 { ".FORCEWORD", TOK_FORCEWORD },
201 { ".GLOBAL", TOK_GLOBAL },
202 { ".GLOBALZP", TOK_GLOBALZP },
203 { ".HIBYTE", TOK_HIBYTE },
204 { ".HIBYTES", TOK_HIBYTES },
205 { ".HIWORD", TOK_HIWORD },
208 { ".IDENT", TOK_MAKEIDENT },
210 { ".IFBLANK", TOK_IFBLANK },
211 { ".IFCONST", TOK_IFCONST },
212 { ".IFDEF", TOK_IFDEF },
213 { ".IFNBLANK", TOK_IFNBLANK },
214 { ".IFNCONST", TOK_IFNCONST },
215 { ".IFNDEF", TOK_IFNDEF },
216 { ".IFNREF", TOK_IFNREF },
217 { ".IFP02", TOK_IFP02 },
218 { ".IFP816", TOK_IFP816 },
219 { ".IFPC02", TOK_IFPC02 },
220 { ".IFPSC02", TOK_IFPSC02 },
221 { ".IFREF", TOK_IFREF },
222 { ".IMPORT", TOK_IMPORT },
223 { ".IMPORTZP", TOK_IMPORTZP },
224 { ".INCBIN", TOK_INCBIN },
225 { ".INCLUDE", TOK_INCLUDE },
226 { ".INTERRUPTOR", TOK_INTERRUPTOR },
227 { ".ISMNEM", TOK_ISMNEMONIC },
228 { ".ISMNEMONIC", TOK_ISMNEMONIC },
229 { ".LEFT", TOK_LEFT },
230 { ".LINECONT", TOK_LINECONT },
231 { ".LIST", TOK_LIST },
232 { ".LISTBYTES", TOK_LISTBYTES },
233 { ".LOBYTE", TOK_LOBYTE },
234 { ".LOBYTES", TOK_LOBYTES },
235 { ".LOCAL", TOK_LOCAL },
236 { ".LOCALCHAR", TOK_LOCALCHAR },
237 { ".LOWORD", TOK_LOWORD },
238 { ".MAC", TOK_MACRO },
239 { ".MACPACK", TOK_MACPACK },
240 { ".MACRO", TOK_MACRO },
241 { ".MATCH", TOK_MATCH },
246 { ".NOT", TOK_BOOLNOT },
247 { ".NULL", TOK_NULL },
248 { ".OR", TOK_BOOLOR },
252 { ".P816", TOK_P816 },
253 { ".PAGELEN", TOK_PAGELENGTH },
254 { ".PAGELENGTH", TOK_PAGELENGTH },
255 { ".PARAMCOUNT", TOK_PARAMCOUNT },
256 { ".PC02", TOK_PC02 },
257 { ".POPCPU", TOK_POPCPU },
258 { ".POPSEG", TOK_POPSEG },
259 { ".PROC", TOK_PROC },
260 { ".PSC02", TOK_PSC02 },
261 { ".PUSHCPU", TOK_PUSHCPU },
262 { ".PUSHSEG", TOK_PUSHSEG },
263 { ".REF", TOK_REFERENCED },
264 { ".REFERENCED", TOK_REFERENCED },
265 { ".RELOC", TOK_RELOC },
266 { ".REPEAT", TOK_REPEAT },
268 { ".RIGHT", TOK_RIGHT },
269 { ".RODATA", TOK_RODATA },
270 { ".SCOPE", TOK_SCOPE },
271 { ".SEGMENT", TOK_SEGMENT },
273 { ".SETCPU", TOK_SETCPU },
276 { ".SIZEOF", TOK_SIZEOF },
277 { ".SMART", TOK_SMART },
278 { ".SPRINTF", TOK_SPRINTF },
279 { ".STRAT", TOK_STRAT },
280 { ".STRING", TOK_STRING },
281 { ".STRLEN", TOK_STRLEN },
282 { ".STRUCT", TOK_STRUCT },
284 { ".TCOUNT", TOK_TCOUNT },
285 { ".TIME", TOK_TIME },
286 { ".UNDEF", TOK_UNDEF },
287 { ".UNDEFINE", TOK_UNDEF },
288 { ".UNION", TOK_UNION },
289 { ".VERSION", TOK_VERSION },
290 { ".WARNING", TOK_WARNING },
291 { ".WORD", TOK_WORD },
292 { ".XMATCH", TOK_XMATCH },
293 { ".XOR", TOK_BOOLXOR },
294 { ".ZEROPAGE", TOK_ZEROPAGE },
299 /*****************************************************************************/
300 /* CharSource functions */
301 /*****************************************************************************/
305 static void UseCharSource (CharSource* S)
306 /* Initialize a new input source and start to use it. */
308 /* Remember the current input char and token */
312 /* Use the new input source */
316 /* Read the first character from the new file */
317 S->Func->NextChar (S);
319 /* Setup the next token so it will be skipped on the next call to
322 CurTok.Tok = TOK_SEP;
327 static void DoneCharSource (void)
328 /* Close the top level character source */
332 /* First, call the type specific function */
333 Source->Func->Done (Source);
335 /* Restore the old token */
336 CurTok.Tok = Source->Tok;
339 /* Remember the last stacked input source */
342 /* Delete the top level one ... */
345 /* ... and use the one before */
351 /*****************************************************************************/
352 /* InputFile functions */
353 /*****************************************************************************/
357 static void IFMarkStart (CharSource* S)
358 /* Mark the start of the next token */
360 CurTok.Pos = S->V.File.Pos;
365 static void IFNextChar (CharSource* S)
366 /* Read the next character from the input file */
368 /* Check for end of line, read the next line if needed */
369 while (SB_GetIndex (&S->V.File.Line) >= SB_GetLen (&S->V.File.Line)) {
373 /* End of current line reached, read next line */
374 SB_Clear (&S->V.File.Line);
377 int N = fgetc (S->V.File.F);
379 /* End of file. Accept files without a newline at the end */
380 if (SB_NotEmpty (&S->V.File.Line)) {
384 /* No more data - add an empty line to the listing. This
385 ** is a small hack needed to keep the PC output in sync.
387 NewListingLine (&EmptyStrBuf, S->V.File.Pos.Name, FCount);
391 /* Check for end of line */
392 } else if (N == '\n') {
397 /* Collect other stuff */
400 /* Append data to line */
401 SB_AppendChar (&S->V.File.Line, N);
407 /* If we come here, we have a new input line. To avoid problems
408 ** with strange line terminators, remove all whitespace from the
409 ** end of the line, the add a single newline.
411 Len = SB_GetLen (&S->V.File.Line);
412 while (Len > 0 && IsSpace (SB_AtUnchecked (&S->V.File.Line, Len-1))) {
415 SB_Drop (&S->V.File.Line, SB_GetLen (&S->V.File.Line) - Len);
416 SB_AppendChar (&S->V.File.Line, '\n');
418 /* Terminate the string buffer */
419 SB_Terminate (&S->V.File.Line);
422 S->V.File.Pos.Line++;
424 /* Remember the new line for the listing */
425 NewListingLine (&S->V.File.Line, S->V.File.Pos.Name, FCount);
429 /* Set the column pointer */
430 S->V.File.Pos.Col = SB_GetIndex (&S->V.File.Line);
432 /* Return the next character from the buffer */
433 C = SB_Get (&S->V.File.Line);
438 void IFDone (CharSource* S)
439 /* Close the current input file */
441 /* We're at the end of an include file. Check if we have any
442 ** open .IFs, or any open token lists in this file. This
443 ** enforcement is artificial, using conditionals that start
444 ** in one file and end in another are uncommon, and don't
445 ** allowing these things will help finding errors.
449 /* If we've added search paths for this file, remove them */
450 if (S->V.File.IncSearchPath) {
451 PopSearchPath (IncSearchPath);
453 if (S->V.File.BinSearchPath) {
454 PopSearchPath (BinSearchPath);
457 /* Free the line buffer */
458 SB_Done (&S->V.File.Line);
460 /* Close the input file and decrement the file count. We will ignore
461 ** errors here, since we were just reading from the file.
463 (void) fclose (S->V.File.F);
469 /* Set of input file handling functions */
470 static const CharSourceFunctions IFFunc = {
478 int NewInputFile (const char* Name)
479 /* Open a new input file. Returns true if the file could be successfully opened
480 ** and false otherwise.
483 int RetCode = 0; /* Return code. Assume an error. */
487 StrBuf NameBuf; /* No need to initialize */
488 StrBuf Path = AUTO_STRBUF_INITIALIZER;
493 /* If this is the main file, just try to open it. If it's an include file,
494 ** search for it using the include path list.
498 F = fopen (Name, "r");
500 Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
503 /* We are on include level. Search for the file in the include
506 PathName = SearchFile (IncSearchPath, Name);
507 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
508 /* Not found or cannot open, print an error and bail out */
509 Error ("Cannot open include file `%s': %s", Name, strerror (errno));
513 /* Use the path name from now on */
517 /* Stat the file and remember the values. There's a race condition here,
518 ** since we cannot use fileno() (non-standard identifier in standard
519 ** header file), and therefore not fstat. When using stat with the
520 ** file name, there's a risk that the file was deleted and recreated
521 ** while it was open. Since mtime and size are only used to check
522 ** if a file has changed in the debugger, we will ignore this problem
525 if (FileStat (Name, &Buf) != 0) {
526 Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno));
529 /* Add the file to the input file table and remember the index */
530 FileIdx = AddFile (SB_InitFromString (&NameBuf, Name),
531 (FCount == 0)? FT_MAIN : FT_INCLUDE,
532 Buf.st_size, (unsigned long) Buf.st_mtime);
534 /* Create a new input source variable and initialize it */
535 S = xmalloc (sizeof (*S));
538 S->V.File.Pos.Line = 0;
539 S->V.File.Pos.Col = 0;
540 S->V.File.Pos.Name = FileIdx;
541 SB_Init (&S->V.File.Line);
543 /* Push the path for this file onto the include search lists */
544 SB_CopyBuf (&Path, Name, FindName (Name) - Name);
545 SB_Terminate (&Path);
546 S->V.File.IncSearchPath = PushSearchPath (IncSearchPath, SB_GetConstBuf (&Path));
547 S->V.File.BinSearchPath = PushSearchPath (BinSearchPath, SB_GetConstBuf (&Path));
550 /* Count active input files */
553 /* Use this input source */
556 /* File successfully opened */
560 /* Free an allocated name buffer */
563 /* Return the success code */
569 /*****************************************************************************/
570 /* InputData functions */
571 /*****************************************************************************/
575 static void IDMarkStart (CharSource* S attribute ((unused)))
576 /* Mark the start of the next token */
578 /* Nothing to do here */
583 static void IDNextChar (CharSource* S)
584 /* Read the next character from the input text */
586 C = *S->V.Data.Pos++;
588 /* End of input data */
596 void IDDone (CharSource* S)
597 /* Close the current input data */
599 /* Cleanup the current stuff */
600 if (S->V.Data.Malloced) {
601 xfree (S->V.Data.Text);
607 /* Set of input data handling functions */
608 static const CharSourceFunctions IDFunc = {
616 void NewInputData (char* Text, int Malloced)
617 /* Add a chunk of input data to the input stream */
621 /* Create a new input source variable and initialize it */
622 S = xmalloc (sizeof (*S));
624 S->V.Data.Text = Text;
625 S->V.Data.Pos = Text;
626 S->V.Data.Malloced = Malloced;
628 /* Use this input source */
634 /*****************************************************************************/
635 /* Character classification functions */
636 /*****************************************************************************/
641 /* Return true if the character is a valid character for an identifier */
643 return IsAlNum (C) ||
645 (C == '@' && AtInIdents) ||
646 (C == '$' && DollarInIdents);
651 int IsIdStart (int C)
652 /* Return true if the character may start an identifier */
654 return IsAlpha (C) || C == '_';
659 /*****************************************************************************/
661 /*****************************************************************************/
665 static unsigned DigitVal (unsigned char C)
666 /* Convert a digit into it's numerical representation */
671 return toupper (C) - 'A' + 10;
677 static void NextChar (void)
678 /* Read the next character from the input file */
680 Source->Func->NextChar (Source);
685 void LocaseSVal (void)
686 /* Make SVal lower case */
688 SB_ToLower (&CurTok.SVal);
693 void UpcaseSVal (void)
694 /* Make SVal upper case */
696 SB_ToUpper (&CurTok.SVal);
701 static int CmpDotKeyword (const void* K1, const void* K2)
702 /* Compare function for the dot keyword search */
704 return strcmp (((struct DotKeyword*)K1)->Key, ((struct DotKeyword*)K2)->Key);
709 static token_t FindDotKeyword (void)
710 /* Find the dot keyword in SVal. Return the corresponding token if found,
711 ** return TOK_NONE if not found.
715 struct DotKeyword* R;
718 K.Key = SB_GetConstBuf (&CurTok.SVal);
721 /* If we aren't in ignore case mode, we have to uppercase the keyword */
726 /* Search for the keyword */
727 R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]),
728 sizeof (DotKeywords [0]), CmpDotKeyword);
731 /* By default, disable any somewhat experiemental DotKeyword. */
736 /* Disallow .ADDRSIZE function by default */
755 static void ReadIdent (void)
756 /* Read an identifier from the current input position into Ident. Filling SVal
757 ** starts at the current position with the next character in C. It is assumed
758 ** that any characters already filled in are ok, and the character in C is
762 /* Read the identifier */
764 SB_AppendChar (&CurTok.SVal, C);
766 } while (IsIdChar (C));
767 SB_Terminate (&CurTok.SVal);
769 /* If we should ignore case, convert the identifier to upper case */
777 static void ReadStringConst (int StringTerm)
778 /* Read a string constant into SVal. */
780 /* Skip the leading string terminator */
783 /* Read the string */
785 if (C == StringTerm) {
788 if (C == '\n' || C == EOF) {
789 Error ("Newline in string constant");
793 /* Append the char to the string */
794 SB_AppendChar (&CurTok.SVal, C);
796 /* Skip the character */
800 /* Skip the trailing terminator */
803 /* Terminate the string */
804 SB_Terminate (&CurTok.SVal);
809 static int Sweet16Reg (const StrBuf* Id)
810 /* Check if the given identifier is a sweet16 register. Return -1 if this is
811 ** not the case, return the register number otherwise.
817 if (SB_GetLen (Id) < 2) {
820 if (toupper (SB_AtUnchecked (Id, 0)) != 'R') {
823 if (!IsDigit (SB_AtUnchecked (Id, 1))) {
827 if (sscanf (SB_GetConstBuf (Id)+1, "%u%c", &RegNum, &Check) != 1 || RegNum > 15) {
828 /* Invalid register */
832 /* The register number is valid */
838 void NextRawTok (void)
839 /* Read the next raw token from the input stream */
843 /* If we've a forced end of assembly, don't read further */
845 CurTok.Tok = TOK_EOF;
850 /* Check if we have tokens from another input source */
851 if (InputFromStack ()) {
852 if (CurTok.Tok == TOK_IDENT && (M = FindDefine (&CurTok.SVal)) != 0) {
853 /* This is a define style macro - expand it */
861 /* Skip whitespace, remember if we had some */
862 if ((CurTok.WS = IsBlank (C)) != 0) {
865 } while (IsBlank (C));
868 /* Mark the file position of the next token */
869 Source->Func->MarkStart (Source);
871 /* Clear the string attribute */
872 SB_Clear (&CurTok.SVal);
874 /* Generate line info for the current token */
877 /* Hex number or PC symbol? */
881 /* Hex digit must follow or DollarIsPC must be enabled */
887 Error ("Hexadecimal digit expected");
891 /* Read the number */
894 if (UnderlineInNumbers && C == '_') {
899 Error ("Number may not end with underline");
903 if (CurTok.IVal & 0xF0000000) {
904 Error ("Overflow in hexadecimal number");
907 CurTok.IVal = (CurTok.IVal << 4) + DigitVal (C);
914 /* This is an integer constant */
915 CurTok.Tok = TOK_INTCON;
923 /* 0 or 1 must follow */
925 Error ("Binary digit expected");
928 /* Read the number */
931 if (UnderlineInNumbers && C == '_') {
936 Error ("Number may not end with underline");
940 if (CurTok.IVal & 0x80000000) {
941 Error ("Overflow in binary number");
944 CurTok.IVal = (CurTok.IVal << 1) + DigitVal (C);
951 /* This is an integer constant */
952 CurTok.Tok = TOK_INTCON;
966 /* Ignore leading zeros */
971 /* Read the number into Buf counting the digits */
974 if (UnderlineInNumbers && C == '_') {
979 Error ("Number may not end with underline");
983 /* Buf is big enough to allow any decimal and hex number to
984 ** overflow, so ignore excess digits here, they will be detected
985 ** when we convert the value.
987 if (Digits < sizeof (Buf)) {
996 /* Allow zilog/intel style hex numbers with a 'h' suffix */
997 if (C == 'h' || C == 'H') {
1000 Max = 0xFFFFFFFFUL / 16;
1003 Max = 0xFFFFFFFFUL / 10;
1006 /* Convert the number using the given base */
1008 for (I = 0; I < Digits; ++I) {
1009 if (CurTok.IVal > Max) {
1010 Error ("Number out of range");
1014 DVal = DigitVal (Buf[I]);
1016 Error ("Invalid digits in number");
1020 CurTok.IVal = (CurTok.IVal * Base) + DVal;
1023 /* This is an integer constant */
1024 CurTok.Tok = TOK_INTCON;
1028 /* Control command? */
1031 /* Remember and skip the dot */
1034 /* Check if it's just a dot */
1035 if (!IsIdStart (C)) {
1038 CurTok.Tok = TOK_DOT;
1042 /* Read the remainder of the identifier */
1043 SB_AppendChar (&CurTok.SVal, '.');
1046 /* Dot keyword, search for it */
1047 CurTok.Tok = FindDotKeyword ();
1048 if (CurTok.Tok == TOK_NONE) {
1051 if (!LeadingDotInIdents) {
1052 /* Invalid pseudo instruction */
1053 Error ("`%m%p' is not a recognized control command", &CurTok.SVal);
1057 /* An identifier with a dot. Check if it's a define style
1060 if ((M = FindDefine (&CurTok.SVal)) != 0) {
1061 /* This is a define style macro - expand it */
1066 /* Just an identifier with a dot */
1067 CurTok.Tok = TOK_IDENT;
1074 /* Indirect op for sweet16 cpu. Must check this before checking for local
1075 ** symbols, because these may also use the '@' symbol.
1077 if (CPU == CPU_SWEET16 && C == '@') {
1079 CurTok.Tok = TOK_AT;
1084 if (C == LocalStart) {
1086 /* Read the identifier. */
1089 /* Start character alone is not enough */
1090 if (SB_GetLen (&CurTok.SVal) == 1) {
1091 Error ("Invalid cheap local symbol");
1095 /* A local identifier */
1096 CurTok.Tok = TOK_LOCAL_IDENT;
1101 /* Identifier or keyword? */
1102 if (IsIdStart (C)) {
1104 /* Read the identifier */
1107 /* Check for special names. Bail out if we have identified the type of
1108 ** the token. Go on if the token is an identifier.
1110 if (SB_GetLen (&CurTok.SVal) == 1) {
1111 switch (toupper (SB_AtUnchecked (&CurTok.SVal, 0))) {
1116 CurTok.Tok = TOK_OVERRIDE_ABS;
1125 CurTok.Tok = TOK_OVERRIDE_FAR;
1131 if (CPU == CPU_65816) {
1148 CurTok.Tok = TOK_OVERRIDE_ZP;
1157 } else if (CPU == CPU_SWEET16 &&
1158 (CurTok.IVal = Sweet16Reg (&CurTok.SVal)) >= 0) {
1160 /* A sweet16 register number in sweet16 mode */
1161 CurTok.Tok = TOK_REG;
1166 /* Check for define style macro */
1167 if ((M = FindDefine (&CurTok.SVal)) != 0) {
1168 /* Macro - expand it */
1173 CurTok.Tok = TOK_IDENT;
1178 /* Ok, let's do the switch */
1184 CurTok.Tok = TOK_PLUS;
1189 CurTok.Tok = TOK_MINUS;
1195 CurTok.Tok = TOK_DIV;
1196 } else if (CComments) {
1197 /* Remember the position, then skip the '*' */
1198 Collection LineInfos = STATIC_COLLECTION_INITIALIZER;
1199 GetFullLineInfo (&LineInfos);
1204 LIError (&LineInfos, "Unterminated comment");
1205 ReleaseFullLineInfo (&LineInfos);
1206 DoneCollection (&LineInfos);
1214 ReleaseFullLineInfo (&LineInfos);
1215 DoneCollection (&LineInfos);
1222 CurTok.Tok = TOK_MUL;
1227 CurTok.Tok = TOK_XOR;
1234 CurTok.Tok = TOK_BOOLAND;
1236 CurTok.Tok = TOK_AND;
1244 CurTok.Tok = TOK_BOOLOR;
1246 CurTok.Tok = TOK_OR;
1256 CurTok.Tok = TOK_NAMESPACE;
1265 CurTok.Tok = TOK_ULABEL;
1274 CurTok.Tok = TOK_ULABEL;
1279 CurTok.Tok = TOK_ASSIGN;
1283 CurTok.Tok = TOK_COLON;
1290 CurTok.Tok = TOK_COMMA;
1295 while (C != '\n' && C != EOF) {
1302 CurTok.Tok = TOK_HASH;
1307 CurTok.Tok = TOK_LPAREN;
1312 CurTok.Tok = TOK_RPAREN;
1317 CurTok.Tok = TOK_LBRACK;
1322 CurTok.Tok = TOK_RBRACK;
1327 CurTok.Tok = TOK_LCURLY;
1332 CurTok.Tok = TOK_RCURLY;
1339 CurTok.Tok = TOK_LE;
1340 } else if (C == '<') {
1342 CurTok.Tok = TOK_SHL;
1343 } else if (C == '>') {
1345 CurTok.Tok = TOK_NE;
1347 CurTok.Tok = TOK_LT;
1353 CurTok.Tok = TOK_EQ;
1358 CurTok.Tok = TOK_BOOLNOT;
1365 CurTok.Tok = TOK_GE;
1366 } else if (C == '>') {
1368 CurTok.Tok = TOK_SHR;
1370 CurTok.Tok = TOK_GT;
1376 CurTok.Tok = TOK_NOT;
1380 /* Hack: If we allow ' as terminating character for strings, read
1381 ** the following stuff as a string, and check for a one character
1384 if (LooseStringTerm) {
1385 ReadStringConst ('\'');
1386 if (SB_GetLen (&CurTok.SVal) == 1) {
1387 CurTok.IVal = SB_AtUnchecked (&CurTok.SVal, 0);
1388 CurTok.Tok = TOK_CHARCON;
1390 CurTok.Tok = TOK_STRCON;
1393 /* Always a character constant */
1395 if (C == EOF || IsControl (C)) {
1396 Error ("Illegal character constant");
1400 CurTok.Tok = TOK_CHARCON;
1403 if (!MissingCharTerm) {
1404 Error ("Illegal character constant");
1413 ReadStringConst ('\"');
1414 CurTok.Tok = TOK_STRCON;
1418 /* Line continuation? */
1421 /* Next char should be a LF, if not, will result in an error later */
1423 /* Ignore the '\n' */
1428 /* Make it clear what the problem is: */
1429 Error ("EOL expected.");
1436 CurTok.Tok = TOK_SEP;
1441 /* In case of the main file, do not close it, but return EOF. */
1442 if (Source && Source->Next) {
1446 CurTok.Tok = TOK_EOF;
1451 /* If we go here, we could not identify the current character. Skip it
1454 Error ("Invalid input character: 0x%02X", C & 0xFF);
1461 int GetSubKey (const char** Keys, unsigned Count)
1462 /* Search for a subkey in a table of keywords. The current token must be an
1463 ** identifier and all keys must be in upper case. The identifier will be
1464 ** uppercased in the process. The function returns the index of the keyword,
1465 ** or -1 if the keyword was not found.
1470 /* Must have an identifier */
1471 PRECONDITION (CurTok.Tok == TOK_IDENT);
1473 /* If we aren't in ignore case mode, we have to uppercase the identifier */
1478 /* Do a linear search (a binary search is not worth the effort) */
1479 for (I = 0; I < Count; ++I) {
1480 if (SB_CompareStr (&CurTok.SVal, Keys [I]) == 0) {
1492 unsigned char ParseAddrSize (void)
1493 /* Check if the next token is a keyword that denotes an address size specifier.
1494 ** If so, return the corresponding address size constant, otherwise output an
1495 ** error message and return ADDR_SIZE_DEFAULT.
1498 unsigned char AddrSize;
1500 /* Check for an identifier */
1501 if (CurTok.Tok != TOK_IDENT) {
1502 Error ("Address size specifier expected");
1503 return ADDR_SIZE_DEFAULT;
1506 /* Convert the attribute */
1507 AddrSize = AddrSizeFromStr (SB_GetConstBuf (&CurTok.SVal));
1508 if (AddrSize == ADDR_SIZE_INVALID) {
1509 Error ("Address size specifier expected");
1510 AddrSize = ADDR_SIZE_DEFAULT;
1519 void InitScanner (const char* InFile)
1520 /* Initialize the scanner, open the given input file */
1522 /* Open the input file */
1523 NewInputFile (InFile);
1528 void DoneScanner (void)
1529 /* Release scanner resources */