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
28 #include "lang_features.h"
29 #include "adt/array.h"
31 static symbol_t *token_symbols[T_LAST_TOKEN];
33 source_position_t builtin_source_position = { "<built-in>", 0 };
35 void init_tokens(void)
40 memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
42 #define T(mode,x,str,val) \
44 assert(T_##x >= last_id); \
47 if (c_mode & (mode)) { \
48 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
49 symbol = symbol_table_insert(str); \
51 token_symbols[T_##x] = symbol; \
54 #define TS(x,str,val) \
55 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN); \
56 symbol = symbol_table_insert(str); \
57 token_symbols[T_##x] = symbol;
64 #define T(mode,x,str,val) \
65 assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN); \
66 symbol = symbol_table_insert(str); \
67 symbol->pp_ID = TP_##x;
69 #include "tokens_preprocessor.inc"
74 void exit_tokens(void)
78 void print_token_type(FILE *f, token_type_t token_type)
80 if(token_type == T_EOF) {
81 fputs("end of file", f);
84 if(token_type == T_ERROR) {
89 int token_symbols_len = T_LAST_TOKEN;
90 if(token_type < 0 || token_type >= token_symbols_len) {
91 fputs("invalid token", f);
95 const symbol_t *symbol = token_symbols[token_type];
97 fprintf(f, "'%s'", symbol->string);
99 if(token_type >= 0 && token_type < 256) {
100 fprintf(f, "'%c'", token_type);
103 fputs("unknown token", f);
107 void print_token(FILE *f, const token_t *token)
109 switch(token->type) {
111 fprintf(f, "symbol '%s'", token->v.symbol->string);
114 fprintf(f, "integer number %lld", token->v.intvalue);
116 case T_FLOATINGPOINT:
117 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
119 case T_STRING_LITERAL:
120 fprintf(f, "string '%s'", token->v.string.begin); /* TODO suboptimal */
123 print_token_type(f, (token_type_t)token->type);