]> git.sur5r.net Git - cc65/blob - src/cc65/scanner.c
Fixed problem with last change. Wide string constants were not handled
[cc65] / src / cc65 / scanner.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.c                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2009, 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     /* String buffer */
411     StrBuf S = AUTO_STRBUF_INITIALIZER;
412
413     /* Assume next token is a string constant */
414     NextTok.Tok  = TOK_SCONST;
415
416     /* Concatenate strings. If at least one of the concenated strings is a wide
417      * character literal, the whole string is a wide char literal, otherwise
418      * it's a normal string literal.
419      */
420     while (1) {
421
422         /* Check if this is a normal or a wide char string */
423         if (CurC == 'L' && NextC == '\"') {
424             /* Wide character literal */
425             NextTok.Tok = TOK_WCSCONST;
426             NextChar ();
427             NextChar ();
428         } else if (CurC == '\"') {
429             /* Skip the quote char */
430             NextChar ();
431         } else {
432             /* No string */
433             break;
434         }
435
436         /* Read until end of string */
437         while (CurC != '\"') {
438             if (CurC == '\0') {
439                 Error ("Unexpected newline");
440                 break;
441             }
442             SB_AppendChar (&S, ParseChar ());
443         }
444
445         /* Skip closing quote char if there was one */
446         NextChar ();
447
448         /* Skip white space, read new input */
449         SkipWhite ();
450
451     }
452
453     /* Terminate the string */
454     SB_AppendChar (&S, '\0');
455
456     /* Add the whole string to the literal pool */
457     NextTok.IVal = AddLiteralStr (&S);
458
459     /* Free the buffer */
460     SB_Done (&S);
461 }
462
463
464
465 static void NumericConst (void)
466 /* Parse a numeric constant */
467 {
468     unsigned Base;              /* Temporary number base */
469     unsigned Prefix;            /* Base according to prefix */
470     StrBuf   S = STATIC_STRBUF_INITIALIZER;
471     int      IsFloat;
472     char     C;
473     unsigned DigitVal;
474     unsigned long IVal;         /* Value */
475
476     /* Check for a leading hex or octal prefix and determine the possible
477      * integer types.
478      */
479     if (CurC == '0') {
480         /* Gobble 0 and examine next char */
481         NextChar ();
482         if (toupper (CurC) == 'X') {
483             Base = Prefix = 16;
484             NextChar ();        /* gobble "x" */
485         } else {
486             Base = 10;          /* Assume 10 for now - see below */
487             Prefix = 8;         /* Actual prefix says octal */
488         }
489     } else {
490         Base  = Prefix = 10;
491     }
492
493     /* Because floating point numbers don't have octal prefixes (a number
494      * with a leading zero is decimal), we first have to read the number
495      * before converting it, so we can determine if it's a float or an
496      * integer.
497      */
498     while (IsXDigit (CurC) && HexVal (CurC) < Base) {
499         SB_AppendChar (&S, CurC);
500         NextChar ();
501     }
502     SB_Terminate (&S);
503
504     /* The following character tells us if we have an integer or floating
505      * point constant. Note: Hexadecimal floating point constants aren't
506      * supported in C89.
507      */
508     IsFloat = (CurC == '.' ||
509                (Base == 10 && toupper (CurC) == 'E') ||
510                (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
511
512     /* If we don't have a floating point type, an octal prefix results in an
513      * octal base.
514      */
515     if (!IsFloat && Prefix == 8) {
516         Base = 8;
517     }
518
519     /* Since we do now know the correct base, convert the remembered input
520      * into a number.
521      */
522     SB_Reset (&S);
523     IVal = 0;
524     while ((C = SB_Get (&S)) != '\0') {
525         DigitVal = HexVal (C);
526         if (DigitVal >= Base) {
527             Error ("Numeric constant contains digits beyond the radix");
528         }
529         IVal = (IVal * Base) + DigitVal;
530     }
531
532     /* We don't need the string buffer any longer */
533     SB_Done (&S);
534
535     /* Distinguish between integer and floating point constants */
536     if (!IsFloat) {
537
538         unsigned Types;
539         int      HaveSuffix;
540
541         /* Check for a suffix and determine the possible types */
542         HaveSuffix = 1;
543         if (toupper (CurC) == 'U') {
544             /* Unsigned type */
545             NextChar ();
546             if (toupper (CurC) != 'L') {
547                 Types = IT_UINT | IT_ULONG;
548             } else {
549                 NextChar ();
550                 Types = IT_ULONG;
551             }
552         } else if (toupper (CurC) == 'L') {
553             /* Long type */
554             NextChar ();
555             if (toupper (CurC) != 'U') {
556                 Types = IT_LONG | IT_ULONG;
557             } else {
558                 NextChar ();
559                 Types = IT_ULONG;
560             }
561         } else {
562             HaveSuffix = 0;
563             if (Prefix == 10) {
564                 /* Decimal constants are of any type but uint */
565                 Types = IT_INT | IT_LONG | IT_ULONG;
566             } else {
567                 /* Octal or hex constants are of any type */
568                 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
569             }
570         }
571
572         /* Check the range to determine the type */
573         if (IVal > 0x7FFF) {
574             /* Out of range for int */
575             Types &= ~IT_INT;
576             /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
577              * allowed, and we don't have a type specifying suffix, emit a
578              * warning, because the constant is of type long.
579              */
580             if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
581                 Warning ("Constant is long");
582             }
583         }
584         if (IVal > 0xFFFF) {
585             /* Out of range for unsigned int */
586             Types &= ~IT_UINT;
587         }
588         if (IVal > 0x7FFFFFFF) {
589             /* Out of range for long int */
590             Types &= ~IT_LONG;
591         }
592
593         /* Now set the type string to the smallest type in types */
594         if (Types & IT_INT) {
595             NextTok.Type = type_int;
596         } else if (Types & IT_UINT) {
597             NextTok.Type = type_uint;
598         } else if (Types & IT_LONG) {
599             NextTok.Type = type_long;
600         } else {
601             NextTok.Type = type_ulong;
602         }
603
604         /* Set the value and the token */
605         NextTok.IVal = IVal;
606         NextTok.Tok  = TOK_ICONST;
607
608     } else {
609
610         /* Float constant */
611         Double FVal = FP_D_FromInt (IVal);      /* Convert to double */
612
613         /* Check for a fractional part and read it */
614         if (CurC == '.') {
615
616             Double Scale;
617
618             /* Skip the dot */
619             NextChar ();
620
621             /* Read fractional digits */
622             Scale  = FP_D_Make (1.0);
623             while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
624                 /* Get the value of this digit */
625                 Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
626                 /* Add it to the float value */
627                 FVal = FP_D_Add (FVal, FracVal);
628                 /* Scale base */
629                 Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
630                 /* Skip the digit */
631                 NextChar ();
632             }
633         }
634
635         /* Check for an exponent and read it */
636         if ((Base == 16 && toupper (CurC) == 'F') ||
637             (Base == 10 && toupper (CurC) == 'E')) {
638
639             int Sign;
640             unsigned Digits;
641             unsigned Exp;
642
643             /* Skip the exponent notifier */
644             NextChar ();
645
646             /* Read an optional sign */
647             Sign = 1;
648             if (CurC == '-') {
649                 Sign = -1;
650                 NextChar ();
651             } else if (CurC == '+') {
652                 NextChar ();
653             }
654
655             /* Read exponent digits. Since we support only 32 bit floats
656              * with a maximum exponent of +-/127, we read the exponent
657              * part as integer with up to 3 digits and drop the remainder.
658              * This avoids an overflow of Exp. The exponent is always
659              * decimal, even for hex float consts.
660              */
661             Digits = 0;
662             Exp    = 0;
663             while (IsDigit (CurC)) {
664                 if (++Digits <= 3) {
665                     Exp = Exp * 10 + HexVal (CurC);
666                 }
667                 NextChar ();
668             }
669
670             /* Check for errors: We must have exponent digits, and not more
671              * than three.
672              */
673             if (Digits == 0) {
674                 Error ("Floating constant exponent has no digits");
675             } else if (Digits > 3) {
676                 Warning ("Floating constant exponent is too large");
677             }
678
679             /* Scale the exponent and adjust the value accordingly */
680             if (Exp) {
681                 FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
682             }
683         }
684
685         /* Check for a suffix and determine the type of the constant */
686         if (toupper (CurC) == 'F') {
687             NextChar ();
688             NextTok.Type = type_float;
689         } else {
690             NextTok.Type = type_double;
691         }
692
693         /* Set the value and the token */
694         NextTok.FVal = FVal;
695         NextTok.Tok  = TOK_FCONST;
696
697     }
698 }
699
700
701
702 void NextToken (void)
703 /* Get next token from input stream */
704 {
705     ident token;
706
707     /* We have to skip white space here before shifting tokens, since the
708      * tokens and the current line info is invalid at startup and will get
709      * initialized by reading the first time from the file. Remember if
710      * we were at end of input and handle that later.
711      */
712     int GotEOF = (SkipWhite() == 0);
713
714     /* Current token is the lookahead token */
715     if (CurTok.LI) {
716         ReleaseLineInfo (CurTok.LI);
717     }
718     CurTok = NextTok;
719
720     /* When reading the first time from the file, the line info in NextTok,
721      * which was copied to CurTok is invalid. Since the information from
722      * the token is used for error messages, we must make it valid.
723      */
724     if (CurTok.LI == 0) {
725         CurTok.LI = UseLineInfo (GetCurLineInfo ());
726     }
727
728     /* Remember the starting position of the next token */
729     NextTok.LI = UseLineInfo (GetCurLineInfo ());
730
731     /* Now handle end of input. */
732     if (GotEOF) {
733         /* End of file reached */
734         NextTok.Tok = TOK_CEOF;
735         return;
736     }
737
738     /* Determine the next token from the lookahead */
739     if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
740         /* A number */
741         NumericConst ();
742         return;
743     }
744
745     /* Check for wide character literals */
746     if (CurC == 'L' && NextC == '\"') {
747         StringConst ();
748         return;
749     }
750
751     /* Check for keywords and identifiers */
752     if (IsSym (token)) {
753
754         /* Check for a keyword */
755         if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
756             /* Reserved word found */
757             return;
758         }
759         /* No reserved word, check for special symbols */
760         if (token[0] == '_' && token[1] == '_') {
761             /* Special symbols */
762             if (strcmp (token+2, "FILE__") == 0) {
763                 NextTok.IVal = AddLiteral (GetCurrentFile());
764                 NextTok.Tok  = TOK_SCONST;
765                 return;
766             } else if (strcmp (token+2, "LINE__") == 0) {
767                 NextTok.Tok  = TOK_ICONST;
768                 NextTok.IVal = GetCurrentLine();
769                 NextTok.Type = type_int;
770                 return;
771             } else if (strcmp (token+2, "func__") == 0) {
772                 /* __func__ is only defined in functions */
773                 if (CurrentFunc) {
774                     NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
775                     NextTok.Tok  = TOK_SCONST;
776                     return;
777                 }
778             }
779         }
780
781         /* No reserved word but identifier */
782         strcpy (NextTok.Ident, token);
783         NextTok.Tok = TOK_IDENT;
784         return;
785     }
786
787     /* Monstrous switch statement ahead... */
788     switch (CurC) {
789
790         case '!':
791             NextChar ();
792             if (CurC == '=') {
793                 SetTok (TOK_NE);
794             } else {
795                 NextTok.Tok = TOK_BOOL_NOT;
796             }
797             break;
798
799         case '\"':
800             StringConst ();
801             break;
802
803         case '%':
804             NextChar ();
805             if (CurC == '=') {
806                 SetTok (TOK_MOD_ASSIGN);
807             } else {
808                 NextTok.Tok = TOK_MOD;
809             }
810             break;
811
812         case '&':
813             NextChar ();
814             switch (CurC) {
815                 case '&':
816                     SetTok (TOK_BOOL_AND);
817                     break;
818                 case '=':
819                     SetTok (TOK_AND_ASSIGN);
820                     break;
821                 default:
822                     NextTok.Tok = TOK_AND;
823             }
824             break;
825
826         case '\'':
827             CharConst ();
828             break;
829
830         case '(':
831             SetTok (TOK_LPAREN);
832             break;
833
834         case ')':
835             SetTok (TOK_RPAREN);
836             break;
837
838         case '*':
839             NextChar ();
840             if (CurC == '=') {
841                 SetTok (TOK_MUL_ASSIGN);
842             } else {
843                 NextTok.Tok = TOK_STAR;
844             }
845             break;
846
847         case '+':
848             NextChar ();
849             switch (CurC) {
850                 case '+':
851                     SetTok (TOK_INC);
852                     break;
853                 case '=':
854                     SetTok (TOK_PLUS_ASSIGN);
855                     break;
856                 default:
857                     NextTok.Tok = TOK_PLUS;
858             }
859             break;
860
861         case ',':
862             SetTok (TOK_COMMA);
863             break;
864
865         case '-':
866             NextChar ();
867             switch (CurC) {
868                 case '-':
869                     SetTok (TOK_DEC);
870                     break;
871                 case '=':
872                     SetTok (TOK_MINUS_ASSIGN);
873                     break;
874                 case '>':
875                     SetTok (TOK_PTR_REF);
876                     break;
877                 default:
878                     NextTok.Tok = TOK_MINUS;
879             }
880             break;
881
882         case '.':
883             NextChar ();
884             if (CurC == '.') {
885                 NextChar ();
886                 if (CurC == '.') {
887                     SetTok (TOK_ELLIPSIS);
888                 } else {
889                     UnknownChar (CurC);
890                 }
891             } else {
892                 NextTok.Tok = TOK_DOT;
893             }
894             break;
895
896         case '/':
897             NextChar ();
898             if (CurC == '=') {
899                 SetTok (TOK_DIV_ASSIGN);
900             } else {
901                 NextTok.Tok = TOK_DIV;
902             }
903             break;
904
905         case ':':
906             SetTok (TOK_COLON);
907             break;
908
909         case ';':
910             SetTok (TOK_SEMI);
911             break;
912
913         case '<':
914             NextChar ();
915             switch (CurC) {
916                 case '=':
917                     SetTok (TOK_LE);
918                     break;
919                 case '<':
920                     NextChar ();
921                     if (CurC == '=') {
922                         SetTok (TOK_SHL_ASSIGN);
923                     } else {
924                         NextTok.Tok = TOK_SHL;
925                     }
926                     break;
927                 default:
928                     NextTok.Tok = TOK_LT;
929             }
930             break;
931
932         case '=':
933             NextChar ();
934             if (CurC == '=') {
935                 SetTok (TOK_EQ);
936             } else {
937                 NextTok.Tok = TOK_ASSIGN;
938             }
939             break;
940
941         case '>':
942             NextChar ();
943             switch (CurC) {
944                 case '=':
945                     SetTok (TOK_GE);
946                     break;
947                 case '>':
948                     NextChar ();
949                     if (CurC == '=') {
950                         SetTok (TOK_SHR_ASSIGN);
951                     } else {
952                         NextTok.Tok = TOK_SHR;
953                     }
954                     break;
955                 default:
956                     NextTok.Tok = TOK_GT;
957             }
958             break;
959
960         case '?':
961             SetTok (TOK_QUEST);
962             break;
963
964         case '[':
965             SetTok (TOK_LBRACK);
966             break;
967
968         case ']':
969             SetTok (TOK_RBRACK);
970             break;
971
972         case '^':
973             NextChar ();
974             if (CurC == '=') {
975                 SetTok (TOK_XOR_ASSIGN);
976             } else {
977                 NextTok.Tok = TOK_XOR;
978             }
979             break;
980
981         case '{':
982             SetTok (TOK_LCURLY);
983             break;
984
985         case '|':
986             NextChar ();
987             switch (CurC) {
988                 case '|':
989                     SetTok (TOK_BOOL_OR);
990                     break;
991                 case '=':
992                     SetTok (TOK_OR_ASSIGN);
993                     break;
994                 default:
995                     NextTok.Tok = TOK_OR;
996             }
997             break;
998
999         case '}':
1000             SetTok (TOK_RCURLY);
1001             break;
1002
1003         case '~':
1004             SetTok (TOK_COMP);
1005             break;
1006
1007         default:
1008             UnknownChar (CurC);
1009
1010     }
1011
1012 }
1013
1014
1015
1016 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
1017 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
1018  * This routine is used for error recovery.
1019  */
1020 {
1021     while (CurTok.Tok != TOK_CEOF) {
1022
1023         /* Check if the current token is in the token list */
1024         unsigned I;
1025         for (I = 0; I < TokenCount; ++I) {
1026             if (CurTok.Tok == TokenList[I]) {
1027                 /* Found a token in the list */
1028                 return;
1029             }
1030         }
1031
1032         /* Not in the list: Skip it */
1033         NextToken ();
1034
1035     }
1036 }
1037
1038
1039
1040 int Consume (token_t Token, const char* ErrorMsg)
1041 /* Eat token if it is the next in the input stream, otherwise print an error
1042  * message. Returns true if the token was found and false otherwise.
1043  */
1044 {
1045     if (CurTok.Tok == Token) {
1046         NextToken ();
1047         return 1;
1048     } else {
1049         Error ("%s", ErrorMsg);
1050         return 0;
1051     }
1052 }
1053
1054
1055
1056 int ConsumeColon (void)
1057 /* Check for a colon and skip it. */
1058 {
1059     return Consume (TOK_COLON, "`:' expected");
1060 }
1061
1062
1063
1064 int ConsumeSemi (void)
1065 /* Check for a semicolon and skip it. */
1066 {
1067     /* Try do be smart about typos... */
1068     if (CurTok.Tok == TOK_SEMI) {
1069         NextToken ();
1070         return 1;
1071     } else {
1072         Error ("`;' expected");
1073         if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1074             NextToken ();
1075         }
1076         return 0;
1077     }
1078 }
1079
1080
1081
1082 int ConsumeComma (void)
1083 /* Check for a comma and skip it. */
1084 {
1085     /* Try do be smart about typos... */
1086     if (CurTok.Tok == TOK_COMMA) {
1087         NextToken ();
1088         return 1;
1089     } else {
1090         Error ("`,' expected");
1091         if (CurTok.Tok == TOK_SEMI) {
1092             NextToken ();
1093         }
1094         return 0;
1095     }
1096 }
1097
1098
1099
1100 int ConsumeLParen (void)
1101 /* Check for a left parenthesis and skip it */
1102 {
1103     return Consume (TOK_LPAREN, "`(' expected");
1104 }
1105
1106
1107
1108 int ConsumeRParen (void)
1109 /* Check for a right parenthesis and skip it */
1110 {
1111     return Consume (TOK_RPAREN, "`)' expected");
1112 }
1113
1114
1115
1116 int ConsumeLBrack (void)
1117 /* Check for a left bracket and skip it */
1118 {
1119     return Consume (TOK_LBRACK, "`[' expected");
1120 }
1121
1122
1123
1124 int ConsumeRBrack (void)
1125 /* Check for a right bracket and skip it */
1126 {
1127     return Consume (TOK_RBRACK, "`]' expected");
1128 }
1129
1130
1131
1132 int ConsumeLCurly (void)
1133 /* Check for a left curly brace and skip it */
1134 {
1135     return Consume (TOK_LCURLY, "`{' expected");
1136 }
1137
1138
1139
1140 int ConsumeRCurly (void)
1141 /* Check for a right curly brace and skip it */
1142 {
1143     return Consume (TOK_RCURLY, "`}' expected");
1144 }
1145
1146
1147