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"
32 static symbol_t *token_symbols[T_LAST_TOKEN];
33 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
35 const source_position_t builtin_source_position = { "<built-in>", 0 };
39 static symbol_t *intern_register_token(token_type_t id, const char *string)
41 assert(0 <= id && id < T_LAST_TOKEN);
42 symbol_t *symbol = symbol_table_insert(string);
43 if (token_symbols[id] == NULL)
44 token_symbols[id] = symbol;
48 static symbol_t *intern_register_pp_token(token_type_t id, const char *string)
50 assert(0 <= id && id < TP_LAST_TOKEN);
51 symbol_t *symbol = symbol_table_insert(string);
52 if (pp_token_symbols[id] == NULL)
53 pp_token_symbols[id] = symbol;
57 static void register_token(unsigned mode, token_type_t id, const char *string)
60 assert(id >= last_id);
64 symbol_t *symbol = intern_register_token(id, string);
69 static void register_pp_token(unsigned mode, token_type_t id,
72 if (! (c_mode & mode))
75 symbol_t *symbol = intern_register_token(id, string);
79 void init_tokens(void)
81 memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
82 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_type(FILE *f, token_type_t token_type)
105 if(token_type == T_EOF) {
106 fputs("end of file", f);
109 if(token_type == T_ERROR) {
114 int token_symbols_len = T_LAST_TOKEN;
115 if(token_type < 0 || token_type >= token_symbols_len) {
116 fputs("invalid token", f);
120 const symbol_t *symbol = token_symbols[token_type];
122 fputs(symbol->string, f);
124 if(token_type >= 0 && token_type < 256) {
125 fputc(token_type, f);
128 fputs("unknown token", f);
132 symbol_t *get_token_symbol(const token_t *token)
134 return token_symbols[token->type];
137 static void print_stringrep(const string_t *string, FILE *f)
139 for (size_t i = 0; i < string->size; ++i) {
140 fputc(string->begin[i], f);
144 void print_token(FILE *f, const token_t *token)
146 switch(token->type) {
148 fprintf(f, "identifier '%s'", token->symbol->string);
151 case T_INTEGER_OCTAL:
152 case T_INTEGER_HEXADECIMAL:
153 case T_FLOATINGPOINT:
154 case T_FLOATINGPOINT_HEXADECIMAL:
155 print_token_type(f, (token_type_t)token->type);
157 print_stringrep(&token->literal, f);
158 if (token->symbol != NULL)
159 fputs(token->symbol->string, f);
162 case T_WIDE_STRING_LITERAL:
163 case T_STRING_LITERAL:
164 print_token_type(f, (token_type_t)token->type);
165 fprintf(f, " \"%s\"", token->literal.begin);
167 case T_CHARACTER_CONSTANT:
168 case T_WIDE_CHARACTER_CONSTANT:
169 print_token_type(f, (token_type_t)token->type);
171 print_stringrep(&token->literal, f);
176 print_token_type(f, (token_type_t)token->type);
182 void print_pp_token_type(FILE *f, int token_type)
184 if (token_type == TP_EOF) {
185 fputs("end of file", f);
188 if (token_type == TP_ERROR) {
193 int token_symbols_len = TP_LAST_TOKEN;
194 if (token_type < 0 || token_type >= token_symbols_len) {
195 fputs("invalid token", f);
199 const symbol_t *symbol = pp_token_symbols[token_type];
200 if (symbol != NULL) {
201 fputs(symbol->string, f);
203 if(token_type >= 0 && token_type < 256) {
204 fputc(token_type, f);
207 fputs("unknown token", f);
211 void print_pp_token(FILE *f, const token_t *token)
213 switch((preprocessor_token_type_t) token->type) {
215 fprintf(f, "identifier '%s'", token->symbol->string);
218 fprintf(f, "number '%s'", token->literal.begin);
220 case TP_STRING_LITERAL:
221 fprintf(f, "string \"%s\"", token->literal.begin);
224 print_pp_token_type(f, (preprocessor_token_type_t) token->type);