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 stream_getc(struct isl_stream *s)
187 static int isl_stream_getc(struct isl_stream *s)
201 static void isl_stream_ungetc(struct isl_stream *s, int c)
210 static int isl_stream_push_char(struct isl_stream *s, int c)
212 if (s->len >= s->size) {
213 s->size = (3*s->size)/2;
214 s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
218 s->buffer[s->len++] = c;
222 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
224 isl_assert(s->ctx, s->n_token < 5, return);
225 s->tokens[s->n_token++] = tok;
228 static enum isl_token_type check_keywords(struct isl_stream *s)
230 struct isl_hash_table_entry *entry;
231 struct isl_keyword *keyword;
234 if (!strcasecmp(s->buffer, "exists"))
235 return ISL_TOKEN_EXISTS;
236 if (!strcasecmp(s->buffer, "and"))
237 return ISL_TOKEN_AND;
238 if (!strcasecmp(s->buffer, "or"))
240 if (!strcasecmp(s->buffer, "not"))
241 return ISL_TOKEN_NOT;
242 if (!strcasecmp(s->buffer, "infty"))
243 return ISL_TOKEN_INFTY;
244 if (!strcasecmp(s->buffer, "infinity"))
245 return ISL_TOKEN_INFTY;
246 if (!strcasecmp(s->buffer, "NaN"))
247 return ISL_TOKEN_NAN;
248 if (!strcasecmp(s->buffer, "max"))
249 return ISL_TOKEN_MAX;
250 if (!strcasecmp(s->buffer, "rat"))
251 return ISL_TOKEN_RAT;
252 if (!strcasecmp(s->buffer, "true"))
253 return ISL_TOKEN_TRUE;
254 if (!strcasecmp(s->buffer, "false"))
255 return ISL_TOKEN_FALSE;
258 return ISL_TOKEN_IDENT;
260 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
261 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
264 keyword = entry->data;
265 return keyword->type;
268 return ISL_TOKEN_IDENT;
271 int isl_stream_skip_line(struct isl_stream *s)
275 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
279 return c == -1 ? -1 : 0;
282 static struct isl_token *next_token(struct isl_stream *s, int same_line)
285 struct isl_token *tok = NULL;
287 int old_line = s->line;
290 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
292 return s->tokens[--s->n_token];
295 if (same_line && s->c == '\n')
300 /* skip spaces and comment lines */
301 while ((c = isl_stream_getc(s)) != -1) {
303 if (isl_stream_skip_line(s) < 0)
308 } else if (!isspace(c) || (same_line && c == '\n'))
315 if (c == -1 || (same_line && c == '\n'))
333 tok = isl_token_new(s->ctx, line, col, old_line != line);
336 tok->type = (enum isl_token_type)c;
341 if ((c = isl_stream_getc(s)) == '>') {
342 tok = isl_token_new(s->ctx, line, col, old_line != line);
345 tok->u.s = strdup("->");
346 tok->type = ISL_TOKEN_TO;
350 isl_stream_ungetc(s, c);
352 tok = isl_token_new(s->ctx, line, col, old_line != line);
355 tok->type = (enum isl_token_type) '-';
359 if (c == '-' || isdigit(c)) {
360 tok = isl_token_new(s->ctx, line, col, old_line != line);
363 tok->type = ISL_TOKEN_VALUE;
364 isl_int_init(tok->u.v);
365 if (isl_stream_push_char(s, c))
367 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
368 if (isl_stream_push_char(s, c))
371 isl_stream_ungetc(s, c);
372 isl_stream_push_char(s, '\0');
373 isl_int_read(tok->u.v, s->buffer);
376 if (isalpha(c) || c == '_') {
377 tok = isl_token_new(s->ctx, line, col, old_line != line);
380 isl_stream_push_char(s, c);
381 while ((c = isl_stream_getc(s)) != -1 &&
382 (isalnum(c) || c == '_'))
383 isl_stream_push_char(s, c);
385 isl_stream_ungetc(s, c);
386 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
387 isl_stream_push_char(s, c);
389 isl_stream_ungetc(s, c);
390 isl_stream_push_char(s, '\0');
391 tok->type = check_keywords(s);
392 if (tok->type != ISL_TOKEN_IDENT)
394 tok->u.s = strdup(s->buffer);
400 tok = isl_token_new(s->ctx, line, col, old_line != line);
403 tok->type = ISL_TOKEN_STRING;
405 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
406 isl_stream_push_char(s, c);
408 isl_stream_error(s, NULL, "unterminated string");
411 isl_stream_push_char(s, '\0');
412 tok->u.s = strdup(s->buffer);
417 tok = isl_token_new(s->ctx, line, col, old_line != line);
420 if ((c = isl_stream_getc(s)) == '=') {
421 tok->u.s = strdup(":=");
422 tok->type = ISL_TOKEN_DEF;
426 isl_stream_ungetc(s, c);
427 tok->type = (enum isl_token_type) ':';
432 tok = isl_token_new(s->ctx, line, col, old_line != line);
435 if ((c = isl_stream_getc(s)) == '=') {
436 tok->u.s = strdup(">=");
437 tok->type = ISL_TOKEN_GE;
439 } else if (c == '>') {
440 if ((c = isl_stream_getc(s)) == '=') {
441 tok->u.s = strdup(">>=");
442 tok->type = ISL_TOKEN_LEX_GE;
445 tok->u.s = strdup(">>");
446 tok->type = ISL_TOKEN_LEX_GT;
448 tok->u.s = strdup(">");
449 tok->type = ISL_TOKEN_GT;
452 isl_stream_ungetc(s, c);
457 tok = isl_token_new(s->ctx, line, col, old_line != line);
460 if ((c = isl_stream_getc(s)) == '=') {
461 tok->u.s = strdup("<=");
462 tok->type = ISL_TOKEN_LE;
464 } else if (c == '<') {
465 if ((c = isl_stream_getc(s)) == '=') {
466 tok->u.s = strdup("<<=");
467 tok->type = ISL_TOKEN_LEX_LE;
470 tok->u.s = strdup("<<");
471 tok->type = ISL_TOKEN_LEX_LT;
473 tok->u.s = strdup("<");
474 tok->type = ISL_TOKEN_LT;
477 isl_stream_ungetc(s, c);
481 tok = isl_token_new(s->ctx, line, col, old_line != line);
484 tok->type = ISL_TOKEN_AND;
485 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
486 tok->u.s = strdup("&");
487 isl_stream_ungetc(s, c);
489 tok->u.s = strdup("&&");
493 tok = isl_token_new(s->ctx, line, col, old_line != line);
496 tok->type = ISL_TOKEN_OR;
497 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
498 tok->u.s = strdup("|");
499 isl_stream_ungetc(s, c);
501 tok->u.s = strdup("||");
505 tok = isl_token_new(s->ctx, line, col, old_line != line);
508 tok->type = ISL_TOKEN_NOT;
509 tok->u.s = strdup("!");
513 tok = isl_token_new(s->ctx, line, col, old_line != line);
516 tok->type = ISL_TOKEN_UNKNOWN;
523 struct isl_token *isl_stream_next_token(struct isl_stream *s)
525 return next_token(s, 0);
528 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
530 return next_token(s, 1);
533 int isl_stream_eat_if_available(struct isl_stream *s, int type)
535 struct isl_token *tok;
537 tok = isl_stream_next_token(s);
540 if (tok->type == type) {
544 isl_stream_push_token(s, tok);
548 int isl_stream_next_token_is(struct isl_stream *s, int type)
550 struct isl_token *tok;
553 tok = isl_stream_next_token(s);
556 r = tok->type == type;
557 isl_stream_push_token(s, tok);
561 char *isl_stream_read_ident_if_available(struct isl_stream *s)
563 struct isl_token *tok;
565 tok = isl_stream_next_token(s);
568 if (tok->type == ISL_TOKEN_IDENT) {
569 char *ident = strdup(tok->u.s);
573 isl_stream_push_token(s, tok);
577 int isl_stream_eat(struct isl_stream *s, int type)
579 struct isl_token *tok;
581 tok = isl_stream_next_token(s);
584 if (tok->type == type) {
588 isl_stream_error(s, tok, "expecting other token");
589 isl_stream_push_token(s, tok);
593 int isl_stream_is_empty(struct isl_stream *s)
595 struct isl_token *tok;
597 tok = isl_stream_next_token(s);
602 isl_stream_push_token(s, tok);
606 static int free_keyword(void **p, void *user)
608 struct isl_keyword *keyword = *p;
616 void isl_stream_flush_tokens(struct isl_stream *s)
622 for (i = 0; i < s->n_token; ++i)
623 isl_token_free(s->tokens[i]);
627 void isl_stream_free(struct isl_stream *s)
632 if (s->n_token != 0) {
633 struct isl_token *tok = isl_stream_next_token(s);
634 isl_stream_error(s, tok, "unexpected token");
638 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
639 isl_hash_table_free(s->ctx, s->keywords);
641 isl_ctx_deref(s->ctx);