commit some files I forgot
[cparser] / token.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "token_t.h"
23 #include "symbol_t.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include "symbol.h"
29 #include "lang_features.h"
30 #include "adt/array.h"
31
32 static symbol_t *token_symbols[T_LAST_TOKEN];
33 static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
34
35 source_position_t builtin_source_position = { "<built-in>", 0 };
36
37 void init_tokens(void)
38 {
39         symbol_t *symbol;
40         int       last_id = -2;
41
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]));
44
45 #define T(mode,x,str,val)                                          \
46         if (T_##x > 255) {                                             \
47                 assert(T_##x >= last_id);                                  \
48                 last_id = T_##x;                                           \
49         }                                                              \
50         if (c_mode & (mode)) {                                         \
51                 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                \
52                 symbol               = symbol_table_insert(str);           \
53                 symbol->ID           = T_##x;                              \
54                 token_symbols[T_##x] = symbol;                             \
55         }
56
57 #define TS(x,str,val)                                              \
58         assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                    \
59         symbol               = symbol_table_insert(str);               \
60         token_symbols[T_##x] = symbol;
61
62 #include "tokens.inc"
63
64 #undef TS
65 #undef T
66
67 #define T(mode,x,str,val)                                          \
68         assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN);                 \
69         symbol                   = symbol_table_insert(str);           \
70         symbol->pp_ID            = TP_##x;                             \
71         pp_token_symbols[TP_##x] = symbol;
72
73 #define TS(x,str,val)                                              \
74         assert(TP_##x >= 0 && TP_##x < T_LAST_TOKEN);                  \
75         symbol                   = symbol_table_insert(str);           \
76         pp_token_symbols[TP_##x] = symbol;
77
78 #include "tokens_preprocessor.inc"
79
80 #undef T
81 }
82
83 void exit_tokens(void)
84 {
85 }
86
87 void print_token_type(FILE *f, token_type_t token_type)
88 {
89         if(token_type == T_EOF) {
90                 fputs("end of file", f);
91                 return;
92         }
93         if(token_type == T_ERROR) {
94                 fputs("error", f);
95                 return;
96         }
97
98         int token_symbols_len = T_LAST_TOKEN;
99         if(token_type < 0 || token_type >= token_symbols_len) {
100                 fputs("invalid token", f);
101                 return;
102         }
103
104         const symbol_t *symbol = token_symbols[token_type];
105         if(symbol != NULL) {
106                 fprintf(f, "'%s'", symbol->string);
107         } else {
108                 if(token_type >= 0 && token_type < 256) {
109                         fprintf(f, "'%c'", token_type);
110                         return;
111                 }
112                 fputs("unknown token", f);
113         }
114 }
115
116 void print_token(FILE *f, const token_t *token)
117 {
118         switch(token->type) {
119         case T_IDENTIFIER:
120                 fprintf(f, "symbol '%s'", token->v.symbol->string);
121                 break;
122         case T_INTEGER:
123                 fprintf(f, "integer number %lld", token->v.intvalue);
124                 break;
125         case T_FLOATINGPOINT:
126                 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
127                 break;
128         case T_STRING_LITERAL:
129                 fprintf(f, "string '%s'", token->v.string.begin); /* TODO suboptimal */
130                 break;
131         default:
132                 print_token_type(f, (token_type_t)token->type);
133                 break;
134         }
135 }
136
137 void print_pp_token_type(FILE *f, preprocessor_token_type_t token_type)
138 {
139         if(token_type == TP_EOF) {
140                 fputs("end of file", f);
141                 return;
142         }
143         if(token_type == TP_ERROR) {
144                 fputs("error", f);
145                 return;
146         }
147
148         int token_symbols_len = TP_LAST_TOKEN;
149         if(token_type < 0 || token_type >= token_symbols_len) {
150                 fputs("invalid token", f);
151                 return;
152         }
153
154         const symbol_t *symbol = pp_token_symbols[token_type];
155         if(symbol != NULL) {
156                 fprintf(f, "'%s'", symbol->string);
157         } else {
158                 if(token_type >= 0 && token_type < 256) {
159                         fprintf(f, "'%c'", token_type);
160                         return;
161                 }
162                 fputs("unknown token", f);
163         }
164 }
165
166 void print_pp_token(FILE *f, const token_t *token)
167 {
168         switch((preprocessor_token_type_t) token->type) {
169         case TP_IDENTIFIER:
170                 fprintf(f, "symbol '%s'", token->v.symbol->string);
171                 break;
172         case TP_NUMBER:
173                 fprintf(f, "number %s", token->v.string.begin);
174                 break;
175         case TP_STRING_LITERAL:
176                 fprintf(f, "string '%s'", token->v.string.begin);
177                 break;
178         default:
179                 print_pp_token_type(f, (preprocessor_token_type_t) token->type);
180                 break;
181         }
182 }