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
14 #include <isl/stream.h>
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,
31 struct isl_hash_table_entry *entry;
32 struct isl_keyword *keyword;
36 s->keywords = isl_hash_table_alloc(s->ctx, 10);
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,
47 return ISL_TOKEN_ERROR;
49 keyword = entry->data;
53 keyword = isl_calloc_type(s->ctx, struct isl_keyword);
55 return ISL_TOKEN_ERROR;
56 keyword->type = s->next_type++;
57 keyword->name = strdup(name);
60 return ISL_TOKEN_ERROR;
62 entry->data = keyword;
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);
75 tok->on_new_line = on_new_line;
81 void isl_token_free(struct isl_token *tok)
85 if (tok->type == ISL_TOKEN_VALUE)
86 isl_int_clear(tok->u.v);
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);
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");
109 fprintf(stderr, "got token '%s'\n", tok->u.s);
111 fprintf(stderr, "got token type %d\n", tok->type);
115 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
118 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
131 for (i = 0; i < 5; ++i)
136 s->buffer = isl_alloc_array(ctx, char, s->size);
145 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
147 struct isl_stream *s = isl_stream_new(ctx);
154 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
156 struct isl_stream *s = isl_stream_new(ctx);
163 static int stream_getc(struct isl_stream *s)
169 return s->c = s->un[--s->n_un];
190 static void isl_stream_ungetc(struct isl_stream *s, int c)
192 isl_assert(s->ctx, s->n_un < 5, return);
193 s->un[s->n_un++] = c;
197 static int isl_stream_getc(struct isl_stream *s)
208 isl_stream_ungetc(s, c);
213 static int isl_stream_push_char(struct isl_stream *s, int c)
215 if (s->len >= s->size) {
217 s->size = (3*s->size)/2;
218 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
223 s->buffer[s->len++] = c;
227 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
229 isl_assert(s->ctx, s->n_token < 5, return);
230 s->tokens[s->n_token++] = tok;
233 static enum isl_token_type check_keywords(struct isl_stream *s)
235 struct isl_hash_table_entry *entry;
236 struct isl_keyword *keyword;
239 if (!strcasecmp(s->buffer, "exists"))
240 return ISL_TOKEN_EXISTS;
241 if (!strcasecmp(s->buffer, "and"))
242 return ISL_TOKEN_AND;
243 if (!strcasecmp(s->buffer, "or"))
245 if (!strcasecmp(s->buffer, "not"))
246 return ISL_TOKEN_NOT;
247 if (!strcasecmp(s->buffer, "infty"))
248 return ISL_TOKEN_INFTY;
249 if (!strcasecmp(s->buffer, "infinity"))
250 return ISL_TOKEN_INFTY;
251 if (!strcasecmp(s->buffer, "NaN"))
252 return ISL_TOKEN_NAN;
253 if (!strcasecmp(s->buffer, "min"))
254 return ISL_TOKEN_MIN;
255 if (!strcasecmp(s->buffer, "max"))
256 return ISL_TOKEN_MAX;
257 if (!strcasecmp(s->buffer, "rat"))
258 return ISL_TOKEN_RAT;
259 if (!strcasecmp(s->buffer, "true"))
260 return ISL_TOKEN_TRUE;
261 if (!strcasecmp(s->buffer, "false"))
262 return ISL_TOKEN_FALSE;
265 return ISL_TOKEN_IDENT;
267 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
268 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
271 keyword = entry->data;
272 return keyword->type;
275 return ISL_TOKEN_IDENT;
278 int isl_stream_skip_line(struct isl_stream *s)
282 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
286 return c == -1 ? -1 : 0;
289 static struct isl_token *next_token(struct isl_stream *s, int same_line)
292 struct isl_token *tok = NULL;
294 int old_line = s->line;
297 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
299 return s->tokens[--s->n_token];
302 if (same_line && s->c == '\n')
307 /* skip spaces and comment lines */
308 while ((c = isl_stream_getc(s)) != -1) {
310 if (isl_stream_skip_line(s) < 0)
315 } else if (!isspace(c) || (same_line && c == '\n'))
322 if (c == -1 || (same_line && c == '\n'))
340 tok = isl_token_new(s->ctx, line, col, old_line != line);
343 tok->type = (enum isl_token_type)c;
348 if ((c = isl_stream_getc(s)) == '>') {
349 tok = isl_token_new(s->ctx, line, col, old_line != line);
352 tok->u.s = strdup("->");
353 tok->type = ISL_TOKEN_TO;
357 isl_stream_ungetc(s, c);
359 tok = isl_token_new(s->ctx, line, col, old_line != line);
362 tok->type = (enum isl_token_type) '-';
366 if (c == '-' || isdigit(c)) {
367 tok = isl_token_new(s->ctx, line, col, old_line != line);
370 tok->type = ISL_TOKEN_VALUE;
371 isl_int_init(tok->u.v);
372 if (isl_stream_push_char(s, c))
374 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
375 if (isl_stream_push_char(s, c))
378 isl_stream_ungetc(s, c);
379 isl_stream_push_char(s, '\0');
380 isl_int_read(tok->u.v, s->buffer);
383 if (isalpha(c) || c == '_') {
384 tok = isl_token_new(s->ctx, line, col, old_line != line);
387 isl_stream_push_char(s, c);
388 while ((c = isl_stream_getc(s)) != -1 &&
389 (isalnum(c) || c == '_'))
390 isl_stream_push_char(s, c);
392 isl_stream_ungetc(s, c);
393 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
394 isl_stream_push_char(s, c);
396 isl_stream_ungetc(s, c);
397 isl_stream_push_char(s, '\0');
398 tok->type = check_keywords(s);
399 if (tok->type != ISL_TOKEN_IDENT)
401 tok->u.s = strdup(s->buffer);
407 tok = isl_token_new(s->ctx, line, col, old_line != line);
410 tok->type = ISL_TOKEN_STRING;
412 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
413 isl_stream_push_char(s, c);
415 isl_stream_error(s, NULL, "unterminated string");
418 isl_stream_push_char(s, '\0');
419 tok->u.s = strdup(s->buffer);
424 tok = isl_token_new(s->ctx, line, col, old_line != line);
427 if ((c = isl_stream_getc(s)) == '=') {
428 tok->u.s = strdup(":=");
429 tok->type = ISL_TOKEN_DEF;
433 isl_stream_ungetc(s, c);
434 tok->type = (enum isl_token_type) ':';
439 tok = isl_token_new(s->ctx, line, col, old_line != line);
442 if ((c = isl_stream_getc(s)) == '=') {
443 tok->u.s = strdup(">=");
444 tok->type = ISL_TOKEN_GE;
446 } else if (c == '>') {
447 if ((c = isl_stream_getc(s)) == '=') {
448 tok->u.s = strdup(">>=");
449 tok->type = ISL_TOKEN_LEX_GE;
452 tok->u.s = strdup(">>");
453 tok->type = ISL_TOKEN_LEX_GT;
455 tok->u.s = strdup(">");
456 tok->type = ISL_TOKEN_GT;
459 isl_stream_ungetc(s, c);
464 tok = isl_token_new(s->ctx, line, col, old_line != line);
467 if ((c = isl_stream_getc(s)) == '=') {
468 tok->u.s = strdup("<=");
469 tok->type = ISL_TOKEN_LE;
471 } else if (c == '<') {
472 if ((c = isl_stream_getc(s)) == '=') {
473 tok->u.s = strdup("<<=");
474 tok->type = ISL_TOKEN_LEX_LE;
477 tok->u.s = strdup("<<");
478 tok->type = ISL_TOKEN_LEX_LT;
480 tok->u.s = strdup("<");
481 tok->type = ISL_TOKEN_LT;
484 isl_stream_ungetc(s, c);
488 tok = isl_token_new(s->ctx, line, col, old_line != line);
491 tok->type = ISL_TOKEN_AND;
492 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
493 tok->u.s = strdup("&");
494 isl_stream_ungetc(s, c);
496 tok->u.s = strdup("&&");
500 tok = isl_token_new(s->ctx, line, col, old_line != line);
503 tok->type = ISL_TOKEN_OR;
504 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
505 tok->u.s = strdup("|");
506 isl_stream_ungetc(s, c);
508 tok->u.s = strdup("||");
512 tok = isl_token_new(s->ctx, line, col, old_line != line);
515 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
516 tok->type = (enum isl_token_type) '/';
517 isl_stream_ungetc(s, c);
519 tok->u.s = strdup("/\\");
520 tok->type = ISL_TOKEN_AND;
525 tok = isl_token_new(s->ctx, line, col, old_line != line);
528 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
529 tok->type = (enum isl_token_type) '\\';
530 isl_stream_ungetc(s, c);
532 tok->u.s = strdup("\\/");
533 tok->type = ISL_TOKEN_OR;
538 tok = isl_token_new(s->ctx, line, col, old_line != line);
541 tok->type = ISL_TOKEN_NOT;
542 tok->u.s = strdup("!");
546 tok = isl_token_new(s->ctx, line, col, old_line != line);
549 tok->type = ISL_TOKEN_UNKNOWN;
556 struct isl_token *isl_stream_next_token(struct isl_stream *s)
558 return next_token(s, 0);
561 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
563 return next_token(s, 1);
566 int isl_stream_eat_if_available(struct isl_stream *s, int type)
568 struct isl_token *tok;
570 tok = isl_stream_next_token(s);
573 if (tok->type == type) {
577 isl_stream_push_token(s, tok);
581 int isl_stream_next_token_is(struct isl_stream *s, int type)
583 struct isl_token *tok;
586 tok = isl_stream_next_token(s);
589 r = tok->type == type;
590 isl_stream_push_token(s, tok);
594 char *isl_stream_read_ident_if_available(struct isl_stream *s)
596 struct isl_token *tok;
598 tok = isl_stream_next_token(s);
601 if (tok->type == ISL_TOKEN_IDENT) {
602 char *ident = strdup(tok->u.s);
606 isl_stream_push_token(s, tok);
610 int isl_stream_eat(struct isl_stream *s, int type)
612 struct isl_token *tok;
614 tok = isl_stream_next_token(s);
617 if (tok->type == type) {
621 isl_stream_error(s, tok, "expecting other token");
622 isl_stream_push_token(s, tok);
626 int isl_stream_is_empty(struct isl_stream *s)
628 struct isl_token *tok;
630 tok = isl_stream_next_token(s);
635 isl_stream_push_token(s, tok);
639 static int free_keyword(void **p, void *user)
641 struct isl_keyword *keyword = *p;
649 void isl_stream_flush_tokens(struct isl_stream *s)
655 for (i = 0; i < s->n_token; ++i)
656 isl_token_free(s->tokens[i]);
660 void isl_stream_free(struct isl_stream *s)
665 if (s->n_token != 0) {
666 struct isl_token *tok = isl_stream_next_token(s);
667 isl_stream_error(s, tok, "unexpected token");
671 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
672 isl_hash_table_free(s->ctx, s->keywords);
674 isl_ctx_deref(s->ctx);