2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
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.
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.
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
29 #include "lang_features.h"
30 #include "adt/array.h"
33 static symbol_t *token_symbols[T_LAST_TOKEN];
34 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
36 const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
38 static token_kind_t last_id;
40 static symbol_t *intern_register_token(token_kind_t id, const char *string)
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;
49 static symbol_t *intern_register_pp_token(preprocessor_token_kind_t id, const char *string)
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;
58 static void register_token(unsigned mode, token_kind_t id, const char *string)
61 assert(id >= last_id);
65 symbol_t *symbol = intern_register_token(id, string);
70 static void register_pp_token(unsigned mode, preprocessor_token_kind_t id,
73 if (! (c_mode & mode))
76 symbol_t *symbol = intern_register_pp_token(id, string);
80 void init_tokens(void)
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]));
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);
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"
98 void exit_tokens(void)
102 void print_token_kind(FILE *f, token_kind_t token_kind)
104 if(token_kind == T_EOF) {
105 fputs("end of file", f);
109 if (token_kind >= lengthof(token_symbols)) {
110 fputs("invalid token", f);
114 const symbol_t *symbol = token_symbols[token_kind];
116 fputs(symbol->string, f);
118 if (token_kind < 256) {
119 fputc(token_kind, f);
122 fputs("unknown token", f);
126 static void print_stringrep(const string_t *string, FILE *f)
128 for (size_t i = 0; i < string->size; ++i) {
129 fputc(string->begin[i], f);
133 void print_token(FILE *f, const token_t *token)
135 switch(token->kind) {
137 fprintf(f, "identifier '%s'", token->identifier.symbol->string);
140 case T_FLOATINGPOINT:
141 print_token_kind(f, (token_kind_t)token->kind);
143 print_stringrep(&token->number.number, f);
144 if (token->number.suffix.size > 0)
145 print_stringrep(&token->number.suffix, f);
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);
153 case T_CHARACTER_CONSTANT:
154 case T_WIDE_CHARACTER_CONSTANT:
155 print_token_kind(f, (token_kind_t)token->kind);
157 print_stringrep(&token->string.string, f);
162 print_token_kind(f, (token_kind_t)token->kind);
168 void print_pp_token_kind(FILE *f, int token_kind)
170 if (token_kind == TP_EOF) {
171 fputs("end of file", f);
175 int token_symbols_len = TP_LAST_TOKEN;
176 if (token_kind < 0 || token_kind >= token_symbols_len) {
177 fputs("invalid token", f);
181 const symbol_t *symbol = pp_token_symbols[token_kind];
182 if (symbol != NULL) {
183 fputs(symbol->string, f);
185 if(token_kind >= 0 && token_kind < 256) {
186 fputc(token_kind, f);
189 fputs("unknown token", f);
193 void print_pp_token(FILE *f, const token_t *token)
195 switch((preprocessor_token_kind_t) token->kind) {
197 fprintf(f, "identifier '%s'", token->identifier.symbol->string);
200 fprintf(f, "number '%s'", token->number.number.begin);
202 case TP_STRING_LITERAL:
203 fprintf(f, "string \"%s\"", token->string.string.begin);
206 print_pp_token_kind(f, (preprocessor_token_kind_t) token->kind);
211 bool tokens_would_paste(preprocessor_token_kind_t token1,
212 preprocessor_token_kind_t token2)
214 char c = token2 < 256 ? (char) token2 : pp_token_symbols[token2]->string[0];
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 == '=';
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 */
240 return token2 == TP_NUMBER || token2 == TP_IDENTIFIER ||
241 token2 == '.' || token2 == '+' || token2 == '-';