Before printing a token using its kind, check whether it has a symbol and print this...
[cparser] / token.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "token_t.h"
23 #include "symbol_t.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include "symbol.h"
29 #include "lang_features.h"
30 #include "adt/array.h"
31 #include "adt/util.h"
32
33 static symbol_t *token_symbols[T_LAST_TOKEN];
34 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
35
36 const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
37
38 static token_kind_t last_id;
39
40 static symbol_t *intern_register_token(token_kind_t id, const char *string)
41 {
42         assert(id < T_LAST_TOKEN);
43         symbol_t *symbol = symbol_table_insert(string);
44         if (token_symbols[id] == NULL)
45                 token_symbols[id] = symbol;
46         return symbol;
47 }
48
49 static symbol_t *intern_register_pp_token(preprocessor_token_kind_t id, const char *string)
50 {
51         assert(id < TP_LAST_TOKEN);
52         symbol_t *symbol = symbol_table_insert(string);
53         if (pp_token_symbols[id] == NULL)
54                 pp_token_symbols[id] = symbol;
55         return symbol;
56 }
57
58 static void register_token(unsigned mode, token_kind_t id, const char *string)
59 {
60         if (id > 255) {
61                 assert(id >= last_id);
62                 last_id = id;
63         }
64         if (c_mode & mode) {
65                 symbol_t *symbol = intern_register_token(id, string);
66                 symbol->ID = id;
67         }
68 }
69
70 static void register_pp_token(unsigned mode, preprocessor_token_kind_t id,
71                               const char *string)
72 {
73         if (! (c_mode & mode))
74                 return;
75
76         symbol_t *symbol = intern_register_pp_token(id, string);
77         symbol->pp_ID = id;
78 }
79
80 void init_tokens(void)
81 {
82         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
83         memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0]));
84
85 #define T(mode,x,str,val)  register_token(mode, T_##x, str);
86 #define TS(x,str,val)      intern_register_token(T_##x, str);
87 #include "tokens.inc"
88 #undef TS
89 #undef T
90
91 #define T(mode,x,str,val)  register_pp_token(mode, TP_##x, str);
92 #define TS(x,str,val)      intern_register_pp_token(TP_##x, str);
93 #include "tokens_preprocessor.inc"
94 #undef TS
95 #undef T
96 }
97
98 void exit_tokens(void)
99 {
100 }
101
102 void print_token_kind(FILE *f, token_kind_t token_kind)
103 {
104         if(token_kind == T_EOF) {
105                 fputs("end of file", f);
106                 return;
107         }
108
109         if (token_kind >= lengthof(token_symbols)) {
110                 fputs("invalid token", f);
111                 return;
112         }
113
114         const symbol_t *symbol = token_symbols[token_kind];
115         if(symbol != NULL) {
116                 fputs(symbol->string, f);
117         } else {
118                 if (token_kind < 256) {
119                         fputc(token_kind, f);
120                         return;
121                 }
122                 fputs("unknown token", f);
123         }
124 }
125
126 static void print_stringrep(const string_t *string, FILE *f)
127 {
128         for (size_t i = 0; i < string->size; ++i) {
129                 fputc(string->begin[i], f);
130         }
131 }
132
133 void print_token(FILE *f, const token_t *token)
134 {
135         switch(token->kind) {
136         case T_IDENTIFIER:
137                 fprintf(f, "identifier '%s'", token->base.symbol->string);
138                 break;
139         case T_INTEGER:
140         case T_FLOATINGPOINT:
141                 print_token_kind(f, (token_kind_t)token->kind);
142                 fputs(" '", f);
143                 print_stringrep(&token->number.number, f);
144                 if (token->number.suffix.size > 0)
145                         print_stringrep(&token->number.suffix, f);
146                 fputc('\'', f);
147                 break;
148         case T_WIDE_STRING_LITERAL:
149         case T_STRING_LITERAL:
150                 print_token_kind(f, (token_kind_t)token->kind);
151                 fprintf(f, " \"%s\"", token->string.string.begin);
152                 break;
153         case T_CHARACTER_CONSTANT:
154         case T_WIDE_CHARACTER_CONSTANT:
155                 print_token_kind(f, (token_kind_t)token->kind);
156                 fputs(" \'", f);
157                 print_stringrep(&token->string.string, f);
158                 fputs("'", f);
159                 break;
160
161         default:
162                 if (token->base.symbol) {
163                         fprintf(f, "'%s'", token->base.symbol->string);
164                 } else {
165                         fputc('\'', f);
166                         print_token_kind(f, (token_kind_t)token->kind);
167                         fputc('\'', f);
168                 }
169                 break;
170         }
171 }
172
173 void print_pp_token_kind(FILE *f, int token_kind)
174 {
175         if (token_kind == TP_EOF) {
176                 fputs("end of file", f);
177                 return;
178         }
179
180         int token_symbols_len = TP_LAST_TOKEN;
181         if (token_kind < 0 || token_kind >= token_symbols_len) {
182                 fputs("invalid token", f);
183                 return;
184         }
185
186         const symbol_t *symbol = pp_token_symbols[token_kind];
187         if (symbol != NULL) {
188                 fputs(symbol->string, f);
189         } else {
190                 if(token_kind >= 0 && token_kind < 256) {
191                         fputc(token_kind, f);
192                         return;
193                 }
194                 fputs("unknown token", f);
195         }
196 }
197
198 void print_pp_token(FILE *f, const token_t *token)
199 {
200         switch((preprocessor_token_kind_t) token->kind) {
201         case TP_IDENTIFIER:
202                 fprintf(f, "identifier '%s'", token->base.symbol->string);
203                 break;
204         case TP_NUMBER:
205                 fprintf(f, "number '%s'", token->number.number.begin);
206                 break;
207         case TP_STRING_LITERAL:
208                 fprintf(f, "string \"%s\"", token->string.string.begin);
209                 break;
210         default:
211                 print_pp_token_kind(f, (preprocessor_token_kind_t) token->kind);
212                 break;
213         }
214 }
215
216 bool tokens_would_paste(preprocessor_token_kind_t token1,
217                         preprocessor_token_kind_t token2)
218 {
219         char c = token2 < 256 ? (char) token2 : pp_token_symbols[token2]->string[0];
220
221         switch (token1) {
222         case '>': return c == '>' || c == '=';
223         case '<': return c == '<' || c == '=' || c == '%' || c == ':';
224         case '+': return c == '+' || c == '=';
225         case '-': return c == '-' || c == '>';
226         case '/': return c == '/' || c == '=' || c == '*';
227         case '%': return c == ':' || c == '=' || c == '>';
228         case '&': return c == '&' || c == '=';
229         case '|': return c == '|' || c == '=';
230         case ':': return c == ':' || c == '>';
231         case '*': return c == '*' || c == '=';
232         case '.': return c == '.' || c == '%' || token2 == TP_NUMBER;
233         case '#': return c == '#' || c == '%';
234         case TP_GREATERGREATER: return c == '=';
235         case TP_LESSLESS:       return c == '=';
236         case '^':               return c == '=';
237         case '!':               return c == '=';
238         case TP_IDENTIFIER:
239                 return token2 == TP_IDENTIFIER || token2 == TP_NUMBER ||
240                        token2 == TP_CHARACTER_CONSTANT ||
241                        token2 == TP_WIDE_CHARACTER_CONSTANT ||
242                        token2 == TP_WIDE_STRING_LITERAL ||
243                        token2 == TP_STRING_LITERAL; /* L */
244         case TP_NUMBER:
245                 return token2 == TP_NUMBER || token2 == TP_IDENTIFIER ||
246                        token2 == '.' || token2 == '+' || token2 == '-';
247         default:
248                 return false;
249         }
250 }