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