1fbaafd0410933055172733dd48c3096de58d2ab
[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
32 static symbol_t *token_symbols[T_LAST_TOKEN];
33 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
34
35 const source_position_t builtin_source_position = { "<built-in>", 0 };
36
37 void init_tokens(void)
38 {
39         symbol_t *symbol;
40         int       last_id = -2;
41
42         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
43         memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0]));
44
45 #define T(mode,x,str,val)                                          \
46         if (T_##x > 255) {                                             \
47                 assert(T_##x >= last_id);                                  \
48                 last_id = T_##x;                                           \
49         }                                                              \
50         if (c_mode & (mode)) {                                         \
51                 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                \
52                 symbol               = symbol_table_insert(str);           \
53                 symbol->ID           = T_##x;                              \
54                 if (token_symbols[T_##x] == NULL)                          \
55                         token_symbols[T_##x] = symbol;                         \
56         }
57
58 #define TS(x,str,val)                                              \
59         assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                    \
60         symbol               = symbol_table_insert(str);               \
61         if (token_symbols[T_##x] == NULL)                              \
62                 token_symbols[T_##x] = symbol;                             \
63
64 #include "tokens.inc"
65
66 #undef TS
67 #undef T
68
69 #define T(mode,x,str,val)                                          \
70         if (c_mode & (mode)) {                                         \
71                 assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN);             \
72                 symbol                   = symbol_table_insert(str);       \
73                 symbol->pp_ID            = TP_##x;                         \
74                 if (pp_token_symbols[TP_##x] == NULL)                      \
75                         pp_token_symbols[TP_##x] = symbol;                     \
76         }
77
78 #define TS(x,str,val)                                              \
79         assert(TP_##x >= 0 && TP_##x < T_LAST_TOKEN);                  \
80         symbol                   = symbol_table_insert(str);           \
81         if (pp_token_symbols[TP_##x] == NULL)                          \
82                 pp_token_symbols[TP_##x] = symbol;
83
84 #include "tokens_preprocessor.inc"
85
86 #undef T
87 }
88
89 void exit_tokens(void)
90 {
91 }
92
93 void print_token_type(FILE *f, token_type_t token_type)
94 {
95         if(token_type == T_EOF) {
96                 fputs("end of file", f);
97                 return;
98         }
99         if(token_type == T_ERROR) {
100                 fputs("error", f);
101                 return;
102         }
103
104         int token_symbols_len = T_LAST_TOKEN;
105         if(token_type < 0 || token_type >= token_symbols_len) {
106                 fputs("invalid token", f);
107                 return;
108         }
109
110         const symbol_t *symbol = token_symbols[token_type];
111         if(symbol != NULL) {
112                 fputs(symbol->string, f);
113         } else {
114                 if(token_type >= 0 && token_type < 256) {
115                         fputc(token_type, f);
116                         return;
117                 }
118                 fputs("unknown token", f);
119         }
120 }
121
122 symbol_t *get_token_symbol(const token_t *token)
123 {
124         return token_symbols[token->type];
125 }
126
127 static void print_stringrep(const string_t *string, FILE *f)
128 {
129         for (size_t i = 0; i < string->size; ++i) {
130                 fputc(string->begin[i], f);
131         }
132 }
133
134 void print_token(FILE *f, const token_t *token)
135 {
136         switch(token->type) {
137         case T_IDENTIFIER:
138                 fprintf(f, "identifier '%s'", token->symbol->string);
139                 break;
140         case T_INTEGER:
141         case T_INTEGER_OCTAL:
142         case T_INTEGER_HEXADECIMAL:
143         case T_FLOATINGPOINT:
144         case T_FLOATINGPOINT_HEXADECIMAL:
145                 print_token_type(f, (token_type_t)token->type);
146                 fputs(" '", f);
147                 print_stringrep(&token->literal, f);
148                 if (token->symbol != NULL)
149                         fputs(token->symbol->string, f);
150                 fputc('\'', f);
151                 break;
152         case T_WIDE_STRING_LITERAL:
153         case T_STRING_LITERAL:
154                 print_token_type(f, (token_type_t)token->type);
155                 fprintf(f, " \"%s\"", token->literal.begin);
156                 break;
157         case T_CHARACTER_CONSTANT:
158         case T_WIDE_CHARACTER_CONSTANT:
159                 print_token_type(f, (token_type_t)token->type);
160                 fputs(" \'", f);
161                 print_stringrep(&token->literal, f);
162                 fputs("'", f);
163                 break;
164         default:
165                 fputc('\'', f);
166                 print_token_type(f, (token_type_t)token->type);
167                 fputc('\'', f);
168                 break;
169         }
170 }
171
172 void print_pp_token_type(FILE *f, int token_type)
173 {
174         if (token_type == TP_EOF) {
175                 fputs("end of file", f);
176                 return;
177         }
178         if (token_type == TP_ERROR) {
179                 fputs("error", f);
180                 return;
181         }
182
183         int token_symbols_len = TP_LAST_TOKEN;
184         if (token_type < 0 || token_type >= token_symbols_len) {
185                 fputs("invalid token", f);
186                 return;
187         }
188
189         const symbol_t *symbol = pp_token_symbols[token_type];
190         if (symbol != NULL) {
191                 fputs(symbol->string, f);
192         } else {
193                 if(token_type >= 0 && token_type < 256) {
194                         fputc(token_type, f);
195                         return;
196                 }
197                 fputs("unknown token", f);
198         }
199 }
200
201 void print_pp_token(FILE *f, const token_t *token)
202 {
203         switch((preprocessor_token_type_t) token->type) {
204         case TP_IDENTIFIER:
205                 fprintf(f, "identifier '%s'", token->symbol->string);
206                 break;
207         case TP_NUMBER:
208                 fprintf(f, "number '%s'", token->literal.begin);
209                 break;
210         case TP_STRING_LITERAL:
211                 fprintf(f, "string \"%s\"", token->literal.begin);
212                 break;
213         default:
214                 print_pp_token_type(f, (preprocessor_token_type_t) token->type);
215                 break;
216         }
217 }