Merge T_WIDE_CHARACTER_CONSTANT into T_CHARACTER_CONSTANT.
[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/error.h"
32 #include "adt/util.h"
33
34 static symbol_t *token_symbols[T_LAST_TOKEN];
35 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
36
37 const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
38
39 static token_kind_t last_id;
40
41 static symbol_t *intern_register_token(token_kind_t id, const char *string)
42 {
43         assert(id < T_LAST_TOKEN);
44         symbol_t *symbol = symbol_table_insert(string);
45         if (token_symbols[id] == NULL)
46                 token_symbols[id] = symbol;
47         return symbol;
48 }
49
50 static symbol_t *intern_register_pp_token(preprocessor_token_kind_t id, const char *string)
51 {
52         assert(id < TP_LAST_TOKEN);
53         symbol_t *symbol = symbol_table_insert(string);
54         if (pp_token_symbols[id] == NULL)
55                 pp_token_symbols[id] = symbol;
56         return symbol;
57 }
58
59 static void register_token(unsigned mode, token_kind_t id, const char *string)
60 {
61         if (id > 255) {
62                 assert(id >= last_id);
63                 last_id = id;
64         }
65         if (c_mode & mode) {
66                 symbol_t *symbol = intern_register_token(id, string);
67                 symbol->ID = id;
68         }
69 }
70
71 static void register_pp_token(unsigned mode, preprocessor_token_kind_t id,
72                               const char *string)
73 {
74         if (! (c_mode & mode))
75                 return;
76
77         symbol_t *symbol = intern_register_pp_token(id, string);
78         symbol->pp_ID = id;
79 }
80
81 void init_tokens(void)
82 {
83         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
84         memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0]));
85
86 #define T(mode,x,str,val)  register_token(mode, T_##x, str);
87 #define TS(x,str,val)      intern_register_token(T_##x, str);
88 #include "tokens.inc"
89 #undef TS
90 #undef T
91
92 #define T(mode,x,str,val)  register_pp_token(mode, TP_##x, str);
93 #define TS(x,str,val)      intern_register_pp_token(TP_##x, str);
94 #include "tokens_preprocessor.inc"
95 #undef TS
96 #undef T
97 }
98
99 void exit_tokens(void)
100 {
101 }
102
103 void print_token_kind(FILE *f, token_kind_t token_kind)
104 {
105         if(token_kind == T_EOF) {
106                 fputs("end of file", f);
107                 return;
108         }
109
110         if (token_kind >= lengthof(token_symbols)) {
111                 fputs("invalid token", f);
112                 return;
113         }
114
115         const symbol_t *symbol = token_symbols[token_kind];
116         if(symbol != NULL) {
117                 fputs(symbol->string, f);
118         } else {
119                 if (token_kind < 256) {
120                         fputc(token_kind, f);
121                         return;
122                 }
123                 fputs("unknown token", f);
124         }
125 }
126
127 char const *get_string_encoding_prefix(string_encoding_t const enc)
128 {
129         switch (enc) {
130         case STRING_ENCODING_CHAR: return "";
131         case STRING_ENCODING_WIDE: return "L";
132         }
133         panic("invalid string encoding");
134 }
135
136 static void print_stringrep(const string_t *string, FILE *f)
137 {
138         for (size_t i = 0; i < string->size; ++i) {
139                 fputc(string->begin[i], f);
140         }
141 }
142
143 void print_token(FILE *f, const token_t *token)
144 {
145         switch(token->kind) {
146         case T_IDENTIFIER:
147                 fprintf(f, "identifier '%s'", token->base.symbol->string);
148                 break;
149         case T_INTEGER:
150         case T_FLOATINGPOINT:
151                 print_token_kind(f, (token_kind_t)token->kind);
152                 fputs(" '", f);
153                 print_stringrep(&token->number.number, f);
154                 if (token->number.suffix.size > 0)
155                         print_stringrep(&token->number.suffix, f);
156                 fputc('\'', f);
157                 break;
158
159         case T_STRING_LITERAL:
160                 print_token_kind(f, (token_kind_t)token->kind);
161                 fprintf(f, " %s\"%s\"", get_string_encoding_prefix(token->string.encoding), token->string.string.begin);
162                 break;
163
164         case T_CHARACTER_CONSTANT:
165                 print_token_kind(f, (token_kind_t)token->kind);
166                 fprintf(f, " %s'", get_string_encoding_prefix(token->string.encoding));
167                 print_stringrep(&token->string.string, f);
168                 fputs("'", f);
169                 break;
170
171         default:
172                 if (token->base.symbol) {
173                         fprintf(f, "'%s'", token->base.symbol->string);
174                 } else {
175                         fputc('\'', f);
176                         print_token_kind(f, (token_kind_t)token->kind);
177                         fputc('\'', f);
178                 }
179                 break;
180         }
181 }
182
183 void print_pp_token_kind(FILE *f, int token_kind)
184 {
185         if (token_kind == TP_EOF) {
186                 fputs("end of file", f);
187                 return;
188         }
189
190         int token_symbols_len = TP_LAST_TOKEN;
191         if (token_kind < 0 || token_kind >= token_symbols_len) {
192                 fputs("invalid token", f);
193                 return;
194         }
195
196         const symbol_t *symbol = pp_token_symbols[token_kind];
197         if (symbol != NULL) {
198                 fputs(symbol->string, f);
199         } else {
200                 if(token_kind >= 0 && token_kind < 256) {
201                         fputc(token_kind, f);
202                         return;
203                 }
204                 fputs("unknown token", f);
205         }
206 }
207
208 void print_pp_token(FILE *f, const token_t *token)
209 {
210         switch((preprocessor_token_kind_t) token->kind) {
211         case TP_IDENTIFIER:
212                 fprintf(f, "identifier '%s'", token->base.symbol->string);
213                 break;
214         case TP_NUMBER:
215                 fprintf(f, "number '%s'", token->number.number.begin);
216                 break;
217         case TP_STRING_LITERAL:
218                 fprintf(f, "string \"%s\"", token->string.string.begin);
219                 break;
220         default:
221                 print_pp_token_kind(f, (preprocessor_token_kind_t) token->kind);
222                 break;
223         }
224 }
225
226 bool tokens_would_paste(preprocessor_token_kind_t token1,
227                         preprocessor_token_kind_t token2)
228 {
229         char c = token2 < 256 ? (char) token2 : pp_token_symbols[token2]->string[0];
230
231         switch (token1) {
232         case '>': return c == '>' || c == '=';
233         case '<': return c == '<' || c == '=' || c == '%' || c == ':';
234         case '+': return c == '+' || c == '=';
235         case '-': return c == '-' || c == '>';
236         case '/': return c == '/' || c == '=' || c == '*';
237         case '%': return c == ':' || c == '=' || c == '>';
238         case '&': return c == '&' || c == '=';
239         case '|': return c == '|' || c == '=';
240         case ':': return c == ':' || c == '>';
241         case '*': return c == '*' || c == '=';
242         case '.': return c == '.' || c == '%' || token2 == TP_NUMBER;
243         case '#': return c == '#' || c == '%';
244         case TP_GREATERGREATER: return c == '=';
245         case TP_LESSLESS:       return c == '=';
246         case '^':               return c == '=';
247         case '!':               return c == '=';
248         case TP_IDENTIFIER:
249                 return token2 == TP_IDENTIFIER || token2 == TP_NUMBER ||
250                        token2 == TP_CHARACTER_CONSTANT ||
251                        token2 == TP_WIDE_CHARACTER_CONSTANT ||
252                        token2 == TP_WIDE_STRING_LITERAL ||
253                        token2 == TP_STRING_LITERAL; /* L */
254         case TP_NUMBER:
255                 return token2 == TP_NUMBER || token2 == TP_IDENTIFIER ||
256                        token2 == '.' || token2 == '+' || token2 == '-';
257         default:
258                 return false;
259         }
260 }