d5671242abfe145f5efca419f314c45df8c5704d
[cparser] / token.c
1 #include <config.h>
2
3 #include "token_t.h"
4
5 #include <assert.h>
6 #include <stdio.h>
7
8 #include "symbol.h"
9 #include "adt/array.h"
10
11 static symbol_t *token_symbols[T_LAST_TOKEN];
12
13 source_position_t builtin_source_position = { "<built-in>", 0 };
14
15 void init_tokens(void)
16 {
17         symbol_t *symbol;
18
19         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
20
21 #define T(x,str,val)                                               \
22         assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                    \
23         symbol               = symbol_table_insert(str);               \
24         symbol->ID           = T_##x;                                  \
25         token_symbols[T_##x] = symbol;
26
27 #define TS(x,str,val)                                              \
28         assert(T_##x >= 0 && T_##x < T_LAST_TOKEN);                    \
29         symbol               = symbol_table_insert(str);               \
30         token_symbols[T_##x] = symbol;
31
32 #include "tokens.inc"
33
34 #undef TS
35 #undef T
36
37 #define T(x,str,val)                                               \
38         assert(TP_##x >= 0 && TP_##x < TP_LAST_TOKEN);                 \
39         symbol               = symbol_table_insert(str);               \
40         symbol->pp_ID        = TP_##x;
41
42 #include "tokens_preprocessor.inc"
43
44 #undef T
45 }
46
47 void exit_tokens(void)
48 {
49 }
50
51 void print_token_type(FILE *f, token_type_t token_type)
52 {
53         if(token_type == T_EOF) {
54                 fputs("end of file", f);
55                 return;
56         }
57         if(token_type == T_ERROR) {
58                 fputs("error", f);
59                 return;
60         }
61
62         int token_symbols_len = T_LAST_TOKEN;
63         if(token_type < 0 || token_type >= token_symbols_len) {
64                 fputs("invalid token", f);
65                 return;
66         }
67
68         const symbol_t *symbol = token_symbols[token_type];
69         if(symbol != NULL) {
70                 fprintf(f, "'%s'", symbol->string);
71         } else {
72                 if(token_type >= 0 && token_type < 256) {
73                         fprintf(f, "'%c'", token_type);
74                         return;
75                 }
76                 fputs("unknown token", f);
77         }
78 }
79
80 void print_token(FILE *f, const token_t *token)
81 {
82         switch(token->type) {
83         case T_IDENTIFIER:
84                 fprintf(f, "symbol '%s'", token->v.symbol->string);
85                 break;
86         case T_INTEGER:
87                 fprintf(f, "integer number %lld", token->v.intvalue);
88                 break;
89         case T_FLOATINGPOINT:
90                 fprintf(f, "floatingpointer number %LF", token->v.floatvalue);
91                 break;
92         case T_STRING_LITERAL:
93                 fprintf(f, "string '%s'", token->v.string);
94                 break;
95         default:
96                 print_token_type(f, (token_type_t)token->type);
97                 break;
98         }
99 }