add license comments
[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
24 #include <assert.h>
25 #include <stdio.h>
26
27 #include "symbol.h"
28 #include "lang_features.h"
29 #include "adt/array.h"
30
31 static symbol_t *token_symbols[T_LAST_TOKEN];
32
33 source_position_t builtin_source_position = { "<built-in>", 0 };
34
35 void init_tokens(void)
36 {
37         symbol_t *symbol;
38         int       last_id = -2;
39
40         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
41
42 #define T(mode,x,str,val)                                          \
43         if (T_##x > 255) {                                             \
44                 assert(T_##x >= last_id);                                  \
45                 last_id = T_##x;                                           \
46         }                                                              \
47         if (c_mode & (mode)) {                                         \
48                 assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                \
49                 symbol               = symbol_table_insert(str);           \
50                 symbol->ID           = T_##x;                              \
51                 token_symbols[T_##x] = symbol;                             \
52         }
53
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;
58
59 #include "tokens.inc"
60
61 #undef TS
62 #undef T
63
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;
68
69 #include "tokens_preprocessor.inc"
70
71 #undef T
72 }
73
74 void exit_tokens(void)
75 {
76 }
77
78 void print_token_type(FILE *f, token_type_t token_type)
79 {
80         if(token_type == T_EOF) {
81                 fputs("end of file", f);
82                 return;
83         }
84         if(token_type == T_ERROR) {
85                 fputs("error", f);
86                 return;
87         }
88
89         int token_symbols_len = T_LAST_TOKEN;
90         if(token_type < 0 || token_type >= token_symbols_len) {
91                 fputs("invalid token", f);
92                 return;
93         }
94
95         const symbol_t *symbol = token_symbols[token_type];
96         if(symbol != NULL) {
97                 fprintf(f, "'%s'", symbol->string);
98         } else {
99                 if(token_type >= 0 && token_type < 256) {
100                         fprintf(f, "'%c'", token_type);
101                         return;
102                 }
103                 fputs("unknown token", f);
104         }
105 }
106
107 void print_token(FILE *f, const token_t *token)
108 {
109         switch(token->type) {
110         case T_IDENTIFIER:
111                 fprintf(f, "symbol '%s'", token->v.symbol->string);
112                 break;
113         case T_INTEGER:
114                 fprintf(f, "integer number %lld", token->v.intvalue);
115                 break;
116         case T_FLOATINGPOINT:
117                 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
118                 break;
119         case T_STRING_LITERAL:
120                 fprintf(f, "string '%s'", token->v.string.begin); /* TODO suboptimal */
121                 break;
122         default:
123                 print_token_type(f, (token_type_t)token->type);
124                 break;
125         }
126 }