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;
79 void isl_token_free(struct isl_token *tok)
83 if (tok->type == ISL_TOKEN_VALUE)
84 isl_int_clear(tok->u.v);
85 else if (tok->type == ISL_TOKEN_IDENT || tok->type == ISL_TOKEN_STRING)
90 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
92 int line = tok ? tok->line : s->line;
93 int col = tok ? tok->col : s->col;
94 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
97 fprintf(stderr, "got '%c'\n", tok->type);
99 fprintf(stderr, "got token type %d\n", tok->type);
103 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
106 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
118 for (i = 0; i < 5; ++i)
123 s->buffer = isl_alloc_array(ctx, char, s->size);
132 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
134 struct isl_stream *s = isl_stream_new(ctx);
141 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
143 struct isl_stream *s = isl_stream_new(ctx);
150 static int isl_stream_getc(struct isl_stream *s)
175 static void isl_stream_ungetc(struct isl_stream *s, int c)
184 static int isl_stream_push_char(struct isl_stream *s, int c)
186 if (s->len >= s->size) {
187 s->size = (3*s->size)/2;
188 s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
192 s->buffer[s->len++] = c;
196 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
198 isl_assert(s->ctx, s->n_token < 5, return);
199 s->tokens[s->n_token++] = tok;
202 static enum isl_token_type check_keywords(struct isl_stream *s)
204 struct isl_hash_table_entry *entry;
205 struct isl_keyword *keyword;
208 if (!strcasecmp(s->buffer, "exists"))
209 return ISL_TOKEN_EXISTS;
210 if (!strcasecmp(s->buffer, "and"))
211 return ISL_TOKEN_AND;
212 if (!strcasecmp(s->buffer, "or"))
214 if (!strcasecmp(s->buffer, "infty"))
215 return ISL_TOKEN_INFTY;
216 if (!strcasecmp(s->buffer, "infinity"))
217 return ISL_TOKEN_INFTY;
218 if (!strcasecmp(s->buffer, "NaN"))
219 return ISL_TOKEN_NAN;
222 return ISL_TOKEN_IDENT;
224 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
225 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
228 keyword = entry->data;
229 return keyword->type;
232 return ISL_TOKEN_IDENT;
235 int isl_stream_skip_line(struct isl_stream *s)
239 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
243 return c == -1 ? -1 : 0;
246 static struct isl_token *next_token(struct isl_stream *s, int same_line)
249 struct isl_token *tok = NULL;
251 int old_line = s->line;
254 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
256 return s->tokens[--s->n_token];
259 if (same_line && s->c == '\n')
264 /* skip spaces and comment lines */
265 while ((c = isl_stream_getc(s)) != -1) {
267 if (isl_stream_skip_line(s) < 0)
272 } else if (!isspace(c) || (same_line && c == '\n'))
279 if (c == -1 || (same_line && c == '\n'))
297 tok = isl_token_new(s->ctx, line, col, old_line != line);
300 tok->type = (enum isl_token_type)c;
305 if ((c = isl_stream_getc(s)) == '>') {
306 tok = isl_token_new(s->ctx, line, col, old_line != line);
309 tok->type = ISL_TOKEN_TO;
313 isl_stream_ungetc(s, c);
315 tok = isl_token_new(s->ctx, line, col, old_line != line);
318 tok->type = (enum isl_token_type) '-';
322 if (c == '-' || isdigit(c)) {
323 tok = isl_token_new(s->ctx, line, col, old_line != line);
326 tok->type = ISL_TOKEN_VALUE;
327 isl_int_init(tok->u.v);
328 if (isl_stream_push_char(s, c))
330 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
331 if (isl_stream_push_char(s, c))
334 isl_stream_ungetc(s, c);
335 isl_stream_push_char(s, '\0');
336 isl_int_read(tok->u.v, s->buffer);
340 tok = isl_token_new(s->ctx, line, col, old_line != line);
343 isl_stream_push_char(s, c);
344 while ((c = isl_stream_getc(s)) != -1 &&
345 (isalnum(c) || c == '_'))
346 isl_stream_push_char(s, c);
348 isl_stream_ungetc(s, c);
349 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
350 isl_stream_push_char(s, c);
352 isl_stream_ungetc(s, c);
353 isl_stream_push_char(s, '\0');
354 tok->type = check_keywords(s);
355 if (tok->type == ISL_TOKEN_IDENT)
356 tok->u.s = strdup(s->buffer);
360 tok = isl_token_new(s->ctx, line, col, old_line != line);
363 tok->type = ISL_TOKEN_STRING;
365 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
366 isl_stream_push_char(s, c);
368 isl_stream_error(s, NULL, "unterminated string");
371 isl_stream_push_char(s, '\0');
372 tok->u.s = strdup(s->buffer);
377 tok = isl_token_new(s->ctx, line, col, old_line != line);
380 if ((c = isl_stream_getc(s)) == '=') {
381 tok->type = ISL_TOKEN_DEF;
385 isl_stream_ungetc(s, c);
386 tok->type = (enum isl_token_type) ':';
391 tok = isl_token_new(s->ctx, line, col, old_line != line);
394 if ((c = isl_stream_getc(s)) == '=') {
395 tok->type = ISL_TOKEN_GE;
397 } else if (c == '>') {
398 if ((c = isl_stream_getc(s)) == '=') {
399 tok->type = ISL_TOKEN_LEX_GE;
402 tok->type = ISL_TOKEN_LEX_GT;
404 tok->type = ISL_TOKEN_GT;
406 isl_stream_ungetc(s, c);
411 tok = isl_token_new(s->ctx, line, col, old_line != line);
414 if ((c = isl_stream_getc(s)) == '=') {
415 tok->type = ISL_TOKEN_LE;
417 } else if (c == '<') {
418 if ((c = isl_stream_getc(s)) == '=') {
419 tok->type = ISL_TOKEN_LEX_LE;
422 tok->type = ISL_TOKEN_LEX_LT;
424 tok->type = ISL_TOKEN_LT;
426 isl_stream_ungetc(s, c);
430 tok = isl_token_new(s->ctx, line, col, old_line != line);
433 tok->type = ISL_TOKEN_AND;
434 if ((c = isl_stream_getc(s)) != '&' && c != -1)
435 isl_stream_ungetc(s, c);
439 tok = isl_token_new(s->ctx, line, col, old_line != line);
442 tok->type = ISL_TOKEN_OR;
443 if ((c = isl_stream_getc(s)) != '|' && c != -1)
444 isl_stream_ungetc(s, c);
448 tok = isl_token_new(s->ctx, line, col, old_line != line);
451 tok->type = ISL_TOKEN_UNKNOWN;
458 struct isl_token *isl_stream_next_token(struct isl_stream *s)
460 return next_token(s, 0);
463 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
465 return next_token(s, 1);
468 int isl_stream_eat_if_available(struct isl_stream *s, int type)
470 struct isl_token *tok;
472 tok = isl_stream_next_token(s);
475 if (tok->type == type) {
479 isl_stream_push_token(s, tok);
483 int isl_stream_next_token_is(struct isl_stream *s, int type)
485 struct isl_token *tok;
488 tok = isl_stream_next_token(s);
491 r = tok->type == type;
492 isl_stream_push_token(s, tok);
496 char *isl_stream_read_ident_if_available(struct isl_stream *s)
498 struct isl_token *tok;
500 tok = isl_stream_next_token(s);
503 if (tok->type == ISL_TOKEN_IDENT) {
504 char *ident = strdup(tok->u.s);
508 isl_stream_push_token(s, tok);
512 int isl_stream_eat(struct isl_stream *s, int type)
514 struct isl_token *tok;
516 tok = isl_stream_next_token(s);
519 if (tok->type == type) {
523 isl_stream_error(s, tok, "expecting other token");
524 isl_stream_push_token(s, tok);
528 int isl_stream_is_empty(struct isl_stream *s)
530 struct isl_token *tok;
532 tok = isl_stream_next_token(s);
537 isl_stream_push_token(s, tok);
541 static int free_keyword(void **p, void *user)
543 struct isl_keyword *keyword = *p;
551 void isl_stream_flush_tokens(struct isl_stream *s)
557 for (i = 0; i < s->n_token; ++i)
558 isl_token_free(s->tokens[i]);
562 void isl_stream_free(struct isl_stream *s)
567 if (s->n_token != 0) {
568 struct isl_token *tok = isl_stream_next_token(s);
569 isl_stream_error(s, tok, "unexpected token");
573 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
574 isl_hash_table_free(s->ctx, s->keywords);
576 isl_ctx_deref(s->ctx);