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 ParseChar (void)
241 /* Parse a character. Converts \n into EOL, etc. */
247 /* Check for escape chars */
277 /* Hex character constant */
279 val = HexVal (CurC) << 4;
281 C = val | HexVal (CurC); /* Do not translate */
288 while (NextC >= '0' && NextC <= '7' && i++ < 4) {
290 C = (C << 3) | (CurC - '0');
294 Error ("Illegal character constant");
302 /* Skip the character read */
305 /* Do correct sign extension */
306 return SignExtendChar (C);
311 static void CharConst (void)
312 /* Parse a character constant. */
322 /* Check for closing quote */
324 Error ("`\'' expected");
330 /* Setup values and attributes */
331 NextTok.Tok = TOK_CCONST;
333 /* Translate into target charset */
334 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
336 /* Character constants have type int */
337 NextTok.Type = type_int;
342 static void StringConst (void)
343 /* Parse a quoted string */
345 NextTok.IVal = GetLiteralPoolOffs ();
346 NextTok.Tok = TOK_SCONST;
348 /* Be sure to concatenate strings */
349 while (CurC == '\"') {
351 /* Skip the quote char */
354 while (CurC != '\"') {
356 Error ("Unexpected newline");
359 AddLiteralChar (ParseChar ());
362 /* Skip closing quote char if there was one */
365 /* Skip white space, read new input */
370 /* Terminate the string */
371 AddLiteralChar ('\0');
376 void NextToken (void)
377 /* Get next token from input stream */
381 /* We have to skip white space here before shifting tokens, since the
382 * tokens and the current line info is invalid at startup and will get
383 * initialized by reading the first time from the file. Remember if
384 * we were at end of input and handle that later.
386 int GotEOF = (SkipWhite() == 0);
388 /* Current token is the lookahead token */
390 ReleaseLineInfo (CurTok.LI);
394 /* Remember the starting position of the next token */
395 NextTok.LI = UseLineInfo (GetCurLineInfo ());
397 /* Now handle end of input. */
399 /* End of file reached */
400 NextTok.Tok = TOK_CEOF;
404 /* Determine the next token from the lookahead */
405 if (IsDigit (CurC)) {
408 int HaveSuffix; /* True if we have a type suffix */
409 unsigned types; /* Possible types */
412 unsigned long k; /* Value */
416 types = IT_INT | IT_LONG | IT_ULONG;
419 /* Octal or hex constants may also be of type unsigned int */
420 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
421 /* gobble 0 and examin next char */
423 if (toupper (CurC) == 'X') {
425 NextTok.Type = type_uint;
426 NextChar (); /* gobble "x" */
431 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
432 k = k * Base + DigitVal;
435 /* Check for errorneous digits */
436 if (Base == 8 && IsDigit (CurC)) {
437 Error ("Numeric constant contains digits beyond the radix");
438 /* Do error recovery */
441 } while (IsDigit (CurC));
442 } else if (Base != 16 && IsXDigit (CurC)) {
443 Error ("Nondigits in number and not hexadecimal");
446 } while (IsXDigit (CurC));
449 /* Check for a suffix */
451 if (CurC == 'u' || CurC == 'U') {
454 if (toupper (CurC) != 'L') {
455 types = IT_UINT | IT_ULONG;
460 } else if (CurC == 'l' || CurC == 'L') {
463 if (toupper (CurC) != 'U') {
464 types = IT_LONG | IT_ULONG;
473 /* Check the range to determine the type */
475 /* Out of range for int */
477 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
478 * allowed, and we don't have a type specifying suffix, emit a
481 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
482 Warning ("Constant is long");
486 /* Out of range for unsigned int */
489 if (k > 0x7FFFFFFF) {
490 /* Out of range for long int */
494 /* Now set the type string to the smallest type in types */
495 if (types & IT_INT) {
496 NextTok.Type = type_int;
497 } else if (types & IT_UINT) {
498 NextTok.Type = type_uint;
499 } else if (types & IT_LONG) {
500 NextTok.Type = type_long;
502 NextTok.Type = type_ulong;
505 /* Set the value and the token */
507 NextTok.Tok = TOK_ICONST;
513 /* Check for a keyword */
514 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
515 /* Reserved word found */
518 /* No reserved word, check for special symbols */
519 if (token [0] == '_') {
520 /* Special symbols */
521 if (strcmp (token, "__FILE__") == 0) {
522 NextTok.IVal = AddLiteral (GetCurrentFile());
523 NextTok.Tok = TOK_SCONST;
525 } else if (strcmp (token, "__LINE__") == 0) {
526 NextTok.Tok = TOK_ICONST;
527 NextTok.IVal = GetCurrentLine();
528 NextTok.Type = type_int;
530 } else if (strcmp (token, "__func__") == 0) {
531 /* __func__ is only defined in functions */
533 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
534 NextTok.Tok = TOK_SCONST;
540 /* No reserved word but identifier */
541 strcpy (NextTok.Ident, token);
542 NextTok.Tok = TOK_IDENT;
546 /* Monstrous switch statement ahead... */
554 NextTok.Tok = TOK_BOOL_NOT;
565 SetTok (TOK_MOD_ASSIGN);
567 NextTok.Tok = TOK_MOD;
575 SetTok (TOK_BOOL_AND);
578 SetTok (TOK_AND_ASSIGN);
581 NextTok.Tok = TOK_AND;
600 SetTok (TOK_MUL_ASSIGN);
602 NextTok.Tok = TOK_STAR;
613 SetTok (TOK_PLUS_ASSIGN);
616 NextTok.Tok = TOK_PLUS;
631 SetTok (TOK_MINUS_ASSIGN);
634 SetTok (TOK_PTR_REF);
637 NextTok.Tok = TOK_MINUS;
646 SetTok (TOK_ELLIPSIS);
651 NextTok.Tok = TOK_DOT;
658 SetTok (TOK_DIV_ASSIGN);
660 NextTok.Tok = TOK_DIV;
681 SetTok (TOK_SHL_ASSIGN);
683 NextTok.Tok = TOK_SHL;
687 NextTok.Tok = TOK_LT;
696 NextTok.Tok = TOK_ASSIGN;
709 SetTok (TOK_SHR_ASSIGN);
711 NextTok.Tok = TOK_SHR;
715 NextTok.Tok = TOK_GT;
734 SetTok (TOK_XOR_ASSIGN);
736 NextTok.Tok = TOK_XOR;
748 SetTok (TOK_BOOL_OR);
751 SetTok (TOK_OR_ASSIGN);
754 NextTok.Tok = TOK_OR;
775 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
776 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
777 * This routine is used for error recovery.
780 while (CurTok.Tok != TOK_CEOF) {
782 /* Check if the current token is in the token list */
784 for (I = 0; I < TokenCount; ++I) {
785 if (CurTok.Tok == TokenList[I]) {
786 /* Found a token in the list */
791 /* Not in the list: Skip it */
799 int Consume (token_t Token, const char* ErrorMsg)
800 /* Eat token if it is the next in the input stream, otherwise print an error
801 * message. Returns true if the token was found and false otherwise.
804 if (CurTok.Tok == Token) {
815 int ConsumeColon (void)
816 /* Check for a colon and skip it. */
818 return Consume (TOK_COLON, "`:' expected");
823 int ConsumeSemi (void)
824 /* Check for a semicolon and skip it. */
826 /* Try do be smart about typos... */
827 if (CurTok.Tok == TOK_SEMI) {
831 Error ("`;' expected");
832 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
841 int ConsumeComma (void)
842 /* Check for a comma and skip it. */
844 /* Try do be smart about typos... */
845 if (CurTok.Tok == TOK_COMMA) {
849 Error ("`,' expected");
850 if (CurTok.Tok == TOK_SEMI) {
859 int ConsumeLParen (void)
860 /* Check for a left parenthesis and skip it */
862 return Consume (TOK_LPAREN, "`(' expected");
867 int ConsumeRParen (void)
868 /* Check for a right parenthesis and skip it */
870 return Consume (TOK_RPAREN, "`)' expected");
875 int ConsumeLBrack (void)
876 /* Check for a left bracket and skip it */
878 return Consume (TOK_LBRACK, "`[' expected");
883 int ConsumeRBrack (void)
884 /* Check for a right bracket and skip it */
886 return Consume (TOK_RBRACK, "`]' expected");
891 int ConsumeLCurly (void)
892 /* Check for a left curly brace and skip it */
894 return Consume (TOK_LCURLY, "`{' expected");
899 int ConsumeRCurly (void)
900 /* Check for a right curly brace and skip it */
902 return Consume (TOK_RCURLY, "`}' expected");