1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 Token CurTok; /* The current token */
69 Token NextTok; /* The next token */
74 #define TT_C 0 /* ANSI C token */
75 #define TT_EXT 1 /* cc65 extension */
78 static const struct Keyword {
79 char* Key; /* Keyword name */
80 unsigned char Tok; /* The token */
81 unsigned char Type; /* Token type */
83 { "_Pragma", TOK_PRAGMA, TT_C },
84 { "__A__", TOK_A, TT_C },
85 { "__AX__", TOK_AX, TT_C },
86 { "__EAX__", TOK_EAX, TT_C },
87 { "__X__", TOK_X, TT_C },
88 { "__Y__", TOK_Y, TT_C },
89 { "__asm__", TOK_ASM, TT_C },
90 { "__attribute__", TOK_ATTRIBUTE, TT_C },
91 { "__far__", TOK_FAR, TT_C },
92 { "__fastcall__", TOK_FASTCALL, TT_C },
93 { "asm", TOK_ASM, TT_EXT },
94 { "auto", TOK_AUTO, TT_C },
95 { "break", TOK_BREAK, TT_C },
96 { "case", TOK_CASE, TT_C },
97 { "char", TOK_CHAR, TT_C },
98 { "const", TOK_CONST, TT_C },
99 { "continue", TOK_CONTINUE, TT_C },
100 { "default", TOK_DEFAULT, TT_C },
101 { "do", TOK_DO, TT_C },
102 { "double", TOK_DOUBLE, TT_C },
103 { "else", TOK_ELSE, TT_C },
104 { "enum", TOK_ENUM, TT_C },
105 { "extern", TOK_EXTERN, TT_C },
106 { "far", TOK_FAR, TT_EXT },
107 { "fastcall", TOK_FASTCALL, TT_EXT },
108 { "float", TOK_FLOAT, TT_C },
109 { "for", TOK_FOR, TT_C },
110 { "goto", TOK_GOTO, TT_C },
111 { "if", TOK_IF, TT_C },
112 { "int", TOK_INT, TT_C },
113 { "long", TOK_LONG, TT_C },
114 { "register", TOK_REGISTER, TT_C },
115 { "return", TOK_RETURN, TT_C },
116 { "short", TOK_SHORT, TT_C },
117 { "signed", TOK_SIGNED, TT_C },
118 { "sizeof", TOK_SIZEOF, TT_C },
119 { "static", TOK_STATIC, TT_C },
120 { "struct", TOK_STRUCT, TT_C },
121 { "switch", TOK_SWITCH, TT_C },
122 { "typedef", TOK_TYPEDEF, TT_C },
123 { "union", TOK_UNION, TT_C },
124 { "unsigned", TOK_UNSIGNED, TT_C },
125 { "void", TOK_VOID, TT_C },
126 { "volatile", TOK_VOLATILE, TT_C },
127 { "while", TOK_WHILE, TT_C },
129 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
133 /* Stuff for determining the type of an integer constant */
137 #define IT_ULONG 0x08
141 /*****************************************************************************/
143 /*****************************************************************************/
147 static int CmpKey (const void* Key, const void* Elem)
148 /* Compare function for bsearch */
150 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
155 static int FindKey (const char* Key)
156 /* Find a keyword and return the token. Return IDENT if the token is not a
161 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
162 if (K && (K->Type != TT_EXT || ANSI == 0)) {
171 static int SkipWhite (void)
172 /* Skip white space in the input stream, reading and preprocessing new lines
173 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
178 if (NextLine () == 0) {
183 if (IsSpace (CurC)) {
193 void SymName (char* s)
194 /* Get symbol from input stream */
198 if (k != MAX_IDENTLEN) {
203 } while (IsIdent (CurC) || IsDigit (CurC));
210 /* Get symbol from input stream or return 0 if not a symbol. */
212 if (IsIdent (CurC)) {
222 static void UnknownChar (char C)
223 /* Error message for unknown character */
225 Error ("Invalid input character with code %02X", C & 0xFF);
226 NextChar (); /* Skip */
231 static void SetTok (int tok)
232 /* Set NextTok.Tok and bump line ptr */
240 static int SignExtendChar (int C)
241 /* Do correct sign extension of a character */
243 if (SignedChars && (C & 0x80) != 0) {
252 static int ParseChar (void)
253 /* Parse a character. Converts \n into EOL, etc. */
259 /* Check for escape chars */
289 /* Hex character constant */
291 val = HexVal (CurC) << 4;
293 C = val | HexVal (CurC); /* Do not translate */
300 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
302 C = (C << 3) | (CurC - '0');
306 Error ("Illegal character constant");
314 /* Skip the character read */
317 /* Do correct sign extension */
318 return SignExtendChar (C);
323 static void CharConst (void)
324 /* Parse a character constant. */
334 /* Check for closing quote */
336 Error ("`\'' expected");
342 /* Setup values and attributes */
343 NextTok.Tok = TOK_CCONST;
345 /* Translate into target charset */
346 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
348 /* Character constants have type int */
349 NextTok.Type = type_int;
354 static void StringConst (void)
355 /* Parse a quoted string */
357 NextTok.IVal = GetLiteralPoolOffs ();
358 NextTok.Tok = TOK_SCONST;
360 /* Be sure to concatenate strings */
361 while (CurC == '\"') {
363 /* Skip the quote char */
366 while (CurC != '\"') {
368 Error ("Unexpected newline");
371 AddLiteralChar (ParseChar ());
374 /* Skip closing quote char if there was one */
377 /* Skip white space, read new input */
382 /* Terminate the string */
383 AddLiteralChar ('\0');
388 void NextToken (void)
389 /* Get next token from input stream */
393 /* We have to skip white space here before shifting tokens, since the
394 * tokens and the current line info is invalid at startup and will get
395 * initialized by reading the first time from the file. Remember if
396 * we were at end of input and handle that later.
398 int GotEOF = (SkipWhite() == 0);
400 /* Current token is the lookahead token */
402 ReleaseLineInfo (CurTok.LI);
406 /* Remember the starting position of the next token */
407 NextTok.LI = UseLineInfo (GetCurLineInfo ());
409 /* Now handle end of input. */
411 /* End of file reached */
412 NextTok.Tok = TOK_CEOF;
416 /* Determine the next token from the lookahead */
417 if (IsDigit (CurC)) {
420 int HaveSuffix; /* True if we have a type suffix */
421 unsigned types; /* Possible types */
423 unsigned long k; /* Value */
427 types = IT_INT | IT_LONG | IT_ULONG;
430 /* Octal or hex constants may also be of type unsigned int */
431 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
432 /* gobble 0 and examin next char */
434 if (toupper (CurC) == 'X') {
436 NextTok.Type = type_uint;
437 NextChar (); /* gobble "x" */
443 if (IsDigit (CurC)) {
444 k = k * base + (CurC - '0');
445 } else if (base == 16 && IsXDigit (CurC)) {
446 k = (k << 4) + HexVal (CurC);
448 break; /* not digit */
450 NextChar (); /* gobble char */
453 /* Check for a suffix */
455 if (CurC == 'u' || CurC == 'U') {
458 if (toupper (CurC) != 'L') {
459 types = IT_UINT | IT_ULONG;
464 } else if (CurC == 'l' || CurC == 'L') {
467 if (toupper (CurC) != 'U') {
468 types = IT_LONG | IT_ULONG;
477 /* Check the range to determine the type */
479 /* Out of range for int */
481 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
482 * allowed, and we don't have a type specifying suffix, emit a
485 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
486 Warning ("Constant is long");
490 /* Out of range for unsigned int */
493 if (k > 0x7FFFFFFF) {
494 /* Out of range for long int */
498 /* Now set the type string to the smallest type in types */
499 if (types & IT_INT) {
500 NextTok.Type = type_int;
501 } else if (types & IT_UINT) {
502 NextTok.Type = type_uint;
503 } else if (types & IT_LONG) {
504 NextTok.Type = type_long;
506 NextTok.Type = type_ulong;
509 /* Set the value and the token */
511 NextTok.Tok = TOK_ICONST;
517 /* Check for a keyword */
518 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
519 /* Reserved word found */
522 /* No reserved word, check for special symbols */
523 if (token [0] == '_') {
524 /* Special symbols */
525 if (strcmp (token, "__FILE__") == 0) {
526 NextTok.IVal = AddLiteral (GetCurrentFile());
527 NextTok.Tok = TOK_SCONST;
529 } else if (strcmp (token, "__LINE__") == 0) {
530 NextTok.Tok = TOK_ICONST;
531 NextTok.IVal = GetCurrentLine();
532 NextTok.Type = type_int;
534 } else if (strcmp (token, "__func__") == 0) {
535 /* __func__ is only defined in functions */
537 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
538 NextTok.Tok = TOK_SCONST;
544 /* No reserved word but identifier */
545 strcpy (NextTok.Ident, token);
546 NextTok.Tok = TOK_IDENT;
550 /* Monstrous switch statement ahead... */
558 NextTok.Tok = TOK_BOOL_NOT;
569 SetTok (TOK_MOD_ASSIGN);
571 NextTok.Tok = TOK_MOD;
579 SetTok (TOK_BOOL_AND);
582 SetTok (TOK_AND_ASSIGN);
585 NextTok.Tok = TOK_AND;
604 SetTok (TOK_MUL_ASSIGN);
606 NextTok.Tok = TOK_STAR;
617 SetTok (TOK_PLUS_ASSIGN);
620 NextTok.Tok = TOK_PLUS;
635 SetTok (TOK_MINUS_ASSIGN);
638 SetTok (TOK_PTR_REF);
641 NextTok.Tok = TOK_MINUS;
650 SetTok (TOK_ELLIPSIS);
655 NextTok.Tok = TOK_DOT;
662 SetTok (TOK_DIV_ASSIGN);
664 NextTok.Tok = TOK_DIV;
685 SetTok (TOK_SHL_ASSIGN);
687 NextTok.Tok = TOK_SHL;
691 NextTok.Tok = TOK_LT;
700 NextTok.Tok = TOK_ASSIGN;
713 SetTok (TOK_SHR_ASSIGN);
715 NextTok.Tok = TOK_SHR;
719 NextTok.Tok = TOK_GT;
738 SetTok (TOK_XOR_ASSIGN);
740 NextTok.Tok = TOK_XOR;
752 SetTok (TOK_BOOL_OR);
755 SetTok (TOK_OR_ASSIGN);
758 NextTok.Tok = TOK_OR;
779 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
780 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
781 * This routine is used for error recovery.
784 while (CurTok.Tok != TOK_CEOF) {
786 /* Check if the current token is in the token list */
788 for (I = 0; I < TokenCount; ++I) {
789 if (CurTok.Tok == TokenList[I]) {
790 /* Found a token in the list */
795 /* Not in the list: Skip it */
803 int Consume (token_t Token, const char* ErrorMsg)
804 /* Eat token if it is the next in the input stream, otherwise print an error
805 * message. Returns true if the token was found and false otherwise.
808 if (CurTok.Tok == Token) {
819 int ConsumeColon (void)
820 /* Check for a colon and skip it. */
822 return Consume (TOK_COLON, "`:' expected");
827 int ConsumeSemi (void)
828 /* Check for a semicolon and skip it. */
830 /* Try do be smart about typos... */
831 if (CurTok.Tok == TOK_SEMI) {
835 Error ("`;' expected");
836 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
845 int ConsumeComma (void)
846 /* Check for a comma and skip it. */
848 /* Try do be smart about typos... */
849 if (CurTok.Tok == TOK_COMMA) {
853 Error ("`,' expected");
854 if (CurTok.Tok == TOK_SEMI) {
863 int ConsumeLParen (void)
864 /* Check for a left parenthesis and skip it */
866 return Consume (TOK_LPAREN, "`(' expected");
871 int ConsumeRParen (void)
872 /* Check for a right parenthesis and skip it */
874 return Consume (TOK_RPAREN, "`)' expected");
879 int ConsumeLBrack (void)
880 /* Check for a left bracket and skip it */
882 return Consume (TOK_LBRACK, "`[' expected");
887 int ConsumeRBrack (void)
888 /* Check for a right bracket and skip it */
890 return Consume (TOK_RBRACK, "`]' expected");
895 int ConsumeLCurly (void)
896 /* Check for a left curly brace and skip it */
898 return Consume (TOK_LCURLY, "`{' expected");
903 int ConsumeRCurly (void)
904 /* Check for a right curly brace and skip it */
906 return Consume (TOK_RCURLY, "`}' expected");