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