isl_stream_read_map: allow extra ';' at end of map description
[isl.git] / isl_stream.c
blobce506570604ba65fcc107db406f3ff202dad56fe
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <ctype.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <isl/ctx.h>
14 #include <isl/stream.h>
16 struct isl_keyword {
17 char *name;
18 enum isl_token_type type;
21 static int same_name(const void *entry, const void *val)
23 const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
25 return !strcmp(keyword->name, val);
28 enum isl_token_type isl_stream_register_keyword(struct isl_stream *s,
29 const char *name)
31 struct isl_hash_table_entry *entry;
32 struct isl_keyword *keyword;
33 uint32_t name_hash;
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;
42 name_hash = isl_hash_string(isl_hash_init(), name);
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;
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;
62 entry->data = keyword;
64 return keyword->type;
67 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
68 int line, int col, unsigned on_new_line)
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;
81 void isl_token_free(struct isl_token *tok)
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);
92 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
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);
115 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
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;
144 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
146 struct isl_stream *s = isl_stream_new(ctx);
147 if (!s)
148 return NULL;
149 s->file = file;
150 return s;
153 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
155 struct isl_stream *s = isl_stream_new(ctx);
156 if (!s)
157 return NULL;
158 s->str = str;
159 return s;
162 static int isl_stream_getc(struct isl_stream *s)
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;
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++;
183 s->c = c;
184 return c;
187 static void isl_stream_ungetc(struct isl_stream *s, int c)
189 if (s->file)
190 ungetc(c, s->file);
191 else
192 --s->str;
193 s->c = -1;
196 static int isl_stream_push_char(struct isl_stream *s, int c)
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;
204 s->buffer[s->len++] = c;
205 return 0;
208 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
210 isl_assert(s->ctx, s->n_token < 5, return);
211 s->tokens[s->n_token++] = tok;
214 static enum isl_token_type check_keywords(struct isl_stream *s)
216 struct isl_hash_table_entry *entry;
217 struct isl_keyword *keyword;
218 uint32_t name_hash;
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, "not"))
227 return ISL_TOKEN_NOT;
228 if (!strcasecmp(s->buffer, "infty"))
229 return ISL_TOKEN_INFTY;
230 if (!strcasecmp(s->buffer, "infinity"))
231 return ISL_TOKEN_INFTY;
232 if (!strcasecmp(s->buffer, "NaN"))
233 return ISL_TOKEN_NAN;
234 if (!strcasecmp(s->buffer, "max"))
235 return ISL_TOKEN_MAX;
236 if (!strcasecmp(s->buffer, "rat"))
237 return ISL_TOKEN_RAT;
239 if (!s->keywords)
240 return ISL_TOKEN_IDENT;
242 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
243 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
244 s->buffer, 0);
245 if (entry) {
246 keyword = entry->data;
247 return keyword->type;
250 return ISL_TOKEN_IDENT;
253 int isl_stream_skip_line(struct isl_stream *s)
255 int c;
257 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
258 /* nothing */
261 return c == -1 ? -1 : 0;
264 static struct isl_token *next_token(struct isl_stream *s, int same_line)
266 int c;
267 struct isl_token *tok = NULL;
268 int line, col;
269 int old_line = s->line;
271 if (s->n_token) {
272 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
273 return NULL;
274 return s->tokens[--s->n_token];
277 if (same_line && s->c == '\n')
278 return NULL;
280 s->len = 0;
282 /* skip spaces and comment lines */
283 while ((c = isl_stream_getc(s)) != -1) {
284 if (c == '#') {
285 if (isl_stream_skip_line(s) < 0)
286 break;
287 c = '\n';
288 if (same_line)
289 break;
290 } else if (!isspace(c) || (same_line && c == '\n'))
291 break;
294 line = s->line;
295 col = s->col;
297 if (c == -1 || (same_line && c == '\n'))
298 return NULL;
299 if (c == '(' ||
300 c == ')' ||
301 c == '+' ||
302 c == '/' ||
303 c == '*' ||
304 c == '%' ||
305 c == '^' ||
306 c == '=' ||
307 c == '@' ||
308 c == ',' ||
309 c == '.' ||
310 c == ';' ||
311 c == '[' ||
312 c == ']' ||
313 c == '{' ||
314 c == '}') {
315 tok = isl_token_new(s->ctx, line, col, old_line != line);
316 if (!tok)
317 return NULL;
318 tok->type = (enum isl_token_type)c;
319 return tok;
321 if (c == '-') {
322 int c;
323 if ((c = isl_stream_getc(s)) == '>') {
324 tok = isl_token_new(s->ctx, line, col, old_line != line);
325 if (!tok)
326 return NULL;
327 tok->u.s = strdup("->");
328 tok->type = ISL_TOKEN_TO;
329 return tok;
331 if (c != -1)
332 isl_stream_ungetc(s, c);
333 if (!isdigit(c)) {
334 tok = isl_token_new(s->ctx, line, col, old_line != line);
335 if (!tok)
336 return NULL;
337 tok->type = (enum isl_token_type) '-';
338 return tok;
341 if (c == '-' || isdigit(c)) {
342 tok = isl_token_new(s->ctx, line, col, old_line != line);
343 if (!tok)
344 return NULL;
345 tok->type = ISL_TOKEN_VALUE;
346 isl_int_init(tok->u.v);
347 if (isl_stream_push_char(s, c))
348 goto error;
349 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
350 if (isl_stream_push_char(s, c))
351 goto error;
352 if (c != -1)
353 isl_stream_ungetc(s, c);
354 isl_stream_push_char(s, '\0');
355 isl_int_read(tok->u.v, s->buffer);
356 return tok;
358 if (isalpha(c) || c == '_') {
359 tok = isl_token_new(s->ctx, line, col, old_line != line);
360 if (!tok)
361 return NULL;
362 isl_stream_push_char(s, c);
363 while ((c = isl_stream_getc(s)) != -1 &&
364 (isalnum(c) || c == '_'))
365 isl_stream_push_char(s, c);
366 if (c != -1)
367 isl_stream_ungetc(s, c);
368 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
369 isl_stream_push_char(s, c);
370 if (c != -1)
371 isl_stream_ungetc(s, c);
372 isl_stream_push_char(s, '\0');
373 tok->type = check_keywords(s);
374 if (tok->type != ISL_TOKEN_IDENT)
375 tok->is_keyword = 1;
376 tok->u.s = strdup(s->buffer);
377 if (!tok->u.s)
378 goto error;
379 return tok;
381 if (c == '"') {
382 tok = isl_token_new(s->ctx, line, col, old_line != line);
383 if (!tok)
384 return NULL;
385 tok->type = ISL_TOKEN_STRING;
386 tok->u.s = NULL;
387 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
388 isl_stream_push_char(s, c);
389 if (c != '"') {
390 isl_stream_error(s, NULL, "unterminated string");
391 goto error;
393 isl_stream_push_char(s, '\0');
394 tok->u.s = strdup(s->buffer);
395 return tok;
397 if (c == ':') {
398 int c;
399 tok = isl_token_new(s->ctx, line, col, old_line != line);
400 if (!tok)
401 return NULL;
402 if ((c = isl_stream_getc(s)) == '=') {
403 tok->u.s = strdup(":=");
404 tok->type = ISL_TOKEN_DEF;
405 return tok;
407 if (c != -1)
408 isl_stream_ungetc(s, c);
409 tok->type = (enum isl_token_type) ':';
410 return tok;
412 if (c == '>') {
413 int c;
414 tok = isl_token_new(s->ctx, line, col, old_line != line);
415 if (!tok)
416 return NULL;
417 if ((c = isl_stream_getc(s)) == '=') {
418 tok->u.s = strdup(">=");
419 tok->type = ISL_TOKEN_GE;
420 return tok;
421 } else if (c == '>') {
422 if ((c = isl_stream_getc(s)) == '=') {
423 tok->u.s = strdup(">>=");
424 tok->type = ISL_TOKEN_LEX_GE;
425 return tok;
427 tok->u.s = strdup(">>");
428 tok->type = ISL_TOKEN_LEX_GT;
429 } else {
430 tok->u.s = strdup(">");
431 tok->type = ISL_TOKEN_GT;
433 if (c != -1)
434 isl_stream_ungetc(s, c);
435 return tok;
437 if (c == '<') {
438 int c;
439 tok = isl_token_new(s->ctx, line, col, old_line != line);
440 if (!tok)
441 return NULL;
442 if ((c = isl_stream_getc(s)) == '=') {
443 tok->u.s = strdup("<=");
444 tok->type = ISL_TOKEN_LE;
445 return tok;
446 } else if (c == '<') {
447 if ((c = isl_stream_getc(s)) == '=') {
448 tok->u.s = strdup("<<=");
449 tok->type = ISL_TOKEN_LEX_LE;
450 return tok;
452 tok->u.s = strdup("<<");
453 tok->type = ISL_TOKEN_LEX_LT;
454 } else {
455 tok->u.s = strdup("<");
456 tok->type = ISL_TOKEN_LT;
458 if (c != -1)
459 isl_stream_ungetc(s, c);
460 return tok;
462 if (c == '&') {
463 tok = isl_token_new(s->ctx, line, col, old_line != line);
464 if (!tok)
465 return NULL;
466 tok->type = ISL_TOKEN_AND;
467 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
468 tok->u.s = strdup("&");
469 isl_stream_ungetc(s, c);
470 } else
471 tok->u.s = strdup("&&");
472 return tok;
474 if (c == '|') {
475 tok = isl_token_new(s->ctx, line, col, old_line != line);
476 if (!tok)
477 return NULL;
478 tok->type = ISL_TOKEN_OR;
479 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
480 tok->u.s = strdup("|");
481 isl_stream_ungetc(s, c);
482 } else
483 tok->u.s = strdup("||");
484 return tok;
486 if (c == '!') {
487 tok = isl_token_new(s->ctx, line, col, old_line != line);
488 if (!tok)
489 return NULL;
490 tok->type = ISL_TOKEN_NOT;
491 tok->u.s = strdup("!");
492 return tok;
495 tok = isl_token_new(s->ctx, line, col, old_line != line);
496 if (!tok)
497 return NULL;
498 tok->type = ISL_TOKEN_UNKNOWN;
499 return tok;
500 error:
501 isl_token_free(tok);
502 return NULL;
505 struct isl_token *isl_stream_next_token(struct isl_stream *s)
507 return next_token(s, 0);
510 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
512 return next_token(s, 1);
515 int isl_stream_eat_if_available(struct isl_stream *s, int type)
517 struct isl_token *tok;
519 tok = isl_stream_next_token(s);
520 if (!tok)
521 return 0;
522 if (tok->type == type) {
523 isl_token_free(tok);
524 return 1;
526 isl_stream_push_token(s, tok);
527 return 0;
530 int isl_stream_next_token_is(struct isl_stream *s, int type)
532 struct isl_token *tok;
533 int r;
535 tok = isl_stream_next_token(s);
536 if (!tok)
537 return 0;
538 r = tok->type == type;
539 isl_stream_push_token(s, tok);
540 return r;
543 char *isl_stream_read_ident_if_available(struct isl_stream *s)
545 struct isl_token *tok;
547 tok = isl_stream_next_token(s);
548 if (!tok)
549 return NULL;
550 if (tok->type == ISL_TOKEN_IDENT) {
551 char *ident = strdup(tok->u.s);
552 isl_token_free(tok);
553 return ident;
555 isl_stream_push_token(s, tok);
556 return NULL;
559 int isl_stream_eat(struct isl_stream *s, int type)
561 struct isl_token *tok;
563 tok = isl_stream_next_token(s);
564 if (!tok)
565 return -1;
566 if (tok->type == type) {
567 isl_token_free(tok);
568 return 0;
570 isl_stream_error(s, tok, "expecting other token");
571 isl_stream_push_token(s, tok);
572 return -1;
575 int isl_stream_is_empty(struct isl_stream *s)
577 struct isl_token *tok;
579 tok = isl_stream_next_token(s);
581 if (!tok)
582 return 1;
584 isl_stream_push_token(s, tok);
585 return 0;
588 static int free_keyword(void **p, void *user)
590 struct isl_keyword *keyword = *p;
592 free(keyword->name);
593 free(keyword);
595 return 0;
598 void isl_stream_flush_tokens(struct isl_stream *s)
600 int i;
602 if (!s)
603 return;
604 for (i = 0; i < s->n_token; ++i)
605 isl_token_free(s->tokens[i]);
606 s->n_token = 0;
609 void isl_stream_free(struct isl_stream *s)
611 if (!s)
612 return;
613 free(s->buffer);
614 if (s->n_token != 0) {
615 struct isl_token *tok = isl_stream_next_token(s);
616 isl_stream_error(s, tok, "unexpected token");
617 isl_token_free(tok);
619 if (s->keywords) {
620 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
621 isl_hash_table_free(s->ctx, s->keywords);
623 isl_ctx_deref(s->ctx);
624 free(s);