Move the symbol to token_base_t.
[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         default:
161                 fputc('\'', f);
162                 print_token_kind(f, (token_kind_t)token->kind);
163                 fputc('\'', f);
164                 break;
165         }
166 }
167
168 void print_pp_token_kind(FILE *f, int token_kind)
169 {
170         if (token_kind == TP_EOF) {
171                 fputs("end of file", f);
172                 return;
173         }
174
175         int token_symbols_len = TP_LAST_TOKEN;
176         if (token_kind < 0 || token_kind >= token_symbols_len) {
177                 fputs("invalid token", f);
178                 return;
179         }
180
181         const symbol_t *symbol = pp_token_symbols[token_kind];
182         if (symbol != NULL) {
183                 fputs(symbol->string, f);
184         } else {
185                 if(token_kind >= 0 && token_kind < 256) {
186                         fputc(token_kind, f);
187                         return;
188                 }
189                 fputs("unknown token", f);
190         }
191 }
192
193 void print_pp_token(FILE *f, const token_t *token)
194 {
195         switch((preprocessor_token_kind_t) token->kind) {
196         case TP_IDENTIFIER:
197                 fprintf(f, "identifier '%s'", token->base.symbol->string);
198                 break;
199         case TP_NUMBER:
200                 fprintf(f, "number '%s'", token->number.number.begin);
201                 break;
202         case TP_STRING_LITERAL:
203                 fprintf(f, "string \"%s\"", token->string.string.begin);
204                 break;
205         default:
206                 print_pp_token_kind(f, (preprocessor_token_kind_t) token->kind);
207                 break;
208         }
209 }
210
211 bool tokens_would_paste(preprocessor_token_kind_t token1,
212                         preprocessor_token_kind_t token2)
213 {
214         char c = token2 < 256 ? (char) token2 : pp_token_symbols[token2]->string[0];
215
216         switch (token1) {
217         case '>': return c == '>' || c == '=';
218         case '<': return c == '<' || c == '=' || c == '%' || c == ':';
219         case '+': return c == '+' || c == '=';
220         case '-': return c == '-' || c == '>';
221         case '/': return c == '/' || c == '=' || c == '*';
222         case '%': return c == ':' || c == '=' || c == '>';
223         case '&': return c == '&' || c == '=';
224         case '|': return c == '|' || c == '=';
225         case ':': return c == ':' || c == '>';
226         case '*': return c == '*' || c == '=';
227         case '.': return c == '.' || c == '%' || token2 == TP_NUMBER;
228         case '#': return c == '#' || c == '%';
229         case TP_GREATERGREATER: return c == '=';
230         case TP_LESSLESS:       return c == '=';
231         case '^':               return c == '=';
232         case '!':               return c == '=';
233         case TP_IDENTIFIER:
234                 return token2 == TP_IDENTIFIER || token2 == TP_NUMBER ||
235                        token2 == TP_CHARACTER_CONSTANT ||
236                        token2 == TP_WIDE_CHARACTER_CONSTANT ||
237                        token2 == TP_WIDE_STRING_LITERAL ||
238                        token2 == TP_STRING_LITERAL; /* L */
239         case TP_NUMBER:
240                 return token2 == TP_NUMBER || token2 == TP_IDENTIFIER ||
241                        token2 == '.' || token2 == '+' || token2 == '-';
242         default:
243                 return false;
244         }
245 }