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 { "__AX__", TOK_AX, TT_C },
85 { "__A__", TOK_A, 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 { "__near__", TOK_NEAR, TT_C },
94 { "asm", TOK_ASM, TT_EXT },
95 { "auto", TOK_AUTO, TT_C },
96 { "break", TOK_BREAK, TT_C },
97 { "case", TOK_CASE, TT_C },
98 { "char", TOK_CHAR, TT_C },
99 { "const", TOK_CONST, TT_C },
100 { "continue", TOK_CONTINUE, TT_C },
101 { "default", TOK_DEFAULT, TT_C },
102 { "do", TOK_DO, TT_C },
103 { "double", TOK_DOUBLE, TT_C },
104 { "else", TOK_ELSE, TT_C },
105 { "enum", TOK_ENUM, TT_C },
106 { "extern", TOK_EXTERN, TT_C },
107 { "far", TOK_FAR, TT_EXT },
108 { "fastcall", TOK_FASTCALL, TT_EXT },
109 { "float", TOK_FLOAT, TT_C },
110 { "for", TOK_FOR, TT_C },
111 { "goto", TOK_GOTO, TT_C },
112 { "if", TOK_IF, TT_C },
113 { "int", TOK_INT, TT_C },
114 { "long", TOK_LONG, TT_C },
115 { "near", TOK_NEAR, TT_EXT },
116 { "register", TOK_REGISTER, TT_C },
117 { "restrict", TOK_RESTRICT, TT_C },
118 { "return", TOK_RETURN, TT_C },
119 { "short", TOK_SHORT, TT_C },
120 { "signed", TOK_SIGNED, TT_C },
121 { "sizeof", TOK_SIZEOF, TT_C },
122 { "static", TOK_STATIC, TT_C },
123 { "struct", TOK_STRUCT, TT_C },
124 { "switch", TOK_SWITCH, TT_C },
125 { "typedef", TOK_TYPEDEF, TT_C },
126 { "union", TOK_UNION, TT_C },
127 { "unsigned", TOK_UNSIGNED, TT_C },
128 { "void", TOK_VOID, TT_C },
129 { "volatile", TOK_VOLATILE, TT_C },
130 { "while", TOK_WHILE, TT_C },
132 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
136 /* Stuff for determining the type of an integer constant */
140 #define IT_ULONG 0x08
144 /*****************************************************************************/
146 /*****************************************************************************/
150 static int CmpKey (const void* Key, const void* Elem)
151 /* Compare function for bsearch */
153 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
158 static int FindKey (const char* Key)
159 /* Find a keyword and return the token. Return IDENT if the token is not a
164 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
165 if (K && (K->Type != TT_EXT || ANSI == 0)) {
174 static int SkipWhite (void)
175 /* Skip white space in the input stream, reading and preprocessing new lines
176 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
181 if (NextLine () == 0) {
186 if (IsSpace (CurC)) {
196 void SymName (char* s)
197 /* Get symbol from input stream */
201 if (k != MAX_IDENTLEN) {
206 } while (IsIdent (CurC) || IsDigit (CurC));
213 /* Get symbol from input stream or return 0 if not a symbol. */
215 if (IsIdent (CurC)) {
225 static void UnknownChar (char C)
226 /* Error message for unknown character */
228 Error ("Invalid input character with code %02X", C & 0xFF);
229 NextChar (); /* Skip */
234 static void SetTok (int tok)
235 /* Set NextTok.Tok and bump line ptr */
243 static int ParseChar (void)
244 /* Parse a character. Converts \n into EOL, etc. */
250 /* Check for escape chars */
289 /* Hex character constant */
291 Val = HexVal (CurC) << 4;
293 C = Val | HexVal (CurC); /* Do not translate */
306 while (NextC >= '0' && NextC <= '7' && ++I <= 3) {
308 Val = (Val << 3) | (CurC - '0');
312 Error ("Character constant out of range");
317 Error ("Illegal character constant");
319 /* Try to do error recovery, otherwise the compiler will spit
320 * out thousands of errors in this place and abort.
322 if (CurC != '\'' && CurC != '\0') {
323 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
333 /* Skip the character read */
336 /* Do correct sign extension */
337 return SignExtendChar (C);
342 static void CharConst (void)
343 /* Parse a character constant. */
353 /* Check for closing quote */
355 Error ("`\'' expected");
361 /* Setup values and attributes */
362 NextTok.Tok = TOK_CCONST;
364 /* Translate into target charset */
365 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
367 /* Character constants have type int */
368 NextTok.Type = type_int;
373 static void StringConst (void)
374 /* Parse a quoted string */
376 NextTok.IVal = GetLiteralPoolOffs ();
377 NextTok.Tok = TOK_SCONST;
379 /* Be sure to concatenate strings */
380 while (CurC == '\"') {
382 /* Skip the quote char */
385 while (CurC != '\"') {
387 Error ("Unexpected newline");
390 AddLiteralChar (ParseChar ());
393 /* Skip closing quote char if there was one */
396 /* Skip white space, read new input */
401 /* Terminate the string */
402 AddLiteralChar ('\0');
407 void NextToken (void)
408 /* Get next token from input stream */
412 /* We have to skip white space here before shifting tokens, since the
413 * tokens and the current line info is invalid at startup and will get
414 * initialized by reading the first time from the file. Remember if
415 * we were at end of input and handle that later.
417 int GotEOF = (SkipWhite() == 0);
419 /* Current token is the lookahead token */
421 ReleaseLineInfo (CurTok.LI);
425 /* When reading the first time from the file, the line info in NextTok,
426 * which was copied to CurTok is invalid. Since the information from
427 * the token is used for error messages, we must make it valid.
429 if (CurTok.LI == 0) {
430 CurTok.LI = UseLineInfo (GetCurLineInfo ());
433 /* Remember the starting position of the next token */
434 NextTok.LI = UseLineInfo (GetCurLineInfo ());
436 /* Now handle end of input. */
438 /* End of file reached */
439 NextTok.Tok = TOK_CEOF;
443 /* Determine the next token from the lookahead */
444 if (IsDigit (CurC)) {
447 int HaveSuffix; /* True if we have a type suffix */
448 unsigned types; /* Possible types */
451 unsigned long k; /* Value */
455 types = IT_INT | IT_LONG | IT_ULONG;
458 /* Octal or hex constants may also be of type unsigned int */
459 types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
460 /* gobble 0 and examin next char */
462 if (toupper (CurC) == 'X') {
464 NextTok.Type = type_uint;
465 NextChar (); /* gobble "x" */
470 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
471 k = k * Base + DigitVal;
474 /* Check for errorneous digits */
475 if (Base == 8 && IsDigit (CurC)) {
476 Error ("Numeric constant contains digits beyond the radix");
477 /* Do error recovery */
480 } while (IsDigit (CurC));
481 } else if (Base != 16 && IsXDigit (CurC)) {
482 Error ("Nondigits in number and not hexadecimal");
485 } while (IsXDigit (CurC));
488 /* Check for a suffix */
490 if (CurC == 'u' || CurC == 'U') {
493 if (toupper (CurC) != 'L') {
494 types = IT_UINT | IT_ULONG;
499 } else if (CurC == 'l' || CurC == 'L') {
502 if (toupper (CurC) != 'U') {
503 types = IT_LONG | IT_ULONG;
512 /* Check the range to determine the type */
514 /* Out of range for int */
516 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
517 * allowed, and we don't have a type specifying suffix, emit a
520 if (k <= 0xFFFF && (types & IT_UINT) == 0 && !HaveSuffix) {
521 Warning ("Constant is long");
525 /* Out of range for unsigned int */
528 if (k > 0x7FFFFFFF) {
529 /* Out of range for long int */
533 /* Now set the type string to the smallest type in types */
534 if (types & IT_INT) {
535 NextTok.Type = type_int;
536 } else if (types & IT_UINT) {
537 NextTok.Type = type_uint;
538 } else if (types & IT_LONG) {
539 NextTok.Type = type_long;
541 NextTok.Type = type_ulong;
544 /* Set the value and the token */
546 NextTok.Tok = TOK_ICONST;
552 /* Check for a keyword */
553 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
554 /* Reserved word found */
557 /* No reserved word, check for special symbols */
558 if (token [0] == '_') {
559 /* Special symbols */
560 if (strcmp (token, "__FILE__") == 0) {
561 NextTok.IVal = AddLiteral (GetCurrentFile());
562 NextTok.Tok = TOK_SCONST;
564 } else if (strcmp (token, "__LINE__") == 0) {
565 NextTok.Tok = TOK_ICONST;
566 NextTok.IVal = GetCurrentLine();
567 NextTok.Type = type_int;
569 } else if (strcmp (token, "__func__") == 0) {
570 /* __func__ is only defined in functions */
572 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
573 NextTok.Tok = TOK_SCONST;
579 /* No reserved word but identifier */
580 strcpy (NextTok.Ident, token);
581 NextTok.Tok = TOK_IDENT;
585 /* Monstrous switch statement ahead... */
593 NextTok.Tok = TOK_BOOL_NOT;
604 SetTok (TOK_MOD_ASSIGN);
606 NextTok.Tok = TOK_MOD;
614 SetTok (TOK_BOOL_AND);
617 SetTok (TOK_AND_ASSIGN);
620 NextTok.Tok = TOK_AND;
639 SetTok (TOK_MUL_ASSIGN);
641 NextTok.Tok = TOK_STAR;
652 SetTok (TOK_PLUS_ASSIGN);
655 NextTok.Tok = TOK_PLUS;
670 SetTok (TOK_MINUS_ASSIGN);
673 SetTok (TOK_PTR_REF);
676 NextTok.Tok = TOK_MINUS;
685 SetTok (TOK_ELLIPSIS);
690 NextTok.Tok = TOK_DOT;
697 SetTok (TOK_DIV_ASSIGN);
699 NextTok.Tok = TOK_DIV;
720 SetTok (TOK_SHL_ASSIGN);
722 NextTok.Tok = TOK_SHL;
726 NextTok.Tok = TOK_LT;
735 NextTok.Tok = TOK_ASSIGN;
748 SetTok (TOK_SHR_ASSIGN);
750 NextTok.Tok = TOK_SHR;
754 NextTok.Tok = TOK_GT;
773 SetTok (TOK_XOR_ASSIGN);
775 NextTok.Tok = TOK_XOR;
787 SetTok (TOK_BOOL_OR);
790 SetTok (TOK_OR_ASSIGN);
793 NextTok.Tok = TOK_OR;
814 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
815 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
816 * This routine is used for error recovery.
819 while (CurTok.Tok != TOK_CEOF) {
821 /* Check if the current token is in the token list */
823 for (I = 0; I < TokenCount; ++I) {
824 if (CurTok.Tok == TokenList[I]) {
825 /* Found a token in the list */
830 /* Not in the list: Skip it */
838 int Consume (token_t Token, const char* ErrorMsg)
839 /* Eat token if it is the next in the input stream, otherwise print an error
840 * message. Returns true if the token was found and false otherwise.
843 if (CurTok.Tok == Token) {
854 int ConsumeColon (void)
855 /* Check for a colon and skip it. */
857 return Consume (TOK_COLON, "`:' expected");
862 int ConsumeSemi (void)
863 /* Check for a semicolon and skip it. */
865 /* Try do be smart about typos... */
866 if (CurTok.Tok == TOK_SEMI) {
870 Error ("`;' expected");
871 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
880 int ConsumeComma (void)
881 /* Check for a comma and skip it. */
883 /* Try do be smart about typos... */
884 if (CurTok.Tok == TOK_COMMA) {
888 Error ("`,' expected");
889 if (CurTok.Tok == TOK_SEMI) {
898 int ConsumeLParen (void)
899 /* Check for a left parenthesis and skip it */
901 return Consume (TOK_LPAREN, "`(' expected");
906 int ConsumeRParen (void)
907 /* Check for a right parenthesis and skip it */
909 return Consume (TOK_RPAREN, "`)' expected");
914 int ConsumeLBrack (void)
915 /* Check for a left bracket and skip it */
917 return Consume (TOK_LBRACK, "`[' expected");
922 int ConsumeRBrack (void)
923 /* Check for a right bracket and skip it */
925 return Consume (TOK_RBRACK, "`]' expected");
930 int ConsumeLCurly (void)
931 /* Check for a left curly brace and skip it */
933 return Consume (TOK_LCURLY, "`{' expected");
938 int ConsumeRCurly (void)
939 /* Check for a right curly brace and skip it */
941 return Consume (TOK_RCURLY, "`}' expected");