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