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"
31 #include "adt/error.h"
34 static symbol_t *token_symbols[T_LAST_TOKEN];
35 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
37 const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
39 static token_kind_t last_id;
41 static symbol_t *intern_register_token(token_kind_t id, const char *string)
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;
50 static symbol_t *intern_register_pp_token(token_kind_t id, const char *string)
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;
59 static void register_token(unsigned mode, token_kind_t id, const char *string)
62 assert(id >= last_id);
66 symbol_t *symbol = intern_register_token(id, string);
71 static void register_pp_token(unsigned mode, token_kind_t id,
74 if (! (c_mode & mode))
77 symbol_t *symbol = intern_register_pp_token(id, string);
81 void init_tokens(void)
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]));
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);
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"
99 void exit_tokens(void)
103 void print_token_kind(FILE *f, token_kind_t token_kind)
105 if(token_kind == T_EOF) {
106 fputs("end of file", f);
110 if (token_kind >= lengthof(token_symbols)) {
111 fputs("invalid token", f);
115 const symbol_t *symbol = token_symbols[token_kind];
117 fputs(symbol->string, f);
119 if (token_kind < 256) {
120 fputc(token_kind, f);
123 fputs("unknown token", f);
127 char const *get_string_encoding_prefix(string_encoding_t const enc)
130 case STRING_ENCODING_CHAR: return "";
131 case STRING_ENCODING_WIDE: return "L";
133 panic("invalid string encoding");
136 static void print_stringrep(const string_t *string, FILE *f)
138 for (size_t i = 0; i < string->size; ++i) {
139 fputc(string->begin[i], f);
143 void print_token(FILE *f, const token_t *token)
145 switch(token->kind) {
147 fprintf(f, "identifier '%s'", token->base.symbol->string);
151 fprintf(f, "number '%s'", token->literal.string.begin);
155 case T_STRING_LITERAL: delim = '"'; goto print_string;
156 case T_CHARACTER_CONSTANT: delim = '\''; goto print_string;
158 print_token_kind(f, (token_kind_t)token->kind);
159 fprintf(f, " %s%c", get_string_encoding_prefix(token->literal.string.encoding), delim);
160 print_stringrep(&token->literal.string, f);
165 if (token->base.symbol) {
166 fprintf(f, "'%s'", token->base.symbol->string);
169 print_token_kind(f, (token_kind_t)token->kind);
176 void print_pp_token_kind(FILE *f, int token_kind)
178 if (token_kind == TP_EOF) {
179 fputs("end of file", f);
183 int token_symbols_len = TP_LAST_TOKEN;
184 if (token_kind < 0 || token_kind >= token_symbols_len) {
185 fputs("invalid token", f);
189 const symbol_t *symbol = pp_token_symbols[token_kind];
190 if (symbol != NULL) {
191 fputs(symbol->string, f);
193 if(token_kind >= 0 && token_kind < 256) {
194 fputc(token_kind, f);
197 fputs("unknown token", f);
201 void print_pp_token(FILE *f, const token_t *token)
203 switch ((token_kind_t)token->kind) {
205 fprintf(f, "identifier '%s'", token->base.symbol->string);
208 fprintf(f, "number '%s'", token->literal.string.begin);
210 case TP_STRING_LITERAL:
211 fprintf(f, "string \"%s\"", token->literal.string.begin);
214 print_pp_token_kind(f, (token_kind_t) token->kind);
219 bool tokens_would_paste(token_kind_t token1, token_kind_t token2)
221 char c = token2 < 256 ? (char) token2 : pp_token_symbols[token2]->string[0];
224 case '>': return c == '>' || c == '=';
225 case '<': return c == '<' || c == '=' || c == '%' || c == ':';
226 case '+': return c == '+' || c == '=';
227 case '-': return c == '-' || c == '>';
228 case '/': return c == '/' || c == '=' || c == '*';
229 case '%': return c == ':' || c == '=' || c == '>';
230 case '&': return c == '&' || c == '=';
231 case '|': return c == '|' || c == '=';
232 case ':': return c == ':' || c == '>';
233 case '*': return c == '*' || c == '=';
234 case '.': return c == '.' || c == '%' || token2 == TP_NUMBER;
235 case '#': return c == '#' || c == '%';
236 case TP_GREATERGREATER: return c == '=';
237 case TP_LESSLESS: return c == '=';
238 case '^': return c == '=';
239 case '!': return c == '=';
241 return token2 == TP_IDENTIFIER || token2 == TP_NUMBER ||
242 token2 == TP_CHARACTER_CONSTANT ||
243 token2 == TP_STRING_LITERAL; /* L */
245 return token2 == TP_NUMBER || token2 == TP_IDENTIFIER ||
246 token2 == '.' || token2 == '+' || token2 == '-';