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 { "__A__", TOK_A, TT_C },
52 { "__AX__", TOK_AX, TT_C },
53 { "__EAX__", TOK_EAX, TT_C },
54 { "__X__", TOK_X, TT_C },
55 { "__Y__", TOK_Y, TT_C },
56 { "__asm__", TOK_ASM, TT_C },
57 { "__attribute__", TOK_ATTRIBUTE, TT_C },
58 { "__far__", TOK_FAR, TT_C },
59 { "__fastcall__", TOK_FASTCALL, TT_C },
60 { "asm", TOK_ASM, TT_EXT },
61 { "auto", TOK_AUTO, TT_C },
62 { "break", TOK_BREAK, TT_C },
63 { "case", TOK_CASE, TT_C },
64 { "char", TOK_CHAR, TT_C },
65 { "const", TOK_CONST, TT_C },
66 { "continue", TOK_CONTINUE, TT_C },
67 { "default", TOK_DEFAULT, TT_C },
68 { "do", TOK_DO, TT_C },
69 { "double", TOK_DOUBLE, TT_C },
70 { "else", TOK_ELSE, TT_C },
71 { "enum", TOK_ENUM, TT_C },
72 { "extern", TOK_EXTERN, TT_C },
73 { "far", TOK_FAR, TT_EXT },
74 { "fastcall", TOK_FASTCALL, TT_EXT },
75 { "float", TOK_FLOAT, TT_C },
76 { "for", TOK_FOR, TT_C },
77 { "goto", TOK_GOTO, TT_C },
78 { "if", TOK_IF, TT_C },
79 { "int", TOK_INT, TT_C },
80 { "long", TOK_LONG, TT_C },
81 { "register", TOK_REGISTER, TT_C },
82 { "return", TOK_RETURN, TT_C },
83 { "short", TOK_SHORT, TT_C },
84 { "signed", TOK_SIGNED, TT_C },
85 { "sizeof", TOK_SIZEOF, TT_C },
86 { "static", TOK_STATIC, TT_C },
87 { "struct", TOK_STRUCT, TT_C },
88 { "switch", TOK_SWITCH, TT_C },
89 { "typedef", TOK_TYPEDEF, TT_C },
90 { "union", TOK_UNION, TT_C },
91 { "unsigned", TOK_UNSIGNED, TT_C },
92 { "void", TOK_VOID, TT_C },
93 { "volatile", TOK_VOLATILE, TT_C },
94 { "while", TOK_WHILE, TT_C },
96 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
100 /* Stuff for determining the type of an integer constant */
104 #define IT_ULONG 0x08
108 /*****************************************************************************/
110 /*****************************************************************************/
114 static int CmpKey (const void* Key, const void* Elem)
115 /* Compare function for bsearch */
117 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
122 static int FindKey (const char* Key)
123 /* Find a keyword and return the token. Return IDENT if the token is not a
128 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
129 if (K && (K->Type != TT_EXT || ANSI == 0)) {
138 static int SkipWhite (void)
139 /* Skip white space in the input stream, reading and preprocessing new lines
140 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
145 if (NextLine () == 0) {
150 if (CurC == ' ' || CurC == '\r') {
160 void SymName (char* s)
161 /* Get symbol from input stream */
165 if (k != MAX_IDENTLEN) {
170 } while (IsIdent (CurC) || isdigit (CurC));
177 /* Get symbol from input stream or return 0 if not a symbol. */
179 if (IsIdent (CurC)) {
189 static void unknown (char C)
190 /* Error message for unknown character */
192 Error (ERR_INVALID_CHAR, C);
193 NextChar (); /* Skip */
198 static unsigned hexval (int c)
199 /* Convert a hex digit into a value */
202 Error (ERR_ILLEGAL_HEX_DIGIT);
207 return toupper (c) - 'A' + 10;
213 static void SetTok (int tok)
214 /* set nxttok and bump line ptr */
222 static int SignExtendChar (int C)
223 /* Do correct sign extension of a character */
225 if (SignedChars && (C & 0x80) != 0) {
234 static int ParseChar (void)
235 /* Parse a character. Converts \n into EOL, etc. */
241 /* Check for escape chars */
271 /* Hex character constant */
273 val = hexval (CurC) << 4;
275 C = val | hexval (CurC); /* Do not translate */
282 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
284 C = (C << 3) | (CurC - '0');
288 Error (ERR_ILLEGAL_CHARCONST);
296 /* Skip the character read */
299 /* Do correct sign extension */
300 return SignExtendChar (C);
305 static void CharConst (void)
306 /* Parse a character constant. */
316 /* Check for closing quote */
318 Error (ERR_QUOTE_EXPECTED);
324 /* Setup values and attributes */
326 nxtval = SignExtendChar (ctrans (C)); /* Translate into target charset */
327 nxttype = type_int; /* Character constants have type int */
332 static void StringConst (void)
333 /* Parse a quoted string */
335 nxtval = GetLiteralOffs ();
338 /* Be sure to concatenate strings */
339 while (CurC == '\"') {
341 /* Skip the quote char */
344 while (CurC != '\"') {
346 Error (ERR_UNEXPECTED_NEWLINE);
349 AddLiteralChar (ParseChar ());
352 /* Skip closing quote char if there was one */
355 /* Skip white space, read new input */
360 /* Terminate the string */
361 AddLiteralChar ('\0');
366 void NextToken (void)
367 /* Get next token from input stream */
371 /* Current token is the lookahead token */
374 /* Remember the starting position of the next token */
375 NextTok.Pos = GetCurrentLine();
377 /* Skip spaces and read the next line if needed */
378 if (SkipWhite () == 0) {
379 /* End of file reached */
384 /* Determine the next token from the lookahead */
385 if (isdigit (CurC)) {
388 int HaveSuffix; /* True if we have a type suffix */
389 unsigned types; /* Possible types */
391 unsigned long k; /* Value */
395 types = IT_INT | IT_LONG | IT_ULONG;
398 /* Octal or hex constants may also be of type unsigned int */
399 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
400 /* gobble 0 and examin next char */
402 if (toupper (CurC) == 'X') {
405 NextChar (); /* gobble "x" */
411 if (isdigit (CurC)) {
412 k = k * base + (CurC - '0');
413 } else if (base == 16 && isxdigit (CurC)) {
414 k = (k << 4) + hexval (CurC);
416 break; /* not digit */
418 NextChar (); /* gobble char */
421 /* Check for a suffix */
423 if (CurC == 'u' || CurC == 'U') {
426 if (toupper (CurC) != 'L') {
427 types = IT_UINT | IT_ULONG;
432 } else if (CurC == 'l' || CurC == 'L') {
435 if (toupper (CurC) != 'U') {
436 types = IT_LONG | IT_ULONG;
445 /* Check the range to determine the type */
447 /* Out of range for int */
449 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
450 * allowed, and we don't have a type specifying suffix, emit a
453 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
454 Warning (WARN_CONSTANT_IS_LONG);
458 /* Out of range for unsigned int */
461 if (k > 0x7FFFFFFF) {
462 /* Out of range for long int */
466 /* Now set the type string to the smallest type in types */
467 if (types & IT_INT) {
469 } else if (types & IT_UINT) {
471 } else if (types & IT_LONG) {
474 nxttype = type_ulong;
477 /* Set the value and the token */
485 /* Check for a keyword */
486 if ((nxttok = FindKey (token)) != TOK_IDENT) {
487 /* Reserved word found */
490 /* No reserved word, check for special symbols */
491 if (token [0] == '_') {
492 /* Special symbols */
493 if (strcmp (token, "__FILE__") == 0) {
494 nxtval = AddLiteral (GetCurrentFile());
497 } else if (strcmp (token, "__LINE__") == 0) {
499 nxtval = GetCurrentLine();
502 } else if (strcmp (token, "__fixargs__") == 0) {
504 nxtval = GetParamSize (CurrentFunc);
507 } else if (strcmp (token, "__func__") == 0) {
508 /* __func__ is only defined in functions */
510 nxtval = AddLiteral (GetFuncName (CurrentFunc));
517 /* No reserved word but identifier */
518 strcpy (NextTok.Ident, token);
519 NextTok.Tok = TOK_IDENT;
523 /* Monstrous switch statement ahead... */
531 nxttok = TOK_BOOL_NOT;
542 SetTok (TOK_MOD_ASSIGN);
552 SetTok (TOK_BOOL_AND);
555 SetTok (TOK_AND_ASSIGN);
577 SetTok (TOK_MUL_ASSIGN);
590 SetTok (TOK_PLUS_ASSIGN);
608 SetTok (TOK_MINUS_ASSIGN);
611 SetTok (TOK_PTR_REF);
623 SetTok (TOK_ELLIPSIS);
635 SetTok (TOK_DIV_ASSIGN);
658 SetTok (TOK_SHL_ASSIGN);
686 SetTok (TOK_SHR_ASSIGN);
711 SetTok (TOK_XOR_ASSIGN);
725 SetTok (TOK_BOOL_OR);
728 SetTok (TOK_OR_ASSIGN);
744 /* Skip it and following whitespace */
747 } while (CurC == ' ');
748 if (!IsSym (token) || strcmp (token, "pragma") != 0) {
749 /* OOPS - should not happen */
750 Error (ERR_CPP_DIRECTIVE_EXPECTED);
764 void Consume (token_t Token, unsigned ErrNum)
765 /* Eat token if it is the next in the input stream, otherwise print an error
769 if (curtok == Token) {
778 void ConsumeColon (void)
779 /* Check for a colon and skip it. */
781 Consume (TOK_COLON, ERR_COLON_EXPECTED);
786 void ConsumeSemi (void)
787 /* Check for a semicolon and skip it. */
789 /* Try do be smart about typos... */
790 if (curtok == TOK_SEMI) {
793 Error (ERR_SEMICOLON_EXPECTED);
794 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
802 void ConsumeLParen (void)
803 /* Check for a left parenthesis and skip it */
805 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
810 void ConsumeRParen (void)
811 /* Check for a right parenthesis and skip it */
813 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
818 void ConsumeLBrack (void)
819 /* Check for a left bracket and skip it */
821 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
826 void ConsumeRBrack (void)
827 /* Check for a right bracket and skip it */
829 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
834 void ConsumeLCurly (void)
835 /* Check for a left curly brace and skip it */
837 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
842 void ConsumeRCurly (void)
843 /* Check for a right curly brace and skip it */
845 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);