64f81eeddd99d191e40a84e1d5859cbc3fc398b4
[platform/upstream/isl.git] / isl_stream.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <ctype.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <isl_ctx.h>
14 #include "isl_stream.h"
15
16 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
17         int line, int col, unsigned on_new_line)
18 {
19         struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
20         if (!tok)
21                 return NULL;
22         tok->line = line;
23         tok->col = col;
24         tok->on_new_line = on_new_line;
25         return tok;
26 }
27
28 void isl_token_free(struct isl_token *tok)
29 {
30         if (!tok)
31                 return;
32         if (tok->type == ISL_TOKEN_VALUE)
33                 isl_int_clear(tok->u.v);
34         else if (tok->type == ISL_TOKEN_IDENT)
35                 free(tok->u.s);
36         free(tok);
37 }
38
39 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
40 {
41         int line = tok ? tok->line : s->line;
42         int col = tok ? tok->col : s->col;
43         fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
44         if (tok) {
45                 if (tok->type < 256)
46                         fprintf(stderr, "got '%c'\n", tok->type);
47                 else
48                         fprintf(stderr, "got token type %d\n", tok->type);
49         }
50 }
51
52 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
53 {
54         int i;
55         struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
56         if (!s)
57                 return NULL;
58         s->ctx = ctx;
59         isl_ctx_ref(s->ctx);
60         s->size = 256;
61         s->file = NULL;
62         s->str = NULL;
63         s->buffer = isl_alloc_array(ctx, char, s->size);
64         if (!s->buffer)
65                 goto error;
66         s->len = 0;
67         s->line = 1;
68         s->col = 0;
69         s->eof = 0;
70         s->c = -1;
71         for (i = 0; i < 5; ++i)
72                 s->tokens[i] = NULL;
73         s->n_token = 0;
74         return s;
75 error:
76         isl_stream_free(s);
77         return NULL;
78 }
79
80 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
81 {
82         struct isl_stream *s = isl_stream_new(ctx);
83         if (!s)
84                 return NULL;
85         s->file = file;
86         return s;
87 }
88
89 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
90 {
91     struct isl_stream *s = isl_stream_new(ctx);
92     s->str = str;
93     return s;
94 }
95
96 static int isl_stream_getc(struct isl_stream *s)
97 {
98         int c;
99         if (s->eof)
100                 return -1;
101         if (s->file)
102                 c = fgetc(s->file);
103         else {
104                 c = *s->str++;
105                 if (c == '\0')
106                         c = -1;
107         }
108         if (c == -1)
109                 s->eof = 1;
110         if (!s->eof) {
111                 if (s->c == '\n') {
112                         s->line++;
113                         s->col = 0;
114                 } else
115                         s->col++;
116         }
117         s->c = c;
118         return c;
119 }
120
121 static void isl_stream_ungetc(struct isl_stream *s, int c)
122 {
123         if (s->file)
124                 ungetc(c, s->file);
125         else
126                 --s->str;
127         s->c = -1;
128 }
129
130 static int isl_stream_push_char(struct isl_stream *s, int c)
131 {
132         if (s->len >= s->size) {
133                 s->size = (3*s->size)/2;
134                 s->buffer = isl_realloc_array(ctx, s->buffer, char, s->size);
135                 if (!s->buffer)
136                         return -1;
137         }
138         s->buffer[s->len++] = c;
139         return 0;
140 }
141
142 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
143 {
144         isl_assert(s->ctx, s->n_token < 5, return);
145         s->tokens[s->n_token++] = tok;
146 }
147
148 static struct isl_token *next_token(struct isl_stream *s, int same_line)
149 {
150         int c;
151         struct isl_token *tok = NULL;
152         int line, col;
153         int old_line = s->line;
154
155         if (s->n_token) {
156                 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
157                         return NULL;
158                 return s->tokens[--s->n_token];
159         }
160
161         if (same_line && s->c == '\n')
162                 return NULL;
163
164         s->len = 0;
165
166         /* skip spaces and comment lines */
167         while ((c = isl_stream_getc(s)) != -1) {
168                 if (c == '#') {
169                         while ((c = isl_stream_getc(s)) != -1 && c != '\n')
170                                 /* nothing */
171                                 ;
172                         if (c == -1 || (same_line && c == '\n'))
173                                 break;
174                 } else if (!isspace(c) || (same_line && c == '\n'))
175                         break;
176         }
177
178         line = s->line;
179         col = s->col;
180
181         if (c == -1 || (same_line && c == '\n'))
182                 return NULL;
183         if (c == '(' ||
184             c == ')' ||
185             c == '+' ||
186             c == '/' ||
187             c == '*' ||
188             c == '^' ||
189             c == '=' ||
190             c == ',' ||
191             c == ';' ||
192             c == '[' ||
193             c == ']' ||
194             c == '{' ||
195             c == '}') {
196                 tok = isl_token_new(s->ctx, line, col, old_line != line);
197                 if (!tok)
198                         return NULL;
199                 tok->type = (enum isl_token_type)c;
200                 return tok;
201         }
202         if (c == '-') {
203                 int c;
204                 if ((c = isl_stream_getc(s)) == '>') {
205                         tok = isl_token_new(s->ctx, line, col, old_line != line);
206                         if (!tok)
207                                 return NULL;
208                         tok->type = ISL_TOKEN_TO;
209                         return tok;
210                 }
211                 if (c != -1)
212                         isl_stream_ungetc(s, c);
213                 if (!isdigit(c)) {
214                         tok = isl_token_new(s->ctx, line, col, old_line != line);
215                         if (!tok)
216                                 return NULL;
217                         tok->type = (enum isl_token_type) '-';
218                         return tok;
219                 }
220         }
221         if (c == '-' || isdigit(c)) {
222                 tok = isl_token_new(s->ctx, line, col, old_line != line);
223                 if (!tok)
224                         return NULL;
225                 tok->type = ISL_TOKEN_VALUE;
226                 isl_int_init(tok->u.v);
227                 if (isl_stream_push_char(s, c))
228                         goto error;
229                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
230                         if (isl_stream_push_char(s, c))
231                                 goto error;
232                 if (c != -1)
233                         isl_stream_ungetc(s, c);
234                 isl_stream_push_char(s, '\0');
235                 isl_int_read(tok->u.v, s->buffer);
236                 return tok;
237         }
238         if (isalpha(c)) {
239                 tok = isl_token_new(s->ctx, line, col, old_line != line);
240                 if (!tok)
241                         return NULL;
242                 isl_stream_push_char(s, c);
243                 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
244                         isl_stream_push_char(s, c);
245                 if (c != -1)
246                         isl_stream_ungetc(s, c);
247                 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
248                         isl_stream_push_char(s, c);
249                 if (c != -1)
250                         isl_stream_ungetc(s, c);
251                 isl_stream_push_char(s, '\0');
252                 if (!strcasecmp(s->buffer, "exists"))
253                         tok->type = ISL_TOKEN_EXISTS;
254                 else if (!strcasecmp(s->buffer, "and"))
255                         tok->type = ISL_TOKEN_AND;
256                 else if (!strcasecmp(s->buffer, "or"))
257                         tok->type = ISL_TOKEN_OR;
258                 else {
259                         tok->type = ISL_TOKEN_IDENT;
260                         tok->u.s = strdup(s->buffer);
261                 }
262                 return tok;
263         }
264         if (c == ':') {
265                 int c;
266                 tok = isl_token_new(s->ctx, line, col, old_line != line);
267                 if (!tok)
268                         return NULL;
269                 if ((c = isl_stream_getc(s)) == '=') {
270                         tok->type = ISL_TOKEN_DEF;
271                         return tok;
272                 }
273                 if (c != -1)
274                         isl_stream_ungetc(s, c);
275                 tok->type = ':';
276                 return tok;
277         }
278         if (c == '>') {
279                 int c;
280                 tok = isl_token_new(s->ctx, line, col, old_line != line);
281                 if (!tok)
282                         return NULL;
283                 if ((c = isl_stream_getc(s)) == '=') {
284                         tok->type = ISL_TOKEN_GE;
285                         return tok;
286                 }
287                 if (c != -1)
288                         isl_stream_ungetc(s, c);
289                 tok->type = ISL_TOKEN_GT;
290                 return tok;
291         }
292         if (c == '<') {
293                 int c;
294                 tok = isl_token_new(s->ctx, line, col, old_line != line);
295                 if (!tok)
296                         return NULL;
297                 if ((c = isl_stream_getc(s)) == '=') {
298                         tok->type = ISL_TOKEN_LE;
299                         return tok;
300                 }
301                 if (c != -1)
302                         isl_stream_ungetc(s, c);
303                 tok->type = ISL_TOKEN_LT;
304                 return tok;
305         }
306         if (c == '&') {
307                 tok = isl_token_new(s->ctx, line, col, old_line != line);
308                 if (!tok)
309                         return NULL;
310                 tok->type = ISL_TOKEN_AND;
311                 if ((c = isl_stream_getc(s)) != '&' && c != -1)
312                         isl_stream_ungetc(s, c);
313                 return tok;
314         }
315         if (c == '|') {
316                 tok = isl_token_new(s->ctx, line, col, old_line != line);
317                 if (!tok)
318                         return NULL;
319                 tok->type = ISL_TOKEN_OR;
320                 if ((c = isl_stream_getc(s)) != '|' && c != -1)
321                         isl_stream_ungetc(s, c);
322                 return tok;
323         }
324
325         tok = isl_token_new(s->ctx, line, col, old_line != line);
326         if (!tok)
327                 return NULL;
328         tok->type = ISL_TOKEN_UNKNOWN;
329         return tok;
330 error:
331         isl_token_free(tok);
332         return NULL;
333 }
334
335 struct isl_token *isl_stream_next_token(struct isl_stream *s)
336 {
337         return next_token(s, 0);
338 }
339
340 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
341 {
342         return next_token(s, 1);
343 }
344
345 int isl_stream_eat_if_available(struct isl_stream *s, int type)
346 {
347         struct isl_token *tok;
348
349         tok = isl_stream_next_token(s);
350         if (!tok)
351                 return 0;
352         if (tok->type == type) {
353                 isl_token_free(tok);
354                 return 1;
355         }
356         isl_stream_push_token(s, tok);
357         return 0;
358 }
359
360 int isl_stream_next_token_is(struct isl_stream *s, int type)
361 {
362         struct isl_token *tok;
363         int r;
364
365         tok = isl_stream_next_token(s);
366         if (!tok)
367                 return 0;
368         r = tok->type == type;
369         isl_stream_push_token(s, tok);
370         return r;
371 }
372
373 char *isl_stream_read_ident_if_available(struct isl_stream *s)
374 {
375         struct isl_token *tok;
376
377         tok = isl_stream_next_token(s);
378         if (!tok)
379                 return NULL;
380         if (tok->type == ISL_TOKEN_IDENT) {
381                 char *ident = strdup(tok->u.s);
382                 isl_token_free(tok);
383                 return ident;
384         }
385         isl_stream_push_token(s, tok);
386         return NULL;
387 }
388
389 int isl_stream_eat(struct isl_stream *s, int type)
390 {
391         struct isl_token *tok;
392
393         tok = isl_stream_next_token(s);
394         if (!tok)
395                 return -1;
396         if (tok->type == type) {
397                 isl_token_free(tok);
398                 return 0;
399         }
400         isl_stream_error(s, tok, "expecting other token");
401         isl_stream_push_token(s, tok);
402         return -1;
403 }
404
405 int isl_stream_is_empty(struct isl_stream *s)
406 {
407         struct isl_token *tok;
408
409         tok = isl_stream_next_token(s);
410
411         if (!tok)
412                 return 1;
413
414         isl_stream_push_token(s, tok);
415         return 0;
416 }
417
418 void isl_stream_free(struct isl_stream *s)
419 {
420         if (!s)
421                 return;
422         free(s->buffer);
423         if (s->n_token != 0) {
424                 struct isl_token *tok = isl_stream_next_token(s);
425                 isl_stream_error(s, tok, "unexpected token");
426                 isl_token_free(tok);
427         }
428         isl_ctx_deref(s->ctx);
429         free(s);
430 }