]> git.sur5r.net Git - cc65/blob - src/cc65/scanner.c
2f5bfecb09216c0e23c7b6074cc818d8f54889a3
[cc65] / src / cc65 / scanner.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.c                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2008 Ullrich von Bassewitz                                       */
10 /*               Roemerstrasse 52                                            */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <ctype.h>
41 #include <math.h>
42
43 /* common */
44 #include "chartype.h"
45 #include "fp.h"
46 #include "tgttrans.h"
47
48 /* cc65 */
49 #include "datatype.h"
50 #include "error.h"
51 #include "function.h"
52 #include "global.h"
53 #include "hexval.h"
54 #include "ident.h"
55 #include "input.h"
56 #include "litpool.h"
57 #include "preproc.h"
58 #include "scanner.h"
59 #include "standard.h"
60 #include "symtab.h"
61
62
63
64 /*****************************************************************************/
65 /*                                   data                                    */
66 /*****************************************************************************/
67
68
69
70 Token CurTok;           /* The current token */
71 Token NextTok;          /* The next token */
72
73
74
75 /* Token types */
76 enum {
77     TT_C89      = 0x01 << STD_C89,      /* Token valid in C89 */
78     TT_C99      = 0x01 << STD_C99,      /* Token valid in C99 */
79     TT_CC65     = 0x01 << STD_CC65      /* Token valid in cc65 */
80 };
81
82 /* Token table */
83 static const struct Keyword {
84     char*           Key;        /* Keyword name */
85     unsigned char   Tok;        /* The token */
86     unsigned char   Std;        /* Token supported in which standards? */
87 } Keywords [] = {
88     { "_Pragma",        TOK_PRAGMA,     TT_C89 | TT_C99 | TT_CC65  },   /* !! */
89     { "__AX__",         TOK_AX,         TT_C89 | TT_C99 | TT_CC65  },
90     { "__A__",          TOK_A,          TT_C89 | TT_C99 | TT_CC65  },
91     { "__EAX__",        TOK_EAX,        TT_C89 | TT_C99 | TT_CC65  },
92     { "__X__",          TOK_X,          TT_C89 | TT_C99 | TT_CC65  },
93     { "__Y__",          TOK_Y,          TT_C89 | TT_C99 | TT_CC65  },
94     { "__asm__",        TOK_ASM,        TT_C89 | TT_C99 | TT_CC65  },
95     { "__attribute__",  TOK_ATTRIBUTE,  TT_C89 | TT_C99 | TT_CC65  },
96     { "__far__",        TOK_FAR,        TT_C89 | TT_C99 | TT_CC65  },
97     { "__fastcall__",   TOK_FASTCALL,   TT_C89 | TT_C99 | TT_CC65  },
98     { "__inline__",     TOK_INLINE,     TT_C89 | TT_C99 | TT_CC65  },
99     { "__near__",       TOK_NEAR,       TT_C89 | TT_C99 | TT_CC65  },
100     { "asm",            TOK_ASM,                          TT_CC65  },
101     { "auto",           TOK_AUTO,       TT_C89 | TT_C99 | TT_CC65  },
102     { "break",          TOK_BREAK,      TT_C89 | TT_C99 | TT_CC65  },
103     { "case",           TOK_CASE,       TT_C89 | TT_C99 | TT_CC65  },
104     { "char",           TOK_CHAR,       TT_C89 | TT_C99 | TT_CC65  },
105     { "const",          TOK_CONST,      TT_C89 | TT_C99 | TT_CC65  },
106     { "continue",       TOK_CONTINUE,   TT_C89 | TT_C99 | TT_CC65  },
107     { "default",        TOK_DEFAULT,    TT_C89 | TT_C99 | TT_CC65  },
108     { "do",             TOK_DO,         TT_C89 | TT_C99 | TT_CC65  },
109     { "double",         TOK_DOUBLE,     TT_C89 | TT_C99 | TT_CC65  },
110     { "else",           TOK_ELSE,       TT_C89 | TT_C99 | TT_CC65  },
111     { "enum",           TOK_ENUM,       TT_C89 | TT_C99 | TT_CC65  },
112     { "extern",         TOK_EXTERN,     TT_C89 | TT_C99 | TT_CC65  },
113     { "far",            TOK_FAR,                          TT_CC65  },
114     { "fastcall",       TOK_FASTCALL,                     TT_CC65  },
115     { "float",          TOK_FLOAT,      TT_C89 | TT_C99 | TT_CC65  },
116     { "for",            TOK_FOR,        TT_C89 | TT_C99 | TT_CC65  },
117     { "goto",           TOK_GOTO,       TT_C89 | TT_C99 | TT_CC65  },
118     { "if",             TOK_IF,         TT_C89 | TT_C99 | TT_CC65  },
119     { "inline",         TOK_INLINE,              TT_C99 | TT_CC65  },
120     { "int",            TOK_INT,        TT_C89 | TT_C99 | TT_CC65  },
121     { "long",           TOK_LONG,       TT_C89 | TT_C99 | TT_CC65  },
122     { "near",           TOK_NEAR,                         TT_CC65  },
123     { "register",       TOK_REGISTER,   TT_C89 | TT_C99 | TT_CC65  },
124     { "restrict",       TOK_RESTRICT,            TT_C99 | TT_CC65  },
125     { "return",         TOK_RETURN,     TT_C89 | TT_C99 | TT_CC65  },
126     { "short",          TOK_SHORT,      TT_C89 | TT_C99 | TT_CC65  },
127     { "signed",         TOK_SIGNED,     TT_C89 | TT_C99 | TT_CC65  },
128     { "sizeof",         TOK_SIZEOF,     TT_C89 | TT_C99 | TT_CC65  },
129     { "static",         TOK_STATIC,     TT_C89 | TT_C99 | TT_CC65  },
130     { "struct",         TOK_STRUCT,     TT_C89 | TT_C99 | TT_CC65  },
131     { "switch",         TOK_SWITCH,     TT_C89 | TT_C99 | TT_CC65  },
132     { "typedef",        TOK_TYPEDEF,    TT_C89 | TT_C99 | TT_CC65  },
133     { "union",          TOK_UNION,      TT_C89 | TT_C99 | TT_CC65  },
134     { "unsigned",       TOK_UNSIGNED,   TT_C89 | TT_C99 | TT_CC65  },
135     { "void",           TOK_VOID,       TT_C89 | TT_C99 | TT_CC65  },
136     { "volatile",       TOK_VOLATILE,   TT_C89 | TT_C99 | TT_CC65  },
137     { "while",          TOK_WHILE,      TT_C89 | TT_C99 | TT_CC65  },
138 };
139 #define KEY_COUNT       (sizeof (Keywords) / sizeof (Keywords [0]))
140
141
142
143 /* Stuff for determining the type of an integer constant */
144 #define IT_INT          0x01
145 #define IT_UINT         0x02
146 #define IT_LONG         0x04
147 #define IT_ULONG        0x08
148
149
150
151 /*****************************************************************************/
152 /*                                   code                                    */
153 /*****************************************************************************/
154
155
156
157 static int CmpKey (const void* Key, const void* Elem)
158 /* Compare function for bsearch */
159 {
160     return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
161 }
162
163
164
165 static token_t FindKey (const char* Key)
166 /* Find a keyword and return the token. Return IDENT if the token is not a
167  * keyword.
168  */
169 {
170     struct Keyword* K;
171     K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
172     if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
173         return K->Tok;
174     } else {
175         return TOK_IDENT;
176     }
177 }
178
179
180
181 static int SkipWhite (void)
182 /* Skip white space in the input stream, reading and preprocessing new lines
183  * if necessary. Return 0 if end of file is reached, return 1 otherwise.
184  */
185 {
186     while (1) {
187         while (CurC == '\0') {
188             if (NextLine () == 0) {
189                 return 0;
190             }
191             Preprocess ();
192         }
193         if (IsSpace (CurC)) {
194             NextChar ();
195         } else {
196             return 1;
197         }
198     }
199 }
200
201
202
203 int TokIsFuncSpec (const Token* T)
204 /* Return true if the token is a function specifier */
205 {
206     return (T->Tok == TOK_INLINE) || (T->Tok == TOK_FASTCALL) ||
207            (T->Tok == TOK_NEAR)   || (T->Tok == TOK_FAR);
208 }
209
210
211
212 void SymName (char* S)
213 /* Read a symbol from the input stream. The first character must have been
214  * checked before calling this function. The buffer is expected to be at
215  * least of size MAX_IDENTLEN+1.
216  */
217 {
218     unsigned Len = 0;
219     do {
220         if (Len < MAX_IDENTLEN) {
221             ++Len;
222             *S++ = CurC;
223         }
224         NextChar ();
225     } while (IsIdent (CurC) || IsDigit (CurC));
226     *S = '\0';
227 }
228
229
230
231 int IsSym (char* S)
232 /* If a symbol follows, read it and return 1, otherwise return 0 */
233 {
234     if (IsIdent (CurC)) {
235         SymName (S);
236         return 1;
237     } else {
238         return 0;
239     }
240 }
241
242
243
244 static void UnknownChar (char C)
245 /* Error message for unknown character */
246 {
247     Error ("Invalid input character with code %02X", C & 0xFF);
248     NextChar ();                        /* Skip */
249 }
250
251
252
253 static void SetTok (int tok)
254 /* Set NextTok.Tok and bump line ptr */
255 {
256     NextTok.Tok = tok;
257     NextChar ();
258 }
259
260
261
262 static int ParseChar (void)
263 /* Parse a character. Converts escape chars into character codes. */
264 {
265     int C;
266     int HadError;
267
268     /* Check for escape chars */
269     if (CurC == '\\') {
270         NextChar ();
271         switch (CurC) {
272             case '?':
273                 C = '\?';
274                 break;
275             case 'a':
276                 C = '\a';
277                 break;
278             case 'b':
279                 C = '\b';
280                 break;
281             case 'f':
282                 C = '\f';
283                 break;
284             case 'r':
285                 C = '\r';
286                 break;
287             case 'n':
288                 C = '\n';
289                 break;
290             case 't':
291                 C = '\t';
292                 break;
293             case 'v':
294                 C = '\v';
295                 break;
296             case '\"':
297                 C = '\"';
298                 break;
299             case '\'':
300                 C = '\'';
301                 break;
302             case '\\':
303                 C = '\\';
304                 break;
305             case 'x':
306             case 'X':
307                 /* Hex character constant */
308                 if (!IsXDigit (NextC)) {
309                     Error ("\\x used with no following hex digits");
310                     C = ' ';
311                 } else {
312                     HadError = 0;
313                     C = 0;
314                     while (IsXDigit (NextC)) {
315                         if ((C << 4) >= 256) {
316                             if (!HadError) {
317                                 Error ("Hex character constant out of range");
318                                 HadError = 1;
319                             }
320                         } else {
321                             C = (C << 4) | HexVal (NextC);
322                         }
323                         NextChar ();
324                     }
325                 }
326                 break;
327             case '0':
328             case '1':
329             case '2':
330             case '3':
331             case '4':
332             case '5':
333             case '6':
334             case '7':
335                 /* Octal constant */
336                 HadError = 0;
337                 C = HexVal (CurC);
338                 while (IsODigit (NextC)) {
339                     if ((C << 3) >= 256) {
340                         if (!HadError) {
341                             Error ("Octal character constant out of range");
342                             HadError = 1;
343                         }
344                     } else {
345                         C = (C << 3) | HexVal (NextC);
346                     }
347                     NextChar ();
348                 }
349                 break;
350             default:
351                 Error ("Illegal character constant");
352                 C = ' ';
353                 /* Try to do error recovery, otherwise the compiler will spit
354                  * out thousands of errors in this place and abort.
355                  */
356                 if (CurC != '\'' && CurC != '\0') {
357                     while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
358                         NextChar ();
359                     }
360                 }
361                 break;
362         }
363     } else {
364         C = CurC;
365     }
366
367     /* Skip the character read */
368     NextChar ();
369
370     /* Do correct sign extension */
371     return SignExtendChar (C);
372 }
373
374
375
376 static void CharConst (void)
377 /* Parse a character constant. */
378 {
379     int C;
380
381     /* Skip the quote */
382     NextChar ();
383
384     /* Get character */
385     C = ParseChar ();
386
387     /* Check for closing quote */
388     if (CurC != '\'') {
389         Error ("`\'' expected");
390     } else {
391         /* Skip the quote */
392         NextChar ();
393     }
394
395     /* Setup values and attributes */
396     NextTok.Tok  = TOK_CCONST;
397
398     /* Translate into target charset */
399     NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
400
401     /* Character constants have type int */
402     NextTok.Type = type_int;
403 }
404
405
406
407 static void StringConst (void)
408 /* Parse a quoted string */
409 {
410     NextTok.IVal = GetLiteralPoolOffs ();
411     NextTok.Tok  = TOK_SCONST;
412
413     /* Be sure to concatenate strings */
414     while (CurC == '\"') {
415
416         /* Skip the quote char */
417         NextChar ();
418
419         while (CurC != '\"') {
420             if (CurC == '\0') {
421                 Error ("Unexpected newline");
422                 break;
423             }
424             AddLiteralChar (ParseChar ());
425         }
426
427         /* Skip closing quote char if there was one */
428         NextChar ();
429
430         /* Skip white space, read new input */
431         SkipWhite ();
432
433     }
434
435     /* Terminate the string */
436     AddLiteralChar ('\0');
437 }
438
439
440
441 static void NumericConst (void)
442 /* Parse a numeric constant */
443 {
444     unsigned Base;              /* Temporary number base */
445     unsigned Prefix;            /* Base according to prefix */
446     StrBuf   S = STATIC_STRBUF_INITIALIZER;
447     int      IsFloat;
448     char     C;
449     unsigned DigitVal;
450     unsigned long IVal;         /* Value */
451
452     /* Check for a leading hex or octal prefix and determine the possible
453      * integer types.
454      */
455     if (CurC == '0') {
456         /* Gobble 0 and examine next char */
457         NextChar ();
458         if (toupper (CurC) == 'X') {
459             Base = Prefix = 16;
460             NextChar ();        /* gobble "x" */
461         } else {
462             Base = 10;          /* Assume 10 for now - see below */
463             Prefix = 8;         /* Actual prefix says octal */
464         }
465     } else {
466         Base  = Prefix = 10;
467     }
468
469     /* Because floating point numbers don't have octal prefixes (a number
470      * with a leading zero is decimal), we first have to read the number
471      * before converting it, so we can determine if it's a float or an
472      * integer.
473      */
474     while (IsXDigit (CurC) && HexVal (CurC) < Base) {
475         SB_AppendChar (&S, CurC);
476         NextChar ();
477     }
478     SB_Terminate (&S);
479
480     /* The following character tells us if we have an integer or floating
481      * point constant. Note: Hexadecimal floating point constants aren't
482      * supported in C89.
483      */
484     IsFloat = (CurC == '.' ||
485                (Base == 10 && toupper (CurC) == 'E') ||
486                (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
487
488     /* If we don't have a floating point type, an octal prefix results in an
489      * octal base.
490      */
491     if (!IsFloat && Prefix == 8) {
492         Base = 8;
493     }
494
495     /* Since we do now know the correct base, convert the remembered input
496      * into a number.
497      */
498     SB_Reset (&S);
499     IVal = 0;
500     while ((C = SB_Get (&S)) != '\0') {
501         DigitVal = HexVal (C);
502         if (DigitVal >= Base) {
503             Error ("Numeric constant contains digits beyond the radix");
504         }
505         IVal = (IVal * Base) + DigitVal;
506     }
507
508     /* We don't need the string buffer any longer */
509     SB_Done (&S);
510
511     /* Distinguish between integer and floating point constants */
512     if (!IsFloat) {
513
514         unsigned Types;
515         int      HaveSuffix;
516
517         /* Check for a suffix and determine the possible types */
518         HaveSuffix = 1;
519         if (toupper (CurC) == 'U') {
520             /* Unsigned type */
521             NextChar ();
522             if (toupper (CurC) != 'L') {
523                 Types = IT_UINT | IT_ULONG;
524             } else {
525                 NextChar ();
526                 Types = IT_ULONG;
527             }
528         } else if (toupper (CurC) == 'L') {
529             /* Long type */
530             NextChar ();
531             if (toupper (CurC) != 'U') {
532                 Types = IT_LONG | IT_ULONG;
533             } else {
534                 NextChar ();
535                 Types = IT_ULONG;
536             }
537         } else {
538             HaveSuffix = 0;
539             if (Prefix == 10) {
540                 /* Decimal constants are of any type but uint */
541                 Types = IT_INT | IT_LONG | IT_ULONG;
542             } else {
543                 /* Octal or hex constants are of any type */
544                 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
545             }
546         }
547
548         /* Check the range to determine the type */
549         if (IVal > 0x7FFF) {
550             /* Out of range for int */
551             Types &= ~IT_INT;
552             /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
553              * allowed, and we don't have a type specifying suffix, emit a
554              * warning, because the constant is of type long.
555              */
556             if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
557                 Warning ("Constant is long");
558             }
559         }
560         if (IVal > 0xFFFF) {
561             /* Out of range for unsigned int */
562             Types &= ~IT_UINT;
563         }
564         if (IVal > 0x7FFFFFFF) {
565             /* Out of range for long int */
566             Types &= ~IT_LONG;
567         }
568
569         /* Now set the type string to the smallest type in types */
570         if (Types & IT_INT) {
571             NextTok.Type = type_int;
572         } else if (Types & IT_UINT) {
573             NextTok.Type = type_uint;
574         } else if (Types & IT_LONG) {
575             NextTok.Type = type_long;
576         } else {
577             NextTok.Type = type_ulong;
578         }
579
580         /* Set the value and the token */
581         NextTok.IVal = IVal;
582         NextTok.Tok  = TOK_ICONST;
583
584     } else {
585
586         /* Float constant */
587         Double FVal = FP_D_FromInt (IVal);      /* Convert to double */
588
589         /* Check for a fractional part and read it */
590         if (CurC == '.') {
591
592             Double Scale;
593
594             /* Skip the dot */
595             NextChar ();
596
597             /* Read fractional digits */
598             Scale  = FP_D_Make (1.0);
599             while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
600                 /* Get the value of this digit */
601                 Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
602                 /* Add it to the float value */
603                 FVal = FP_D_Add (FVal, FracVal);
604                 /* Scale base */
605                 Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
606                 /* Skip the digit */
607                 NextChar ();
608             }
609         }
610
611         /* Check for an exponent and read it */
612         if ((Base == 16 && toupper (CurC) == 'F') ||
613             (Base == 10 && toupper (CurC) == 'E')) {
614
615             int Sign;
616             unsigned Digits;
617             unsigned Exp;
618
619             /* Skip the exponent notifier */
620             NextChar ();
621
622             /* Read an optional sign */
623             Sign = 1;
624             if (CurC == '-') {
625                 Sign = -1;
626                 NextChar ();
627             } else if (CurC == '+') {
628                 NextChar ();
629             }
630
631             /* Read exponent digits. Since we support only 32 bit floats
632              * with a maximum exponent of +-/127, we read the exponent
633              * part as integer with up to 3 digits and drop the remainder.
634              * This avoids an overflow of Exp. The exponent is always
635              * decimal, even for hex float consts.
636              */
637             Digits = 0;
638             Exp    = 0;
639             while (IsDigit (CurC)) {
640                 if (++Digits <= 3) {
641                     Exp = Exp * 10 + HexVal (CurC);
642                 }
643                 NextChar ();
644             }
645
646             /* Check for errors: We must have exponent digits, and not more
647              * than three.
648              */
649             if (Digits == 0) {
650                 Error ("Floating constant exponent has no digits");
651             } else if (Digits > 3) {
652                 Warning ("Floating constant exponent is too large");
653             }
654
655             /* Scale the exponent and adjust the value accordingly */
656             if (Exp) {
657                 FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
658             }
659         }
660
661         /* Check for a suffix and determine the type of the constant */
662         if (toupper (CurC) == 'F') {
663             NextChar ();
664             NextTok.Type = type_float;
665         } else {
666             NextTok.Type = type_double;
667         }
668
669         /* Set the value and the token */
670         NextTok.FVal = FVal;
671         NextTok.Tok  = TOK_FCONST;
672
673     }
674 }
675
676
677
678 void NextToken (void)
679 /* Get next token from input stream */
680 {
681     ident token;
682
683     /* We have to skip white space here before shifting tokens, since the
684      * tokens and the current line info is invalid at startup and will get
685      * initialized by reading the first time from the file. Remember if
686      * we were at end of input and handle that later.
687      */
688     int GotEOF = (SkipWhite() == 0);
689
690     /* Current token is the lookahead token */
691     if (CurTok.LI) {
692         ReleaseLineInfo (CurTok.LI);
693     }
694     CurTok = NextTok;
695
696     /* When reading the first time from the file, the line info in NextTok,
697      * which was copied to CurTok is invalid. Since the information from
698      * the token is used for error messages, we must make it valid.
699      */
700     if (CurTok.LI == 0) {
701         CurTok.LI = UseLineInfo (GetCurLineInfo ());
702     }
703
704     /* Remember the starting position of the next token */
705     NextTok.LI = UseLineInfo (GetCurLineInfo ());
706
707     /* Now handle end of input. */
708     if (GotEOF) {
709         /* End of file reached */
710         NextTok.Tok = TOK_CEOF;
711         return;
712     }
713
714     /* Determine the next token from the lookahead */
715     if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
716         /* A number */
717         NumericConst ();
718         return;
719     }
720
721     if (IsSym (token)) {
722
723         /* Check for a keyword */
724         if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
725             /* Reserved word found */
726             return;
727         }
728         /* No reserved word, check for special symbols */
729         if (token[0] == '_' && token[1] == '_') {
730             /* Special symbols */
731             if (strcmp (token+2, "FILE__") == 0) {
732                 NextTok.IVal = AddLiteral (GetCurrentFile());
733                 NextTok.Tok  = TOK_SCONST;
734                 return;
735             } else if (strcmp (token+2, "LINE__") == 0) {
736                 NextTok.Tok  = TOK_ICONST;
737                 NextTok.IVal = GetCurrentLine();
738                 NextTok.Type = type_int;
739                 return;
740             } else if (strcmp (token+2, "func__") == 0) {
741                 /* __func__ is only defined in functions */
742                 if (CurrentFunc) {
743                     NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
744                     NextTok.Tok  = TOK_SCONST;
745                     return;
746                 }
747             }
748         }
749
750         /* No reserved word but identifier */
751         strcpy (NextTok.Ident, token);
752         NextTok.Tok = TOK_IDENT;
753         return;
754     }
755
756     /* Monstrous switch statement ahead... */
757     switch (CurC) {
758
759         case '!':
760             NextChar ();
761             if (CurC == '=') {
762                 SetTok (TOK_NE);
763             } else {
764                 NextTok.Tok = TOK_BOOL_NOT;
765             }
766             break;
767
768         case '\"':
769             StringConst ();
770             break;
771
772         case '%':
773             NextChar ();
774             if (CurC == '=') {
775                 SetTok (TOK_MOD_ASSIGN);
776             } else {
777                 NextTok.Tok = TOK_MOD;
778             }
779             break;
780
781         case '&':
782             NextChar ();
783             switch (CurC) {
784                 case '&':
785                     SetTok (TOK_BOOL_AND);
786                     break;
787                 case '=':
788                     SetTok (TOK_AND_ASSIGN);
789                     break;
790                 default:
791                     NextTok.Tok = TOK_AND;
792             }
793             break;
794
795         case '\'':
796             CharConst ();
797             break;
798
799         case '(':
800             SetTok (TOK_LPAREN);
801             break;
802
803         case ')':
804             SetTok (TOK_RPAREN);
805             break;
806
807         case '*':
808             NextChar ();
809             if (CurC == '=') {
810                 SetTok (TOK_MUL_ASSIGN);
811             } else {
812                 NextTok.Tok = TOK_STAR;
813             }
814             break;
815
816         case '+':
817             NextChar ();
818             switch (CurC) {
819                 case '+':
820                     SetTok (TOK_INC);
821                     break;
822                 case '=':
823                     SetTok (TOK_PLUS_ASSIGN);
824                     break;
825                 default:
826                     NextTok.Tok = TOK_PLUS;
827             }
828             break;
829
830         case ',':
831             SetTok (TOK_COMMA);
832             break;
833
834         case '-':
835             NextChar ();
836             switch (CurC) {
837                 case '-':
838                     SetTok (TOK_DEC);
839                     break;
840                 case '=':
841                     SetTok (TOK_MINUS_ASSIGN);
842                     break;
843                 case '>':
844                     SetTok (TOK_PTR_REF);
845                     break;
846                 default:
847                     NextTok.Tok = TOK_MINUS;
848             }
849             break;
850
851         case '.':
852             NextChar ();
853             if (CurC == '.') {
854                 NextChar ();
855                 if (CurC == '.') {
856                     SetTok (TOK_ELLIPSIS);
857                 } else {
858                     UnknownChar (CurC);
859                 }
860             } else {
861                 NextTok.Tok = TOK_DOT;
862             }
863             break;
864
865         case '/':
866             NextChar ();
867             if (CurC == '=') {
868                 SetTok (TOK_DIV_ASSIGN);
869             } else {
870                 NextTok.Tok = TOK_DIV;
871             }
872             break;
873
874         case ':':
875             SetTok (TOK_COLON);
876             break;
877
878         case ';':
879             SetTok (TOK_SEMI);
880             break;
881
882         case '<':
883             NextChar ();
884             switch (CurC) {
885                 case '=':
886                     SetTok (TOK_LE);
887                     break;
888                 case '<':
889                     NextChar ();
890                     if (CurC == '=') {
891                         SetTok (TOK_SHL_ASSIGN);
892                     } else {
893                         NextTok.Tok = TOK_SHL;
894                     }
895                     break;
896                 default:
897                     NextTok.Tok = TOK_LT;
898             }
899             break;
900
901         case '=':
902             NextChar ();
903             if (CurC == '=') {
904                 SetTok (TOK_EQ);
905             } else {
906                 NextTok.Tok = TOK_ASSIGN;
907             }
908             break;
909
910         case '>':
911             NextChar ();
912             switch (CurC) {
913                 case '=':
914                     SetTok (TOK_GE);
915                     break;
916                 case '>':
917                     NextChar ();
918                     if (CurC == '=') {
919                         SetTok (TOK_SHR_ASSIGN);
920                     } else {
921                         NextTok.Tok = TOK_SHR;
922                     }
923                     break;
924                 default:
925                     NextTok.Tok = TOK_GT;
926             }
927             break;
928
929         case '?':
930             SetTok (TOK_QUEST);
931             break;
932
933         case '[':
934             SetTok (TOK_LBRACK);
935             break;
936
937         case ']':
938             SetTok (TOK_RBRACK);
939             break;
940
941         case '^':
942             NextChar ();
943             if (CurC == '=') {
944                 SetTok (TOK_XOR_ASSIGN);
945             } else {
946                 NextTok.Tok = TOK_XOR;
947             }
948             break;
949
950         case '{':
951             SetTok (TOK_LCURLY);
952             break;
953
954         case '|':
955             NextChar ();
956             switch (CurC) {
957                 case '|':
958                     SetTok (TOK_BOOL_OR);
959                     break;
960                 case '=':
961                     SetTok (TOK_OR_ASSIGN);
962                     break;
963                 default:
964                     NextTok.Tok = TOK_OR;
965             }
966             break;
967
968         case '}':
969             SetTok (TOK_RCURLY);
970             break;
971
972         case '~':
973             SetTok (TOK_COMP);
974             break;
975
976         default:
977             UnknownChar (CurC);
978
979     }
980
981 }
982
983
984
985 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
986 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
987  * This routine is used for error recovery.
988  */
989 {
990     while (CurTok.Tok != TOK_CEOF) {
991
992         /* Check if the current token is in the token list */
993         unsigned I;
994         for (I = 0; I < TokenCount; ++I) {
995             if (CurTok.Tok == TokenList[I]) {
996                 /* Found a token in the list */
997                 return;
998             }
999         }
1000
1001         /* Not in the list: Skip it */
1002         NextToken ();
1003
1004     }
1005 }
1006
1007
1008
1009 int Consume (token_t Token, const char* ErrorMsg)
1010 /* Eat token if it is the next in the input stream, otherwise print an error
1011  * message. Returns true if the token was found and false otherwise.
1012  */
1013 {
1014     if (CurTok.Tok == Token) {
1015         NextToken ();
1016         return 1;
1017     } else {
1018         Error (ErrorMsg);
1019         return 0;
1020     }
1021 }
1022
1023
1024
1025 int ConsumeColon (void)
1026 /* Check for a colon and skip it. */
1027 {
1028     return Consume (TOK_COLON, "`:' expected");
1029 }
1030
1031
1032
1033 int ConsumeSemi (void)
1034 /* Check for a semicolon and skip it. */
1035 {
1036     /* Try do be smart about typos... */
1037     if (CurTok.Tok == TOK_SEMI) {
1038         NextToken ();
1039         return 1;
1040     } else {
1041         Error ("`;' expected");
1042         if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1043             NextToken ();
1044         }
1045         return 0;
1046     }
1047 }
1048
1049
1050
1051 int ConsumeComma (void)
1052 /* Check for a comma and skip it. */
1053 {
1054     /* Try do be smart about typos... */
1055     if (CurTok.Tok == TOK_COMMA) {
1056         NextToken ();
1057         return 1;
1058     } else {
1059         Error ("`,' expected");
1060         if (CurTok.Tok == TOK_SEMI) {
1061             NextToken ();
1062         }
1063         return 0;
1064     }
1065 }
1066
1067
1068
1069 int ConsumeLParen (void)
1070 /* Check for a left parenthesis and skip it */
1071 {
1072     return Consume (TOK_LPAREN, "`(' expected");
1073 }
1074
1075
1076
1077 int ConsumeRParen (void)
1078 /* Check for a right parenthesis and skip it */
1079 {
1080     return Consume (TOK_RPAREN, "`)' expected");
1081 }
1082
1083
1084
1085 int ConsumeLBrack (void)
1086 /* Check for a left bracket and skip it */
1087 {
1088     return Consume (TOK_LBRACK, "`[' expected");
1089 }
1090
1091
1092
1093 int ConsumeRBrack (void)
1094 /* Check for a right bracket and skip it */
1095 {
1096     return Consume (TOK_RBRACK, "`]' expected");
1097 }
1098
1099
1100
1101 int ConsumeLCurly (void)
1102 /* Check for a left curly brace and skip it */
1103 {
1104     return Consume (TOK_LCURLY, "`{' expected");
1105 }
1106
1107
1108
1109 int ConsumeRCurly (void)
1110 /* Check for a right curly brace and skip it */
1111 {
1112     return Consume (TOK_RCURLY, "`}' expected");
1113 }
1114
1115
1116