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 void isl_token_free(struct isl_token *tok)
101 if (tok->type == ISL_TOKEN_VALUE)
102 isl_int_clear(tok->u.v);
103 else if (tok->type == ISL_TOKEN_MAP)
104 isl_map_free(tok->u.map);
105 else if (tok->type == ISL_TOKEN_AFF)
106 isl_pw_aff_free(tok->u.pwaff);
112 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
114 int line = tok ? tok->line : s->line;
115 int col = tok ? tok->col : s->col;
116 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
119 fprintf(stderr, "got '%c'\n", tok->type);
120 else if (tok->type == ISL_TOKEN_IDENT)
121 fprintf(stderr, "got ident '%s'\n", tok->u.s);
122 else if (tok->is_keyword)
123 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
124 else if (tok->type == ISL_TOKEN_VALUE) {
125 fprintf(stderr, "got value '");
126 isl_int_print(stderr, tok->u.v, 0);
127 fprintf(stderr, "'\n");
128 } else if (tok->type == ISL_TOKEN_MAP) {
130 fprintf(stderr, "got map '");
131 p = isl_printer_to_file(s->ctx, stderr);
132 p = isl_printer_print_map(p, tok->u.map);
134 fprintf(stderr, "'\n");
135 } else if (tok->type == ISL_TOKEN_AFF) {
137 fprintf(stderr, "got affine expression '");
138 p = isl_printer_to_file(s->ctx, stderr);
139 p = isl_printer_print_pw_aff(p, tok->u.pwaff);
141 fprintf(stderr, "'\n");
143 fprintf(stderr, "got token '%s'\n", tok->u.s);
145 fprintf(stderr, "got token type %d\n", tok->type);
149 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
152 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
165 for (i = 0; i < 5; ++i)
170 s->buffer = isl_alloc_array(ctx, char, s->size);
179 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
181 struct isl_stream *s = isl_stream_new(ctx);
188 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
190 struct isl_stream *s;
193 s = isl_stream_new(ctx);
200 static int stream_getc(struct isl_stream *s)
206 return s->c = s->un[--s->n_un];
227 static void isl_stream_ungetc(struct isl_stream *s, int c)
229 isl_assert(s->ctx, s->n_un < 5, return);
230 s->un[s->n_un++] = c;
234 static int isl_stream_getc(struct isl_stream *s)
245 isl_stream_ungetc(s, c);
250 static int isl_stream_push_char(struct isl_stream *s, int c)
252 if (s->len >= s->size) {
254 s->size = (3*s->size)/2;
255 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
260 s->buffer[s->len++] = c;
264 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
266 isl_assert(s->ctx, s->n_token < 5, return);
267 s->tokens[s->n_token++] = tok;
270 static enum isl_token_type check_keywords(struct isl_stream *s)
272 struct isl_hash_table_entry *entry;
273 struct isl_keyword *keyword;
276 if (!strcasecmp(s->buffer, "exists"))
277 return ISL_TOKEN_EXISTS;
278 if (!strcasecmp(s->buffer, "and"))
279 return ISL_TOKEN_AND;
280 if (!strcasecmp(s->buffer, "or"))
282 if (!strcasecmp(s->buffer, "not"))
283 return ISL_TOKEN_NOT;
284 if (!strcasecmp(s->buffer, "infty"))
285 return ISL_TOKEN_INFTY;
286 if (!strcasecmp(s->buffer, "infinity"))
287 return ISL_TOKEN_INFTY;
288 if (!strcasecmp(s->buffer, "NaN"))
289 return ISL_TOKEN_NAN;
290 if (!strcasecmp(s->buffer, "min"))
291 return ISL_TOKEN_MIN;
292 if (!strcasecmp(s->buffer, "max"))
293 return ISL_TOKEN_MAX;
294 if (!strcasecmp(s->buffer, "rat"))
295 return ISL_TOKEN_RAT;
296 if (!strcasecmp(s->buffer, "true"))
297 return ISL_TOKEN_TRUE;
298 if (!strcasecmp(s->buffer, "false"))
299 return ISL_TOKEN_FALSE;
300 if (!strcasecmp(s->buffer, "ceild"))
301 return ISL_TOKEN_CEILD;
302 if (!strcasecmp(s->buffer, "floord"))
303 return ISL_TOKEN_FLOORD;
304 if (!strcasecmp(s->buffer, "mod"))
305 return ISL_TOKEN_MOD;
308 return ISL_TOKEN_IDENT;
310 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
311 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
314 keyword = entry->data;
315 return keyword->type;
318 return ISL_TOKEN_IDENT;
321 int isl_stream_skip_line(struct isl_stream *s)
325 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
329 return c == -1 ? -1 : 0;
332 static struct isl_token *next_token(struct isl_stream *s, int same_line)
335 struct isl_token *tok = NULL;
337 int old_line = s->line;
340 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
342 return s->tokens[--s->n_token];
345 if (same_line && s->c == '\n')
350 /* skip spaces and comment lines */
351 while ((c = isl_stream_getc(s)) != -1) {
353 if (isl_stream_skip_line(s) < 0)
358 } else if (!isspace(c) || (same_line && c == '\n'))
365 if (c == -1 || (same_line && c == '\n'))
383 tok = isl_token_new(s->ctx, line, col, old_line != line);
386 tok->type = (enum isl_token_type)c;
391 if ((c = isl_stream_getc(s)) == '>') {
392 tok = isl_token_new(s->ctx, line, col, old_line != line);
395 tok->u.s = strdup("->");
396 tok->type = ISL_TOKEN_TO;
400 isl_stream_ungetc(s, c);
402 tok = isl_token_new(s->ctx, line, col, old_line != line);
405 tok->type = (enum isl_token_type) '-';
409 if (c == '-' || isdigit(c)) {
410 int minus = c == '-';
411 tok = isl_token_new(s->ctx, line, col, old_line != line);
414 tok->type = ISL_TOKEN_VALUE;
415 isl_int_init(tok->u.v);
416 if (isl_stream_push_char(s, c))
418 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
419 if (isl_stream_push_char(s, c))
422 isl_stream_ungetc(s, c);
423 isl_stream_push_char(s, '\0');
424 isl_int_read(tok->u.v, s->buffer);
425 if (minus && isl_int_is_zero(tok->u.v)) {
427 tok->on_new_line = 0;
428 isl_stream_push_token(s, tok);
429 tok = isl_token_new(s->ctx, line, col, old_line != line);
432 tok->type = (enum isl_token_type) '-';
436 if (isalpha(c) || c == '_') {
437 tok = isl_token_new(s->ctx, line, col, old_line != line);
440 isl_stream_push_char(s, c);
441 while ((c = isl_stream_getc(s)) != -1 &&
442 (isalnum(c) || c == '_'))
443 isl_stream_push_char(s, c);
445 isl_stream_ungetc(s, c);
446 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
447 isl_stream_push_char(s, c);
449 isl_stream_ungetc(s, c);
450 isl_stream_push_char(s, '\0');
451 tok->type = check_keywords(s);
452 if (tok->type != ISL_TOKEN_IDENT)
454 tok->u.s = strdup(s->buffer);
460 tok = isl_token_new(s->ctx, line, col, old_line != line);
463 tok->type = ISL_TOKEN_STRING;
465 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
466 isl_stream_push_char(s, c);
468 isl_stream_error(s, NULL, "unterminated string");
471 isl_stream_push_char(s, '\0');
472 tok->u.s = strdup(s->buffer);
477 tok = isl_token_new(s->ctx, line, col, old_line != line);
480 if ((c = isl_stream_getc(s)) == '=') {
481 tok->u.s = strdup("==");
482 tok->type = ISL_TOKEN_EQ_EQ;
486 isl_stream_ungetc(s, c);
487 tok->type = (enum isl_token_type) '=';
492 tok = isl_token_new(s->ctx, line, col, old_line != line);
495 if ((c = isl_stream_getc(s)) == '=') {
496 tok->u.s = strdup(":=");
497 tok->type = ISL_TOKEN_DEF;
501 isl_stream_ungetc(s, c);
502 tok->type = (enum isl_token_type) ':';
507 tok = isl_token_new(s->ctx, line, col, old_line != line);
510 if ((c = isl_stream_getc(s)) == '=') {
511 tok->u.s = strdup(">=");
512 tok->type = ISL_TOKEN_GE;
514 } else if (c == '>') {
515 if ((c = isl_stream_getc(s)) == '=') {
516 tok->u.s = strdup(">>=");
517 tok->type = ISL_TOKEN_LEX_GE;
520 tok->u.s = strdup(">>");
521 tok->type = ISL_TOKEN_LEX_GT;
523 tok->u.s = strdup(">");
524 tok->type = ISL_TOKEN_GT;
527 isl_stream_ungetc(s, c);
532 tok = isl_token_new(s->ctx, line, col, old_line != line);
535 if ((c = isl_stream_getc(s)) == '=') {
536 tok->u.s = strdup("<=");
537 tok->type = ISL_TOKEN_LE;
539 } else if (c == '<') {
540 if ((c = isl_stream_getc(s)) == '=') {
541 tok->u.s = strdup("<<=");
542 tok->type = ISL_TOKEN_LEX_LE;
545 tok->u.s = strdup("<<");
546 tok->type = ISL_TOKEN_LEX_LT;
548 tok->u.s = strdup("<");
549 tok->type = ISL_TOKEN_LT;
552 isl_stream_ungetc(s, c);
556 tok = isl_token_new(s->ctx, line, col, old_line != line);
559 tok->type = ISL_TOKEN_AND;
560 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
561 tok->u.s = strdup("&");
562 isl_stream_ungetc(s, c);
564 tok->u.s = strdup("&&");
568 tok = isl_token_new(s->ctx, line, col, old_line != line);
571 tok->type = ISL_TOKEN_OR;
572 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
573 tok->u.s = strdup("|");
574 isl_stream_ungetc(s, c);
576 tok->u.s = strdup("||");
580 tok = isl_token_new(s->ctx, line, col, old_line != line);
583 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
584 tok->type = (enum isl_token_type) '/';
585 isl_stream_ungetc(s, c);
587 tok->u.s = strdup("/\\");
588 tok->type = ISL_TOKEN_AND;
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_OR;
606 tok = isl_token_new(s->ctx, line, col, old_line != line);
609 if ((c = isl_stream_getc(s)) == '=') {
610 tok->u.s = strdup("!=");
611 tok->type = ISL_TOKEN_NE;
614 tok->type = ISL_TOKEN_NOT;
615 tok->u.s = strdup("!");
618 isl_stream_ungetc(s, c);
622 tok = isl_token_new(s->ctx, line, col, old_line != line);
625 tok->type = ISL_TOKEN_UNKNOWN;
632 struct isl_token *isl_stream_next_token(struct isl_stream *s)
634 return next_token(s, 0);
637 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
639 return next_token(s, 1);
642 int isl_stream_eat_if_available(struct isl_stream *s, int type)
644 struct isl_token *tok;
646 tok = isl_stream_next_token(s);
649 if (tok->type == type) {
653 isl_stream_push_token(s, tok);
657 int isl_stream_next_token_is(struct isl_stream *s, int type)
659 struct isl_token *tok;
662 tok = isl_stream_next_token(s);
665 r = tok->type == type;
666 isl_stream_push_token(s, tok);
670 char *isl_stream_read_ident_if_available(struct isl_stream *s)
672 struct isl_token *tok;
674 tok = isl_stream_next_token(s);
677 if (tok->type == ISL_TOKEN_IDENT) {
678 char *ident = strdup(tok->u.s);
682 isl_stream_push_token(s, tok);
686 int isl_stream_eat(struct isl_stream *s, int type)
688 struct isl_token *tok;
690 tok = isl_stream_next_token(s);
693 if (tok->type == type) {
697 isl_stream_error(s, tok, "expecting other token");
698 isl_stream_push_token(s, tok);
702 int isl_stream_is_empty(struct isl_stream *s)
704 struct isl_token *tok;
706 tok = isl_stream_next_token(s);
711 isl_stream_push_token(s, tok);
715 static int free_keyword(void **p, void *user)
717 struct isl_keyword *keyword = *p;
725 void isl_stream_flush_tokens(struct isl_stream *s)
731 for (i = 0; i < s->n_token; ++i)
732 isl_token_free(s->tokens[i]);
736 void isl_stream_free(struct isl_stream *s)
741 if (s->n_token != 0) {
742 struct isl_token *tok = isl_stream_next_token(s);
743 isl_stream_error(s, tok, "unexpected token");
747 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
748 isl_hash_table_free(s->ctx, s->keywords);
750 isl_ctx_deref(s->ctx);