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