2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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 };
37 void init_tokens(void)
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]));
45 #define T(mode,x,str,val) \
47 assert(T_##x >= last_id); \
50 if (c_mode & (mode)) { \
51 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
52 symbol = symbol_table_insert(str); \
54 if (token_symbols[T_##x] == NULL) \
55 token_symbols[T_##x] = symbol; \
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; \
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; \
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;
84 #include "tokens_preprocessor.inc"
89 void exit_tokens(void)
93 void print_token_type(FILE *f, token_type_t token_type)
95 if(token_type == T_EOF) {
96 fputs("end of file", f);
99 if(token_type == T_ERROR) {
104 int token_symbols_len = T_LAST_TOKEN;
105 if(token_type < 0 || token_type >= token_symbols_len) {
106 fputs("invalid token", f);
110 const symbol_t *symbol = token_symbols[token_type];
112 fputs(symbol->string, f);
114 if(token_type >= 0 && token_type < 256) {
115 fputc(token_type, f);
118 fputs("unknown token", f);
122 void print_token(FILE *f, const token_t *token)
124 switch(token->type) {
126 fprintf(f, "identifier '%s'", token->v.symbol->string);
129 fprintf(f, "integer number '%lld'", token->v.intvalue);
131 case T_FLOATINGPOINT:
132 fprintf(f, "floating-point number '%LF'", token->v.floatvalue);
134 case T_STRING_LITERAL:
135 fprintf(f, "string \"%s\"", token->v.string.begin);
139 print_token_type(f, (token_type_t)token->type);
145 void print_pp_token_type(FILE *f, int token_type)
147 if (token_type == TP_EOF) {
148 fputs("end of file", f);
151 if (token_type == TP_ERROR) {
156 int token_symbols_len = TP_LAST_TOKEN;
157 if (token_type < 0 || token_type >= token_symbols_len) {
158 fputs("invalid token", f);
162 const symbol_t *symbol = pp_token_symbols[token_type];
163 if (symbol != NULL) {
164 fputs(symbol->string, f);
166 if(token_type >= 0 && token_type < 256) {
167 fputc(token_type, f);
170 fputs("unknown token", f);
174 void print_pp_token(FILE *f, const token_t *token)
176 switch((preprocessor_token_type_t) token->type) {
178 fprintf(f, "identifier '%s'", token->v.symbol->string);
181 fprintf(f, "number '%s'", token->v.string.begin);
183 case TP_STRING_LITERAL:
184 fprintf(f, "string \"%s\"", token->v.string.begin);
187 print_pp_token_type(f, (preprocessor_token_type_t) token->type);