isl_stream_read_map: read rational maps
[isl.git] / isl_stream.c
blob3d37343eef36e012d5429aa63c36579603daddf2
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, "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 if (!strcasecmp(s->buffer, "rat"))
235 return ISL_TOKEN_RAT;
237 if (!s->keywords)
238 return ISL_TOKEN_IDENT;
240 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
241 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
242 s->buffer, 0);
243 if (entry) {
244 keyword = entry->data;
245 return keyword->type;
248 return ISL_TOKEN_IDENT;
251 int isl_stream_skip_line(struct isl_stream *s)
253 int c;
255 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
256 /* nothing */
259 return c == -1 ? -1 : 0;
262 static struct isl_token *next_token(struct isl_stream *s, int same_line)
264 int c;
265 struct isl_token *tok = NULL;
266 int line, col;
267 int old_line = s->line;
269 if (s->n_token) {
270 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
271 return NULL;
272 return s->tokens[--s->n_token];
275 if (same_line && s->c == '\n')
276 return NULL;
278 s->len = 0;
280 /* skip spaces and comment lines */
281 while ((c = isl_stream_getc(s)) != -1) {
282 if (c == '#') {
283 if (isl_stream_skip_line(s) < 0)
284 break;
285 c = '\n';
286 if (same_line)
287 break;
288 } else if (!isspace(c) || (same_line && c == '\n'))
289 break;
292 line = s->line;
293 col = s->col;
295 if (c == -1 || (same_line && c == '\n'))
296 return NULL;
297 if (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 c == '{' ||
312 c == '}') {
313 tok = isl_token_new(s->ctx, line, col, old_line != line);
314 if (!tok)
315 return NULL;
316 tok->type = (enum isl_token_type)c;
317 return tok;
319 if (c == '-') {
320 int c;
321 if ((c = isl_stream_getc(s)) == '>') {
322 tok = isl_token_new(s->ctx, line, col, old_line != line);
323 if (!tok)
324 return NULL;
325 tok->u.s = strdup("->");
326 tok->type = ISL_TOKEN_TO;
327 return tok;
329 if (c != -1)
330 isl_stream_ungetc(s, c);
331 if (!isdigit(c)) {
332 tok = isl_token_new(s->ctx, line, col, old_line != line);
333 if (!tok)
334 return NULL;
335 tok->type = (enum isl_token_type) '-';
336 return tok;
339 if (c == '-' || isdigit(c)) {
340 tok = isl_token_new(s->ctx, line, col, old_line != line);
341 if (!tok)
342 return NULL;
343 tok->type = ISL_TOKEN_VALUE;
344 isl_int_init(tok->u.v);
345 if (isl_stream_push_char(s, c))
346 goto error;
347 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
348 if (isl_stream_push_char(s, c))
349 goto error;
350 if (c != -1)
351 isl_stream_ungetc(s, c);
352 isl_stream_push_char(s, '\0');
353 isl_int_read(tok->u.v, s->buffer);
354 return tok;
356 if (isalpha(c) || c == '_') {
357 tok = isl_token_new(s->ctx, line, col, old_line != line);
358 if (!tok)
359 return NULL;
360 isl_stream_push_char(s, c);
361 while ((c = isl_stream_getc(s)) != -1 &&
362 (isalnum(c) || c == '_'))
363 isl_stream_push_char(s, c);
364 if (c != -1)
365 isl_stream_ungetc(s, c);
366 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
367 isl_stream_push_char(s, c);
368 if (c != -1)
369 isl_stream_ungetc(s, c);
370 isl_stream_push_char(s, '\0');
371 tok->type = check_keywords(s);
372 if (tok->type != ISL_TOKEN_IDENT)
373 tok->is_keyword = 1;
374 tok->u.s = strdup(s->buffer);
375 if (!tok->u.s)
376 goto error;
377 return tok;
379 if (c == '"') {
380 tok = isl_token_new(s->ctx, line, col, old_line != line);
381 if (!tok)
382 return NULL;
383 tok->type = ISL_TOKEN_STRING;
384 tok->u.s = NULL;
385 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
386 isl_stream_push_char(s, c);
387 if (c != '"') {
388 isl_stream_error(s, NULL, "unterminated string");
389 goto error;
391 isl_stream_push_char(s, '\0');
392 tok->u.s = strdup(s->buffer);
393 return tok;
395 if (c == ':') {
396 int c;
397 tok = isl_token_new(s->ctx, line, col, old_line != line);
398 if (!tok)
399 return NULL;
400 if ((c = isl_stream_getc(s)) == '=') {
401 tok->u.s = strdup(":=");
402 tok->type = ISL_TOKEN_DEF;
403 return tok;
405 if (c != -1)
406 isl_stream_ungetc(s, c);
407 tok->type = (enum isl_token_type) ':';
408 return tok;
410 if (c == '>') {
411 int c;
412 tok = isl_token_new(s->ctx, line, col, old_line != line);
413 if (!tok)
414 return NULL;
415 if ((c = isl_stream_getc(s)) == '=') {
416 tok->u.s = strdup(">=");
417 tok->type = ISL_TOKEN_GE;
418 return tok;
419 } else if (c == '>') {
420 if ((c = isl_stream_getc(s)) == '=') {
421 tok->u.s = strdup(">>=");
422 tok->type = ISL_TOKEN_LEX_GE;
423 return tok;
425 tok->u.s = strdup(">>");
426 tok->type = ISL_TOKEN_LEX_GT;
427 } else {
428 tok->u.s = strdup(">");
429 tok->type = ISL_TOKEN_GT;
431 if (c != -1)
432 isl_stream_ungetc(s, c);
433 return tok;
435 if (c == '<') {
436 int c;
437 tok = isl_token_new(s->ctx, line, col, old_line != line);
438 if (!tok)
439 return NULL;
440 if ((c = isl_stream_getc(s)) == '=') {
441 tok->u.s = strdup("<=");
442 tok->type = ISL_TOKEN_LE;
443 return tok;
444 } else if (c == '<') {
445 if ((c = isl_stream_getc(s)) == '=') {
446 tok->u.s = strdup("<<=");
447 tok->type = ISL_TOKEN_LEX_LE;
448 return tok;
450 tok->u.s = strdup("<<");
451 tok->type = ISL_TOKEN_LEX_LT;
452 } else {
453 tok->u.s = strdup("<");
454 tok->type = ISL_TOKEN_LT;
456 if (c != -1)
457 isl_stream_ungetc(s, c);
458 return tok;
460 if (c == '&') {
461 tok = isl_token_new(s->ctx, line, col, old_line != line);
462 if (!tok)
463 return NULL;
464 tok->type = ISL_TOKEN_AND;
465 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
466 tok->u.s = strdup("&");
467 isl_stream_ungetc(s, c);
468 } else
469 tok->u.s = strdup("&&");
470 return tok;
472 if (c == '|') {
473 tok = isl_token_new(s->ctx, line, col, old_line != line);
474 if (!tok)
475 return NULL;
476 tok->type = ISL_TOKEN_OR;
477 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
478 tok->u.s = strdup("|");
479 isl_stream_ungetc(s, c);
480 } else
481 tok->u.s = strdup("||");
482 return tok;
485 tok = isl_token_new(s->ctx, line, col, old_line != line);
486 if (!tok)
487 return NULL;
488 tok->type = ISL_TOKEN_UNKNOWN;
489 return tok;
490 error:
491 isl_token_free(tok);
492 return NULL;
495 struct isl_token *isl_stream_next_token(struct isl_stream *s)
497 return next_token(s, 0);
500 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
502 return next_token(s, 1);
505 int isl_stream_eat_if_available(struct isl_stream *s, int type)
507 struct isl_token *tok;
509 tok = isl_stream_next_token(s);
510 if (!tok)
511 return 0;
512 if (tok->type == type) {
513 isl_token_free(tok);
514 return 1;
516 isl_stream_push_token(s, tok);
517 return 0;
520 int isl_stream_next_token_is(struct isl_stream *s, int type)
522 struct isl_token *tok;
523 int r;
525 tok = isl_stream_next_token(s);
526 if (!tok)
527 return 0;
528 r = tok->type == type;
529 isl_stream_push_token(s, tok);
530 return r;
533 char *isl_stream_read_ident_if_available(struct isl_stream *s)
535 struct isl_token *tok;
537 tok = isl_stream_next_token(s);
538 if (!tok)
539 return NULL;
540 if (tok->type == ISL_TOKEN_IDENT) {
541 char *ident = strdup(tok->u.s);
542 isl_token_free(tok);
543 return ident;
545 isl_stream_push_token(s, tok);
546 return NULL;
549 int isl_stream_eat(struct isl_stream *s, int type)
551 struct isl_token *tok;
553 tok = isl_stream_next_token(s);
554 if (!tok)
555 return -1;
556 if (tok->type == type) {
557 isl_token_free(tok);
558 return 0;
560 isl_stream_error(s, tok, "expecting other token");
561 isl_stream_push_token(s, tok);
562 return -1;
565 int isl_stream_is_empty(struct isl_stream *s)
567 struct isl_token *tok;
569 tok = isl_stream_next_token(s);
571 if (!tok)
572 return 1;
574 isl_stream_push_token(s, tok);
575 return 0;
578 static int free_keyword(void **p, void *user)
580 struct isl_keyword *keyword = *p;
582 free(keyword->name);
583 free(keyword);
585 return 0;
588 void isl_stream_flush_tokens(struct isl_stream *s)
590 int i;
592 if (!s)
593 return;
594 for (i = 0; i < s->n_token; ++i)
595 isl_token_free(s->tokens[i]);
596 s->n_token = 0;
599 void isl_stream_free(struct isl_stream *s)
601 if (!s)
602 return;
603 free(s->buffer);
604 if (s->n_token != 0) {
605 struct isl_token *tok = isl_stream_next_token(s);
606 isl_stream_error(s, tok, "unexpected token");
607 isl_token_free(tok);
609 if (s->keywords) {
610 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
611 isl_hash_table_free(s->ctx, s->keywords);
613 isl_ctx_deref(s->ctx);
614 free(s);