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