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 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 
intern_register_token(token_kind_t id,const char * string)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 
register_token(unsigned mode,token_kind_t id,const char * string)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 
register_pp_token(pp_token_kind_t const id,char const * const string)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 
init_tokens(void)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, x, str);
73 #include "tokens.inc"
74 #undef T
75 
76 #define T(token) register_pp_token(TP_##token, #token);
77 #include "tokens_preprocessor.inc"
78 #undef T
79 }
80 
exit_tokens(void)81 void exit_tokens(void)
82 {
83 }
84 
print_token_kind(FILE * f,token_kind_t token_kind)85 void print_token_kind(FILE *f, token_kind_t token_kind)
86 {
87 	if (token_kind >= lengthof(token_symbols)) {
88 		fputs("invalid token", f);
89 		return;
90 	}
91 
92 	fputs(token_symbols[token_kind]->string, f);
93 }
94 
get_string_encoding_prefix(string_encoding_t const enc)95 char const *get_string_encoding_prefix(string_encoding_t const enc)
96 {
97 	switch (enc) {
98 	case STRING_ENCODING_CHAR:   return "";
99 	case STRING_ENCODING_CHAR16: return "u";
100 	case STRING_ENCODING_CHAR32: return "U";
101 	case STRING_ENCODING_UTF8:   return "u8";
102 	case STRING_ENCODING_WIDE:   return "L";
103 	}
104 	panic("invalid string encoding");
105 }
106 
print_token(FILE * f,const token_t * token)107 void print_token(FILE *f, const token_t *token)
108 {
109 	char        delim = '\'';
110 	char const *enc   = "";
111 	char const *val;
112 	switch (token->kind) {
113 	case T_IDENTIFIER:
114 	case T_MACRO_PARAMETER:
115 	case T_UNKNOWN_CHAR:
116 		val = token->base.symbol->string;
117 		break;
118 
119 	case T_STRING_LITERAL:
120 		delim = '"';
121 		/* FALLTHROUGH */
122 	case T_CHARACTER_CONSTANT:
123 		enc = get_string_encoding_prefix(token->literal.string.encoding);
124 		/* FALLTHROUGH */
125 	case T_NUMBER:
126 		val = token->literal.string.begin;
127 		break;
128 
129 	default: {
130 		char const *kind  = (token->base.symbol ? token->base.symbol : token_symbols[token->kind])->string;
131 		fprintf(f, "'%s'", kind);
132 		return;
133 	}
134 	}
135 	fprintf(f, "%s %s%c%s%c", token_symbols[token->kind]->string, enc, delim, val, delim);
136 }
137 
tokens_would_paste(token_kind_t token1,token_kind_t token2)138 bool tokens_would_paste(token_kind_t token1, token_kind_t token2)
139 {
140 	char const c = token_symbols[token2]->string[0];
141 
142 	switch (token1) {
143 	case '>': return c == '>' || c == '=';
144 	case '<': return c == '<' || c == '=' || c == '%' || c == ':';
145 	case '+': return c == '+' || c == '=';
146 	case '-': return c == '-' || c == '>';
147 	case '/': return c == '/' || c == '=' || c == '*';
148 	case '%': return c == ':' || c == '=' || c == '>';
149 	case '&': return c == '&' || c == '=';
150 	case '|': return c == '|' || c == '=';
151 	case ':': return c == ':' || c == '>';
152 	case '*': return c == '*' || c == '=';
153 	case '.': return c == '.' || c == '%' || token2 == T_NUMBER;
154 	case '#': return c == '#' || c == '%';
155 	case T_GREATERGREATER: return c == '=';
156 	case T_LESSLESS:       return c == '=';
157 	case '^':              return c == '=';
158 	case '!':              return c == '=';
159 
160 	case T_IDENTIFIER:
161 		return token2 == T_CHARACTER_CONSTANT ||
162 		       token2 == T_IDENTIFIER         ||
163 		       token2 == T_NUMBER             ||
164 		       token2 == T_STRING_LITERAL; /* L */
165 
166 	case T_NUMBER:
167 		return token2 == T_IDENTIFIER || token2 == T_NUMBER ||
168 		       token2 == '.' || token2 == '+' || token2 == '-';
169 
170 	default:
171 		return false;
172 	}
173 }
174