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 { "__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 (const 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 (CurC == ' ' || CurC == '\r') {
155 void SymName (char* s)
156 /* Get symbol from input stream */
160 if (k != MAX_IDENTLEN) {
165 } while (IsIdent (CurC) || isdigit (CurC));
172 /* Get symbol from input stream or return 0 if not a symbol. */
174 if (IsIdent (CurC)) {
184 static void unknown (char C)
185 /* Error message for unknown character */
187 Error (ERR_INVALID_CHAR, C);
188 NextChar (); /* Skip */
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 (void)
230 /* Parse a character. Converts \n into EOL, etc. */
236 /* Check for escape chars */
266 /* Hex character constant */
268 val = hexval (CurC) << 4;
270 C = val | hexval (CurC); /* Do not translate */
277 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
279 C = (C << 3) | (CurC - '0');
283 Error (ERR_ILLEGAL_CHARCONST);
291 /* Skip the character read */
294 /* Do correct sign extension */
295 return SignExtendChar (C);
300 static void CharConst (void)
301 /* Parse a character constant. */
311 /* Check for closing quote */
313 Error (ERR_QUOTE_EXPECTED);
319 /* Setup values and attributes */
321 nxtval = SignExtendChar (ctrans (C)); /* Translate into target charset */
322 nxttype = type_int; /* Character constants have type int */
327 static void StringConst (void)
328 /* Parse a quoted string */
330 nxtval = GetLiteralOffs ();
333 /* Be sure to concatenate strings */
334 while (CurC == '\"') {
336 /* Skip the quote char */
339 while (CurC != '\"') {
341 Error (ERR_UNEXPECTED_NEWLINE);
344 AddLiteralChar (ParseChar ());
347 /* Skip closing quote char if there was one */
350 /* Skip white space, read new input */
355 /* Terminate the string */
356 AddLiteralChar ('\0');
361 void NextToken (void)
362 /* Get next token from input stream */
366 /* Current token is the lookahead token */
369 /* Remember the starting position of the next token */
370 NextTok.Pos = GetCurrentLine();
372 /* Skip spaces and read the next line if needed */
373 if (SkipWhite () == 0) {
374 /* End of file reached */
379 /* Determine the next token from the lookahead */
380 if (isdigit (CurC)) {
383 int HaveSuffix; /* True if we have a type suffix */
384 unsigned types; /* Possible types */
386 unsigned long k; /* Value */
390 types = IT_INT | IT_LONG | IT_ULONG;
393 /* Octal or hex constants may also be of type unsigned int */
394 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
395 /* gobble 0 and examin next char */
397 if (toupper (CurC) == 'X') {
400 NextChar (); /* gobble "x" */
406 if (isdigit (CurC)) {
407 k = k * base + (CurC - '0');
408 } else if (base == 16 && isxdigit (CurC)) {
409 k = (k << 4) + hexval (CurC);
411 break; /* not digit */
413 NextChar (); /* gobble char */
416 /* Check for a suffix */
418 if (CurC == 'u' || CurC == 'U') {
421 if (toupper (CurC) != 'L') {
422 types = IT_UINT | IT_ULONG;
427 } else if (CurC == 'l' || CurC == 'L') {
430 if (toupper (CurC) != 'U') {
431 types = IT_LONG | IT_ULONG;
440 /* Check the range to determine the type */
442 /* Out of range for int */
444 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
445 * allowed, and we don't have a type specifying suffix, emit a
448 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
449 Warning (WARN_CONSTANT_IS_LONG);
453 /* Out of range for unsigned int */
456 if (k > 0x7FFFFFFF) {
457 /* Out of range for long int */
461 /* Now set the type string to the smallest type in types */
462 if (types & IT_INT) {
464 } else if (types & IT_UINT) {
466 } else if (types & IT_LONG) {
469 nxttype = type_ulong;
472 /* Set the value and the token */
480 /* Check for a keyword */
481 if ((nxttok = FindKey (token)) != TOK_IDENT) {
482 /* Reserved word found */
485 /* No reserved word, check for special symbols */
486 if (token [0] == '_') {
487 /* Special symbols */
488 if (strcmp (token, "__FILE__") == 0) {
489 nxtval = AddLiteral (GetCurrentFile());
492 } else if (strcmp (token, "__LINE__") == 0) {
494 nxtval = GetCurrentLine();
497 } else if (strcmp (token, "__fixargs__") == 0) {
499 nxtval = GetParamSize (CurrentFunc);
502 } else if (strcmp (token, "__func__") == 0) {
503 /* __func__ is only defined in functions */
505 nxtval = AddLiteral (GetFuncName (CurrentFunc));
512 /* No reserved word but identifier */
513 strcpy (NextTok.Ident, token);
514 NextTok.Tok = TOK_IDENT;
518 /* Monstrous switch statement ahead... */
526 nxttok = TOK_BOOL_NOT;
537 SetTok (TOK_MOD_ASSIGN);
547 SetTok (TOK_BOOL_AND);
550 SetTok (TOK_AND_ASSIGN);
572 SetTok (TOK_MUL_ASSIGN);
585 SetTok (TOK_PLUS_ASSIGN);
603 SetTok (TOK_MINUS_ASSIGN);
606 SetTok (TOK_PTR_REF);
618 SetTok (TOK_ELLIPSIS);
630 SetTok (TOK_DIV_ASSIGN);
653 SetTok (TOK_SHL_ASSIGN);
681 SetTok (TOK_SHR_ASSIGN);
706 SetTok (TOK_XOR_ASSIGN);
720 SetTok (TOK_BOOL_OR);
723 SetTok (TOK_OR_ASSIGN);
739 /* Skip it and following whitespace */
742 } while (CurC == ' ');
743 if (!IsSym (token) || strcmp (token, "pragma") != 0) {
744 /* OOPS - should not happen */
745 Error (ERR_CPP_DIRECTIVE_EXPECTED);
759 void Consume (token_t Token, unsigned ErrNum)
760 /* Eat token if it is the next in the input stream, otherwise print an error
764 if (curtok == Token) {
773 void ConsumeColon (void)
774 /* Check for a colon and skip it. */
776 Consume (TOK_COLON, ERR_COLON_EXPECTED);
781 void ConsumeSemi (void)
782 /* Check for a semicolon and skip it. */
784 /* Try do be smart about typos... */
785 if (curtok == TOK_SEMI) {
788 Error (ERR_SEMICOLON_EXPECTED);
789 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
797 void ConsumeLParen (void)
798 /* Check for a left parenthesis and skip it */
800 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
805 void ConsumeRParen (void)
806 /* Check for a right parenthesis and skip it */
808 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
813 void ConsumeLBrack (void)
814 /* Check for a left bracket and skip it */
816 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
821 void ConsumeRBrack (void)
822 /* Check for a right bracket and skip it */
824 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
829 void ConsumeLCurly (void)
830 /* Check for a left curly brace and skip it */
832 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
837 void ConsumeRCurly (void)
838 /* Check for a right curly brace and skip it */
840 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);