preprocessor: keep unknown chars in preproc mode
[cparser] / token.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 #include "adt/error.h"
32 #include "adt/util.h"
33
34 static symbol_t *token_symbols[T_LAST_TOKEN];
35
36 const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
37
38 static token_kind_t last_id;
39
40 static symbol_t *intern_register_token(token_kind_t id, const char *string)
41 {
42         assert(id < T_LAST_TOKEN);
43         symbol_t *symbol = symbol_table_insert(string);
44         if (token_symbols[id] == NULL)
45                 token_symbols[id] = symbol;
46         return symbol;
47 }
48
49 static void register_token(unsigned mode, token_kind_t id, const char *string)
50 {
51         if (id > 255) {
52                 assert(id >= last_id);
53                 last_id = id;
54         }
55         if (c_mode & mode) {
56                 symbol_t *symbol = intern_register_token(id, string);
57                 symbol->ID = id;
58         }
59 }
60
61 static void register_pp_token(pp_token_kind_t const id, char const *const string)
62 {
63         assert(id < TP_LAST_TOKEN);
64         symbol_t *const symbol = symbol_table_insert(string);
65         symbol->pp_ID = id;
66 }
67
68 void init_tokens(void)
69 {
70         memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
71
72 #define T(mode,x,str,val)  register_token(mode, T_##x, str);
73 #define TS(x,str,val)      intern_register_token(T_##x, str);
74 #include "tokens.inc"
75 #undef TS
76 #undef T
77
78 #define T(token) register_pp_token(TP_##token, #token);
79 #include "tokens_preprocessor.inc"
80 #undef T
81 }
82
83 void exit_tokens(void)
84 {
85 }
86
87 void print_token_kind(FILE *f, token_kind_t token_kind)
88 {
89         if(token_kind == T_EOF) {
90                 fputs("end of file", f);
91                 return;
92         }
93
94         if (token_kind >= lengthof(token_symbols)) {
95                 fputs("invalid token", f);
96                 return;
97         }
98
99         fputs(token_symbols[token_kind]->string, f);
100 }
101
102 char const *get_string_encoding_prefix(string_encoding_t const enc)
103 {
104         switch (enc) {
105         case STRING_ENCODING_CHAR: return "";
106         case STRING_ENCODING_WIDE: return "L";
107         }
108         panic("invalid string encoding");
109 }
110
111 static void print_stringrep(const string_t *string, FILE *f)
112 {
113         for (size_t i = 0; i < string->size; ++i) {
114                 fputc(string->begin[i], f);
115         }
116 }
117
118 void print_token(FILE *f, const token_t *token)
119 {
120         switch(token->kind) {
121         case T_IDENTIFIER:
122         case T_UNKNOWN_CHAR:
123                 print_token_kind(f, (token_kind_t)token->kind);
124                 fprintf(f, " '%s'", token->base.symbol->string);
125                 break;
126
127         case T_NUMBER:
128                 fprintf(f, "number '%s'", token->literal.string.begin);
129                 break;
130
131                 char delim;
132         case T_STRING_LITERAL:     delim = '"';  goto print_string;
133         case T_CHARACTER_CONSTANT: delim = '\''; goto print_string;
134 print_string:
135                 print_token_kind(f, (token_kind_t)token->kind);
136                 fprintf(f, " %s%c", get_string_encoding_prefix(token->literal.string.encoding), delim);
137                 print_stringrep(&token->literal.string, f);
138                 fputc(delim, f);
139                 break;
140
141         default:
142                 if (token->base.symbol) {
143                         fprintf(f, "'%s'", token->base.symbol->string);
144                 } else {
145                         fputc('\'', f);
146                         print_token_kind(f, (token_kind_t)token->kind);
147                         fputc('\'', f);
148                 }
149                 break;
150         }
151 }
152
153 bool tokens_would_paste(token_kind_t token1, token_kind_t token2)
154 {
155         char const c = token_symbols[token2]->string[0];
156
157         switch (token1) {
158         case '>': return c == '>' || c == '=';
159         case '<': return c == '<' || c == '=' || c == '%' || c == ':';
160         case '+': return c == '+' || c == '=';
161         case '-': return c == '-' || c == '>';
162         case '/': return c == '/' || c == '=' || c == '*';
163         case '%': return c == ':' || c == '=' || c == '>';
164         case '&': return c == '&' || c == '=';
165         case '|': return c == '|' || c == '=';
166         case ':': return c == ':' || c == '>';
167         case '*': return c == '*' || c == '=';
168         case '.': return c == '.' || c == '%' || token2 == T_NUMBER;
169         case '#': return c == '#' || c == '%';
170         case T_GREATERGREATER: return c == '=';
171         case T_LESSLESS:       return c == '=';
172         case '^':              return c == '=';
173         case '!':              return c == '=';
174
175         case T_IDENTIFIER:
176                 return token2 == T_CHARACTER_CONSTANT ||
177                        token2 == T_IDENTIFIER         ||
178                        token2 == T_NUMBER             ||
179                        token2 == T_STRING_LITERAL; /* L */
180
181         case T_NUMBER:
182                 return token2 == T_IDENTIFIER || token2 == T_NUMBER ||
183                        token2 == '.' || token2 == '+' || token2 == '-';
184
185         default:
186                 return false;
187         }
188 }