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