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;
80 void isl_token_free(struct isl_token *tok)
84 if (tok->type == ISL_TOKEN_VALUE)
85 isl_int_clear(tok->u.v);
86 else if (tok->type == ISL_TOKEN_IDENT ||
87 tok->type == ISL_TOKEN_STRING ||
93 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
95 int line = tok ? tok->line : s->line;
96 int col = tok ? tok->col : s->col;
97 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
100 fprintf(stderr, "got '%c'\n", tok->type);
101 else if (tok->type == ISL_TOKEN_IDENT)
102 fprintf(stderr, "got ident '%s'\n", tok->u.s);
103 else if (tok->is_keyword)
104 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
106 fprintf(stderr, "got token type %d\n", tok->type);
110 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
113 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
125 for (i = 0; i < 5; ++i)
130 s->buffer = isl_alloc_array(ctx, char, s->size);
139 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
141 struct isl_stream *s = isl_stream_new(ctx);
148 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
150 struct isl_stream *s = isl_stream_new(ctx);
157 static int isl_stream_getc(struct isl_stream *s)
182 static void isl_stream_ungetc(struct isl_stream *s, int c)
191 static int isl_stream_push_char(struct isl_stream *s, int c)
193 if (s->len >= s->size) {
194 s->size = (3*s->size)/2;
195 s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
199 s->buffer[s->len++] = c;
203 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
205 isl_assert(s->ctx, s->n_token < 5, return);
206 s->tokens[s->n_token++] = tok;
209 static enum isl_token_type check_keywords(struct isl_stream *s)
211 struct isl_hash_table_entry *entry;
212 struct isl_keyword *keyword;
215 if (!strcasecmp(s->buffer, "exists"))
216 return ISL_TOKEN_EXISTS;
217 if (!strcasecmp(s->buffer, "and"))
218 return ISL_TOKEN_AND;
219 if (!strcasecmp(s->buffer, "or"))
221 if (!strcasecmp(s->buffer, "infty"))
222 return ISL_TOKEN_INFTY;
223 if (!strcasecmp(s->buffer, "infinity"))
224 return ISL_TOKEN_INFTY;
225 if (!strcasecmp(s->buffer, "NaN"))
226 return ISL_TOKEN_NAN;
227 if (!strcasecmp(s->buffer, "max"))
228 return ISL_TOKEN_MAX;
231 return ISL_TOKEN_IDENT;
233 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
234 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
237 keyword = entry->data;
238 return keyword->type;
241 return ISL_TOKEN_IDENT;
244 int isl_stream_skip_line(struct isl_stream *s)
248 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
252 return c == -1 ? -1 : 0;
255 static struct isl_token *next_token(struct isl_stream *s, int same_line)
258 struct isl_token *tok = NULL;
260 int old_line = s->line;
263 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
265 return s->tokens[--s->n_token];
268 if (same_line && s->c == '\n')
273 /* skip spaces and comment lines */
274 while ((c = isl_stream_getc(s)) != -1) {
276 if (isl_stream_skip_line(s) < 0)
281 } else if (!isspace(c) || (same_line && c == '\n'))
288 if (c == -1 || (same_line && c == '\n'))
306 tok = isl_token_new(s->ctx, line, col, old_line != line);
309 tok->type = (enum isl_token_type)c;
314 if ((c = isl_stream_getc(s)) == '>') {
315 tok = isl_token_new(s->ctx, line, col, old_line != line);
318 tok->type = ISL_TOKEN_TO;
322 isl_stream_ungetc(s, c);
324 tok = isl_token_new(s->ctx, line, col, old_line != line);
327 tok->type = (enum isl_token_type) '-';
331 if (c == '-' || isdigit(c)) {
332 tok = isl_token_new(s->ctx, line, col, old_line != line);
335 tok->type = ISL_TOKEN_VALUE;
336 isl_int_init(tok->u.v);
337 if (isl_stream_push_char(s, c))
339 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
340 if (isl_stream_push_char(s, c))
343 isl_stream_ungetc(s, c);
344 isl_stream_push_char(s, '\0');
345 isl_int_read(tok->u.v, s->buffer);
348 if (isalpha(c) || c == '_') {
349 tok = isl_token_new(s->ctx, line, col, old_line != line);
352 isl_stream_push_char(s, c);
353 while ((c = isl_stream_getc(s)) != -1 &&
354 (isalnum(c) || c == '_'))
355 isl_stream_push_char(s, c);
357 isl_stream_ungetc(s, c);
358 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
359 isl_stream_push_char(s, c);
361 isl_stream_ungetc(s, c);
362 isl_stream_push_char(s, '\0');
363 tok->type = check_keywords(s);
364 if (tok->type != ISL_TOKEN_IDENT)
366 tok->u.s = strdup(s->buffer);
372 tok = isl_token_new(s->ctx, line, col, old_line != line);
375 tok->type = ISL_TOKEN_STRING;
377 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
378 isl_stream_push_char(s, c);
380 isl_stream_error(s, NULL, "unterminated string");
383 isl_stream_push_char(s, '\0');
384 tok->u.s = strdup(s->buffer);
389 tok = isl_token_new(s->ctx, line, col, old_line != line);
392 if ((c = isl_stream_getc(s)) == '=') {
393 tok->type = ISL_TOKEN_DEF;
397 isl_stream_ungetc(s, c);
398 tok->type = (enum isl_token_type) ':';
403 tok = isl_token_new(s->ctx, line, col, old_line != line);
406 if ((c = isl_stream_getc(s)) == '=') {
407 tok->type = ISL_TOKEN_GE;
409 } else if (c == '>') {
410 if ((c = isl_stream_getc(s)) == '=') {
411 tok->type = ISL_TOKEN_LEX_GE;
414 tok->type = ISL_TOKEN_LEX_GT;
416 tok->type = ISL_TOKEN_GT;
418 isl_stream_ungetc(s, c);
423 tok = isl_token_new(s->ctx, line, col, old_line != line);
426 if ((c = isl_stream_getc(s)) == '=') {
427 tok->type = ISL_TOKEN_LE;
429 } else if (c == '<') {
430 if ((c = isl_stream_getc(s)) == '=') {
431 tok->type = ISL_TOKEN_LEX_LE;
434 tok->type = ISL_TOKEN_LEX_LT;
436 tok->type = ISL_TOKEN_LT;
438 isl_stream_ungetc(s, c);
442 tok = isl_token_new(s->ctx, line, col, old_line != line);
445 tok->type = ISL_TOKEN_AND;
446 if ((c = isl_stream_getc(s)) != '&' && c != -1)
447 isl_stream_ungetc(s, c);
451 tok = isl_token_new(s->ctx, line, col, old_line != line);
454 tok->type = ISL_TOKEN_OR;
455 if ((c = isl_stream_getc(s)) != '|' && c != -1)
456 isl_stream_ungetc(s, c);
460 tok = isl_token_new(s->ctx, line, col, old_line != line);
463 tok->type = ISL_TOKEN_UNKNOWN;
470 struct isl_token *isl_stream_next_token(struct isl_stream *s)
472 return next_token(s, 0);
475 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
477 return next_token(s, 1);
480 int isl_stream_eat_if_available(struct isl_stream *s, int type)
482 struct isl_token *tok;
484 tok = isl_stream_next_token(s);
487 if (tok->type == type) {
491 isl_stream_push_token(s, tok);
495 int isl_stream_next_token_is(struct isl_stream *s, int type)
497 struct isl_token *tok;
500 tok = isl_stream_next_token(s);
503 r = tok->type == type;
504 isl_stream_push_token(s, tok);
508 char *isl_stream_read_ident_if_available(struct isl_stream *s)
510 struct isl_token *tok;
512 tok = isl_stream_next_token(s);
515 if (tok->type == ISL_TOKEN_IDENT) {
516 char *ident = strdup(tok->u.s);
520 isl_stream_push_token(s, tok);
524 int isl_stream_eat(struct isl_stream *s, int type)
526 struct isl_token *tok;
528 tok = isl_stream_next_token(s);
531 if (tok->type == type) {
535 isl_stream_error(s, tok, "expecting other token");
536 isl_stream_push_token(s, tok);
540 int isl_stream_is_empty(struct isl_stream *s)
542 struct isl_token *tok;
544 tok = isl_stream_next_token(s);
549 isl_stream_push_token(s, tok);
553 static int free_keyword(void **p, void *user)
555 struct isl_keyword *keyword = *p;
563 void isl_stream_flush_tokens(struct isl_stream *s)
569 for (i = 0; i < s->n_token; ++i)
570 isl_token_free(s->tokens[i]);
574 void isl_stream_free(struct isl_stream *s)
579 if (s->n_token != 0) {
580 struct isl_token *tok = isl_stream_next_token(s);
581 isl_stream_error(s, tok, "unexpected token");
585 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
586 isl_hash_table_free(s->ctx, s->keywords);
588 isl_ctx_deref(s->ctx);