add isl_stream_flush_tokens
[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 || tok->type == ISL_TOKEN_STRING)
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         if (!strcasecmp(s->buffer, "NaN"))
217                 return ISL_TOKEN_NAN;
218
219         if (!s->keywords)
220                 return ISL_TOKEN_IDENT;
221
222         name_hash = isl_hash_string(isl_hash_init(), s->buffer);
223         entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
224                                         s->buffer, 0);
225         if (entry) {
226                 keyword = entry->data;
227                 return keyword->type;
228         }
229
230         return ISL_TOKEN_IDENT;
231 }
232
233 int isl_stream_skip_line(struct isl_stream *s)
234 {
235         int c;
236
237         while ((c = isl_stream_getc(s)) != -1 && c != '\n')
238                 /* nothing */
239                 ;
240
241         return c == -1 ? -1 : 0;
242 }
243
244 static struct isl_token *next_token(struct isl_stream *s, int same_line)
245 {
246         int c;
247         struct isl_token *tok = NULL;
248         int line, col;
249         int old_line = s->line;
250
251         if (s->n_token) {
252                 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
253                         return NULL;
254                 return s->tokens[--s->n_token];
255         }
256
257         if (same_line && s->c == '\n')
258                 return NULL;
259
260         s->len = 0;
261
262         /* skip spaces and comment lines */
263         while ((c = isl_stream_getc(s)) != -1) {
264                 if (c == '#') {
265                         if (isl_stream_skip_line(s) < 0)
266                                 break;
267                         c = '\n';
268                         if (same_line)
269                                 break;
270                 } else if (!isspace(c) || (same_line && c == '\n'))
271                         break;
272         }
273
274         line = s->line;
275         col = s->col;
276
277         if (c == -1 || (same_line && c == '\n'))
278                 return NULL;
279         if (c == '(' ||
280             c == ')' ||
281             c == '+' ||
282             c == '/' ||
283             c == '*' ||
284             c == '%' ||
285             c == '^' ||
286             c == '=' ||
287             c == '@' ||
288             c == ',' ||
289             c == '.' ||
290             c == ';' ||
291             c == '[' ||
292             c == ']' ||
293             c == '{' ||
294             c == '}') {
295                 tok = isl_token_new(s->ctx, line, col, old_line != line);
296                 if (!tok)
297                         return NULL;
298                 tok->type = (enum isl_token_type)c;
299                 return tok;
300         }
301         if (c == '-') {
302                 int c;
303                 if ((c = isl_stream_getc(s)) == '>') {
304                         tok = isl_token_new(s->ctx, line, col, old_line != line);
305                         if (!tok)
306                                 return NULL;
307                         tok->type = ISL_TOKEN_TO;
308                         return tok;
309                 }
310                 if (c != -1)
311                         isl_stream_ungetc(s, c);
312                 if (!isdigit(c)) {
313                         tok = isl_token_new(s->ctx, line, col, old_line != line);
314                         if (!tok)
315                                 return NULL;
316                         tok->type = (enum isl_token_type) '-';
317                         return tok;
318                 }
319         }
320         if (c == '-' || isdigit(c)) {
321                 tok = isl_token_new(s->ctx, line, col, old_line != line);
322                 if (!tok)
323                         return NULL;
324                 tok->type = ISL_TOKEN_VALUE;
325                 isl_int_init(tok->u.v);
326                 if (isl_stream_push_char(s, c))
327                         goto error;
328                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
329                         if (isl_stream_push_char(s, c))
330                                 goto error;
331                 if (c != -1)
332                         isl_stream_ungetc(s, c);
333                 isl_stream_push_char(s, '\0');
334                 isl_int_read(tok->u.v, s->buffer);
335                 return tok;
336         }
337         if (isalpha(c)) {
338                 tok = isl_token_new(s->ctx, line, col, old_line != line);
339                 if (!tok)
340                         return NULL;
341                 isl_stream_push_char(s, c);
342                 while ((c = isl_stream_getc(s)) != -1 &&
343                                 (isalnum(c) || c == '_'))
344                         isl_stream_push_char(s, c);
345                 if (c != -1)
346                         isl_stream_ungetc(s, c);
347                 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
348                         isl_stream_push_char(s, c);
349                 if (c != -1)
350                         isl_stream_ungetc(s, c);
351                 isl_stream_push_char(s, '\0');
352                 tok->type = check_keywords(s);
353                 if (tok->type == ISL_TOKEN_IDENT)
354                         tok->u.s = strdup(s->buffer);
355                 return tok;
356         }
357         if (c == '"') {
358                 tok = isl_token_new(s->ctx, line, col, old_line != line);
359                 if (!tok)
360                         return NULL;
361                 tok->type = ISL_TOKEN_STRING;
362                 tok->u.s = NULL;
363                 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
364                         isl_stream_push_char(s, c);
365                 if (c != '"') {
366                         isl_stream_error(s, NULL, "unterminated string");
367                         goto error;
368                 }
369                 isl_stream_push_char(s, '\0');
370                 tok->u.s = strdup(s->buffer);
371                 return tok;
372         }
373         if (c == ':') {
374                 int c;
375                 tok = isl_token_new(s->ctx, line, col, old_line != line);
376                 if (!tok)
377                         return NULL;
378                 if ((c = isl_stream_getc(s)) == '=') {
379                         tok->type = ISL_TOKEN_DEF;
380                         return tok;
381                 }
382                 if (c != -1)
383                         isl_stream_ungetc(s, c);
384                 tok->type = (enum isl_token_type) ':';
385                 return tok;
386         }
387         if (c == '>') {
388                 int c;
389                 tok = isl_token_new(s->ctx, line, col, old_line != line);
390                 if (!tok)
391                         return NULL;
392                 if ((c = isl_stream_getc(s)) == '=') {
393                         tok->type = ISL_TOKEN_GE;
394                         return tok;
395                 } else if (c == '>') {
396                         if ((c = isl_stream_getc(s)) == '=') {
397                                 tok->type = ISL_TOKEN_LEX_GE;
398                                 return tok;
399                         }
400                         tok->type = ISL_TOKEN_LEX_GT;
401                 } else
402                         tok->type = ISL_TOKEN_GT;
403                 if (c != -1)
404                         isl_stream_ungetc(s, c);
405                 return tok;
406         }
407         if (c == '<') {
408                 int c;
409                 tok = isl_token_new(s->ctx, line, col, old_line != line);
410                 if (!tok)
411                         return NULL;
412                 if ((c = isl_stream_getc(s)) == '=') {
413                         tok->type = ISL_TOKEN_LE;
414                         return tok;
415                 } else if (c == '<') {
416                         if ((c = isl_stream_getc(s)) == '=') {
417                                 tok->type = ISL_TOKEN_LEX_LE;
418                                 return tok;
419                         }
420                         tok->type = ISL_TOKEN_LEX_LT;
421                 } else
422                         tok->type = ISL_TOKEN_LT;
423                 if (c != -1)
424                         isl_stream_ungetc(s, c);
425                 return tok;
426         }
427         if (c == '&') {
428                 tok = isl_token_new(s->ctx, line, col, old_line != line);
429                 if (!tok)
430                         return NULL;
431                 tok->type = ISL_TOKEN_AND;
432                 if ((c = isl_stream_getc(s)) != '&' && c != -1)
433                         isl_stream_ungetc(s, c);
434                 return tok;
435         }
436         if (c == '|') {
437                 tok = isl_token_new(s->ctx, line, col, old_line != line);
438                 if (!tok)
439                         return NULL;
440                 tok->type = ISL_TOKEN_OR;
441                 if ((c = isl_stream_getc(s)) != '|' && c != -1)
442                         isl_stream_ungetc(s, c);
443                 return tok;
444         }
445
446         tok = isl_token_new(s->ctx, line, col, old_line != line);
447         if (!tok)
448                 return NULL;
449         tok->type = ISL_TOKEN_UNKNOWN;
450         return tok;
451 error:
452         isl_token_free(tok);
453         return NULL;
454 }
455
456 struct isl_token *isl_stream_next_token(struct isl_stream *s)
457 {
458         return next_token(s, 0);
459 }
460
461 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
462 {
463         return next_token(s, 1);
464 }
465
466 int isl_stream_eat_if_available(struct isl_stream *s, int type)
467 {
468         struct isl_token *tok;
469
470         tok = isl_stream_next_token(s);
471         if (!tok)
472                 return 0;
473         if (tok->type == type) {
474                 isl_token_free(tok);
475                 return 1;
476         }
477         isl_stream_push_token(s, tok);
478         return 0;
479 }
480
481 int isl_stream_next_token_is(struct isl_stream *s, int type)
482 {
483         struct isl_token *tok;
484         int r;
485
486         tok = isl_stream_next_token(s);
487         if (!tok)
488                 return 0;
489         r = tok->type == type;
490         isl_stream_push_token(s, tok);
491         return r;
492 }
493
494 char *isl_stream_read_ident_if_available(struct isl_stream *s)
495 {
496         struct isl_token *tok;
497
498         tok = isl_stream_next_token(s);
499         if (!tok)
500                 return NULL;
501         if (tok->type == ISL_TOKEN_IDENT) {
502                 char *ident = strdup(tok->u.s);
503                 isl_token_free(tok);
504                 return ident;
505         }
506         isl_stream_push_token(s, tok);
507         return NULL;
508 }
509
510 int isl_stream_eat(struct isl_stream *s, int type)
511 {
512         struct isl_token *tok;
513
514         tok = isl_stream_next_token(s);
515         if (!tok)
516                 return -1;
517         if (tok->type == type) {
518                 isl_token_free(tok);
519                 return 0;
520         }
521         isl_stream_error(s, tok, "expecting other token");
522         isl_stream_push_token(s, tok);
523         return -1;
524 }
525
526 int isl_stream_is_empty(struct isl_stream *s)
527 {
528         struct isl_token *tok;
529
530         tok = isl_stream_next_token(s);
531
532         if (!tok)
533                 return 1;
534
535         isl_stream_push_token(s, tok);
536         return 0;
537 }
538
539 static int free_keyword(void *p)
540 {
541         struct isl_keyword *keyword = p;
542
543         free(keyword->name);
544         free(keyword);
545
546         return 0;
547 }
548
549 void isl_stream_flush_tokens(struct isl_stream *s)
550 {
551         int i;
552
553         if (!s)
554                 return;
555         for (i = 0; i < s->n_token; ++i)
556                 isl_token_free(s->tokens[i]);
557         s->n_token = 0;
558 }
559
560 void isl_stream_free(struct isl_stream *s)
561 {
562         if (!s)
563                 return;
564         free(s->buffer);
565         if (s->n_token != 0) {
566                 struct isl_token *tok = isl_stream_next_token(s);
567                 isl_stream_error(s, tok, "unexpected token");
568                 isl_token_free(tok);
569         }
570         if (s->keywords) {
571                 isl_hash_table_foreach(s->ctx, s->keywords, free_keyword);
572                 isl_hash_table_free(s->ctx, s->keywords);
573         }
574         isl_ctx_deref(s->ctx);
575         free(s);
576 }