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 /* Return the type of "tok".
86 int isl_token_get_type(struct isl_token *tok)
88 return tok ? tok->type : ISL_TOKEN_ERROR;
91 /* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
93 __isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok)
97 if (tok->type != ISL_TOKEN_VALUE)
98 isl_die(ctx, isl_error_invalid, "not a value token",
101 return isl_val_int_from_isl_int(ctx, tok->u.v);
104 /* Given a token of type ISL_TOKEN_STRING, return the string it represents.
106 __isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok)
110 if (tok->type != ISL_TOKEN_STRING)
111 isl_die(ctx, isl_error_invalid, "not a string token",
114 return strdup(tok->u.s);
117 void isl_token_free(struct isl_token *tok)
121 if (tok->type == ISL_TOKEN_VALUE)
122 isl_int_clear(tok->u.v);
123 else if (tok->type == ISL_TOKEN_MAP)
124 isl_map_free(tok->u.map);
125 else if (tok->type == ISL_TOKEN_AFF)
126 isl_pw_aff_free(tok->u.pwaff);
132 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
134 int line = tok ? tok->line : s->line;
135 int col = tok ? tok->col : s->col;
136 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
139 fprintf(stderr, "got '%c'\n", tok->type);
140 else if (tok->type == ISL_TOKEN_IDENT)
141 fprintf(stderr, "got ident '%s'\n", tok->u.s);
142 else if (tok->is_keyword)
143 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
144 else if (tok->type == ISL_TOKEN_VALUE) {
145 fprintf(stderr, "got value '");
146 isl_int_print(stderr, tok->u.v, 0);
147 fprintf(stderr, "'\n");
148 } else if (tok->type == ISL_TOKEN_MAP) {
150 fprintf(stderr, "got map '");
151 p = isl_printer_to_file(s->ctx, stderr);
152 p = isl_printer_print_map(p, tok->u.map);
154 fprintf(stderr, "'\n");
155 } else if (tok->type == ISL_TOKEN_AFF) {
157 fprintf(stderr, "got affine expression '");
158 p = isl_printer_to_file(s->ctx, stderr);
159 p = isl_printer_print_pw_aff(p, tok->u.pwaff);
161 fprintf(stderr, "'\n");
163 fprintf(stderr, "got token '%s'\n", tok->u.s);
165 fprintf(stderr, "got token type %d\n", tok->type);
169 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
172 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
185 for (i = 0; i < 5; ++i)
190 s->buffer = isl_alloc_array(ctx, char, s->size);
199 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
201 struct isl_stream *s = isl_stream_new(ctx);
208 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
210 struct isl_stream *s;
213 s = isl_stream_new(ctx);
220 static int stream_getc(struct isl_stream *s)
226 return s->c = s->un[--s->n_un];
247 static void isl_stream_ungetc(struct isl_stream *s, int c)
249 isl_assert(s->ctx, s->n_un < 5, return);
250 s->un[s->n_un++] = c;
254 static int isl_stream_getc(struct isl_stream *s)
265 isl_stream_ungetc(s, c);
270 static int isl_stream_push_char(struct isl_stream *s, int c)
272 if (s->len >= s->size) {
274 s->size = (3*s->size)/2;
275 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
280 s->buffer[s->len++] = c;
284 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
286 isl_assert(s->ctx, s->n_token < 5, return);
287 s->tokens[s->n_token++] = tok;
290 static enum isl_token_type check_keywords(struct isl_stream *s)
292 struct isl_hash_table_entry *entry;
293 struct isl_keyword *keyword;
296 if (!strcasecmp(s->buffer, "exists"))
297 return ISL_TOKEN_EXISTS;
298 if (!strcasecmp(s->buffer, "and"))
299 return ISL_TOKEN_AND;
300 if (!strcasecmp(s->buffer, "or"))
302 if (!strcasecmp(s->buffer, "not"))
303 return ISL_TOKEN_NOT;
304 if (!strcasecmp(s->buffer, "infty"))
305 return ISL_TOKEN_INFTY;
306 if (!strcasecmp(s->buffer, "infinity"))
307 return ISL_TOKEN_INFTY;
308 if (!strcasecmp(s->buffer, "NaN"))
309 return ISL_TOKEN_NAN;
310 if (!strcasecmp(s->buffer, "min"))
311 return ISL_TOKEN_MIN;
312 if (!strcasecmp(s->buffer, "max"))
313 return ISL_TOKEN_MAX;
314 if (!strcasecmp(s->buffer, "rat"))
315 return ISL_TOKEN_RAT;
316 if (!strcasecmp(s->buffer, "true"))
317 return ISL_TOKEN_TRUE;
318 if (!strcasecmp(s->buffer, "false"))
319 return ISL_TOKEN_FALSE;
320 if (!strcasecmp(s->buffer, "ceild"))
321 return ISL_TOKEN_CEILD;
322 if (!strcasecmp(s->buffer, "floord"))
323 return ISL_TOKEN_FLOORD;
324 if (!strcasecmp(s->buffer, "mod"))
325 return ISL_TOKEN_MOD;
326 if (!strcasecmp(s->buffer, "ceil"))
327 return ISL_TOKEN_CEIL;
328 if (!strcasecmp(s->buffer, "floor"))
329 return ISL_TOKEN_FLOOR;
332 return ISL_TOKEN_IDENT;
334 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
335 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
338 keyword = entry->data;
339 return keyword->type;
342 return ISL_TOKEN_IDENT;
345 int isl_stream_skip_line(struct isl_stream *s)
349 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
353 return c == -1 ? -1 : 0;
356 static struct isl_token *next_token(struct isl_stream *s, int same_line)
359 struct isl_token *tok = NULL;
361 int old_line = s->line;
364 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
366 return s->tokens[--s->n_token];
369 if (same_line && s->c == '\n')
374 /* skip spaces and comment lines */
375 while ((c = isl_stream_getc(s)) != -1) {
377 if (isl_stream_skip_line(s) < 0)
382 } else if (!isspace(c) || (same_line && c == '\n'))
389 if (c == -1 || (same_line && c == '\n'))
407 tok = isl_token_new(s->ctx, line, col, old_line != line);
410 tok->type = (enum isl_token_type)c;
415 if ((c = isl_stream_getc(s)) == '>') {
416 tok = isl_token_new(s->ctx, line, col, old_line != line);
419 tok->u.s = strdup("->");
420 tok->type = ISL_TOKEN_TO;
424 isl_stream_ungetc(s, c);
426 tok = isl_token_new(s->ctx, line, col, old_line != line);
429 tok->type = (enum isl_token_type) '-';
433 if (c == '-' || isdigit(c)) {
434 int minus = c == '-';
435 tok = isl_token_new(s->ctx, line, col, old_line != line);
438 tok->type = ISL_TOKEN_VALUE;
439 isl_int_init(tok->u.v);
440 if (isl_stream_push_char(s, c))
442 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
443 if (isl_stream_push_char(s, c))
446 isl_stream_ungetc(s, c);
447 isl_stream_push_char(s, '\0');
448 isl_int_read(tok->u.v, s->buffer);
449 if (minus && isl_int_is_zero(tok->u.v)) {
451 tok->on_new_line = 0;
452 isl_stream_push_token(s, tok);
453 tok = isl_token_new(s->ctx, line, col, old_line != line);
456 tok->type = (enum isl_token_type) '-';
460 if (isalpha(c) || c == '_') {
461 tok = isl_token_new(s->ctx, line, col, old_line != line);
464 isl_stream_push_char(s, c);
465 while ((c = isl_stream_getc(s)) != -1 &&
466 (isalnum(c) || c == '_'))
467 isl_stream_push_char(s, c);
469 isl_stream_ungetc(s, c);
470 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
471 isl_stream_push_char(s, c);
473 isl_stream_ungetc(s, c);
474 isl_stream_push_char(s, '\0');
475 tok->type = check_keywords(s);
476 if (tok->type != ISL_TOKEN_IDENT)
478 tok->u.s = strdup(s->buffer);
484 tok = isl_token_new(s->ctx, line, col, old_line != line);
487 tok->type = ISL_TOKEN_STRING;
489 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
490 isl_stream_push_char(s, c);
492 isl_stream_error(s, NULL, "unterminated string");
495 isl_stream_push_char(s, '\0');
496 tok->u.s = strdup(s->buffer);
501 tok = isl_token_new(s->ctx, line, col, old_line != line);
504 if ((c = isl_stream_getc(s)) == '=') {
505 tok->u.s = strdup("==");
506 tok->type = ISL_TOKEN_EQ_EQ;
510 isl_stream_ungetc(s, c);
511 tok->type = (enum isl_token_type) '=';
516 tok = isl_token_new(s->ctx, line, col, old_line != line);
519 if ((c = isl_stream_getc(s)) == '=') {
520 tok->u.s = strdup(":=");
521 tok->type = ISL_TOKEN_DEF;
525 isl_stream_ungetc(s, c);
526 tok->type = (enum isl_token_type) ':';
531 tok = isl_token_new(s->ctx, line, col, old_line != line);
534 if ((c = isl_stream_getc(s)) == '=') {
535 tok->u.s = strdup(">=");
536 tok->type = ISL_TOKEN_GE;
538 } else if (c == '>') {
539 if ((c = isl_stream_getc(s)) == '=') {
540 tok->u.s = strdup(">>=");
541 tok->type = ISL_TOKEN_LEX_GE;
544 tok->u.s = strdup(">>");
545 tok->type = ISL_TOKEN_LEX_GT;
547 tok->u.s = strdup(">");
548 tok->type = ISL_TOKEN_GT;
551 isl_stream_ungetc(s, c);
556 tok = isl_token_new(s->ctx, line, col, old_line != line);
559 if ((c = isl_stream_getc(s)) == '=') {
560 tok->u.s = strdup("<=");
561 tok->type = ISL_TOKEN_LE;
563 } else if (c == '<') {
564 if ((c = isl_stream_getc(s)) == '=') {
565 tok->u.s = strdup("<<=");
566 tok->type = ISL_TOKEN_LEX_LE;
569 tok->u.s = strdup("<<");
570 tok->type = ISL_TOKEN_LEX_LT;
572 tok->u.s = strdup("<");
573 tok->type = ISL_TOKEN_LT;
576 isl_stream_ungetc(s, c);
580 tok = isl_token_new(s->ctx, line, col, old_line != line);
583 tok->type = ISL_TOKEN_AND;
584 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
585 tok->u.s = strdup("&");
586 isl_stream_ungetc(s, c);
588 tok->u.s = strdup("&&");
592 tok = isl_token_new(s->ctx, line, col, old_line != line);
595 tok->type = ISL_TOKEN_OR;
596 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
597 tok->u.s = strdup("|");
598 isl_stream_ungetc(s, c);
600 tok->u.s = strdup("||");
604 tok = isl_token_new(s->ctx, line, col, old_line != line);
607 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
608 tok->type = (enum isl_token_type) '/';
609 isl_stream_ungetc(s, c);
611 tok->u.s = strdup("/\\");
612 tok->type = ISL_TOKEN_AND;
617 tok = isl_token_new(s->ctx, line, col, old_line != line);
620 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
621 tok->type = (enum isl_token_type) '\\';
622 isl_stream_ungetc(s, c);
624 tok->u.s = strdup("\\/");
625 tok->type = ISL_TOKEN_OR;
630 tok = isl_token_new(s->ctx, line, col, old_line != line);
633 if ((c = isl_stream_getc(s)) == '=') {
634 tok->u.s = strdup("!=");
635 tok->type = ISL_TOKEN_NE;
638 tok->type = ISL_TOKEN_NOT;
639 tok->u.s = strdup("!");
642 isl_stream_ungetc(s, c);
646 tok = isl_token_new(s->ctx, line, col, old_line != line);
649 tok->type = ISL_TOKEN_UNKNOWN;
656 struct isl_token *isl_stream_next_token(struct isl_stream *s)
658 return next_token(s, 0);
661 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
663 return next_token(s, 1);
666 int isl_stream_eat_if_available(struct isl_stream *s, int type)
668 struct isl_token *tok;
670 tok = isl_stream_next_token(s);
673 if (tok->type == type) {
677 isl_stream_push_token(s, tok);
681 int isl_stream_next_token_is(struct isl_stream *s, int type)
683 struct isl_token *tok;
686 tok = isl_stream_next_token(s);
689 r = tok->type == type;
690 isl_stream_push_token(s, tok);
694 char *isl_stream_read_ident_if_available(struct isl_stream *s)
696 struct isl_token *tok;
698 tok = isl_stream_next_token(s);
701 if (tok->type == ISL_TOKEN_IDENT) {
702 char *ident = strdup(tok->u.s);
706 isl_stream_push_token(s, tok);
710 int isl_stream_eat(struct isl_stream *s, int type)
712 struct isl_token *tok;
714 tok = isl_stream_next_token(s);
717 if (tok->type == type) {
721 isl_stream_error(s, tok, "expecting other token");
722 isl_stream_push_token(s, tok);
726 int isl_stream_is_empty(struct isl_stream *s)
728 struct isl_token *tok;
730 tok = isl_stream_next_token(s);
735 isl_stream_push_token(s, tok);
739 static int free_keyword(void **p, void *user)
741 struct isl_keyword *keyword = *p;
749 void isl_stream_flush_tokens(struct isl_stream *s)
755 for (i = 0; i < s->n_token; ++i)
756 isl_token_free(s->tokens[i]);
760 void isl_stream_free(struct isl_stream *s)
765 if (s->n_token != 0) {
766 struct isl_token *tok = isl_stream_next_token(s);
767 isl_stream_error(s, tok, "unexpected token");
771 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
772 isl_hash_table_free(s->ctx, s->keywords);
774 isl_ctx_deref(s->ctx);