2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 #include <isl_stream_private.h>
17 #include <isl_val_private.h>
21 enum isl_token_type type;
24 static int same_name(const void *entry, const void *val)
26 const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
28 return !strcmp(keyword->name, val);
31 enum isl_token_type isl_stream_register_keyword(struct isl_stream *s,
34 struct isl_hash_table_entry *entry;
35 struct isl_keyword *keyword;
39 s->keywords = isl_hash_table_alloc(s->ctx, 10);
41 return ISL_TOKEN_ERROR;
42 s->next_type = ISL_TOKEN_LAST;
45 name_hash = isl_hash_string(isl_hash_init(), name);
47 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
50 return ISL_TOKEN_ERROR;
52 keyword = entry->data;
56 keyword = isl_calloc_type(s->ctx, struct isl_keyword);
58 return ISL_TOKEN_ERROR;
59 keyword->type = s->next_type++;
60 keyword->name = strdup(name);
63 return ISL_TOKEN_ERROR;
65 entry->data = keyword;
70 struct isl_token *isl_token_new(isl_ctx *ctx,
71 int line, int col, unsigned on_new_line)
73 struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
78 tok->on_new_line = on_new_line;
84 /* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
86 __isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok)
90 if (tok->type != ISL_TOKEN_VALUE)
91 isl_die(ctx, isl_error_invalid, "not a value token",
94 return isl_val_int_from_isl_int(ctx, tok->u.v);
97 /* Given a token of type ISL_TOKEN_STRING, return the string it represents.
99 __isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok)
103 if (tok->type != ISL_TOKEN_STRING)
104 isl_die(ctx, isl_error_invalid, "not a string token",
107 return strdup(tok->u.s);
110 void isl_token_free(struct isl_token *tok)
114 if (tok->type == ISL_TOKEN_VALUE)
115 isl_int_clear(tok->u.v);
116 else if (tok->type == ISL_TOKEN_MAP)
117 isl_map_free(tok->u.map);
118 else if (tok->type == ISL_TOKEN_AFF)
119 isl_pw_aff_free(tok->u.pwaff);
125 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
127 int line = tok ? tok->line : s->line;
128 int col = tok ? tok->col : s->col;
129 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
132 fprintf(stderr, "got '%c'\n", tok->type);
133 else if (tok->type == ISL_TOKEN_IDENT)
134 fprintf(stderr, "got ident '%s'\n", tok->u.s);
135 else if (tok->is_keyword)
136 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
137 else if (tok->type == ISL_TOKEN_VALUE) {
138 fprintf(stderr, "got value '");
139 isl_int_print(stderr, tok->u.v, 0);
140 fprintf(stderr, "'\n");
141 } else if (tok->type == ISL_TOKEN_MAP) {
143 fprintf(stderr, "got map '");
144 p = isl_printer_to_file(s->ctx, stderr);
145 p = isl_printer_print_map(p, tok->u.map);
147 fprintf(stderr, "'\n");
148 } else if (tok->type == ISL_TOKEN_AFF) {
150 fprintf(stderr, "got affine expression '");
151 p = isl_printer_to_file(s->ctx, stderr);
152 p = isl_printer_print_pw_aff(p, tok->u.pwaff);
154 fprintf(stderr, "'\n");
156 fprintf(stderr, "got token '%s'\n", tok->u.s);
158 fprintf(stderr, "got token type %d\n", tok->type);
162 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
165 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
178 for (i = 0; i < 5; ++i)
183 s->buffer = isl_alloc_array(ctx, char, s->size);
192 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
194 struct isl_stream *s = isl_stream_new(ctx);
201 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
203 struct isl_stream *s;
206 s = isl_stream_new(ctx);
213 static int stream_getc(struct isl_stream *s)
219 return s->c = s->un[--s->n_un];
240 static void isl_stream_ungetc(struct isl_stream *s, int c)
242 isl_assert(s->ctx, s->n_un < 5, return);
243 s->un[s->n_un++] = c;
247 static int isl_stream_getc(struct isl_stream *s)
258 isl_stream_ungetc(s, c);
263 static int isl_stream_push_char(struct isl_stream *s, int c)
265 if (s->len >= s->size) {
267 s->size = (3*s->size)/2;
268 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
273 s->buffer[s->len++] = c;
277 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
279 isl_assert(s->ctx, s->n_token < 5, return);
280 s->tokens[s->n_token++] = tok;
283 static enum isl_token_type check_keywords(struct isl_stream *s)
285 struct isl_hash_table_entry *entry;
286 struct isl_keyword *keyword;
289 if (!strcasecmp(s->buffer, "exists"))
290 return ISL_TOKEN_EXISTS;
291 if (!strcasecmp(s->buffer, "and"))
292 return ISL_TOKEN_AND;
293 if (!strcasecmp(s->buffer, "or"))
295 if (!strcasecmp(s->buffer, "not"))
296 return ISL_TOKEN_NOT;
297 if (!strcasecmp(s->buffer, "infty"))
298 return ISL_TOKEN_INFTY;
299 if (!strcasecmp(s->buffer, "infinity"))
300 return ISL_TOKEN_INFTY;
301 if (!strcasecmp(s->buffer, "NaN"))
302 return ISL_TOKEN_NAN;
303 if (!strcasecmp(s->buffer, "min"))
304 return ISL_TOKEN_MIN;
305 if (!strcasecmp(s->buffer, "max"))
306 return ISL_TOKEN_MAX;
307 if (!strcasecmp(s->buffer, "rat"))
308 return ISL_TOKEN_RAT;
309 if (!strcasecmp(s->buffer, "true"))
310 return ISL_TOKEN_TRUE;
311 if (!strcasecmp(s->buffer, "false"))
312 return ISL_TOKEN_FALSE;
313 if (!strcasecmp(s->buffer, "ceild"))
314 return ISL_TOKEN_CEILD;
315 if (!strcasecmp(s->buffer, "floord"))
316 return ISL_TOKEN_FLOORD;
317 if (!strcasecmp(s->buffer, "mod"))
318 return ISL_TOKEN_MOD;
321 return ISL_TOKEN_IDENT;
323 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
324 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
327 keyword = entry->data;
328 return keyword->type;
331 return ISL_TOKEN_IDENT;
334 int isl_stream_skip_line(struct isl_stream *s)
338 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
342 return c == -1 ? -1 : 0;
345 static struct isl_token *next_token(struct isl_stream *s, int same_line)
348 struct isl_token *tok = NULL;
350 int old_line = s->line;
353 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
355 return s->tokens[--s->n_token];
358 if (same_line && s->c == '\n')
363 /* skip spaces and comment lines */
364 while ((c = isl_stream_getc(s)) != -1) {
366 if (isl_stream_skip_line(s) < 0)
371 } else if (!isspace(c) || (same_line && c == '\n'))
378 if (c == -1 || (same_line && c == '\n'))
396 tok = isl_token_new(s->ctx, line, col, old_line != line);
399 tok->type = (enum isl_token_type)c;
404 if ((c = isl_stream_getc(s)) == '>') {
405 tok = isl_token_new(s->ctx, line, col, old_line != line);
408 tok->u.s = strdup("->");
409 tok->type = ISL_TOKEN_TO;
413 isl_stream_ungetc(s, c);
415 tok = isl_token_new(s->ctx, line, col, old_line != line);
418 tok->type = (enum isl_token_type) '-';
422 if (c == '-' || isdigit(c)) {
423 int minus = c == '-';
424 tok = isl_token_new(s->ctx, line, col, old_line != line);
427 tok->type = ISL_TOKEN_VALUE;
428 isl_int_init(tok->u.v);
429 if (isl_stream_push_char(s, c))
431 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
432 if (isl_stream_push_char(s, c))
435 isl_stream_ungetc(s, c);
436 isl_stream_push_char(s, '\0');
437 isl_int_read(tok->u.v, s->buffer);
438 if (minus && isl_int_is_zero(tok->u.v)) {
440 tok->on_new_line = 0;
441 isl_stream_push_token(s, tok);
442 tok = isl_token_new(s->ctx, line, col, old_line != line);
445 tok->type = (enum isl_token_type) '-';
449 if (isalpha(c) || c == '_') {
450 tok = isl_token_new(s->ctx, line, col, old_line != line);
453 isl_stream_push_char(s, c);
454 while ((c = isl_stream_getc(s)) != -1 &&
455 (isalnum(c) || c == '_'))
456 isl_stream_push_char(s, c);
458 isl_stream_ungetc(s, c);
459 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
460 isl_stream_push_char(s, c);
462 isl_stream_ungetc(s, c);
463 isl_stream_push_char(s, '\0');
464 tok->type = check_keywords(s);
465 if (tok->type != ISL_TOKEN_IDENT)
467 tok->u.s = strdup(s->buffer);
473 tok = isl_token_new(s->ctx, line, col, old_line != line);
476 tok->type = ISL_TOKEN_STRING;
478 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
479 isl_stream_push_char(s, c);
481 isl_stream_error(s, NULL, "unterminated string");
484 isl_stream_push_char(s, '\0');
485 tok->u.s = strdup(s->buffer);
490 tok = isl_token_new(s->ctx, line, col, old_line != line);
493 if ((c = isl_stream_getc(s)) == '=') {
494 tok->u.s = strdup("==");
495 tok->type = ISL_TOKEN_EQ_EQ;
499 isl_stream_ungetc(s, c);
500 tok->type = (enum isl_token_type) '=';
505 tok = isl_token_new(s->ctx, line, col, old_line != line);
508 if ((c = isl_stream_getc(s)) == '=') {
509 tok->u.s = strdup(":=");
510 tok->type = ISL_TOKEN_DEF;
514 isl_stream_ungetc(s, c);
515 tok->type = (enum isl_token_type) ':';
520 tok = isl_token_new(s->ctx, line, col, old_line != line);
523 if ((c = isl_stream_getc(s)) == '=') {
524 tok->u.s = strdup(">=");
525 tok->type = ISL_TOKEN_GE;
527 } else if (c == '>') {
528 if ((c = isl_stream_getc(s)) == '=') {
529 tok->u.s = strdup(">>=");
530 tok->type = ISL_TOKEN_LEX_GE;
533 tok->u.s = strdup(">>");
534 tok->type = ISL_TOKEN_LEX_GT;
536 tok->u.s = strdup(">");
537 tok->type = ISL_TOKEN_GT;
540 isl_stream_ungetc(s, c);
545 tok = isl_token_new(s->ctx, line, col, old_line != line);
548 if ((c = isl_stream_getc(s)) == '=') {
549 tok->u.s = strdup("<=");
550 tok->type = ISL_TOKEN_LE;
552 } else if (c == '<') {
553 if ((c = isl_stream_getc(s)) == '=') {
554 tok->u.s = strdup("<<=");
555 tok->type = ISL_TOKEN_LEX_LE;
558 tok->u.s = strdup("<<");
559 tok->type = ISL_TOKEN_LEX_LT;
561 tok->u.s = strdup("<");
562 tok->type = ISL_TOKEN_LT;
565 isl_stream_ungetc(s, c);
569 tok = isl_token_new(s->ctx, line, col, old_line != line);
572 tok->type = ISL_TOKEN_AND;
573 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
574 tok->u.s = strdup("&");
575 isl_stream_ungetc(s, c);
577 tok->u.s = strdup("&&");
581 tok = isl_token_new(s->ctx, line, col, old_line != line);
584 tok->type = ISL_TOKEN_OR;
585 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
586 tok->u.s = strdup("|");
587 isl_stream_ungetc(s, c);
589 tok->u.s = strdup("||");
593 tok = isl_token_new(s->ctx, line, col, old_line != line);
596 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
597 tok->type = (enum isl_token_type) '/';
598 isl_stream_ungetc(s, c);
600 tok->u.s = strdup("/\\");
601 tok->type = ISL_TOKEN_AND;
606 tok = isl_token_new(s->ctx, line, col, old_line != line);
609 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
610 tok->type = (enum isl_token_type) '\\';
611 isl_stream_ungetc(s, c);
613 tok->u.s = strdup("\\/");
614 tok->type = ISL_TOKEN_OR;
619 tok = isl_token_new(s->ctx, line, col, old_line != line);
622 if ((c = isl_stream_getc(s)) == '=') {
623 tok->u.s = strdup("!=");
624 tok->type = ISL_TOKEN_NE;
627 tok->type = ISL_TOKEN_NOT;
628 tok->u.s = strdup("!");
631 isl_stream_ungetc(s, c);
635 tok = isl_token_new(s->ctx, line, col, old_line != line);
638 tok->type = ISL_TOKEN_UNKNOWN;
645 struct isl_token *isl_stream_next_token(struct isl_stream *s)
647 return next_token(s, 0);
650 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
652 return next_token(s, 1);
655 int isl_stream_eat_if_available(struct isl_stream *s, int type)
657 struct isl_token *tok;
659 tok = isl_stream_next_token(s);
662 if (tok->type == type) {
666 isl_stream_push_token(s, tok);
670 int isl_stream_next_token_is(struct isl_stream *s, int type)
672 struct isl_token *tok;
675 tok = isl_stream_next_token(s);
678 r = tok->type == type;
679 isl_stream_push_token(s, tok);
683 char *isl_stream_read_ident_if_available(struct isl_stream *s)
685 struct isl_token *tok;
687 tok = isl_stream_next_token(s);
690 if (tok->type == ISL_TOKEN_IDENT) {
691 char *ident = strdup(tok->u.s);
695 isl_stream_push_token(s, tok);
699 int isl_stream_eat(struct isl_stream *s, int type)
701 struct isl_token *tok;
703 tok = isl_stream_next_token(s);
706 if (tok->type == type) {
710 isl_stream_error(s, tok, "expecting other token");
711 isl_stream_push_token(s, tok);
715 int isl_stream_is_empty(struct isl_stream *s)
717 struct isl_token *tok;
719 tok = isl_stream_next_token(s);
724 isl_stream_push_token(s, tok);
728 static int free_keyword(void **p, void *user)
730 struct isl_keyword *keyword = *p;
738 void isl_stream_flush_tokens(struct isl_stream *s)
744 for (i = 0; i < s->n_token; ++i)
745 isl_token_free(s->tokens[i]);
749 void isl_stream_free(struct isl_stream *s)
754 if (s->n_token != 0) {
755 struct isl_token *tok = isl_stream_next_token(s);
756 isl_stream_error(s, tok, "unexpected token");
760 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
761 isl_hash_table_free(s->ctx, s->keywords);
763 isl_ctx_deref(s->ctx);