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