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);
130 for (i = 0; i < 5; ++i)
135 s->buffer = isl_alloc_array(ctx, char, s->size);
144 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
146 struct isl_stream *s = isl_stream_new(ctx);
153 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
155 struct isl_stream *s = isl_stream_new(ctx);
162 static int isl_stream_getc(struct isl_stream *s)
187 static void isl_stream_ungetc(struct isl_stream *s, int c)
196 static int isl_stream_push_char(struct isl_stream *s, int c)
198 if (s->len >= s->size) {
199 s->size = (3*s->size)/2;
200 s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
204 s->buffer[s->len++] = c;
208 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
210 isl_assert(s->ctx, s->n_token < 5, return);
211 s->tokens[s->n_token++] = tok;
214 static enum isl_token_type check_keywords(struct isl_stream *s)
216 struct isl_hash_table_entry *entry;
217 struct isl_keyword *keyword;
220 if (!strcasecmp(s->buffer, "exists"))
221 return ISL_TOKEN_EXISTS;
222 if (!strcasecmp(s->buffer, "and"))
223 return ISL_TOKEN_AND;
224 if (!strcasecmp(s->buffer, "or"))
226 if (!strcasecmp(s->buffer, "not"))
227 return ISL_TOKEN_NOT;
228 if (!strcasecmp(s->buffer, "infty"))
229 return ISL_TOKEN_INFTY;
230 if (!strcasecmp(s->buffer, "infinity"))
231 return ISL_TOKEN_INFTY;
232 if (!strcasecmp(s->buffer, "NaN"))
233 return ISL_TOKEN_NAN;
234 if (!strcasecmp(s->buffer, "max"))
235 return ISL_TOKEN_MAX;
236 if (!strcasecmp(s->buffer, "rat"))
237 return ISL_TOKEN_RAT;
238 if (!strcasecmp(s->buffer, "true"))
239 return ISL_TOKEN_TRUE;
240 if (!strcasecmp(s->buffer, "false"))
241 return ISL_TOKEN_FALSE;
244 return ISL_TOKEN_IDENT;
246 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
247 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
250 keyword = entry->data;
251 return keyword->type;
254 return ISL_TOKEN_IDENT;
257 int isl_stream_skip_line(struct isl_stream *s)
261 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
265 return c == -1 ? -1 : 0;
268 static struct isl_token *next_token(struct isl_stream *s, int same_line)
271 struct isl_token *tok = NULL;
273 int old_line = s->line;
276 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
278 return s->tokens[--s->n_token];
281 if (same_line && s->c == '\n')
286 /* skip spaces and comment lines */
287 while ((c = isl_stream_getc(s)) != -1) {
289 if (isl_stream_skip_line(s) < 0)
294 } else if (!isspace(c) || (same_line && c == '\n'))
301 if (c == -1 || (same_line && c == '\n'))
319 tok = isl_token_new(s->ctx, line, col, old_line != line);
322 tok->type = (enum isl_token_type)c;
327 if ((c = isl_stream_getc(s)) == '>') {
328 tok = isl_token_new(s->ctx, line, col, old_line != line);
331 tok->u.s = strdup("->");
332 tok->type = ISL_TOKEN_TO;
336 isl_stream_ungetc(s, c);
338 tok = isl_token_new(s->ctx, line, col, old_line != line);
341 tok->type = (enum isl_token_type) '-';
345 if (c == '-' || isdigit(c)) {
346 tok = isl_token_new(s->ctx, line, col, old_line != line);
349 tok->type = ISL_TOKEN_VALUE;
350 isl_int_init(tok->u.v);
351 if (isl_stream_push_char(s, c))
353 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
354 if (isl_stream_push_char(s, c))
357 isl_stream_ungetc(s, c);
358 isl_stream_push_char(s, '\0');
359 isl_int_read(tok->u.v, s->buffer);
362 if (isalpha(c) || c == '_') {
363 tok = isl_token_new(s->ctx, line, col, old_line != line);
366 isl_stream_push_char(s, c);
367 while ((c = isl_stream_getc(s)) != -1 &&
368 (isalnum(c) || c == '_'))
369 isl_stream_push_char(s, c);
371 isl_stream_ungetc(s, c);
372 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
373 isl_stream_push_char(s, c);
375 isl_stream_ungetc(s, c);
376 isl_stream_push_char(s, '\0');
377 tok->type = check_keywords(s);
378 if (tok->type != ISL_TOKEN_IDENT)
380 tok->u.s = strdup(s->buffer);
386 tok = isl_token_new(s->ctx, line, col, old_line != line);
389 tok->type = ISL_TOKEN_STRING;
391 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
392 isl_stream_push_char(s, c);
394 isl_stream_error(s, NULL, "unterminated string");
397 isl_stream_push_char(s, '\0');
398 tok->u.s = strdup(s->buffer);
403 tok = isl_token_new(s->ctx, line, col, old_line != line);
406 if ((c = isl_stream_getc(s)) == '=') {
407 tok->u.s = strdup(":=");
408 tok->type = ISL_TOKEN_DEF;
412 isl_stream_ungetc(s, c);
413 tok->type = (enum isl_token_type) ':';
418 tok = isl_token_new(s->ctx, line, col, old_line != line);
421 if ((c = isl_stream_getc(s)) == '=') {
422 tok->u.s = strdup(">=");
423 tok->type = ISL_TOKEN_GE;
425 } else if (c == '>') {
426 if ((c = isl_stream_getc(s)) == '=') {
427 tok->u.s = strdup(">>=");
428 tok->type = ISL_TOKEN_LEX_GE;
431 tok->u.s = strdup(">>");
432 tok->type = ISL_TOKEN_LEX_GT;
434 tok->u.s = strdup(">");
435 tok->type = ISL_TOKEN_GT;
438 isl_stream_ungetc(s, c);
443 tok = isl_token_new(s->ctx, line, col, old_line != line);
446 if ((c = isl_stream_getc(s)) == '=') {
447 tok->u.s = strdup("<=");
448 tok->type = ISL_TOKEN_LE;
450 } else if (c == '<') {
451 if ((c = isl_stream_getc(s)) == '=') {
452 tok->u.s = strdup("<<=");
453 tok->type = ISL_TOKEN_LEX_LE;
456 tok->u.s = strdup("<<");
457 tok->type = ISL_TOKEN_LEX_LT;
459 tok->u.s = strdup("<");
460 tok->type = ISL_TOKEN_LT;
463 isl_stream_ungetc(s, c);
467 tok = isl_token_new(s->ctx, line, col, old_line != line);
470 tok->type = ISL_TOKEN_AND;
471 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
472 tok->u.s = strdup("&");
473 isl_stream_ungetc(s, c);
475 tok->u.s = strdup("&&");
479 tok = isl_token_new(s->ctx, line, col, old_line != line);
482 tok->type = ISL_TOKEN_OR;
483 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
484 tok->u.s = strdup("|");
485 isl_stream_ungetc(s, c);
487 tok->u.s = strdup("||");
491 tok = isl_token_new(s->ctx, line, col, old_line != line);
494 tok->type = ISL_TOKEN_NOT;
495 tok->u.s = strdup("!");
499 tok = isl_token_new(s->ctx, line, col, old_line != line);
502 tok->type = ISL_TOKEN_UNKNOWN;
509 struct isl_token *isl_stream_next_token(struct isl_stream *s)
511 return next_token(s, 0);
514 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
516 return next_token(s, 1);
519 int isl_stream_eat_if_available(struct isl_stream *s, int type)
521 struct isl_token *tok;
523 tok = isl_stream_next_token(s);
526 if (tok->type == type) {
530 isl_stream_push_token(s, tok);
534 int isl_stream_next_token_is(struct isl_stream *s, int type)
536 struct isl_token *tok;
539 tok = isl_stream_next_token(s);
542 r = tok->type == type;
543 isl_stream_push_token(s, tok);
547 char *isl_stream_read_ident_if_available(struct isl_stream *s)
549 struct isl_token *tok;
551 tok = isl_stream_next_token(s);
554 if (tok->type == ISL_TOKEN_IDENT) {
555 char *ident = strdup(tok->u.s);
559 isl_stream_push_token(s, tok);
563 int isl_stream_eat(struct isl_stream *s, int type)
565 struct isl_token *tok;
567 tok = isl_stream_next_token(s);
570 if (tok->type == type) {
574 isl_stream_error(s, tok, "expecting other token");
575 isl_stream_push_token(s, tok);
579 int isl_stream_is_empty(struct isl_stream *s)
581 struct isl_token *tok;
583 tok = isl_stream_next_token(s);
588 isl_stream_push_token(s, tok);
592 static int free_keyword(void **p, void *user)
594 struct isl_keyword *keyword = *p;
602 void isl_stream_flush_tokens(struct isl_stream *s)
608 for (i = 0; i < s->n_token; ++i)
609 isl_token_free(s->tokens[i]);
613 void isl_stream_free(struct isl_stream *s)
618 if (s->n_token != 0) {
619 struct isl_token *tok = isl_stream_next_token(s);
620 isl_stream_error(s, tok, "unexpected token");
624 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
625 isl_hash_table_free(s->ctx, s->keywords);
627 isl_ctx_deref(s->ctx);