2cb6e0bd902f945ff52eed812f99a2e59e720227
[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         tok->is_keyword = 0;
77         tok->u.s = NULL;
78         return tok;
79 }
80
81 void isl_token_free(struct isl_token *tok)
82 {
83         if (!tok)
84                 return;
85         if (tok->type == ISL_TOKEN_VALUE)
86                 isl_int_clear(tok->u.v);
87         else
88                 free(tok->u.s);
89         free(tok);
90 }
91
92 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
93 {
94         int line = tok ? tok->line : s->line;
95         int col = tok ? tok->col : s->col;
96         fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
97         if (tok) {
98                 if (tok->type < 256)
99                         fprintf(stderr, "got '%c'\n", tok->type);
100                 else if (tok->type == ISL_TOKEN_IDENT)
101                         fprintf(stderr, "got ident '%s'\n", tok->u.s);
102                 else if (tok->is_keyword)
103                         fprintf(stderr, "got keyword '%s'\n", tok->u.s);
104                 else if (tok->type == ISL_TOKEN_VALUE) {
105                         fprintf(stderr, "got value '");
106                         isl_int_print(stderr, tok->u.v, 0);
107                         fprintf(stderr, "'\n");
108                 } else if (tok->u.s)
109                         fprintf(stderr, "got token '%s'\n", tok->u.s);
110                 else
111                         fprintf(stderr, "got token type %d\n", tok->type);
112         }
113 }
114
115 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
116 {
117         int i;
118         struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
119         if (!s)
120                 return NULL;
121         s->ctx = ctx;
122         isl_ctx_ref(s->ctx);
123         s->file = NULL;
124         s->str = NULL;
125         s->len = 0;
126         s->line = 1;
127         s->col = 0;
128         s->eof = 0;
129         s->c = -1;
130         s->n_un = 0;
131         for (i = 0; i < 5; ++i)
132                 s->tokens[i] = NULL;
133         s->n_token = 0;
134         s->keywords = NULL;
135         s->size = 256;
136         s->buffer = isl_alloc_array(ctx, char, s->size);
137         if (!s->buffer)
138                 goto error;
139         return s;
140 error:
141         isl_stream_free(s);
142         return NULL;
143 }
144
145 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
146 {
147         struct isl_stream *s = isl_stream_new(ctx);
148         if (!s)
149                 return NULL;
150         s->file = file;
151         return s;
152 }
153
154 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
155 {
156         struct isl_stream *s = isl_stream_new(ctx);
157         if (!s)
158                 return NULL;
159         s->str = str;
160         return s;
161 }
162
163 static int stream_getc(struct isl_stream *s)
164 {
165         int c;
166         if (s->eof)
167                 return -1;
168         if (s->n_un)
169                 return s->c = s->un[--s->n_un];
170         if (s->file)
171                 c = fgetc(s->file);
172         else {
173                 c = *s->str++;
174                 if (c == '\0')
175                         c = -1;
176         }
177         if (c == -1)
178                 s->eof = 1;
179         if (!s->eof) {
180                 if (s->c == '\n') {
181                         s->line++;
182                         s->col = 0;
183                 } else
184                         s->col++;
185         }
186         s->c = c;
187         return c;
188 }
189
190 static void isl_stream_ungetc(struct isl_stream *s, int c)
191 {
192         isl_assert(s->ctx, s->n_un < 5, return);
193         s->un[s->n_un++] = c;
194         s->c = -1;
195 }
196
197 static int isl_stream_getc(struct isl_stream *s)
198 {
199         int c;
200
201         do {
202                 c = stream_getc(s);
203                 if (c != '\\')
204                         return c;
205                 c = stream_getc(s);
206         } while (c == '\n');
207
208         isl_stream_ungetc(s, c);
209
210         return '\\';
211 }
212
213 static int isl_stream_push_char(struct isl_stream *s, int c)
214 {
215         if (s->len >= s->size) {
216                 char *buffer;
217                 s->size = (3*s->size)/2;
218                 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
219                 if (!buffer)
220                         return -1;
221                 s->buffer = buffer;
222         }
223         s->buffer[s->len++] = c;
224         return 0;
225 }
226
227 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
228 {
229         isl_assert(s->ctx, s->n_token < 5, return);
230         s->tokens[s->n_token++] = tok;
231 }
232
233 static enum isl_token_type check_keywords(struct isl_stream *s)
234 {
235         struct isl_hash_table_entry *entry;
236         struct isl_keyword *keyword;
237         uint32_t name_hash;
238
239         if (!strcasecmp(s->buffer, "exists"))
240                 return ISL_TOKEN_EXISTS;
241         if (!strcasecmp(s->buffer, "and"))
242                 return ISL_TOKEN_AND;
243         if (!strcasecmp(s->buffer, "or"))
244                 return ISL_TOKEN_OR;
245         if (!strcasecmp(s->buffer, "not"))
246                 return ISL_TOKEN_NOT;
247         if (!strcasecmp(s->buffer, "infty"))
248                 return ISL_TOKEN_INFTY;
249         if (!strcasecmp(s->buffer, "infinity"))
250                 return ISL_TOKEN_INFTY;
251         if (!strcasecmp(s->buffer, "NaN"))
252                 return ISL_TOKEN_NAN;
253         if (!strcasecmp(s->buffer, "min"))
254                 return ISL_TOKEN_MIN;
255         if (!strcasecmp(s->buffer, "max"))
256                 return ISL_TOKEN_MAX;
257         if (!strcasecmp(s->buffer, "rat"))
258                 return ISL_TOKEN_RAT;
259         if (!strcasecmp(s->buffer, "true"))
260                 return ISL_TOKEN_TRUE;
261         if (!strcasecmp(s->buffer, "false"))
262                 return ISL_TOKEN_FALSE;
263
264         if (!s->keywords)
265                 return ISL_TOKEN_IDENT;
266
267         name_hash = isl_hash_string(isl_hash_init(), s->buffer);
268         entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
269                                         s->buffer, 0);
270         if (entry) {
271                 keyword = entry->data;
272                 return keyword->type;
273         }
274
275         return ISL_TOKEN_IDENT;
276 }
277
278 int isl_stream_skip_line(struct isl_stream *s)
279 {
280         int c;
281
282         while ((c = isl_stream_getc(s)) != -1 && c != '\n')
283                 /* nothing */
284                 ;
285
286         return c == -1 ? -1 : 0;
287 }
288
289 static struct isl_token *next_token(struct isl_stream *s, int same_line)
290 {
291         int c;
292         struct isl_token *tok = NULL;
293         int line, col;
294         int old_line = s->line;
295
296         if (s->n_token) {
297                 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
298                         return NULL;
299                 return s->tokens[--s->n_token];
300         }
301
302         if (same_line && s->c == '\n')
303                 return NULL;
304
305         s->len = 0;
306
307         /* skip spaces and comment lines */
308         while ((c = isl_stream_getc(s)) != -1) {
309                 if (c == '#') {
310                         if (isl_stream_skip_line(s) < 0)
311                                 break;
312                         c = '\n';
313                         if (same_line)
314                                 break;
315                 } else if (!isspace(c) || (same_line && c == '\n'))
316                         break;
317         }
318
319         line = s->line;
320         col = s->col;
321
322         if (c == -1 || (same_line && c == '\n'))
323                 return NULL;
324         if (c == '(' ||
325             c == ')' ||
326             c == '+' ||
327             c == '*' ||
328             c == '%' ||
329             c == '^' ||
330             c == '=' ||
331             c == '@' ||
332             c == '$' ||
333             c == ',' ||
334             c == '.' ||
335             c == ';' ||
336             c == '[' ||
337             c == ']' ||
338             c == '{' ||
339             c == '}') {
340                 tok = isl_token_new(s->ctx, line, col, old_line != line);
341                 if (!tok)
342                         return NULL;
343                 tok->type = (enum isl_token_type)c;
344                 return tok;
345         }
346         if (c == '-') {
347                 int c;
348                 if ((c = isl_stream_getc(s)) == '>') {
349                         tok = isl_token_new(s->ctx, line, col, old_line != line);
350                         if (!tok)
351                                 return NULL;
352                         tok->u.s = strdup("->");
353                         tok->type = ISL_TOKEN_TO;
354                         return tok;
355                 }
356                 if (c != -1)
357                         isl_stream_ungetc(s, c);
358                 if (!isdigit(c)) {
359                         tok = isl_token_new(s->ctx, line, col, old_line != line);
360                         if (!tok)
361                                 return NULL;
362                         tok->type = (enum isl_token_type) '-';
363                         return tok;
364                 }
365         }
366         if (c == '-' || isdigit(c)) {
367                 tok = isl_token_new(s->ctx, line, col, old_line != line);
368                 if (!tok)
369                         return NULL;
370                 tok->type = ISL_TOKEN_VALUE;
371                 isl_int_init(tok->u.v);
372                 if (isl_stream_push_char(s, c))
373                         goto error;
374                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
375                         if (isl_stream_push_char(s, c))
376                                 goto error;
377                 if (c != -1)
378                         isl_stream_ungetc(s, c);
379                 isl_stream_push_char(s, '\0');
380                 isl_int_read(tok->u.v, s->buffer);
381                 return tok;
382         }
383         if (isalpha(c) || c == '_') {
384                 tok = isl_token_new(s->ctx, line, col, old_line != line);
385                 if (!tok)
386                         return NULL;
387                 isl_stream_push_char(s, c);
388                 while ((c = isl_stream_getc(s)) != -1 &&
389                                 (isalnum(c) || c == '_'))
390                         isl_stream_push_char(s, c);
391                 if (c != -1)
392                         isl_stream_ungetc(s, c);
393                 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
394                         isl_stream_push_char(s, c);
395                 if (c != -1)
396                         isl_stream_ungetc(s, c);
397                 isl_stream_push_char(s, '\0');
398                 tok->type = check_keywords(s);
399                 if (tok->type != ISL_TOKEN_IDENT)
400                         tok->is_keyword = 1;
401                 tok->u.s = strdup(s->buffer);
402                 if (!tok->u.s)
403                         goto error;
404                 return tok;
405         }
406         if (c == '"') {
407                 tok = isl_token_new(s->ctx, line, col, old_line != line);
408                 if (!tok)
409                         return NULL;
410                 tok->type = ISL_TOKEN_STRING;
411                 tok->u.s = NULL;
412                 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
413                         isl_stream_push_char(s, c);
414                 if (c != '"') {
415                         isl_stream_error(s, NULL, "unterminated string");
416                         goto error;
417                 }
418                 isl_stream_push_char(s, '\0');
419                 tok->u.s = strdup(s->buffer);
420                 return tok;
421         }
422         if (c == ':') {
423                 int c;
424                 tok = isl_token_new(s->ctx, line, col, old_line != line);
425                 if (!tok)
426                         return NULL;
427                 if ((c = isl_stream_getc(s)) == '=') {
428                         tok->u.s = strdup(":=");
429                         tok->type = ISL_TOKEN_DEF;
430                         return tok;
431                 }
432                 if (c != -1)
433                         isl_stream_ungetc(s, c);
434                 tok->type = (enum isl_token_type) ':';
435                 return tok;
436         }
437         if (c == '>') {
438                 int c;
439                 tok = isl_token_new(s->ctx, line, col, old_line != line);
440                 if (!tok)
441                         return NULL;
442                 if ((c = isl_stream_getc(s)) == '=') {
443                         tok->u.s = strdup(">=");
444                         tok->type = ISL_TOKEN_GE;
445                         return tok;
446                 } else if (c == '>') {
447                         if ((c = isl_stream_getc(s)) == '=') {
448                                 tok->u.s = strdup(">>=");
449                                 tok->type = ISL_TOKEN_LEX_GE;
450                                 return tok;
451                         }
452                         tok->u.s = strdup(">>");
453                         tok->type = ISL_TOKEN_LEX_GT;
454                 } else {
455                         tok->u.s = strdup(">");
456                         tok->type = ISL_TOKEN_GT;
457                 }
458                 if (c != -1)
459                         isl_stream_ungetc(s, c);
460                 return tok;
461         }
462         if (c == '<') {
463                 int c;
464                 tok = isl_token_new(s->ctx, line, col, old_line != line);
465                 if (!tok)
466                         return NULL;
467                 if ((c = isl_stream_getc(s)) == '=') {
468                         tok->u.s = strdup("<=");
469                         tok->type = ISL_TOKEN_LE;
470                         return tok;
471                 } else if (c == '<') {
472                         if ((c = isl_stream_getc(s)) == '=') {
473                                 tok->u.s = strdup("<<=");
474                                 tok->type = ISL_TOKEN_LEX_LE;
475                                 return tok;
476                         }
477                         tok->u.s = strdup("<<");
478                         tok->type = ISL_TOKEN_LEX_LT;
479                 } else {
480                         tok->u.s = strdup("<");
481                         tok->type = ISL_TOKEN_LT;
482                 }
483                 if (c != -1)
484                         isl_stream_ungetc(s, c);
485                 return tok;
486         }
487         if (c == '&') {
488                 tok = isl_token_new(s->ctx, line, col, old_line != line);
489                 if (!tok)
490                         return NULL;
491                 tok->type = ISL_TOKEN_AND;
492                 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
493                         tok->u.s = strdup("&");
494                         isl_stream_ungetc(s, c);
495                 } else
496                         tok->u.s = strdup("&&");
497                 return tok;
498         }
499         if (c == '|') {
500                 tok = isl_token_new(s->ctx, line, col, old_line != line);
501                 if (!tok)
502                         return NULL;
503                 tok->type = ISL_TOKEN_OR;
504                 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
505                         tok->u.s = strdup("|");
506                         isl_stream_ungetc(s, c);
507                 } else
508                         tok->u.s = strdup("||");
509                 return tok;
510         }
511         if (c == '/') {
512                 tok = isl_token_new(s->ctx, line, col, old_line != line);
513                 if (!tok)
514                         return NULL;
515                 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
516                         tok->type = (enum isl_token_type) '/';
517                         isl_stream_ungetc(s, c);
518                 } else {
519                         tok->u.s = strdup("/\\");
520                         tok->type = ISL_TOKEN_AND;
521                 }
522                 return tok;
523         }
524         if (c == '\\') {
525                 tok = isl_token_new(s->ctx, line, col, old_line != line);
526                 if (!tok)
527                         return NULL;
528                 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
529                         tok->type = (enum isl_token_type) '\\';
530                         isl_stream_ungetc(s, c);
531                 } else {
532                         tok->u.s = strdup("\\/");
533                         tok->type = ISL_TOKEN_OR;
534                 }
535                 return tok;
536         }
537         if (c == '!') {
538                 tok = isl_token_new(s->ctx, line, col, old_line != line);
539                 if (!tok)
540                         return NULL;
541                 tok->type = ISL_TOKEN_NOT;
542                 tok->u.s = strdup("!");
543                 return tok;
544         }
545
546         tok = isl_token_new(s->ctx, line, col, old_line != line);
547         if (!tok)
548                 return NULL;
549         tok->type = ISL_TOKEN_UNKNOWN;
550         return tok;
551 error:
552         isl_token_free(tok);
553         return NULL;
554 }
555
556 struct isl_token *isl_stream_next_token(struct isl_stream *s)
557 {
558         return next_token(s, 0);
559 }
560
561 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
562 {
563         return next_token(s, 1);
564 }
565
566 int isl_stream_eat_if_available(struct isl_stream *s, int type)
567 {
568         struct isl_token *tok;
569
570         tok = isl_stream_next_token(s);
571         if (!tok)
572                 return 0;
573         if (tok->type == type) {
574                 isl_token_free(tok);
575                 return 1;
576         }
577         isl_stream_push_token(s, tok);
578         return 0;
579 }
580
581 int isl_stream_next_token_is(struct isl_stream *s, int type)
582 {
583         struct isl_token *tok;
584         int r;
585
586         tok = isl_stream_next_token(s);
587         if (!tok)
588                 return 0;
589         r = tok->type == type;
590         isl_stream_push_token(s, tok);
591         return r;
592 }
593
594 char *isl_stream_read_ident_if_available(struct isl_stream *s)
595 {
596         struct isl_token *tok;
597
598         tok = isl_stream_next_token(s);
599         if (!tok)
600                 return NULL;
601         if (tok->type == ISL_TOKEN_IDENT) {
602                 char *ident = strdup(tok->u.s);
603                 isl_token_free(tok);
604                 return ident;
605         }
606         isl_stream_push_token(s, tok);
607         return NULL;
608 }
609
610 int isl_stream_eat(struct isl_stream *s, int type)
611 {
612         struct isl_token *tok;
613
614         tok = isl_stream_next_token(s);
615         if (!tok)
616                 return -1;
617         if (tok->type == type) {
618                 isl_token_free(tok);
619                 return 0;
620         }
621         isl_stream_error(s, tok, "expecting other token");
622         isl_stream_push_token(s, tok);
623         return -1;
624 }
625
626 int isl_stream_is_empty(struct isl_stream *s)
627 {
628         struct isl_token *tok;
629
630         tok = isl_stream_next_token(s);
631
632         if (!tok)
633                 return 1;
634
635         isl_stream_push_token(s, tok);
636         return 0;
637 }
638
639 static int free_keyword(void **p, void *user)
640 {
641         struct isl_keyword *keyword = *p;
642
643         free(keyword->name);
644         free(keyword);
645
646         return 0;
647 }
648
649 void isl_stream_flush_tokens(struct isl_stream *s)
650 {
651         int i;
652
653         if (!s)
654                 return;
655         for (i = 0; i < s->n_token; ++i)
656                 isl_token_free(s->tokens[i]);
657         s->n_token = 0;
658 }
659
660 void isl_stream_free(struct isl_stream *s)
661 {
662         if (!s)
663                 return;
664         free(s->buffer);
665         if (s->n_token != 0) {
666                 struct isl_token *tok = isl_stream_next_token(s);
667                 isl_stream_error(s, tok, "unexpected token");
668                 isl_token_free(tok);
669         }
670         if (s->keywords) {
671                 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
672                 isl_hash_table_free(s->ctx, s->keywords);
673         }
674         isl_ctx_deref(s->ctx);
675         free(s);
676 }