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 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 { "__fastcall__", TOK_FASTCALL, TT_C },
56 { "asm", TOK_ASM, TT_EXT },
57 { "auto", TOK_AUTO, TT_C },
58 { "break", TOK_BREAK, TT_C },
59 { "case", TOK_CASE, TT_C },
60 { "char", TOK_CHAR, TT_C },
61 { "const", TOK_CONST, TT_C },
62 { "continue", TOK_CONTINUE, TT_C },
63 { "default", TOK_DEFAULT, TT_C },
64 { "do", TOK_DO, TT_C },
65 { "double", TOK_DOUBLE, TT_C },
66 { "else", TOK_ELSE, TT_C },
67 { "enum", TOK_ENUM, TT_C },
68 { "extern", TOK_EXTERN, TT_C },
69 { "fastcall", TOK_FASTCALL, TT_EXT },
70 { "float", TOK_FLOAT, TT_C },
71 { "for", TOK_FOR, TT_C },
72 { "goto", TOK_GOTO, TT_C },
73 { "if", TOK_IF, TT_C },
74 { "int", TOK_INT, TT_C },
75 { "long", TOK_LONG, TT_C },
76 { "register", TOK_REGISTER, TT_C },
77 { "return", TOK_RETURN, TT_C },
78 { "short", TOK_SHORT, TT_C },
79 { "signed", TOK_SIGNED, TT_C },
80 { "sizeof", TOK_SIZEOF, TT_C },
81 { "static", TOK_STATIC, TT_C },
82 { "struct", TOK_STRUCT, TT_C },
83 { "switch", TOK_SWITCH, TT_C },
84 { "typedef", TOK_TYPEDEF, TT_C },
85 { "union", TOK_UNION, TT_C },
86 { "unsigned", TOK_UNSIGNED, TT_C },
87 { "void", TOK_VOID, TT_C },
88 { "volatile", TOK_VOLATILE, TT_C },
89 { "while", TOK_WHILE, TT_C },
91 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
95 /* Stuff for determining the type of an integer constant */
103 /*****************************************************************************/
105 /*****************************************************************************/
109 static int CmpKey (const void* Key, const void* Elem)
110 /* Compare function for bsearch */
112 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
117 static int FindKey (char* Key)
118 /* Find a keyword and return the token. Return IDENT if the token is not a
123 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
124 if (K && (K->Type != TT_EXT || ANSI == 0)) {
133 static int skipwhite (void)
134 /* Skip white space in the input stream, reading and preprocessing new lines
135 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
140 if (NextLine () == 0) {
145 if (*lptr == ' ' || *lptr == '\r') {
155 void symname (char *s)
156 /* Get symbol from input stream */
160 if (k != MAX_IDENTLEN) {
165 } while (IsIdent (*lptr) || isdigit (*lptr));
172 /* Get symbol from input stream or return 0 if not a symbol. */
174 if (IsIdent (*lptr)) {
184 static void unknown (unsigned char c)
185 /* Error message for unknown character */
187 Error (ERR_INVALID_CHAR, c);
193 static unsigned hexval (int c)
194 /* Convert a hex digit into a value */
197 Error (ERR_ILLEGAL_HEX_DIGIT);
202 return toupper (c) - 'A' + 10;
208 static void SetTok (int tok)
209 /* set nxttok and bump line ptr */
217 static int SignExtendChar (int C)
218 /* Do correct sign extension of a character */
220 if (SignedChars && (C & 0x80) != 0) {
229 static int parsechar (int c)
230 /* Parse a character. Converts \n into EOL, etc. */
235 /* Check for escape chars */
237 switch (c = gch ()) {
264 /* Hex character constant */
265 val = hexval (gch ()) << 4;
266 c = val | hexval (gch ()); /* Do not translate */
273 while ((c = *lptr) >= '0' && c <= '7' && i++ < 4) {
274 val = (val << 3) | (c - '0');
277 c = val; /* Do not translate */
280 Error (ERR_ILLEGAL_CHARCONST);
284 /* Do correct sign extension */
285 return SignExtendChar (c);
290 static void CharConst (void)
291 /* Parse a character constant. */
299 c = parsechar (cgch ());
301 /* Check for closing quote */
302 if (cgch () != '\'') {
303 Error (ERR_QUOTE_EXPECTED);
306 /* Setup values and attributes */
308 nxtval = SignExtendChar (ctrans (c)); /* Translate into target charset */
309 nxttype = type_int; /* Character constants have type int */
314 static void StringConst (void)
315 /* Parse a quoted string */
317 nxtval = GetLiteralOffs ();
320 /* Be sure to concatenate strings */
321 while (*lptr == '\"') {
323 /* Skip the quote char */
326 while (*lptr != '\"') {
328 Error (ERR_UNEXPECTED_NEWLINE);
331 AddLiteralChar (parsechar (gch()));
334 /* Skip closing quote char if there was one */
337 /* Skip white space, read new input */
342 /* Terminate the string */
343 AddLiteralChar ('\0');
348 void NextToken (void)
349 /* Get next token from input stream */
354 /* Current token is the lookahead token */
357 /* Remember the starting position of the next token */
358 NextTok.Pos = GetCurrentLine();
360 /* Skip spaces and read the next line if needed */
361 if (skipwhite () == 0) {
362 /* End of file reached */
367 /* Determine the next token from the lookahead */
372 int HaveSuffix; /* True if we have a type suffix */
373 unsigned types; /* Possible types */
375 unsigned long k; /* Value */
379 types = IT_INT | IT_LONG | IT_ULONG;
382 /* Octal or hex constants may also be of type unsigned int */
383 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
384 /* gobble 0 and examin next char */
385 if (toupper (*++lptr) == 'X') {
388 ++lptr; /* gobble "x" */
396 k = k * base + (c - '0');
397 } else if (base == 16 && isxdigit (c)) {
398 k = (k << 4) + hexval (c);
400 break; /* not digit */
402 ++lptr; /* gobble char */
405 /* Check for a suffix */
411 if (toupper (*lptr) != 'L') {
412 types = IT_UINT | IT_ULONG;
417 } else if (c == 'L') {
420 if (toupper (*lptr) != 'U') {
421 types = IT_LONG | IT_ULONG;
430 /* Check the range to determine the type */
432 /* Out of range for int */
434 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
435 * allowed, and we don't have a type specifying suffix, emit a
438 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
439 Warning (WARN_CONSTANT_IS_LONG);
443 /* Out of range for unsigned int */
446 if (k > 0x7FFFFFFF) {
447 /* Out of range for long int */
451 /* Now set the type string to the smallest type in types */
452 if (types & IT_INT) {
454 } else if (types & IT_UINT) {
456 } else if (types & IT_LONG) {
459 nxttype = type_ulong;
462 /* Set the value and the token */
470 /* Check for a keyword */
471 if ((nxttok = FindKey (token)) != TOK_IDENT) {
472 /* Reserved word found */
475 /* No reserved word, check for special symbols */
476 if (token [0] == '_') {
477 /* Special symbols */
478 if (strcmp (token, "__FILE__") == 0) {
479 nxtval = AddLiteral (GetCurrentFile());
482 } else if (strcmp (token, "__LINE__") == 0) {
484 nxtval = GetCurrentLine();
487 } else if (strcmp (token, "__fixargs__") == 0) {
489 nxtval = GetParamSize (CurrentFunc);
492 } else if (strcmp (token, "__func__") == 0) {
493 /* __func__ is only defined in functions */
495 nxtval = AddLiteral (GetFuncName (CurrentFunc));
502 /* No reserved word but identifier */
503 strcpy (NextTok.Ident, token);
504 NextTok.Tok = TOK_IDENT;
508 /* Monstrous switch statement ahead... */
512 if (*++lptr == '=') {
515 nxttok = TOK_BOOL_NOT;
524 if (*++lptr == '=') {
525 SetTok (TOK_MOD_ASSIGN);
534 SetTok (TOK_BOOL_AND);
537 SetTok (TOK_AND_ASSIGN);
557 if (*++lptr == '=') {
558 SetTok (TOK_MUL_ASSIGN);
570 SetTok (TOK_PLUS_ASSIGN);
587 SetTok (TOK_MINUS_ASSIGN);
590 SetTok (TOK_PTR_REF);
598 if (*++lptr == '.') {
599 if (*++lptr == '.') {
600 SetTok (TOK_ELLIPSIS);
610 if (*++lptr == '=') {
611 SetTok (TOK_DIV_ASSIGN);
631 if (*++lptr == '=') {
632 SetTok (TOK_SHL_ASSIGN);
643 if (*++lptr == '=') {
656 if (*++lptr == '=') {
657 SetTok (TOK_SHR_ASSIGN);
680 if (*++lptr == '=') {
681 SetTok (TOK_XOR_ASSIGN);
694 SetTok (TOK_BOOL_OR);
697 SetTok (TOK_OR_ASSIGN);
713 while (*++lptr == ' ') ; /* Skip it and following whitespace */
714 if (!issym (token) || strcmp (token, "pragma") != 0) {
715 /* OOPS - should not happen */
716 Error (ERR_CPP_DIRECTIVE_EXPECTED);
730 void Consume (token_t Token, unsigned ErrNum)
731 /* Eat token if it is the next in the input stream, otherwise print an error
735 if (curtok == Token) {
744 void ConsumeColon (void)
745 /* Check for a colon and skip it. */
747 Consume (TOK_COLON, ERR_COLON_EXPECTED);
752 void ConsumeSemi (void)
753 /* Check for a semicolon and skip it. */
755 /* Try do be smart about typos... */
756 if (curtok == TOK_SEMI) {
759 Error (ERR_SEMICOLON_EXPECTED);
760 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
768 void ConsumeLParen (void)
769 /* Check for a left parenthesis and skip it */
771 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
776 void ConsumeRParen (void)
777 /* Check for a right parenthesis and skip it */
779 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
784 void ConsumeLBrack (void)
785 /* Check for a left bracket and skip it */
787 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
792 void ConsumeRBrack (void)
793 /* Check for a right bracket and skip it */
795 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
800 void ConsumeLCurly (void)
801 /* Check for a left curly brace and skip it */
803 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
808 void ConsumeRCurly (void)
809 /* Check for a right curly brace and skip it */
811 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);