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>
20 enum isl_token_type type;
23 static int same_name(const void *entry, const void *val)
25 const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
27 return !strcmp(keyword->name, val);
30 enum isl_token_type isl_stream_register_keyword(struct isl_stream *s,
33 struct isl_hash_table_entry *entry;
34 struct isl_keyword *keyword;
38 s->keywords = isl_hash_table_alloc(s->ctx, 10);
40 return ISL_TOKEN_ERROR;
41 s->next_type = ISL_TOKEN_LAST;
44 name_hash = isl_hash_string(isl_hash_init(), name);
46 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
49 return ISL_TOKEN_ERROR;
51 keyword = entry->data;
55 keyword = isl_calloc_type(s->ctx, struct isl_keyword);
57 return ISL_TOKEN_ERROR;
58 keyword->type = s->next_type++;
59 keyword->name = strdup(name);
62 return ISL_TOKEN_ERROR;
64 entry->data = keyword;
69 struct isl_token *isl_token_new(isl_ctx *ctx,
70 int line, int col, unsigned on_new_line)
72 struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
77 tok->on_new_line = on_new_line;
83 void isl_token_free(struct isl_token *tok)
87 if (tok->type == ISL_TOKEN_VALUE)
88 isl_int_clear(tok->u.v);
89 else if (tok->type == ISL_TOKEN_MAP)
90 isl_map_free(tok->u.map);
91 else if (tok->type == ISL_TOKEN_AFF)
92 isl_pw_aff_free(tok->u.pwaff);
98 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
100 int line = tok ? tok->line : s->line;
101 int col = tok ? tok->col : s->col;
102 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
105 fprintf(stderr, "got '%c'\n", tok->type);
106 else if (tok->type == ISL_TOKEN_IDENT)
107 fprintf(stderr, "got ident '%s'\n", tok->u.s);
108 else if (tok->is_keyword)
109 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
110 else if (tok->type == ISL_TOKEN_VALUE) {
111 fprintf(stderr, "got value '");
112 isl_int_print(stderr, tok->u.v, 0);
113 fprintf(stderr, "'\n");
114 } else if (tok->type == ISL_TOKEN_MAP) {
116 fprintf(stderr, "got map '");
117 p = isl_printer_to_file(s->ctx, stderr);
118 p = isl_printer_print_map(p, tok->u.map);
120 fprintf(stderr, "'\n");
121 } else if (tok->type == ISL_TOKEN_AFF) {
123 fprintf(stderr, "got affine expression '");
124 p = isl_printer_to_file(s->ctx, stderr);
125 p = isl_printer_print_pw_aff(p, tok->u.pwaff);
127 fprintf(stderr, "'\n");
129 fprintf(stderr, "got token '%s'\n", tok->u.s);
131 fprintf(stderr, "got token type %d\n", tok->type);
135 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
138 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
151 for (i = 0; i < 5; ++i)
156 s->buffer = isl_alloc_array(ctx, char, s->size);
165 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
167 struct isl_stream *s = isl_stream_new(ctx);
174 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
176 struct isl_stream *s;
179 s = isl_stream_new(ctx);
186 static int stream_getc(struct isl_stream *s)
192 return s->c = s->un[--s->n_un];
213 static void isl_stream_ungetc(struct isl_stream *s, int c)
215 isl_assert(s->ctx, s->n_un < 5, return);
216 s->un[s->n_un++] = c;
220 static int isl_stream_getc(struct isl_stream *s)
231 isl_stream_ungetc(s, c);
236 static int isl_stream_push_char(struct isl_stream *s, int c)
238 if (s->len >= s->size) {
240 s->size = (3*s->size)/2;
241 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
246 s->buffer[s->len++] = c;
250 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
252 isl_assert(s->ctx, s->n_token < 5, return);
253 s->tokens[s->n_token++] = tok;
256 static enum isl_token_type check_keywords(struct isl_stream *s)
258 struct isl_hash_table_entry *entry;
259 struct isl_keyword *keyword;
262 if (!strcasecmp(s->buffer, "exists"))
263 return ISL_TOKEN_EXISTS;
264 if (!strcasecmp(s->buffer, "and"))
265 return ISL_TOKEN_AND;
266 if (!strcasecmp(s->buffer, "or"))
268 if (!strcasecmp(s->buffer, "not"))
269 return ISL_TOKEN_NOT;
270 if (!strcasecmp(s->buffer, "infty"))
271 return ISL_TOKEN_INFTY;
272 if (!strcasecmp(s->buffer, "infinity"))
273 return ISL_TOKEN_INFTY;
274 if (!strcasecmp(s->buffer, "NaN"))
275 return ISL_TOKEN_NAN;
276 if (!strcasecmp(s->buffer, "min"))
277 return ISL_TOKEN_MIN;
278 if (!strcasecmp(s->buffer, "max"))
279 return ISL_TOKEN_MAX;
280 if (!strcasecmp(s->buffer, "rat"))
281 return ISL_TOKEN_RAT;
282 if (!strcasecmp(s->buffer, "true"))
283 return ISL_TOKEN_TRUE;
284 if (!strcasecmp(s->buffer, "false"))
285 return ISL_TOKEN_FALSE;
286 if (!strcasecmp(s->buffer, "ceild"))
287 return ISL_TOKEN_CEILD;
288 if (!strcasecmp(s->buffer, "floord"))
289 return ISL_TOKEN_FLOORD;
290 if (!strcasecmp(s->buffer, "mod"))
291 return ISL_TOKEN_MOD;
294 return ISL_TOKEN_IDENT;
296 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
297 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
300 keyword = entry->data;
301 return keyword->type;
304 return ISL_TOKEN_IDENT;
307 int isl_stream_skip_line(struct isl_stream *s)
311 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
315 return c == -1 ? -1 : 0;
318 static struct isl_token *next_token(struct isl_stream *s, int same_line)
321 struct isl_token *tok = NULL;
323 int old_line = s->line;
326 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
328 return s->tokens[--s->n_token];
331 if (same_line && s->c == '\n')
336 /* skip spaces and comment lines */
337 while ((c = isl_stream_getc(s)) != -1) {
339 if (isl_stream_skip_line(s) < 0)
344 } else if (!isspace(c) || (same_line && c == '\n'))
351 if (c == -1 || (same_line && c == '\n'))
369 tok = isl_token_new(s->ctx, line, col, old_line != line);
372 tok->type = (enum isl_token_type)c;
377 if ((c = isl_stream_getc(s)) == '>') {
378 tok = isl_token_new(s->ctx, line, col, old_line != line);
381 tok->u.s = strdup("->");
382 tok->type = ISL_TOKEN_TO;
386 isl_stream_ungetc(s, c);
388 tok = isl_token_new(s->ctx, line, col, old_line != line);
391 tok->type = (enum isl_token_type) '-';
395 if (c == '-' || isdigit(c)) {
396 int minus = c == '-';
397 tok = isl_token_new(s->ctx, line, col, old_line != line);
400 tok->type = ISL_TOKEN_VALUE;
401 isl_int_init(tok->u.v);
402 if (isl_stream_push_char(s, c))
404 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
405 if (isl_stream_push_char(s, c))
408 isl_stream_ungetc(s, c);
409 isl_stream_push_char(s, '\0');
410 isl_int_read(tok->u.v, s->buffer);
411 if (minus && isl_int_is_zero(tok->u.v)) {
413 tok->on_new_line = 0;
414 isl_stream_push_token(s, tok);
415 tok = isl_token_new(s->ctx, line, col, old_line != line);
418 tok->type = (enum isl_token_type) '-';
422 if (isalpha(c) || c == '_') {
423 tok = isl_token_new(s->ctx, line, col, old_line != line);
426 isl_stream_push_char(s, c);
427 while ((c = isl_stream_getc(s)) != -1 &&
428 (isalnum(c) || c == '_'))
429 isl_stream_push_char(s, c);
431 isl_stream_ungetc(s, c);
432 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
433 isl_stream_push_char(s, c);
435 isl_stream_ungetc(s, c);
436 isl_stream_push_char(s, '\0');
437 tok->type = check_keywords(s);
438 if (tok->type != ISL_TOKEN_IDENT)
440 tok->u.s = strdup(s->buffer);
446 tok = isl_token_new(s->ctx, line, col, old_line != line);
449 tok->type = ISL_TOKEN_STRING;
451 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
452 isl_stream_push_char(s, c);
454 isl_stream_error(s, NULL, "unterminated string");
457 isl_stream_push_char(s, '\0');
458 tok->u.s = strdup(s->buffer);
463 tok = isl_token_new(s->ctx, line, col, old_line != line);
466 if ((c = isl_stream_getc(s)) == '=') {
467 tok->u.s = strdup("==");
468 tok->type = ISL_TOKEN_EQ_EQ;
472 isl_stream_ungetc(s, c);
473 tok->type = (enum isl_token_type) '=';
478 tok = isl_token_new(s->ctx, line, col, old_line != line);
481 if ((c = isl_stream_getc(s)) == '=') {
482 tok->u.s = strdup(":=");
483 tok->type = ISL_TOKEN_DEF;
487 isl_stream_ungetc(s, c);
488 tok->type = (enum isl_token_type) ':';
493 tok = isl_token_new(s->ctx, line, col, old_line != line);
496 if ((c = isl_stream_getc(s)) == '=') {
497 tok->u.s = strdup(">=");
498 tok->type = ISL_TOKEN_GE;
500 } else if (c == '>') {
501 if ((c = isl_stream_getc(s)) == '=') {
502 tok->u.s = strdup(">>=");
503 tok->type = ISL_TOKEN_LEX_GE;
506 tok->u.s = strdup(">>");
507 tok->type = ISL_TOKEN_LEX_GT;
509 tok->u.s = strdup(">");
510 tok->type = ISL_TOKEN_GT;
513 isl_stream_ungetc(s, c);
518 tok = isl_token_new(s->ctx, line, col, old_line != line);
521 if ((c = isl_stream_getc(s)) == '=') {
522 tok->u.s = strdup("<=");
523 tok->type = ISL_TOKEN_LE;
525 } else if (c == '<') {
526 if ((c = isl_stream_getc(s)) == '=') {
527 tok->u.s = strdup("<<=");
528 tok->type = ISL_TOKEN_LEX_LE;
531 tok->u.s = strdup("<<");
532 tok->type = ISL_TOKEN_LEX_LT;
534 tok->u.s = strdup("<");
535 tok->type = ISL_TOKEN_LT;
538 isl_stream_ungetc(s, c);
542 tok = isl_token_new(s->ctx, line, col, old_line != line);
545 tok->type = ISL_TOKEN_AND;
546 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
547 tok->u.s = strdup("&");
548 isl_stream_ungetc(s, c);
550 tok->u.s = strdup("&&");
554 tok = isl_token_new(s->ctx, line, col, old_line != line);
557 tok->type = ISL_TOKEN_OR;
558 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
559 tok->u.s = strdup("|");
560 isl_stream_ungetc(s, c);
562 tok->u.s = strdup("||");
566 tok = isl_token_new(s->ctx, line, col, old_line != line);
569 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
570 tok->type = (enum isl_token_type) '/';
571 isl_stream_ungetc(s, c);
573 tok->u.s = strdup("/\\");
574 tok->type = ISL_TOKEN_AND;
579 tok = isl_token_new(s->ctx, line, col, old_line != line);
582 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
583 tok->type = (enum isl_token_type) '\\';
584 isl_stream_ungetc(s, c);
586 tok->u.s = strdup("\\/");
587 tok->type = ISL_TOKEN_OR;
592 tok = isl_token_new(s->ctx, line, col, old_line != line);
595 if ((c = isl_stream_getc(s)) == '=') {
596 tok->u.s = strdup("!=");
597 tok->type = ISL_TOKEN_NE;
600 tok->type = ISL_TOKEN_NOT;
601 tok->u.s = strdup("!");
604 isl_stream_ungetc(s, c);
608 tok = isl_token_new(s->ctx, line, col, old_line != line);
611 tok->type = ISL_TOKEN_UNKNOWN;
618 struct isl_token *isl_stream_next_token(struct isl_stream *s)
620 return next_token(s, 0);
623 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
625 return next_token(s, 1);
628 int isl_stream_eat_if_available(struct isl_stream *s, int type)
630 struct isl_token *tok;
632 tok = isl_stream_next_token(s);
635 if (tok->type == type) {
639 isl_stream_push_token(s, tok);
643 int isl_stream_next_token_is(struct isl_stream *s, int type)
645 struct isl_token *tok;
648 tok = isl_stream_next_token(s);
651 r = tok->type == type;
652 isl_stream_push_token(s, tok);
656 char *isl_stream_read_ident_if_available(struct isl_stream *s)
658 struct isl_token *tok;
660 tok = isl_stream_next_token(s);
663 if (tok->type == ISL_TOKEN_IDENT) {
664 char *ident = strdup(tok->u.s);
668 isl_stream_push_token(s, tok);
672 int isl_stream_eat(struct isl_stream *s, int type)
674 struct isl_token *tok;
676 tok = isl_stream_next_token(s);
679 if (tok->type == type) {
683 isl_stream_error(s, tok, "expecting other token");
684 isl_stream_push_token(s, tok);
688 int isl_stream_is_empty(struct isl_stream *s)
690 struct isl_token *tok;
692 tok = isl_stream_next_token(s);
697 isl_stream_push_token(s, tok);
701 static int free_keyword(void **p, void *user)
703 struct isl_keyword *keyword = *p;
711 void isl_stream_flush_tokens(struct isl_stream *s)
717 for (i = 0; i < s->n_token; ++i)
718 isl_token_free(s->tokens[i]);
722 void isl_stream_free(struct isl_stream *s)
727 if (s->n_token != 0) {
728 struct isl_token *tok = isl_stream_next_token(s);
729 isl_stream_error(s, tok, "unexpected token");
733 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
734 isl_hash_table_free(s->ctx, s->keywords);
736 isl_ctx_deref(s->ctx);