Merge remote branch 'origin/mesa_7_7_branch'
[mesa.git] / src / glsl / apps / tokenise.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33
34
35 int
36 main(int argc,
37 char *argv[])
38 {
39 FILE *in;
40 long size;
41 char *inbuf;
42 struct sl_pp_purify_options options;
43 struct sl_pp_context *context;
44 struct sl_pp_token_info *tokens;
45 FILE *out;
46 unsigned int i;
47
48 if (argc != 3) {
49 return 1;
50 }
51
52 in = fopen(argv[1], "rb");
53 if (!in) {
54 return 1;
55 }
56
57 fseek(in, 0, SEEK_END);
58 size = ftell(in);
59 fseek(in, 0, SEEK_SET);
60
61 out = fopen(argv[2], "wb");
62 if (!out) {
63 fclose(in);
64 return 1;
65 }
66
67 inbuf = malloc(size + 1);
68 if (!inbuf) {
69 fprintf(out, "$OOMERROR\n");
70
71 fclose(out);
72 fclose(in);
73 return 1;
74 }
75
76 if (fread(inbuf, 1, size, in) != size) {
77 fprintf(out, "$READERROR\n");
78
79 free(inbuf);
80 fclose(out);
81 fclose(in);
82 return 1;
83 }
84 inbuf[size] = '\0';
85
86 fclose(in);
87
88 memset(&options, 0, sizeof(options));
89
90 context = sl_pp_context_create(inbuf, &options);
91 if (!context) {
92 fprintf(out, "$CONTEXERROR\n");
93
94 free(inbuf);
95 fclose(out);
96 return 1;
97 }
98
99 if (sl_pp_tokenise(context, &tokens)) {
100 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
101
102 sl_pp_context_destroy(context);
103 free(inbuf);
104 fclose(out);
105 return 1;
106 }
107
108 free(inbuf);
109
110 for (i = 0; tokens[i].token != SL_PP_EOF; i++) {
111 switch (tokens[i].token) {
112 case SL_PP_WHITESPACE:
113 break;
114
115 case SL_PP_NEWLINE:
116 fprintf(out, "\n");
117 break;
118
119 case SL_PP_HASH:
120 fprintf(out, "# ");
121 break;
122
123 case SL_PP_COMMA:
124 fprintf(out, ", ");
125 break;
126
127 case SL_PP_SEMICOLON:
128 fprintf(out, "; ");
129 break;
130
131 case SL_PP_LBRACE:
132 fprintf(out, "{ ");
133 break;
134
135 case SL_PP_RBRACE:
136 fprintf(out, "} ");
137 break;
138
139 case SL_PP_LPAREN:
140 fprintf(out, "( ");
141 break;
142
143 case SL_PP_RPAREN:
144 fprintf(out, ") ");
145 break;
146
147 case SL_PP_LBRACKET:
148 fprintf(out, "[ ");
149 break;
150
151 case SL_PP_RBRACKET:
152 fprintf(out, "] ");
153 break;
154
155 case SL_PP_DOT:
156 fprintf(out, ". ");
157 break;
158
159 case SL_PP_INCREMENT:
160 fprintf(out, "++ ");
161 break;
162
163 case SL_PP_ADDASSIGN:
164 fprintf(out, "+= ");
165 break;
166
167 case SL_PP_PLUS:
168 fprintf(out, "+ ");
169 break;
170
171 case SL_PP_DECREMENT:
172 fprintf(out, "-- ");
173 break;
174
175 case SL_PP_SUBASSIGN:
176 fprintf(out, "-= ");
177 break;
178
179 case SL_PP_MINUS:
180 fprintf(out, "- ");
181 break;
182
183 case SL_PP_BITNOT:
184 fprintf(out, "~ ");
185 break;
186
187 case SL_PP_NOTEQUAL:
188 fprintf(out, "!= ");
189 break;
190
191 case SL_PP_NOT:
192 fprintf(out, "! ");
193 break;
194
195 case SL_PP_MULASSIGN:
196 fprintf(out, "*= ");
197 break;
198
199 case SL_PP_STAR:
200 fprintf(out, "* ");
201 break;
202
203 case SL_PP_DIVASSIGN:
204 fprintf(out, "/= ");
205 break;
206
207 case SL_PP_SLASH:
208 fprintf(out, "/ ");
209 break;
210
211 case SL_PP_MODASSIGN:
212 fprintf(out, "%%= ");
213 break;
214
215 case SL_PP_MODULO:
216 fprintf(out, "%% ");
217 break;
218
219 case SL_PP_LSHIFTASSIGN:
220 fprintf(out, "<<= ");
221 break;
222
223 case SL_PP_LSHIFT:
224 fprintf(out, "<< ");
225 break;
226
227 case SL_PP_LESSEQUAL:
228 fprintf(out, "<= ");
229 break;
230
231 case SL_PP_LESS:
232 fprintf(out, "< ");
233 break;
234
235 case SL_PP_RSHIFTASSIGN:
236 fprintf(out, ">>= ");
237 break;
238
239 case SL_PP_RSHIFT:
240 fprintf(out, ">> ");
241 break;
242
243 case SL_PP_GREATEREQUAL:
244 fprintf(out, ">= ");
245 break;
246
247 case SL_PP_GREATER:
248 fprintf(out, "> ");
249 break;
250
251 case SL_PP_EQUAL:
252 fprintf(out, "== ");
253 break;
254
255 case SL_PP_ASSIGN:
256 fprintf(out, "= ");
257 break;
258
259 case SL_PP_AND:
260 fprintf(out, "&& ");
261 break;
262
263 case SL_PP_BITANDASSIGN:
264 fprintf(out, "&= ");
265 break;
266
267 case SL_PP_BITAND:
268 fprintf(out, "& ");
269 break;
270
271 case SL_PP_XOR:
272 fprintf(out, "^^ ");
273 break;
274
275 case SL_PP_BITXORASSIGN:
276 fprintf(out, "^= ");
277 break;
278
279 case SL_PP_BITXOR:
280 fprintf(out, "^ ");
281 break;
282
283 case SL_PP_OR:
284 fprintf(out, "|| ");
285 break;
286
287 case SL_PP_BITORASSIGN:
288 fprintf(out, "|= ");
289 break;
290
291 case SL_PP_BITOR:
292 fprintf(out, "| ");
293 break;
294
295 case SL_PP_QUESTION:
296 fprintf(out, "? ");
297 break;
298
299 case SL_PP_COLON:
300 fprintf(out, ": ");
301 break;
302
303 case SL_PP_IDENTIFIER:
304 fprintf(out, "%s ", sl_pp_context_cstr(context, tokens[i].data.identifier));
305 break;
306
307 case SL_PP_UINT:
308 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._uint));
309 break;
310
311 case SL_PP_FLOAT:
312 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._float));
313 break;
314
315 case SL_PP_OTHER:
316 if (tokens[i].data.other == '\'') {
317 fprintf(out, "'\\'' ");
318 } else {
319 fprintf(out, "'%c' ", tokens[i].data.other);
320 }
321 break;
322
323 default:
324 assert(0);
325 }
326 }
327
328 sl_pp_context_destroy(context);
329 free(tokens);
330 fclose(out);
331
332 return 0;
333 }