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