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