1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 { "restrict", TOK_RESTRICT, TT_C },
116 { "return", TOK_RETURN, TT_C },
117 { "short", TOK_SHORT, TT_C },
118 { "signed", TOK_SIGNED, TT_C },
119 { "sizeof", TOK_SIZEOF, TT_C },
120 { "static", TOK_STATIC, TT_C },
121 { "struct", TOK_STRUCT, TT_C },
122 { "switch", TOK_SWITCH, TT_C },
123 { "typedef", TOK_TYPEDEF, TT_C },
124 { "union", TOK_UNION, TT_C },
125 { "unsigned", TOK_UNSIGNED, TT_C },
126 { "void", TOK_VOID, TT_C },
127 { "volatile", TOK_VOLATILE, TT_C },
128 { "while", TOK_WHILE, TT_C },
130 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
134 /* Stuff for determining the type of an integer constant */
138 #define IT_ULONG 0x08
142 /*****************************************************************************/
144 /*****************************************************************************/
148 static int CmpKey (const void* Key, const void* Elem)
149 /* Compare function for bsearch */
151 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
156 static int FindKey (const char* Key)
157 /* Find a keyword and return the token. Return IDENT if the token is not a
162 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
163 if (K && (K->Type != TT_EXT || ANSI == 0)) {
172 static int SkipWhite (void)
173 /* Skip white space in the input stream, reading and preprocessing new lines
174 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
179 if (NextLine () == 0) {
184 if (IsSpace (CurC)) {
194 void SymName (char* s)
195 /* Get symbol from input stream */
199 if (k != MAX_IDENTLEN) {
204 } while (IsIdent (CurC) || IsDigit (CurC));
211 /* Get symbol from input stream or return 0 if not a symbol. */
213 if (IsIdent (CurC)) {
223 static void UnknownChar (char C)
224 /* Error message for unknown character */
226 Error ("Invalid input character with code %02X", C & 0xFF);
227 NextChar (); /* Skip */
232 static void SetTok (int tok)
233 /* Set NextTok.Tok and bump line ptr */
241 static int ParseChar (void)
242 /* Parse a character. Converts \n into EOL, etc. */
248 /* Check for escape chars */
278 /* Hex character constant */
280 val = HexVal (CurC) << 4;
282 C = val | HexVal (CurC); /* Do not translate */
291 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
293 C = (C << 3) | (CurC - '0');
297 Error ("Illegal character constant");
305 /* Skip the character read */
308 /* Do correct sign extension */
309 return SignExtendChar (C);
314 static void CharConst (void)
315 /* Parse a character constant. */
325 /* Check for closing quote */
327 Error ("`\'' expected");
333 /* Setup values and attributes */
334 NextTok.Tok = TOK_CCONST;
336 /* Translate into target charset */
337 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
339 /* Character constants have type int */
340 NextTok.Type = type_int;
345 static void StringConst (void)
346 /* Parse a quoted string */
348 NextTok.IVal = GetLiteralPoolOffs ();
349 NextTok.Tok = TOK_SCONST;
351 /* Be sure to concatenate strings */
352 while (CurC == '\"') {
354 /* Skip the quote char */
357 while (CurC != '\"') {
359 Error ("Unexpected newline");
362 AddLiteralChar (ParseChar ());
365 /* Skip closing quote char if there was one */
368 /* Skip white space, read new input */
373 /* Terminate the string */
374 AddLiteralChar ('\0');
379 void NextToken (void)
380 /* Get next token from input stream */
384 /* We have to skip white space here before shifting tokens, since the
385 * tokens and the current line info is invalid at startup and will get
386 * initialized by reading the first time from the file. Remember if
387 * we were at end of input and handle that later.
389 int GotEOF = (SkipWhite() == 0);
391 /* Current token is the lookahead token */
393 ReleaseLineInfo (CurTok.LI);
397 /* Remember the starting position of the next token */
398 NextTok.LI = UseLineInfo (GetCurLineInfo ());
400 /* Now handle end of input. */
402 /* End of file reached */
403 NextTok.Tok = TOK_CEOF;
407 /* Determine the next token from the lookahead */
408 if (IsDigit (CurC)) {
411 int HaveSuffix; /* True if we have a type suffix */
412 unsigned types; /* Possible types */
415 unsigned long k; /* Value */
419 types = IT_INT | IT_LONG | IT_ULONG;
422 /* Octal or hex constants may also be of type unsigned int */
423 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
424 /* gobble 0 and examin next char */
426 if (toupper (CurC) == 'X') {
428 NextTok.Type = type_uint;
429 NextChar (); /* gobble "x" */
434 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
435 k = k * Base + DigitVal;
438 /* Check for errorneous digits */
439 if (Base == 8 && IsDigit (CurC)) {
440 Error ("Numeric constant contains digits beyond the radix");
441 /* Do error recovery */
444 } while (IsDigit (CurC));
445 } else if (Base != 16 && IsXDigit (CurC)) {
446 Error ("Nondigits in number and not hexadecimal");
449 } while (IsXDigit (CurC));
452 /* Check for a suffix */
454 if (CurC == 'u' || CurC == 'U') {
457 if (toupper (CurC) != 'L') {
458 types = IT_UINT | IT_ULONG;
463 } else if (CurC == 'l' || CurC == 'L') {
466 if (toupper (CurC) != 'U') {
467 types = IT_LONG | IT_ULONG;
476 /* Check the range to determine the type */
478 /* Out of range for int */
480 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
481 * allowed, and we don't have a type specifying suffix, emit a
484 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
485 Warning ("Constant is long");
489 /* Out of range for unsigned int */
492 if (k > 0x7FFFFFFF) {
493 /* Out of range for long int */
497 /* Now set the type string to the smallest type in types */
498 if (types & IT_INT) {
499 NextTok.Type = type_int;
500 } else if (types & IT_UINT) {
501 NextTok.Type = type_uint;
502 } else if (types & IT_LONG) {
503 NextTok.Type = type_long;
505 NextTok.Type = type_ulong;
508 /* Set the value and the token */
510 NextTok.Tok = TOK_ICONST;
516 /* Check for a keyword */
517 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
518 /* Reserved word found */
521 /* No reserved word, check for special symbols */
522 if (token [0] == '_') {
523 /* Special symbols */
524 if (strcmp (token, "__FILE__") == 0) {
525 NextTok.IVal = AddLiteral (GetCurrentFile());
526 NextTok.Tok = TOK_SCONST;
528 } else if (strcmp (token, "__LINE__") == 0) {
529 NextTok.Tok = TOK_ICONST;
530 NextTok.IVal = GetCurrentLine();
531 NextTok.Type = type_int;
533 } else if (strcmp (token, "__func__") == 0) {
534 /* __func__ is only defined in functions */
536 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
537 NextTok.Tok = TOK_SCONST;
543 /* No reserved word but identifier */
544 strcpy (NextTok.Ident, token);
545 NextTok.Tok = TOK_IDENT;
549 /* Monstrous switch statement ahead... */
557 NextTok.Tok = TOK_BOOL_NOT;
568 SetTok (TOK_MOD_ASSIGN);
570 NextTok.Tok = TOK_MOD;
578 SetTok (TOK_BOOL_AND);
581 SetTok (TOK_AND_ASSIGN);
584 NextTok.Tok = TOK_AND;
603 SetTok (TOK_MUL_ASSIGN);
605 NextTok.Tok = TOK_STAR;
616 SetTok (TOK_PLUS_ASSIGN);
619 NextTok.Tok = TOK_PLUS;
634 SetTok (TOK_MINUS_ASSIGN);
637 SetTok (TOK_PTR_REF);
640 NextTok.Tok = TOK_MINUS;
649 SetTok (TOK_ELLIPSIS);
654 NextTok.Tok = TOK_DOT;
661 SetTok (TOK_DIV_ASSIGN);
663 NextTok.Tok = TOK_DIV;
684 SetTok (TOK_SHL_ASSIGN);
686 NextTok.Tok = TOK_SHL;
690 NextTok.Tok = TOK_LT;
699 NextTok.Tok = TOK_ASSIGN;
712 SetTok (TOK_SHR_ASSIGN);
714 NextTok.Tok = TOK_SHR;
718 NextTok.Tok = TOK_GT;
737 SetTok (TOK_XOR_ASSIGN);
739 NextTok.Tok = TOK_XOR;
751 SetTok (TOK_BOOL_OR);
754 SetTok (TOK_OR_ASSIGN);
757 NextTok.Tok = TOK_OR;
778 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
779 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
780 * This routine is used for error recovery.
783 while (CurTok.Tok != TOK_CEOF) {
785 /* Check if the current token is in the token list */
787 for (I = 0; I < TokenCount; ++I) {
788 if (CurTok.Tok == TokenList[I]) {
789 /* Found a token in the list */
794 /* Not in the list: Skip it */
802 int Consume (token_t Token, const char* ErrorMsg)
803 /* Eat token if it is the next in the input stream, otherwise print an error
804 * message. Returns true if the token was found and false otherwise.
807 if (CurTok.Tok == Token) {
818 int ConsumeColon (void)
819 /* Check for a colon and skip it. */
821 return Consume (TOK_COLON, "`:' expected");
826 int ConsumeSemi (void)
827 /* Check for a semicolon and skip it. */
829 /* Try do be smart about typos... */
830 if (CurTok.Tok == TOK_SEMI) {
834 Error ("`;' expected");
835 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
844 int ConsumeComma (void)
845 /* Check for a comma and skip it. */
847 /* Try do be smart about typos... */
848 if (CurTok.Tok == TOK_COMMA) {
852 Error ("`,' expected");
853 if (CurTok.Tok == TOK_SEMI) {
862 int ConsumeLParen (void)
863 /* Check for a left parenthesis and skip it */
865 return Consume (TOK_LPAREN, "`(' expected");
870 int ConsumeRParen (void)
871 /* Check for a right parenthesis and skip it */
873 return Consume (TOK_RPAREN, "`)' expected");
878 int ConsumeLBrack (void)
879 /* Check for a left bracket and skip it */
881 return Consume (TOK_LBRACK, "`[' expected");
886 int ConsumeRBrack (void)
887 /* Check for a right bracket and skip it */
889 return Consume (TOK_RBRACK, "`]' expected");
894 int ConsumeLCurly (void)
895 /* Check for a left curly brace and skip it */
897 return Consume (TOK_LCURLY, "`{' expected");
902 int ConsumeRCurly (void)
903 /* Check for a right curly brace and skip it */
905 return Consume (TOK_RCURLY, "`}' expected");