1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2001 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 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
67 Token CurTok; /* The current token */
68 Token NextTok; /* The next token */
73 #define TT_C 0 /* ANSI C token */
74 #define TT_EXT 1 /* cc65 extension */
77 static const struct Keyword {
78 char* Key; /* Keyword name */
79 unsigned char Tok; /* The token */
80 unsigned char Type; /* Token type */
82 { "__A__", TOK_A, TT_C },
83 { "__AX__", TOK_AX, TT_C },
84 { "__EAX__", TOK_EAX, TT_C },
85 { "__X__", TOK_X, TT_C },
86 { "__Y__", TOK_Y, TT_C },
87 { "__asm__", TOK_ASM, TT_C },
88 { "__attribute__", TOK_ATTRIBUTE, TT_C },
89 { "__far__", TOK_FAR, TT_C },
90 { "__fastcall__", TOK_FASTCALL, TT_C },
91 { "asm", TOK_ASM, TT_EXT },
92 { "auto", TOK_AUTO, TT_C },
93 { "break", TOK_BREAK, TT_C },
94 { "case", TOK_CASE, TT_C },
95 { "char", TOK_CHAR, TT_C },
96 { "const", TOK_CONST, TT_C },
97 { "continue", TOK_CONTINUE, TT_C },
98 { "default", TOK_DEFAULT, TT_C },
99 { "do", TOK_DO, TT_C },
100 { "double", TOK_DOUBLE, TT_C },
101 { "else", TOK_ELSE, TT_C },
102 { "enum", TOK_ENUM, TT_C },
103 { "extern", TOK_EXTERN, TT_C },
104 { "far", TOK_FAR, TT_EXT },
105 { "fastcall", TOK_FASTCALL, TT_EXT },
106 { "float", TOK_FLOAT, TT_C },
107 { "for", TOK_FOR, TT_C },
108 { "goto", TOK_GOTO, TT_C },
109 { "if", TOK_IF, TT_C },
110 { "int", TOK_INT, TT_C },
111 { "long", TOK_LONG, TT_C },
112 { "register", TOK_REGISTER, TT_C },
113 { "return", TOK_RETURN, TT_C },
114 { "short", TOK_SHORT, TT_C },
115 { "signed", TOK_SIGNED, TT_C },
116 { "sizeof", TOK_SIZEOF, TT_C },
117 { "static", TOK_STATIC, TT_C },
118 { "struct", TOK_STRUCT, TT_C },
119 { "switch", TOK_SWITCH, TT_C },
120 { "typedef", TOK_TYPEDEF, TT_C },
121 { "union", TOK_UNION, TT_C },
122 { "unsigned", TOK_UNSIGNED, TT_C },
123 { "void", TOK_VOID, TT_C },
124 { "volatile", TOK_VOLATILE, TT_C },
125 { "while", TOK_WHILE, TT_C },
127 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
131 /* Stuff for determining the type of an integer constant */
135 #define IT_ULONG 0x08
139 /*****************************************************************************/
141 /*****************************************************************************/
145 static int CmpKey (const void* Key, const void* Elem)
146 /* Compare function for bsearch */
148 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
153 static int FindKey (const char* Key)
154 /* Find a keyword and return the token. Return IDENT if the token is not a
159 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
160 if (K && (K->Type != TT_EXT || ANSI == 0)) {
169 static int SkipWhite (void)
170 /* Skip white space in the input stream, reading and preprocessing new lines
171 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
176 if (NextLine () == 0) {
181 if (IsSpace (CurC)) {
191 void SymName (char* s)
192 /* Get symbol from input stream */
196 if (k != MAX_IDENTLEN) {
201 } while (IsIdent (CurC) || IsDigit (CurC));
208 /* Get symbol from input stream or return 0 if not a symbol. */
210 if (IsIdent (CurC)) {
220 static void UnknownChar (char C)
221 /* Error message for unknown character */
223 Error ("Invalid input character with code %02X", C & 0xFF);
224 NextChar (); /* Skip */
229 static unsigned hexval (int c)
230 /* Convert a hex digit into a value */
233 Error ("Invalid hexadecimal digit: `%c'", c);
238 return toupper (c) - 'A' + 10;
244 static void SetTok (int tok)
245 /* Set NextTok.Tok and bump line ptr */
253 static int SignExtendChar (int C)
254 /* Do correct sign extension of a character */
256 if (SignedChars && (C & 0x80) != 0) {
265 static int ParseChar (void)
266 /* Parse a character. Converts \n into EOL, etc. */
272 /* Check for escape chars */
302 /* Hex character constant */
304 val = hexval (CurC) << 4;
306 C = val | hexval (CurC); /* Do not translate */
313 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
315 C = (C << 3) | (CurC - '0');
319 Error ("Illegal character constant");
327 /* Skip the character read */
330 /* Do correct sign extension */
331 return SignExtendChar (C);
336 static void CharConst (void)
337 /* Parse a character constant. */
347 /* Check for closing quote */
349 Error ("`\'' expected");
355 /* Setup values and attributes */
356 NextTok.Tok = TOK_CCONST;
358 /* Translate into target charset */
359 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
361 /* Character constants have type int */
362 NextTok.Type = type_int;
367 static void StringConst (void)
368 /* Parse a quoted string */
370 NextTok.IVal = GetLiteralPoolOffs ();
371 NextTok.Tok = TOK_SCONST;
373 /* Be sure to concatenate strings */
374 while (CurC == '\"') {
376 /* Skip the quote char */
379 while (CurC != '\"') {
381 Error ("Unexpected newline");
384 AddLiteralChar (ParseChar ());
387 /* Skip closing quote char if there was one */
390 /* Skip white space, read new input */
395 /* Terminate the string */
396 AddLiteralChar ('\0');
401 void NextToken (void)
402 /* Get next token from input stream */
406 /* We have to skip white space here before shifting tokens, since the
407 * tokens and the current line info is invalid at startup and will get
408 * initialized by reading the first time from the file. Remember if
409 * we were at end of input and handle that later.
411 int GotEOF = (SkipWhite() == 0);
413 /* Current token is the lookahead token */
415 ReleaseLineInfo (CurTok.LI);
419 /* Remember the starting position of the next token */
420 NextTok.LI = UseLineInfo (GetCurLineInfo ());
422 /* Now handle end of input. */
424 /* End of file reached */
425 NextTok.Tok = TOK_CEOF;
429 /* Determine the next token from the lookahead */
430 if (IsDigit (CurC)) {
433 int HaveSuffix; /* True if we have a type suffix */
434 unsigned types; /* Possible types */
436 unsigned long k; /* Value */
440 types = IT_INT | IT_LONG | IT_ULONG;
443 /* Octal or hex constants may also be of type unsigned int */
444 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
445 /* gobble 0 and examin next char */
447 if (toupper (CurC) == 'X') {
449 NextTok.Type = type_uint;
450 NextChar (); /* gobble "x" */
456 if (IsDigit (CurC)) {
457 k = k * base + (CurC - '0');
458 } else if (base == 16 && IsXDigit (CurC)) {
459 k = (k << 4) + hexval (CurC);
461 break; /* not digit */
463 NextChar (); /* gobble char */
466 /* Check for a suffix */
468 if (CurC == 'u' || CurC == 'U') {
471 if (toupper (CurC) != 'L') {
472 types = IT_UINT | IT_ULONG;
477 } else if (CurC == 'l' || CurC == 'L') {
480 if (toupper (CurC) != 'U') {
481 types = IT_LONG | IT_ULONG;
490 /* Check the range to determine the type */
492 /* Out of range for int */
494 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
495 * allowed, and we don't have a type specifying suffix, emit a
498 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
499 Warning ("Constant is long");
503 /* Out of range for unsigned int */
506 if (k > 0x7FFFFFFF) {
507 /* Out of range for long int */
511 /* Now set the type string to the smallest type in types */
512 if (types & IT_INT) {
513 NextTok.Type = type_int;
514 } else if (types & IT_UINT) {
515 NextTok.Type = type_uint;
516 } else if (types & IT_LONG) {
517 NextTok.Type = type_long;
519 NextTok.Type = type_ulong;
522 /* Set the value and the token */
524 NextTok.Tok = TOK_ICONST;
530 /* Check for a keyword */
531 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
532 /* Reserved word found */
535 /* No reserved word, check for special symbols */
536 if (token [0] == '_') {
537 /* Special symbols */
538 if (strcmp (token, "__FILE__") == 0) {
539 NextTok.IVal = AddLiteral (GetCurrentFile());
540 NextTok.Tok = TOK_SCONST;
542 } else if (strcmp (token, "__LINE__") == 0) {
543 NextTok.Tok = TOK_ICONST;
544 NextTok.IVal = GetCurrentLine();
545 NextTok.Type = type_int;
547 } else if (strcmp (token, "__func__") == 0) {
548 /* __func__ is only defined in functions */
550 NextTok.IVal = AddLiteral (GetFuncName (CurrentFunc));
551 NextTok.Tok = TOK_SCONST;
557 /* No reserved word but identifier */
558 strcpy (NextTok.Ident, token);
559 NextTok.Tok = TOK_IDENT;
563 /* Monstrous switch statement ahead... */
571 NextTok.Tok = TOK_BOOL_NOT;
582 SetTok (TOK_MOD_ASSIGN);
584 NextTok.Tok = TOK_MOD;
592 SetTok (TOK_BOOL_AND);
595 SetTok (TOK_AND_ASSIGN);
598 NextTok.Tok = TOK_AND;
617 SetTok (TOK_MUL_ASSIGN);
619 NextTok.Tok = TOK_STAR;
630 SetTok (TOK_PLUS_ASSIGN);
633 NextTok.Tok = TOK_PLUS;
648 SetTok (TOK_MINUS_ASSIGN);
651 SetTok (TOK_PTR_REF);
654 NextTok.Tok = TOK_MINUS;
663 SetTok (TOK_ELLIPSIS);
668 NextTok.Tok = TOK_DOT;
675 SetTok (TOK_DIV_ASSIGN);
677 NextTok.Tok = TOK_DIV;
698 SetTok (TOK_SHL_ASSIGN);
700 NextTok.Tok = TOK_SHL;
704 NextTok.Tok = TOK_LT;
713 NextTok.Tok = TOK_ASSIGN;
726 SetTok (TOK_SHR_ASSIGN);
728 NextTok.Tok = TOK_SHR;
732 NextTok.Tok = TOK_GT;
751 SetTok (TOK_XOR_ASSIGN);
753 NextTok.Tok = TOK_XOR;
765 SetTok (TOK_BOOL_OR);
768 SetTok (TOK_OR_ASSIGN);
771 NextTok.Tok = TOK_OR;
784 /* Skip it and following whitespace */
787 } while (CurC == ' ');
788 if (!IsSym (token) || strcmp (token, "pragma") != 0) {
789 /* OOPS - should not happen */
790 Error ("Preprocessor directive expected");
792 NextTok.Tok = TOK_PRAGMA;
804 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
805 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
806 * This routine is used for error recovery.
809 while (CurTok.Tok != TOK_CEOF) {
811 /* Check if the current token is in the token list */
813 for (I = 0; I < TokenCount; ++I) {
814 if (CurTok.Tok == TokenList[I]) {
815 /* Found a token in the list */
820 /* Not in the list: Skip it */
828 void Consume (token_t Token, const char* ErrorMsg)
829 /* Eat token if it is the next in the input stream, otherwise print an error
833 if (CurTok.Tok == Token) {
842 void ConsumeColon (void)
843 /* Check for a colon and skip it. */
845 Consume (TOK_COLON, "`:' expected");
850 void ConsumeSemi (void)
851 /* Check for a semicolon and skip it. */
853 /* Try do be smart about typos... */
854 if (CurTok.Tok == TOK_SEMI) {
857 Error ("`;' expected");
858 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
866 void ConsumeComma (void)
867 /* Check for a comma and skip it. */
869 /* Try do be smart about typos... */
870 if (CurTok.Tok == TOK_COMMA) {
873 Error ("`,' expected");
874 if (CurTok.Tok == TOK_SEMI) {
882 void ConsumeLParen (void)
883 /* Check for a left parenthesis and skip it */
885 Consume (TOK_LPAREN, "`(' expected");
890 void ConsumeRParen (void)
891 /* Check for a right parenthesis and skip it */
893 Consume (TOK_RPAREN, "`)' expected");
898 void ConsumeLBrack (void)
899 /* Check for a left bracket and skip it */
901 Consume (TOK_LBRACK, "`[' expected");
906 void ConsumeRBrack (void)
907 /* Check for a right bracket and skip it */
909 Consume (TOK_RBRACK, "`]' expected");
914 void ConsumeLCurly (void)
915 /* Check for a left curly brace and skip it */
917 Consume (TOK_LCURLY, "`{' expected");
922 void ConsumeRCurly (void)
923 /* Check for a right curly brace and skip it */
925 Consume (TOK_RCURLY, "`}' expected");