4 * Ullrich von Bassewitz, 07.06.1998
30 /*****************************************************************************/
32 /*****************************************************************************/
36 Token CurTok; /* The current token */
37 Token NextTok; /* The next token */
42 #define TT_C 0 /* ANSI C token */
43 #define TT_EXT 1 /* cc65 extension */
46 static const struct Keyword {
47 char* Key; /* Keyword name */
48 unsigned char Tok; /* The token */
49 unsigned char Type; /* Token type */
51 { "__AX__", TOK_AX, TT_C },
52 { "__EAX__", TOK_EAX, TT_C },
53 { "__asm__", TOK_ASM, TT_C },
54 { "__attribute__", TOK_ATTRIBUTE, TT_C },
55 { "__far__", TOK_FAR, TT_C },
56 { "__fastcall__", TOK_FASTCALL, TT_C },
57 { "asm", TOK_ASM, TT_EXT },
58 { "auto", TOK_AUTO, TT_C },
59 { "break", TOK_BREAK, TT_C },
60 { "case", TOK_CASE, TT_C },
61 { "char", TOK_CHAR, TT_C },
62 { "const", TOK_CONST, TT_C },
63 { "continue", TOK_CONTINUE, TT_C },
64 { "default", TOK_DEFAULT, TT_C },
65 { "do", TOK_DO, TT_C },
66 { "double", TOK_DOUBLE, TT_C },
67 { "else", TOK_ELSE, TT_C },
68 { "enum", TOK_ENUM, TT_C },
69 { "extern", TOK_EXTERN, TT_C },
70 { "far", TOK_FAR, TT_EXT },
71 { "fastcall", TOK_FASTCALL, TT_EXT },
72 { "float", TOK_FLOAT, TT_C },
73 { "for", TOK_FOR, TT_C },
74 { "goto", TOK_GOTO, TT_C },
75 { "if", TOK_IF, TT_C },
76 { "int", TOK_INT, TT_C },
77 { "long", TOK_LONG, TT_C },
78 { "register", TOK_REGISTER, TT_C },
79 { "return", TOK_RETURN, TT_C },
80 { "short", TOK_SHORT, TT_C },
81 { "signed", TOK_SIGNED, TT_C },
82 { "sizeof", TOK_SIZEOF, TT_C },
83 { "static", TOK_STATIC, TT_C },
84 { "struct", TOK_STRUCT, TT_C },
85 { "switch", TOK_SWITCH, TT_C },
86 { "typedef", TOK_TYPEDEF, TT_C },
87 { "union", TOK_UNION, TT_C },
88 { "unsigned", TOK_UNSIGNED, TT_C },
89 { "void", TOK_VOID, TT_C },
90 { "volatile", TOK_VOLATILE, TT_C },
91 { "while", TOK_WHILE, TT_C },
93 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
97 /* Stuff for determining the type of an integer constant */
101 #define IT_ULONG 0x08
105 /*****************************************************************************/
107 /*****************************************************************************/
111 static int CmpKey (const void* Key, const void* Elem)
112 /* Compare function for bsearch */
114 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
119 static int FindKey (const char* Key)
120 /* Find a keyword and return the token. Return IDENT if the token is not a
125 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
126 if (K && (K->Type != TT_EXT || ANSI == 0)) {
135 static int SkipWhite (void)
136 /* Skip white space in the input stream, reading and preprocessing new lines
137 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
142 if (NextLine () == 0) {
147 if (CurC == ' ' || CurC == '\r') {
157 void SymName (char* s)
158 /* Get symbol from input stream */
162 if (k != MAX_IDENTLEN) {
167 } while (IsIdent (CurC) || isdigit (CurC));
174 /* Get symbol from input stream or return 0 if not a symbol. */
176 if (IsIdent (CurC)) {
186 static void unknown (char C)
187 /* Error message for unknown character */
189 Error (ERR_INVALID_CHAR, C);
190 NextChar (); /* Skip */
195 static unsigned hexval (int c)
196 /* Convert a hex digit into a value */
199 Error (ERR_ILLEGAL_HEX_DIGIT);
204 return toupper (c) - 'A' + 10;
210 static void SetTok (int tok)
211 /* set nxttok and bump line ptr */
219 static int SignExtendChar (int C)
220 /* Do correct sign extension of a character */
222 if (SignedChars && (C & 0x80) != 0) {
231 static int ParseChar (void)
232 /* Parse a character. Converts \n into EOL, etc. */
238 /* Check for escape chars */
268 /* Hex character constant */
270 val = hexval (CurC) << 4;
272 C = val | hexval (CurC); /* Do not translate */
279 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
281 C = (C << 3) | (CurC - '0');
285 Error (ERR_ILLEGAL_CHARCONST);
293 /* Skip the character read */
296 /* Do correct sign extension */
297 return SignExtendChar (C);
302 static void CharConst (void)
303 /* Parse a character constant. */
313 /* Check for closing quote */
315 Error (ERR_QUOTE_EXPECTED);
321 /* Setup values and attributes */
323 nxtval = SignExtendChar (ctrans (C)); /* Translate into target charset */
324 nxttype = type_int; /* Character constants have type int */
329 static void StringConst (void)
330 /* Parse a quoted string */
332 nxtval = GetLiteralOffs ();
335 /* Be sure to concatenate strings */
336 while (CurC == '\"') {
338 /* Skip the quote char */
341 while (CurC != '\"') {
343 Error (ERR_UNEXPECTED_NEWLINE);
346 AddLiteralChar (ParseChar ());
349 /* Skip closing quote char if there was one */
352 /* Skip white space, read new input */
357 /* Terminate the string */
358 AddLiteralChar ('\0');
363 void NextToken (void)
364 /* Get next token from input stream */
368 /* Current token is the lookahead token */
371 /* Remember the starting position of the next token */
372 NextTok.Pos = GetCurrentLine();
374 /* Skip spaces and read the next line if needed */
375 if (SkipWhite () == 0) {
376 /* End of file reached */
381 /* Determine the next token from the lookahead */
382 if (isdigit (CurC)) {
385 int HaveSuffix; /* True if we have a type suffix */
386 unsigned types; /* Possible types */
388 unsigned long k; /* Value */
392 types = IT_INT | IT_LONG | IT_ULONG;
395 /* Octal or hex constants may also be of type unsigned int */
396 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
397 /* gobble 0 and examin next char */
399 if (toupper (CurC) == 'X') {
402 NextChar (); /* gobble "x" */
408 if (isdigit (CurC)) {
409 k = k * base + (CurC - '0');
410 } else if (base == 16 && isxdigit (CurC)) {
411 k = (k << 4) + hexval (CurC);
413 break; /* not digit */
415 NextChar (); /* gobble char */
418 /* Check for a suffix */
420 if (CurC == 'u' || CurC == 'U') {
423 if (toupper (CurC) != 'L') {
424 types = IT_UINT | IT_ULONG;
429 } else if (CurC == 'l' || CurC == 'L') {
432 if (toupper (CurC) != 'U') {
433 types = IT_LONG | IT_ULONG;
442 /* Check the range to determine the type */
444 /* Out of range for int */
446 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
447 * allowed, and we don't have a type specifying suffix, emit a
450 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
451 Warning (WARN_CONSTANT_IS_LONG);
455 /* Out of range for unsigned int */
458 if (k > 0x7FFFFFFF) {
459 /* Out of range for long int */
463 /* Now set the type string to the smallest type in types */
464 if (types & IT_INT) {
466 } else if (types & IT_UINT) {
468 } else if (types & IT_LONG) {
471 nxttype = type_ulong;
474 /* Set the value and the token */
482 /* Check for a keyword */
483 if ((nxttok = FindKey (token)) != TOK_IDENT) {
484 /* Reserved word found */
487 /* No reserved word, check for special symbols */
488 if (token [0] == '_') {
489 /* Special symbols */
490 if (strcmp (token, "__FILE__") == 0) {
491 nxtval = AddLiteral (GetCurrentFile());
494 } else if (strcmp (token, "__LINE__") == 0) {
496 nxtval = GetCurrentLine();
499 } else if (strcmp (token, "__fixargs__") == 0) {
501 nxtval = GetParamSize (CurrentFunc);
504 } else if (strcmp (token, "__func__") == 0) {
505 /* __func__ is only defined in functions */
507 nxtval = AddLiteral (GetFuncName (CurrentFunc));
514 /* No reserved word but identifier */
515 strcpy (NextTok.Ident, token);
516 NextTok.Tok = TOK_IDENT;
520 /* Monstrous switch statement ahead... */
528 nxttok = TOK_BOOL_NOT;
539 SetTok (TOK_MOD_ASSIGN);
549 SetTok (TOK_BOOL_AND);
552 SetTok (TOK_AND_ASSIGN);
574 SetTok (TOK_MUL_ASSIGN);
587 SetTok (TOK_PLUS_ASSIGN);
605 SetTok (TOK_MINUS_ASSIGN);
608 SetTok (TOK_PTR_REF);
620 SetTok (TOK_ELLIPSIS);
632 SetTok (TOK_DIV_ASSIGN);
655 SetTok (TOK_SHL_ASSIGN);
683 SetTok (TOK_SHR_ASSIGN);
708 SetTok (TOK_XOR_ASSIGN);
722 SetTok (TOK_BOOL_OR);
725 SetTok (TOK_OR_ASSIGN);
741 /* Skip it and following whitespace */
744 } while (CurC == ' ');
745 if (!IsSym (token) || strcmp (token, "pragma") != 0) {
746 /* OOPS - should not happen */
747 Error (ERR_CPP_DIRECTIVE_EXPECTED);
761 void Consume (token_t Token, unsigned ErrNum)
762 /* Eat token if it is the next in the input stream, otherwise print an error
766 if (curtok == Token) {
775 void ConsumeColon (void)
776 /* Check for a colon and skip it. */
778 Consume (TOK_COLON, ERR_COLON_EXPECTED);
783 void ConsumeSemi (void)
784 /* Check for a semicolon and skip it. */
786 /* Try do be smart about typos... */
787 if (curtok == TOK_SEMI) {
790 Error (ERR_SEMICOLON_EXPECTED);
791 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
799 void ConsumeLParen (void)
800 /* Check for a left parenthesis and skip it */
802 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
807 void ConsumeRParen (void)
808 /* Check for a right parenthesis and skip it */
810 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
815 void ConsumeLBrack (void)
816 /* Check for a left bracket and skip it */
818 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
823 void ConsumeRBrack (void)
824 /* Check for a right bracket and skip it */
826 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
831 void ConsumeLCurly (void)
832 /* Check for a left curly brace and skip it */
834 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
839 void ConsumeRCurly (void)
840 /* Check for a right curly brace and skip it */
842 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);