]> git.sur5r.net Git - cc65/blob - src/cc65/scanner.c
Merge pull request #7 from cvemu/master
[cc65] / src / cc65 / scanner.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.c                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2010, 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     { "__cdecl__",      TOK_CDECL,      TT_C89 | TT_C99 | TT_CC65  },
97     { "__far__",        TOK_FAR,        TT_C89 | TT_C99 | TT_CC65  },
98     { "__fastcall__",   TOK_FASTCALL,   TT_C89 | TT_C99 | TT_CC65  },
99     { "__inline__",     TOK_INLINE,     TT_C89 | TT_C99 | TT_CC65  },
100     { "__near__",       TOK_NEAR,       TT_C89 | TT_C99 | TT_CC65  },
101     { "asm",            TOK_ASM,                          TT_CC65  },
102     { "auto",           TOK_AUTO,       TT_C89 | TT_C99 | TT_CC65  },
103     { "break",          TOK_BREAK,      TT_C89 | TT_C99 | TT_CC65  },
104     { "case",           TOK_CASE,       TT_C89 | TT_C99 | TT_CC65  },
105     { "cdecl",          TOK_CDECL,                        TT_CC65  },
106     { "char",           TOK_CHAR,       TT_C89 | TT_C99 | TT_CC65  },
107     { "const",          TOK_CONST,      TT_C89 | TT_C99 | TT_CC65  },
108     { "continue",       TOK_CONTINUE,   TT_C89 | TT_C99 | TT_CC65  },
109     { "default",        TOK_DEFAULT,    TT_C89 | TT_C99 | TT_CC65  },
110     { "do",             TOK_DO,         TT_C89 | TT_C99 | TT_CC65  },
111     { "double",         TOK_DOUBLE,     TT_C89 | TT_C99 | TT_CC65  },
112     { "else",           TOK_ELSE,       TT_C89 | TT_C99 | TT_CC65  },
113     { "enum",           TOK_ENUM,       TT_C89 | TT_C99 | TT_CC65  },
114     { "extern",         TOK_EXTERN,     TT_C89 | TT_C99 | TT_CC65  },
115     { "far",            TOK_FAR,                          TT_CC65  },
116     { "fastcall",       TOK_FASTCALL,                     TT_CC65  },
117     { "float",          TOK_FLOAT,      TT_C89 | TT_C99 | TT_CC65  },
118     { "for",            TOK_FOR,        TT_C89 | TT_C99 | TT_CC65  },
119     { "goto",           TOK_GOTO,       TT_C89 | TT_C99 | TT_CC65  },
120     { "if",             TOK_IF,         TT_C89 | TT_C99 | TT_CC65  },
121     { "inline",         TOK_INLINE,              TT_C99 | TT_CC65  },
122     { "int",            TOK_INT,        TT_C89 | TT_C99 | TT_CC65  },
123     { "long",           TOK_LONG,       TT_C89 | TT_C99 | TT_CC65  },
124     { "near",           TOK_NEAR,                         TT_CC65  },
125     { "register",       TOK_REGISTER,   TT_C89 | TT_C99 | TT_CC65  },
126     { "restrict",       TOK_RESTRICT,            TT_C99 | TT_CC65  },
127     { "return",         TOK_RETURN,     TT_C89 | TT_C99 | TT_CC65  },
128     { "short",          TOK_SHORT,      TT_C89 | TT_C99 | TT_CC65  },
129     { "signed",         TOK_SIGNED,     TT_C89 | TT_C99 | TT_CC65  },
130     { "sizeof",         TOK_SIZEOF,     TT_C89 | TT_C99 | TT_CC65  },
131     { "static",         TOK_STATIC,     TT_C89 | TT_C99 | TT_CC65  },
132     { "struct",         TOK_STRUCT,     TT_C89 | TT_C99 | TT_CC65  },
133     { "switch",         TOK_SWITCH,     TT_C89 | TT_C99 | TT_CC65  },
134     { "typedef",        TOK_TYPEDEF,    TT_C89 | TT_C99 | TT_CC65  },
135     { "union",          TOK_UNION,      TT_C89 | TT_C99 | TT_CC65  },
136     { "unsigned",       TOK_UNSIGNED,   TT_C89 | TT_C99 | TT_CC65  },
137     { "void",           TOK_VOID,       TT_C89 | TT_C99 | TT_CC65  },
138     { "volatile",       TOK_VOLATILE,   TT_C89 | TT_C99 | TT_CC65  },
139     { "while",          TOK_WHILE,      TT_C89 | TT_C99 | TT_CC65  },
140 };
141 #define KEY_COUNT       (sizeof (Keywords) / sizeof (Keywords [0]))
142
143
144
145 /* Stuff for determining the type of an integer constant */
146 #define IT_INT          0x01
147 #define IT_UINT         0x02
148 #define IT_LONG         0x04
149 #define IT_ULONG        0x08
150
151
152
153 /*****************************************************************************/
154 /*                                   code                                    */
155 /*****************************************************************************/
156
157
158
159 static int CmpKey (const void* Key, const void* Elem)
160 /* Compare function for bsearch */
161 {
162     return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
163 }
164
165
166
167 static token_t FindKey (const char* Key)
168 /* Find a keyword and return the token. Return IDENT if the token is not a
169 ** keyword.
170 */
171 {
172     struct Keyword* K;
173     K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
174     if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
175         return K->Tok;
176     } else {
177         return TOK_IDENT;
178     }
179 }
180
181
182
183 static int SkipWhite (void)
184 /* Skip white space in the input stream, reading and preprocessing new lines
185 ** if necessary. Return 0 if end of file is reached, return 1 otherwise.
186 */
187 {
188     while (1) {
189         while (CurC == '\0') {
190             if (NextLine () == 0) {
191                 return 0;
192             }
193             Preprocess ();
194         }
195         if (IsSpace (CurC)) {
196             NextChar ();
197         } else {
198             return 1;
199         }
200     }
201 }
202
203
204
205 int TokIsFuncSpec (const Token* T)
206 /* Return true if the token is a function specifier */
207 {
208     return (T->Tok == TOK_INLINE)   ||
209            (T->Tok == TOK_FASTCALL) || (T->Tok == TOK_CDECL) ||
210            (T->Tok == TOK_NEAR)     || (T->Tok == TOK_FAR);
211 }
212
213
214
215 void SymName (char* S)
216 /* Read a symbol from the input stream. The first character must have been
217 ** checked before calling this function. The buffer is expected to be at
218 ** least of size MAX_IDENTLEN+1.
219 */
220 {
221     unsigned Len = 0;
222     do {
223         if (Len < MAX_IDENTLEN) {
224             ++Len;
225             *S++ = CurC;
226         }
227         NextChar ();
228     } while (IsIdent (CurC) || IsDigit (CurC));
229     *S = '\0';
230 }
231
232
233
234 int IsSym (char* S)
235 /* If a symbol follows, read it and return 1, otherwise return 0 */
236 {
237     if (IsIdent (CurC)) {
238         SymName (S);
239         return 1;
240     } else {
241         return 0;
242     }
243 }
244
245
246
247 static void UnknownChar (char C)
248 /* Error message for unknown character */
249 {
250     Error ("Invalid input character with code %02X", C & 0xFF);
251     NextChar ();                        /* Skip */
252 }
253
254
255
256 static void SetTok (int tok)
257 /* Set NextTok.Tok and bump line ptr */
258 {
259     NextTok.Tok = tok;
260     NextChar ();
261 }
262
263
264
265 static int ParseChar (void)
266 /* Parse a character. Converts escape chars into character codes. */
267 {
268     int C;
269     int HadError;
270
271     /* Check for escape chars */
272     if (CurC == '\\') {
273         NextChar ();
274         switch (CurC) {
275             case '?':
276                 C = '\?';
277                 break;
278             case 'a':
279                 C = '\a';
280                 break;
281             case 'b':
282                 C = '\b';
283                 break;
284             case 'f':
285                 C = '\f';
286                 break;
287             case 'r':
288                 C = '\r';
289                 break;
290             case 'n':
291                 C = '\n';
292                 break;
293             case 't':
294                 C = '\t';
295                 break;
296             case 'v':
297                 C = '\v';
298                 break;
299             case '\"':
300                 C = '\"';
301                 break;
302             case '\'':
303                 C = '\'';
304                 break;
305             case '\\':
306                 C = '\\';
307                 break;
308             case 'x':
309             case 'X':
310                 /* Hex character constant */
311                 if (!IsXDigit (NextC)) {
312                     Error ("\\x used with no following hex digits");
313                     C = ' ';
314                 } else {
315                     HadError = 0;
316                     C = 0;
317                     while (IsXDigit (NextC)) {
318                         if ((C << 4) >= 256) {
319                             if (!HadError) {
320                                 Error ("Hex character constant out of range");
321                                 HadError = 1;
322                             }
323                         } else {
324                             C = (C << 4) | HexVal (NextC);
325                         }
326                         NextChar ();
327                     }
328                 }
329                 break;
330             case '0':
331             case '1':
332             case '2':
333             case '3':
334             case '4':
335             case '5':
336             case '6':
337             case '7':
338                 /* Octal constant */
339                 HadError = 0;
340                 C = HexVal (CurC);
341                 while (IsODigit (NextC)) {
342                     if ((C << 3) >= 256) {
343                         if (!HadError) {
344                             Error ("Octal character constant out of range");
345                             HadError = 1;
346                         }
347                     } else {
348                         C = (C << 3) | HexVal (NextC);
349                     }
350                     NextChar ();
351                 }
352                 break;
353             default:
354                 Error ("Illegal character constant");
355                 C = ' ';
356                 /* Try to do error recovery, otherwise the compiler will spit
357                 ** out thousands of errors in this place and abort.
358                 */
359                 if (CurC != '\'' && CurC != '\0') {
360                     while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
361                         NextChar ();
362                     }
363                 }
364                 break;
365         }
366     } else {
367         C = CurC;
368     }
369
370     /* Skip the character read */
371     NextChar ();
372
373     /* Do correct sign extension */
374     return SignExtendChar (C);
375 }
376
377
378
379 static void CharConst (void)
380 /* Parse a character constant. */
381 {
382     int C;
383
384     /* Skip the quote */
385     NextChar ();
386
387     /* Get character */
388     C = ParseChar ();
389
390     /* Check for closing quote */
391     if (CurC != '\'') {
392         Error ("`\'' expected");
393     } else {
394         /* Skip the quote */
395         NextChar ();
396     }
397
398     /* Setup values and attributes */
399     NextTok.Tok  = TOK_CCONST;
400
401     /* Translate into target charset */
402     NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
403
404     /* Character constants have type int */
405     NextTok.Type = type_int;
406 }
407
408
409
410 static void StringConst (void)
411 /* Parse a quoted string */
412 {
413     /* String buffer */
414     StrBuf S = AUTO_STRBUF_INITIALIZER;
415
416     /* Assume next token is a string constant */
417     NextTok.Tok  = TOK_SCONST;
418
419     /* Concatenate strings. If at least one of the concenated strings is a wide
420     ** character literal, the whole string is a wide char literal, otherwise
421     ** it's a normal string literal.
422     */
423     while (1) {
424
425         /* Check if this is a normal or a wide char string */
426         if (CurC == 'L' && NextC == '\"') {
427             /* Wide character literal */
428             NextTok.Tok = TOK_WCSCONST;
429             NextChar ();
430             NextChar ();
431         } else if (CurC == '\"') {
432             /* Skip the quote char */
433             NextChar ();
434         } else {
435             /* No string */
436             break;
437         }
438
439         /* Read until end of string */
440         while (CurC != '\"') {
441             if (CurC == '\0') {
442                 Error ("Unexpected newline");
443                 break;
444             }
445             SB_AppendChar (&S, ParseChar ());
446         }
447
448         /* Skip closing quote char if there was one */
449         NextChar ();
450
451         /* Skip white space, read new input */
452         SkipWhite ();
453
454     }
455
456     /* Terminate the string */
457     SB_AppendChar (&S, '\0');
458
459     /* Add the whole string to the literal pool */
460     NextTok.SVal = AddLiteralStr (&S);
461
462     /* Free the buffer */
463     SB_Done (&S);
464 }
465
466
467
468 static void NumericConst (void)
469 /* Parse a numeric constant */
470 {
471     unsigned Base;              /* Temporary number base */
472     unsigned Prefix;            /* Base according to prefix */
473     StrBuf   S = STATIC_STRBUF_INITIALIZER;
474     int      IsFloat;
475     char     C;
476     unsigned DigitVal;
477     unsigned long IVal;         /* Value */
478
479     /* Check for a leading hex or octal prefix and determine the possible
480     ** integer types.
481     */
482     if (CurC == '0') {
483         /* Gobble 0 and examine next char */
484         NextChar ();
485         if (toupper (CurC) == 'X') {
486             Base = Prefix = 16;
487             NextChar ();        /* gobble "x" */
488         } else {
489             Base = 10;          /* Assume 10 for now - see below */
490             Prefix = 8;         /* Actual prefix says octal */
491         }
492     } else {
493         Base  = Prefix = 10;
494     }
495
496     /* Because floating point numbers don't have octal prefixes (a number
497     ** with a leading zero is decimal), we first have to read the number
498     ** before converting it, so we can determine if it's a float or an
499     ** integer.
500     */
501     while (IsXDigit (CurC) && HexVal (CurC) < Base) {
502         SB_AppendChar (&S, CurC);
503         NextChar ();
504     }
505     SB_Terminate (&S);
506
507     /* The following character tells us if we have an integer or floating
508     ** point constant. Note: Hexadecimal floating point constants aren't
509     ** supported in C89.
510     */
511     IsFloat = (CurC == '.' ||
512                (Base == 10 && toupper (CurC) == 'E') ||
513                (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
514
515     /* If we don't have a floating point type, an octal prefix results in an
516     ** octal base.
517     */
518     if (!IsFloat && Prefix == 8) {
519         Base = 8;
520     }
521
522     /* Since we do now know the correct base, convert the remembered input
523     ** into a number.
524     */
525     SB_Reset (&S);
526     IVal = 0;
527     while ((C = SB_Get (&S)) != '\0') {
528         DigitVal = HexVal (C);
529         if (DigitVal >= Base) {
530             Error ("Numeric constant contains digits beyond the radix");
531         }
532         IVal = (IVal * Base) + DigitVal;
533     }
534
535     /* We don't need the string buffer any longer */
536     SB_Done (&S);
537
538     /* Distinguish between integer and floating point constants */
539     if (!IsFloat) {
540
541         unsigned Types;
542         int      HaveSuffix;
543
544         /* Check for a suffix and determine the possible types */
545         HaveSuffix = 1;
546         if (toupper (CurC) == 'U') {
547             /* Unsigned type */
548             NextChar ();
549             if (toupper (CurC) != 'L') {
550                 Types = IT_UINT | IT_ULONG;
551             } else {
552                 NextChar ();
553                 Types = IT_ULONG;
554             }
555         } else if (toupper (CurC) == 'L') {
556             /* Long type */
557             NextChar ();
558             if (toupper (CurC) != 'U') {
559                 Types = IT_LONG | IT_ULONG;
560             } else {
561                 NextChar ();
562                 Types = IT_ULONG;
563             }
564         } else {
565             HaveSuffix = 0;
566             if (Prefix == 10) {
567                 /* Decimal constants are of any type but uint */
568                 Types = IT_INT | IT_LONG | IT_ULONG;
569             } else {
570                 /* Octal or hex constants are of any type */
571                 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
572             }
573         }
574
575         /* Check the range to determine the type */
576         if (IVal > 0x7FFF) {
577             /* Out of range for int */
578             Types &= ~IT_INT;
579             /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
580             ** allowed, and we don't have a type specifying suffix, emit a
581             ** warning, because the constant is of type long.
582             */
583             if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
584                 Warning ("Constant is long");
585             }
586         }
587         if (IVal > 0xFFFF) {
588             /* Out of range for unsigned int */
589             Types &= ~IT_UINT;
590         }
591         if (IVal > 0x7FFFFFFF) {
592             /* Out of range for long int */
593             Types &= ~IT_LONG;
594         }
595
596         /* Now set the type string to the smallest type in types */
597         if (Types & IT_INT) {
598             NextTok.Type = type_int;
599         } else if (Types & IT_UINT) {
600             NextTok.Type = type_uint;
601         } else if (Types & IT_LONG) {
602             NextTok.Type = type_long;
603         } else {
604             NextTok.Type = type_ulong;
605         }
606
607         /* Set the value and the token */
608         NextTok.IVal = IVal;
609         NextTok.Tok  = TOK_ICONST;
610
611     } else {
612
613         /* Float constant */
614         Double FVal = FP_D_FromInt (IVal);      /* Convert to double */
615
616         /* Check for a fractional part and read it */
617         if (CurC == '.') {
618
619             Double Scale;
620
621             /* Skip the dot */
622             NextChar ();
623
624             /* Read fractional digits */
625             Scale  = FP_D_Make (1.0);
626             while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
627                 /* Get the value of this digit */
628                 Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
629                 /* Add it to the float value */
630                 FVal = FP_D_Add (FVal, FracVal);
631                 /* Scale base */
632                 Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
633                 /* Skip the digit */
634                 NextChar ();
635             }
636         }
637
638         /* Check for an exponent and read it */
639         if ((Base == 16 && toupper (CurC) == 'F') ||
640             (Base == 10 && toupper (CurC) == 'E')) {
641
642             unsigned Digits;
643             unsigned Exp;
644
645             /* Skip the exponent notifier */
646             NextChar ();
647
648             /* Read an optional sign */
649             if (CurC == '-') {
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.SVal = 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.SVal = 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 }