isl_stream: parse infty
[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 struct isl_keyword {
17         char                    *name;
18         enum isl_token_type     type;
19 };
20
21 static int same_name(const void *entry, const void *val)
22 {
23         const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
24
25         return !strcmp(keyword->name, val);
26 }
27
28 enum isl_token_type isl_stream_register_keyword(struct isl_stream *s,
29         const char *name)
30 {
31         struct isl_hash_table_entry *entry;
32         struct isl_keyword *keyword;
33         uint32_t name_hash;
34
35         if (!s->keywords) {
36                 s->keywords = isl_hash_table_alloc(s->ctx, 10);
37                 if (!s->keywords)
38                         return ISL_TOKEN_ERROR;
39                 s->next_type = ISL_TOKEN_LAST;
40         }
41
42         name_hash = isl_hash_string(isl_hash_init(), name);
43
44         entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
45                                         same_name, name, 1);
46         if (!entry)
47                 return ISL_TOKEN_ERROR;
48         if (entry->data) {
49                 keyword = entry->data;
50                 return keyword->type;
51         }
52
53         keyword = isl_calloc_type(s->ctx, struct isl_keyword);
54         if (!keyword)
55                 return ISL_TOKEN_ERROR;
56         keyword->type = s->next_type++;
57         keyword->name = strdup(name);
58         if (!keyword->name) {
59                 free(keyword);
60                 return ISL_TOKEN_ERROR;
61         }
62         entry->data = keyword;
63
64         return keyword->type;
65 }
66
67 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
68         int line, int col, unsigned on_new_line)
69 {
70         struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
71         if (!tok)
72                 return NULL;
73         tok->line = line;
74         tok->col = col;
75         tok->on_new_line = on_new_line;
76         return tok;
77 }
78
79 void isl_token_free(struct isl_token *tok)
80 {
81         if (!tok)
82                 return;
83         if (tok->type == ISL_TOKEN_VALUE)
84                 isl_int_clear(tok->u.v);
85         else if (tok->type == ISL_TOKEN_IDENT)
86                 free(tok->u.s);
87         free(tok);
88 }
89
90 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
91 {
92         int line = tok ? tok->line : s->line;
93         int col = tok ? tok->col : s->col;
94         fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
95         if (tok) {
96                 if (tok->type < 256)
97                         fprintf(stderr, "got '%c'\n", tok->type);
98                 else
99                         fprintf(stderr, "got token type %d\n", tok->type);
100         }
101 }
102
103 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
104 {
105         int i;
106         struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
107         if (!s)
108                 return NULL;
109         s->ctx = ctx;
110         isl_ctx_ref(s->ctx);
111         s->size = 256;
112         s->file = NULL;
113         s->str = NULL;
114         s->buffer = isl_alloc_array(ctx, char, s->size);
115         if (!s->buffer)
116                 goto error;
117         s->len = 0;
118         s->line = 1;
119         s->col = 0;
120         s->eof = 0;
121         s->c = -1;
122         for (i = 0; i < 5; ++i)
123                 s->tokens[i] = NULL;
124         s->n_token = 0;
125         s->keywords = NULL;
126         return s;
127 error:
128         isl_stream_free(s);
129         return NULL;
130 }
131
132 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
133 {
134         struct isl_stream *s = isl_stream_new(ctx);
135         if (!s)
136                 return NULL;
137         s->file = file;
138         return s;
139 }
140
141 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
142 {
143     struct isl_stream *s = isl_stream_new(ctx);
144     s->str = str;
145     return s;
146 }
147
148 static int isl_stream_getc(struct isl_stream *s)
149 {
150         int c;
151         if (s->eof)
152                 return -1;
153         if (s->file)
154                 c = fgetc(s->file);
155         else {
156                 c = *s->str++;
157                 if (c == '\0')
158                         c = -1;
159         }
160         if (c == -1)
161                 s->eof = 1;
162         if (!s->eof) {
163                 if (s->c == '\n') {
164                         s->line++;
165                         s->col = 0;
166                 } else
167                         s->col++;
168         }
169         s->c = c;
170         return c;
171 }
172
173 static void isl_stream_ungetc(struct isl_stream *s, int c)
174 {
175         if (s->file)
176                 ungetc(c, s->file);
177         else
178                 --s->str;
179         s->c = -1;
180 }
181
182 static int isl_stream_push_char(struct isl_stream *s, int c)
183 {
184         if (s->len >= s->size) {
185                 s->size = (3*s->size)/2;
186                 s->buffer = isl_realloc_array(ctx, s->buffer, char, s->size);
187                 if (!s->buffer)
188                         return -1;
189         }
190         s->buffer[s->len++] = c;
191         return 0;
192 }
193
194 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
195 {
196         isl_assert(s->ctx, s->n_token < 5, return);
197         s->tokens[s->n_token++] = tok;
198 }
199
200 static enum isl_token_type check_keywords(struct isl_stream *s)
201 {
202         struct isl_hash_table_entry *entry;
203         struct isl_keyword *keyword;
204         uint32_t name_hash;
205
206         if (!strcasecmp(s->buffer, "exists"))
207                 return ISL_TOKEN_EXISTS;
208         if (!strcasecmp(s->buffer, "and"))
209                 return ISL_TOKEN_AND;
210         if (!strcasecmp(s->buffer, "or"))
211                 return ISL_TOKEN_OR;
212         if (!strcasecmp(s->buffer, "infty"))
213                 return ISL_TOKEN_INFTY;
214         if (!strcasecmp(s->buffer, "infinity"))
215                 return ISL_TOKEN_INFTY;
216
217         if (!s->keywords)
218                 return ISL_TOKEN_IDENT;
219
220         name_hash = isl_hash_string(isl_hash_init(), s->buffer);
221         entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
222                                         s->buffer, 0);
223         if (entry) {
224                 keyword = entry->data;
225                 return keyword->type;
226         }
227
228         return ISL_TOKEN_IDENT;
229 }
230
231 static struct isl_token *next_token(struct isl_stream *s, int same_line)
232 {
233         int c;
234         struct isl_token *tok = NULL;
235         int line, col;
236         int old_line = s->line;
237
238         if (s->n_token) {
239                 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
240                         return NULL;
241                 return s->tokens[--s->n_token];
242         }
243
244         if (same_line && s->c == '\n')
245                 return NULL;
246
247         s->len = 0;
248
249         /* skip spaces and comment lines */
250         while ((c = isl_stream_getc(s)) != -1) {
251                 if (c == '#') {
252                         while ((c = isl_stream_getc(s)) != -1 && c != '\n')
253                                 /* nothing */
254                                 ;
255                         if (c == -1 || (same_line && c == '\n'))
256                                 break;
257                 } else if (!isspace(c) || (same_line && c == '\n'))
258                         break;
259         }
260
261         line = s->line;
262         col = s->col;
263
264         if (c == -1 || (same_line && c == '\n'))
265                 return NULL;
266         if (c == '(' ||
267             c == ')' ||
268             c == '+' ||
269             c == '/' ||
270             c == '*' ||
271             c == '^' ||
272             c == '=' ||
273             c == ',' ||
274             c == ';' ||
275             c == '[' ||
276             c == ']' ||
277             c == '{' ||
278             c == '}') {
279                 tok = isl_token_new(s->ctx, line, col, old_line != line);
280                 if (!tok)
281                         return NULL;
282                 tok->type = (enum isl_token_type)c;
283                 return tok;
284         }
285         if (c == '-') {
286                 int c;
287                 if ((c = isl_stream_getc(s)) == '>') {
288                         tok = isl_token_new(s->ctx, line, col, old_line != line);
289                         if (!tok)
290                                 return NULL;
291                         tok->type = ISL_TOKEN_TO;
292                         return tok;
293                 }
294                 if (c != -1)
295                         isl_stream_ungetc(s, c);
296                 if (!isdigit(c)) {
297                         tok = isl_token_new(s->ctx, line, col, old_line != line);
298                         if (!tok)
299                                 return NULL;
300                         tok->type = (enum isl_token_type) '-';
301                         return tok;
302                 }
303         }
304         if (c == '-' || isdigit(c)) {
305                 tok = isl_token_new(s->ctx, line, col, old_line != line);
306                 if (!tok)
307                         return NULL;
308                 tok->type = ISL_TOKEN_VALUE;
309                 isl_int_init(tok->u.v);
310                 if (isl_stream_push_char(s, c))
311                         goto error;
312                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
313                         if (isl_stream_push_char(s, c))
314                                 goto error;
315                 if (c != -1)
316                         isl_stream_ungetc(s, c);
317                 isl_stream_push_char(s, '\0');
318                 isl_int_read(tok->u.v, s->buffer);
319                 return tok;
320         }
321         if (isalpha(c)) {
322                 tok = isl_token_new(s->ctx, line, col, old_line != line);
323                 if (!tok)
324                         return NULL;
325                 isl_stream_push_char(s, c);
326                 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
327                         isl_stream_push_char(s, c);
328                 if (c != -1)
329                         isl_stream_ungetc(s, c);
330                 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
331                         isl_stream_push_char(s, c);
332                 if (c != -1)
333                         isl_stream_ungetc(s, c);
334                 isl_stream_push_char(s, '\0');
335                 tok->type = check_keywords(s);
336                 if (tok->type == ISL_TOKEN_IDENT)
337                         tok->u.s = strdup(s->buffer);
338                 return tok;
339         }
340         if (c == ':') {
341                 int c;
342                 tok = isl_token_new(s->ctx, line, col, old_line != line);
343                 if (!tok)
344                         return NULL;
345                 if ((c = isl_stream_getc(s)) == '=') {
346                         tok->type = ISL_TOKEN_DEF;
347                         return tok;
348                 }
349                 if (c != -1)
350                         isl_stream_ungetc(s, c);
351                 tok->type = ':';
352                 return tok;
353         }
354         if (c == '>') {
355                 int c;
356                 tok = isl_token_new(s->ctx, line, col, old_line != line);
357                 if (!tok)
358                         return NULL;
359                 if ((c = isl_stream_getc(s)) == '=') {
360                         tok->type = ISL_TOKEN_GE;
361                         return tok;
362                 }
363                 if (c != -1)
364                         isl_stream_ungetc(s, c);
365                 tok->type = ISL_TOKEN_GT;
366                 return tok;
367         }
368         if (c == '<') {
369                 int c;
370                 tok = isl_token_new(s->ctx, line, col, old_line != line);
371                 if (!tok)
372                         return NULL;
373                 if ((c = isl_stream_getc(s)) == '=') {
374                         tok->type = ISL_TOKEN_LE;
375                         return tok;
376                 }
377                 if (c != -1)
378                         isl_stream_ungetc(s, c);
379                 tok->type = ISL_TOKEN_LT;
380                 return tok;
381         }
382         if (c == '&') {
383                 tok = isl_token_new(s->ctx, line, col, old_line != line);
384                 if (!tok)
385                         return NULL;
386                 tok->type = ISL_TOKEN_AND;
387                 if ((c = isl_stream_getc(s)) != '&' && c != -1)
388                         isl_stream_ungetc(s, c);
389                 return tok;
390         }
391         if (c == '|') {
392                 tok = isl_token_new(s->ctx, line, col, old_line != line);
393                 if (!tok)
394                         return NULL;
395                 tok->type = ISL_TOKEN_OR;
396                 if ((c = isl_stream_getc(s)) != '|' && c != -1)
397                         isl_stream_ungetc(s, c);
398                 return tok;
399         }
400
401         tok = isl_token_new(s->ctx, line, col, old_line != line);
402         if (!tok)
403                 return NULL;
404         tok->type = ISL_TOKEN_UNKNOWN;
405         return tok;
406 error:
407         isl_token_free(tok);
408         return NULL;
409 }
410
411 struct isl_token *isl_stream_next_token(struct isl_stream *s)
412 {
413         return next_token(s, 0);
414 }
415
416 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
417 {
418         return next_token(s, 1);
419 }
420
421 int isl_stream_eat_if_available(struct isl_stream *s, int type)
422 {
423         struct isl_token *tok;
424
425         tok = isl_stream_next_token(s);
426         if (!tok)
427                 return 0;
428         if (tok->type == type) {
429                 isl_token_free(tok);
430                 return 1;
431         }
432         isl_stream_push_token(s, tok);
433         return 0;
434 }
435
436 int isl_stream_next_token_is(struct isl_stream *s, int type)
437 {
438         struct isl_token *tok;
439         int r;
440
441         tok = isl_stream_next_token(s);
442         if (!tok)
443                 return 0;
444         r = tok->type == type;
445         isl_stream_push_token(s, tok);
446         return r;
447 }
448
449 char *isl_stream_read_ident_if_available(struct isl_stream *s)
450 {
451         struct isl_token *tok;
452
453         tok = isl_stream_next_token(s);
454         if (!tok)
455                 return NULL;
456         if (tok->type == ISL_TOKEN_IDENT) {
457                 char *ident = strdup(tok->u.s);
458                 isl_token_free(tok);
459                 return ident;
460         }
461         isl_stream_push_token(s, tok);
462         return NULL;
463 }
464
465 int isl_stream_eat(struct isl_stream *s, int type)
466 {
467         struct isl_token *tok;
468
469         tok = isl_stream_next_token(s);
470         if (!tok)
471                 return -1;
472         if (tok->type == type) {
473                 isl_token_free(tok);
474                 return 0;
475         }
476         isl_stream_error(s, tok, "expecting other token");
477         isl_stream_push_token(s, tok);
478         return -1;
479 }
480
481 int isl_stream_is_empty(struct isl_stream *s)
482 {
483         struct isl_token *tok;
484
485         tok = isl_stream_next_token(s);
486
487         if (!tok)
488                 return 1;
489
490         isl_stream_push_token(s, tok);
491         return 0;
492 }
493
494 static int free_keyword(void *p)
495 {
496         struct isl_keyword *keyword = p;
497
498         free(keyword->name);
499         free(keyword);
500
501         return 0;
502 }
503
504 void isl_stream_free(struct isl_stream *s)
505 {
506         if (!s)
507                 return;
508         free(s->buffer);
509         if (s->n_token != 0) {
510                 struct isl_token *tok = isl_stream_next_token(s);
511                 isl_stream_error(s, tok, "unexpected token");
512                 isl_token_free(tok);
513         }
514         if (s->keywords) {
515                 isl_hash_table_foreach(s->ctx, s->keywords, free_keyword);
516                 isl_hash_table_free(s->ctx, s->keywords);
517         }
518         isl_ctx_deref(s->ctx);
519         free(s);
520 }