4 * Ullrich von Bassewitz, 07.06.1998
31 /*****************************************************************************/
33 /*****************************************************************************/
37 Token CurTok; /* The current token */
38 Token NextTok; /* The next token */
43 #define TT_C 0 /* ANSI C token */
44 #define TT_EXT 1 /* cc65 extension */
47 static struct Keyword {
48 char* Key; /* Keyword name */
49 unsigned char Tok; /* The token */
50 unsigned char Type; /* Token type */
52 { "__AX__", TOK_AX, TT_C },
53 { "__EAX__", TOK_EAX, TT_C },
54 { "__asm__", TOK_ASM, TT_C },
55 { "__attribute__", TOK_ATTRIBUTE, 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 { "fastcall", TOK_FASTCALL, TT_EXT },
71 { "float", TOK_FLOAT, TT_C },
72 { "for", TOK_FOR, TT_C },
73 { "goto", TOK_GOTO, TT_C },
74 { "if", TOK_IF, TT_C },
75 { "int", TOK_INT, TT_C },
76 { "long", TOK_LONG, TT_C },
77 { "register", TOK_REGISTER, TT_C },
78 { "return", TOK_RETURN, TT_C },
79 { "short", TOK_SHORT, TT_C },
80 { "signed", TOK_SIGNED, TT_C },
81 { "sizeof", TOK_SIZEOF, TT_C },
82 { "static", TOK_STATIC, TT_C },
83 { "struct", TOK_STRUCT, TT_C },
84 { "switch", TOK_SWITCH, TT_C },
85 { "typedef", TOK_TYPEDEF, TT_C },
86 { "union", TOK_UNION, TT_C },
87 { "unsigned", TOK_UNSIGNED, TT_C },
88 { "void", TOK_VOID, TT_C },
89 { "volatile", TOK_VOLATILE, TT_C },
90 { "while", TOK_WHILE, TT_C },
92 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
96 /* Stuff for determining the type of an integer constant */
100 #define IT_ULONG 0x08
104 /*****************************************************************************/
106 /*****************************************************************************/
110 static int CmpKey (const void* Key, const void* Elem)
111 /* Compare function for bsearch */
113 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
118 static int FindKey (char* Key)
119 /* Find a keyword and return the token. Return IDENT if the token is not a
124 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
125 if (K && (K->Type != TT_EXT || ANSI == 0)) {
134 static int skipwhite (void)
135 /* Skip white space in the input stream, reading and preprocessing new lines
136 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
141 if (NextLine () == 0) {
146 if (*lptr == ' ' || *lptr == '\r') {
156 void symname (char *s)
157 /* Get symbol from input stream */
161 if (k != MAX_IDENTLEN) {
166 } while (IsIdent (*lptr) || isdigit (*lptr));
173 /* Get symbol from input stream or return 0 if not a symbol. */
175 if (IsIdent (*lptr)) {
185 static void unknown (unsigned char c)
186 /* Error message for unknown character */
188 Error (ERR_INVALID_CHAR, c);
194 static unsigned hexval (int c)
195 /* Convert a hex digit into a value */
198 Error (ERR_ILLEGAL_HEX_DIGIT);
203 return toupper (c) - 'A' + 10;
209 static void SetTok (int tok)
210 /* set nxttok and bump line ptr */
218 static int SignExtendChar (int C)
219 /* Do correct sign extension of a character */
221 if (SignedChars && (C & 0x80) != 0) {
230 static int parsechar (int c)
231 /* Parse a character. Converts \n into EOL, etc. */
236 /* Check for escape chars */
238 switch (c = gch ()) {
265 /* Hex character constant */
266 val = hexval (gch ()) << 4;
267 c = val | hexval (gch ()); /* Do not translate */
274 while ((c = *lptr) >= '0' && c <= '7' && i++ < 4) {
275 val = (val << 3) | (c - '0');
278 c = val; /* Do not translate */
281 Error (ERR_ILLEGAL_CHARCONST);
285 /* Do correct sign extension */
286 return SignExtendChar (c);
291 static void CharConst (void)
292 /* Parse a character constant. */
300 c = parsechar (cgch ());
302 /* Check for closing quote */
303 if (cgch () != '\'') {
304 Error (ERR_QUOTE_EXPECTED);
307 /* Setup values and attributes */
309 nxtval = SignExtendChar (ctrans (c)); /* Translate into target charset */
310 nxttype = type_int; /* Character constants have type int */
315 static void StringConst (void)
316 /* Parse a quoted string */
318 nxtval = GetLiteralOffs ();
321 /* Be sure to concatenate strings */
322 while (*lptr == '\"') {
324 /* Skip the quote char */
327 while (*lptr != '\"') {
329 Error (ERR_UNEXPECTED_NEWLINE);
332 AddLiteralChar (parsechar (gch()));
335 /* Skip closing quote char if there was one */
338 /* Skip white space, read new input */
343 /* Terminate the string */
344 AddLiteralChar ('\0');
349 void NextToken (void)
350 /* Get next token from input stream */
355 /* Current token is the lookahead token */
358 /* Remember the starting position of the next token */
359 NextTok.Pos = GetCurrentLine();
361 /* Skip spaces and read the next line if needed */
362 if (skipwhite () == 0) {
363 /* End of file reached */
368 /* Determine the next token from the lookahead */
373 int HaveSuffix; /* True if we have a type suffix */
374 unsigned types; /* Possible types */
376 unsigned long k; /* Value */
380 types = IT_INT | IT_LONG | IT_ULONG;
383 /* Octal or hex constants may also be of type unsigned int */
384 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
385 /* gobble 0 and examin next char */
386 if (toupper (*++lptr) == 'X') {
389 ++lptr; /* gobble "x" */
397 k = k * base + (c - '0');
398 } else if (base == 16 && isxdigit (c)) {
399 k = (k << 4) + hexval (c);
401 break; /* not digit */
403 ++lptr; /* gobble char */
406 /* Check for a suffix */
412 if (toupper (*lptr) != 'L') {
413 types = IT_UINT | IT_ULONG;
418 } else if (c == 'L') {
421 if (toupper (*lptr) != 'U') {
422 types = IT_LONG | IT_ULONG;
431 /* Check the range to determine the type */
433 /* Out of range for int */
435 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
436 * allowed, and we don't have a type specifying suffix, emit a
439 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
440 Warning (WARN_CONSTANT_IS_LONG);
444 /* Out of range for unsigned int */
447 if (k > 0x7FFFFFFF) {
448 /* Out of range for long int */
452 /* Now set the type string to the smallest type in types */
453 if (types & IT_INT) {
455 } else if (types & IT_UINT) {
457 } else if (types & IT_LONG) {
460 nxttype = type_ulong;
463 /* Set the value and the token */
471 /* Check for a keyword */
472 if ((nxttok = FindKey (token)) != TOK_IDENT) {
473 /* Reserved word found */
476 /* No reserved word, check for special symbols */
477 if (token [0] == '_') {
478 /* Special symbols */
479 if (strcmp (token, "__FILE__") == 0) {
480 nxtval = AddLiteral (GetCurrentFile());
483 } else if (strcmp (token, "__LINE__") == 0) {
485 nxtval = GetCurrentLine();
488 } else if (strcmp (token, "__fixargs__") == 0) {
490 nxtval = GetParamSize (CurrentFunc);
493 } else if (strcmp (token, "__func__") == 0) {
494 /* __func__ is only defined in functions */
496 nxtval = AddLiteral (GetFuncName (CurrentFunc));
503 /* No reserved word but identifier */
504 strcpy (NextTok.Ident, token);
505 NextTok.Tok = TOK_IDENT;
509 /* Monstrous switch statement ahead... */
513 if (*++lptr == '=') {
516 nxttok = TOK_BOOL_NOT;
525 if (*++lptr == '=') {
526 SetTok (TOK_MOD_ASSIGN);
535 SetTok (TOK_BOOL_AND);
538 SetTok (TOK_AND_ASSIGN);
558 if (*++lptr == '=') {
559 SetTok (TOK_MUL_ASSIGN);
571 SetTok (TOK_PLUS_ASSIGN);
588 SetTok (TOK_MINUS_ASSIGN);
591 SetTok (TOK_PTR_REF);
599 if (*++lptr == '.') {
600 if (*++lptr == '.') {
601 SetTok (TOK_ELLIPSIS);
611 if (*++lptr == '=') {
612 SetTok (TOK_DIV_ASSIGN);
632 if (*++lptr == '=') {
633 SetTok (TOK_SHL_ASSIGN);
644 if (*++lptr == '=') {
657 if (*++lptr == '=') {
658 SetTok (TOK_SHR_ASSIGN);
681 if (*++lptr == '=') {
682 SetTok (TOK_XOR_ASSIGN);
695 SetTok (TOK_BOOL_OR);
698 SetTok (TOK_OR_ASSIGN);
714 while (*++lptr == ' ') ; /* Skip it and following whitespace */
715 if (!issym (token) || strcmp (token, "pragma") != 0) {
716 /* OOPS - should not happen */
717 Error (ERR_CPP_DIRECTIVE_EXPECTED);
731 void Consume (token_t Token, unsigned ErrNum)
732 /* Eat token if it is the next in the input stream, otherwise print an error
736 if (curtok == Token) {
745 void ConsumeColon (void)
746 /* Check for a colon and skip it. */
748 Consume (TOK_COLON, ERR_COLON_EXPECTED);
753 void ConsumeSemi (void)
754 /* Check for a semicolon and skip it. */
756 /* Try do be smart about typos... */
757 if (curtok == TOK_SEMI) {
760 Error (ERR_SEMICOLON_EXPECTED);
761 if (curtok == TOK_COLON || curtok == TOK_COMMA) {
769 void ConsumeLParen (void)
770 /* Check for a left parenthesis and skip it */
772 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
777 void ConsumeRParen (void)
778 /* Check for a right parenthesis and skip it */
780 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
785 void ConsumeLBrack (void)
786 /* Check for a left bracket and skip it */
788 Consume (TOK_LBRACK, ERR_LBRACK_EXPECTED);
793 void ConsumeRBrack (void)
794 /* Check for a right bracket and skip it */
796 Consume (TOK_RBRACK, ERR_RBRACK_EXPECTED);
801 void ConsumeLCurly (void)
802 /* Check for a left curly brace and skip it */
804 Consume (TOK_LCURLY, ERR_LCURLY_EXPECTED);
809 void ConsumeRCurly (void)
810 /* Check for a right curly brace and skip it */
812 Consume (TOK_RCURLY, ERR_RCURLY_EXPECTED);