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 */
289 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
291 C = (C << 3) | (CurC - '0');
295 Error ("Illegal character constant");
303 /* Skip the character read */
306 /* Do correct sign extension */
307 return SignExtendChar (C);
312 static void CharConst (void)
313 /* Parse a character constant. */
323 /* Check for closing quote */
325 Error ("`\'' expected");
331 /* Setup values and attributes */
332 NextTok.Tok = TOK_CCONST;
334 /* Translate into target charset */
335 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
337 /* Character constants have type int */
338 NextTok.Type = type_int;
343 static void StringConst (void)
344 /* Parse a quoted string */
346 NextTok.IVal = GetLiteralPoolOffs ();
347 NextTok.Tok = TOK_SCONST;
349 /* Be sure to concatenate strings */
350 while (CurC == '\"') {
352 /* Skip the quote char */
355 while (CurC != '\"') {
357 Error ("Unexpected newline");
360 AddLiteralChar (ParseChar ());
363 /* Skip closing quote char if there was one */
366 /* Skip white space, read new input */
371 /* Terminate the string */
372 AddLiteralChar ('\0');
377 void NextToken (void)
378 /* Get next token from input stream */
382 /* We have to skip white space here before shifting tokens, since the
383 * tokens and the current line info is invalid at startup and will get
384 * initialized by reading the first time from the file. Remember if
385 * we were at end of input and handle that later.
387 int GotEOF = (SkipWhite() == 0);
389 /* Current token is the lookahead token */
391 ReleaseLineInfo (CurTok.LI);
395 /* Remember the starting position of the next token */
396 NextTok.LI = UseLineInfo (GetCurLineInfo ());
398 /* Now handle end of input. */
400 /* End of file reached */
401 NextTok.Tok = TOK_CEOF;
405 /* Determine the next token from the lookahead */
406 if (IsDigit (CurC)) {
409 int HaveSuffix; /* True if we have a type suffix */
410 unsigned types; /* Possible types */
413 unsigned long k; /* Value */
417 types = IT_INT | IT_LONG | IT_ULONG;
420 /* Octal or hex constants may also be of type unsigned int */
421 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
422 /* gobble 0 and examin next char */
424 if (toupper (CurC) == 'X') {
426 NextTok.Type = type_uint;
427 NextChar (); /* gobble "x" */
432 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
433 k = k * Base + DigitVal;
436 /* Check for errorneous digits */
437 if (Base == 8 && IsDigit (CurC)) {
438 Error ("Numeric constant contains digits beyond the radix");
439 /* Do error recovery */
442 } while (IsDigit (CurC));
443 } else if (Base != 16 && IsXDigit (CurC)) {
444 Error ("Nondigits in number and not hexadecimal");
447 } while (IsXDigit (CurC));
450 /* Check for a suffix */
452 if (CurC == 'u' || CurC == 'U') {
455 if (toupper (CurC) != 'L') {
456 types = IT_UINT | IT_ULONG;
461 } else if (CurC == 'l' || CurC == 'L') {
464 if (toupper (CurC) != 'U') {
465 types = IT_LONG | IT_ULONG;
474 /* Check the range to determine the type */
476 /* Out of range for int */
478 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
479 * allowed, and we don't have a type specifying suffix, emit a
482 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
483 Warning ("Constant is long");
487 /* Out of range for unsigned int */
490 if (k > 0x7FFFFFFF) {
491 /* Out of range for long int */
495 /* Now set the type string to the smallest type in types */
496 if (types & IT_INT) {
497 NextTok.Type = type_int;
498 } else if (types & IT_UINT) {
499 NextTok.Type = type_uint;
500 } else if (types & IT_LONG) {
501 NextTok.Type = type_long;
503 NextTok.Type = type_ulong;
506 /* Set the value and the token */
508 NextTok.Tok = TOK_ICONST;
514 /* Check for a keyword */
515 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
516 /* Reserved word found */
519 /* No reserved word, check for special symbols */
520 if (token [0] == '_') {
521 /* Special symbols */
522 if (strcmp (token, "__FILE__") == 0) {
523 NextTok.IVal = AddLiteral (GetCurrentFile());
524 NextTok.Tok = TOK_SCONST;
526 } else if (strcmp (token, "__LINE__") == 0) {
527 NextTok.Tok = TOK_ICONST;
528 NextTok.IVal = GetCurrentLine();
529 NextTok.Type = type_int;
531 } else if (strcmp (token, "__func__") == 0) {
532 /* __func__ is only defined in functions */
534 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
535 NextTok.Tok = TOK_SCONST;
541 /* No reserved word but identifier */
542 strcpy (NextTok.Ident, token);
543 NextTok.Tok = TOK_IDENT;
547 /* Monstrous switch statement ahead... */
555 NextTok.Tok = TOK_BOOL_NOT;
566 SetTok (TOK_MOD_ASSIGN);
568 NextTok.Tok = TOK_MOD;
576 SetTok (TOK_BOOL_AND);
579 SetTok (TOK_AND_ASSIGN);
582 NextTok.Tok = TOK_AND;
601 SetTok (TOK_MUL_ASSIGN);
603 NextTok.Tok = TOK_STAR;
614 SetTok (TOK_PLUS_ASSIGN);
617 NextTok.Tok = TOK_PLUS;
632 SetTok (TOK_MINUS_ASSIGN);
635 SetTok (TOK_PTR_REF);
638 NextTok.Tok = TOK_MINUS;
647 SetTok (TOK_ELLIPSIS);
652 NextTok.Tok = TOK_DOT;
659 SetTok (TOK_DIV_ASSIGN);
661 NextTok.Tok = TOK_DIV;
682 SetTok (TOK_SHL_ASSIGN);
684 NextTok.Tok = TOK_SHL;
688 NextTok.Tok = TOK_LT;
697 NextTok.Tok = TOK_ASSIGN;
710 SetTok (TOK_SHR_ASSIGN);
712 NextTok.Tok = TOK_SHR;
716 NextTok.Tok = TOK_GT;
735 SetTok (TOK_XOR_ASSIGN);
737 NextTok.Tok = TOK_XOR;
749 SetTok (TOK_BOOL_OR);
752 SetTok (TOK_OR_ASSIGN);
755 NextTok.Tok = TOK_OR;
776 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
777 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
778 * This routine is used for error recovery.
781 while (CurTok.Tok != TOK_CEOF) {
783 /* Check if the current token is in the token list */
785 for (I = 0; I < TokenCount; ++I) {
786 if (CurTok.Tok == TokenList[I]) {
787 /* Found a token in the list */
792 /* Not in the list: Skip it */
800 int Consume (token_t Token, const char* ErrorMsg)
801 /* Eat token if it is the next in the input stream, otherwise print an error
802 * message. Returns true if the token was found and false otherwise.
805 if (CurTok.Tok == Token) {
816 int ConsumeColon (void)
817 /* Check for a colon and skip it. */
819 return Consume (TOK_COLON, "`:' expected");
824 int ConsumeSemi (void)
825 /* Check for a semicolon and skip it. */
827 /* Try do be smart about typos... */
828 if (CurTok.Tok == TOK_SEMI) {
832 Error ("`;' expected");
833 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
842 int ConsumeComma (void)
843 /* Check for a comma and skip it. */
845 /* Try do be smart about typos... */
846 if (CurTok.Tok == TOK_COMMA) {
850 Error ("`,' expected");
851 if (CurTok.Tok == TOK_SEMI) {
860 int ConsumeLParen (void)
861 /* Check for a left parenthesis and skip it */
863 return Consume (TOK_LPAREN, "`(' expected");
868 int ConsumeRParen (void)
869 /* Check for a right parenthesis and skip it */
871 return Consume (TOK_RPAREN, "`)' expected");
876 int ConsumeLBrack (void)
877 /* Check for a left bracket and skip it */
879 return Consume (TOK_LBRACK, "`[' expected");
884 int ConsumeRBrack (void)
885 /* Check for a right bracket and skip it */
887 return Consume (TOK_RBRACK, "`]' expected");
892 int ConsumeLCurly (void)
893 /* Check for a left curly brace and skip it */
895 return Consume (TOK_LCURLY, "`{' expected");
900 int ConsumeRCurly (void)
901 /* Check for a right curly brace and skip it */
903 return Consume (TOK_RCURLY, "`}' expected");