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 || tok->type == ISL_TOKEN_STRING)
91 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
93 int line = tok ? tok->line : s->line;
94 int col = tok ? tok->col : s->col;
95 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
98 fprintf(stderr, "got '%c'\n", tok->type);
99 else if (tok->type == ISL_TOKEN_IDENT)
100 fprintf(stderr, "got ident '%s'\n", tok->u.s);
101 else if (tok->is_keyword)
102 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
104 fprintf(stderr, "got token type %d\n", tok->type);
108 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
111 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
123 for (i = 0; i < 5; ++i)
128 s->buffer = isl_alloc_array(ctx, char, s->size);
137 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
139 struct isl_stream *s = isl_stream_new(ctx);
146 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
148 struct isl_stream *s = isl_stream_new(ctx);
155 static int isl_stream_getc(struct isl_stream *s)
180 static void isl_stream_ungetc(struct isl_stream *s, int c)
189 static int isl_stream_push_char(struct isl_stream *s, int c)
191 if (s->len >= s->size) {
192 s->size = (3*s->size)/2;
193 s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
197 s->buffer[s->len++] = c;
201 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
203 isl_assert(s->ctx, s->n_token < 5, return);
204 s->tokens[s->n_token++] = tok;
207 static enum isl_token_type check_keywords(struct isl_stream *s)
209 struct isl_hash_table_entry *entry;
210 struct isl_keyword *keyword;
213 if (!strcasecmp(s->buffer, "exists"))
214 return ISL_TOKEN_EXISTS;
215 if (!strcasecmp(s->buffer, "and"))
216 return ISL_TOKEN_AND;
217 if (!strcasecmp(s->buffer, "or"))
219 if (!strcasecmp(s->buffer, "infty"))
220 return ISL_TOKEN_INFTY;
221 if (!strcasecmp(s->buffer, "infinity"))
222 return ISL_TOKEN_INFTY;
223 if (!strcasecmp(s->buffer, "NaN"))
224 return ISL_TOKEN_NAN;
225 if (!strcasecmp(s->buffer, "max"))
226 return ISL_TOKEN_MAX;
229 return ISL_TOKEN_IDENT;
231 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
232 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
235 keyword = entry->data;
236 return keyword->type;
239 return ISL_TOKEN_IDENT;
242 int isl_stream_skip_line(struct isl_stream *s)
246 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
250 return c == -1 ? -1 : 0;
253 static struct isl_token *next_token(struct isl_stream *s, int same_line)
256 struct isl_token *tok = NULL;
258 int old_line = s->line;
261 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
263 return s->tokens[--s->n_token];
266 if (same_line && s->c == '\n')
271 /* skip spaces and comment lines */
272 while ((c = isl_stream_getc(s)) != -1) {
274 if (isl_stream_skip_line(s) < 0)
279 } else if (!isspace(c) || (same_line && c == '\n'))
286 if (c == -1 || (same_line && c == '\n'))
304 tok = isl_token_new(s->ctx, line, col, old_line != line);
307 tok->type = (enum isl_token_type)c;
312 if ((c = isl_stream_getc(s)) == '>') {
313 tok = isl_token_new(s->ctx, line, col, old_line != line);
316 tok->type = ISL_TOKEN_TO;
320 isl_stream_ungetc(s, c);
322 tok = isl_token_new(s->ctx, line, col, old_line != line);
325 tok->type = (enum isl_token_type) '-';
329 if (c == '-' || isdigit(c)) {
330 tok = isl_token_new(s->ctx, line, col, old_line != line);
333 tok->type = ISL_TOKEN_VALUE;
334 isl_int_init(tok->u.v);
335 if (isl_stream_push_char(s, c))
337 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
338 if (isl_stream_push_char(s, c))
341 isl_stream_ungetc(s, c);
342 isl_stream_push_char(s, '\0');
343 isl_int_read(tok->u.v, s->buffer);
346 if (isalpha(c) || c == '_') {
347 tok = isl_token_new(s->ctx, line, col, old_line != line);
350 isl_stream_push_char(s, c);
351 while ((c = isl_stream_getc(s)) != -1 &&
352 (isalnum(c) || c == '_'))
353 isl_stream_push_char(s, c);
355 isl_stream_ungetc(s, c);
356 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
357 isl_stream_push_char(s, c);
359 isl_stream_ungetc(s, c);
360 isl_stream_push_char(s, '\0');
361 tok->type = check_keywords(s);
362 if (tok->type != ISL_TOKEN_IDENT)
364 tok->u.s = strdup(s->buffer);
370 tok = isl_token_new(s->ctx, line, col, old_line != line);
373 tok->type = ISL_TOKEN_STRING;
375 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
376 isl_stream_push_char(s, c);
378 isl_stream_error(s, NULL, "unterminated string");
381 isl_stream_push_char(s, '\0');
382 tok->u.s = strdup(s->buffer);
387 tok = isl_token_new(s->ctx, line, col, old_line != line);
390 if ((c = isl_stream_getc(s)) == '=') {
391 tok->type = ISL_TOKEN_DEF;
395 isl_stream_ungetc(s, c);
396 tok->type = (enum isl_token_type) ':';
401 tok = isl_token_new(s->ctx, line, col, old_line != line);
404 if ((c = isl_stream_getc(s)) == '=') {
405 tok->type = ISL_TOKEN_GE;
407 } else if (c == '>') {
408 if ((c = isl_stream_getc(s)) == '=') {
409 tok->type = ISL_TOKEN_LEX_GE;
412 tok->type = ISL_TOKEN_LEX_GT;
414 tok->type = ISL_TOKEN_GT;
416 isl_stream_ungetc(s, c);
421 tok = isl_token_new(s->ctx, line, col, old_line != line);
424 if ((c = isl_stream_getc(s)) == '=') {
425 tok->type = ISL_TOKEN_LE;
427 } else if (c == '<') {
428 if ((c = isl_stream_getc(s)) == '=') {
429 tok->type = ISL_TOKEN_LEX_LE;
432 tok->type = ISL_TOKEN_LEX_LT;
434 tok->type = ISL_TOKEN_LT;
436 isl_stream_ungetc(s, c);
440 tok = isl_token_new(s->ctx, line, col, old_line != line);
443 tok->type = ISL_TOKEN_AND;
444 if ((c = isl_stream_getc(s)) != '&' && c != -1)
445 isl_stream_ungetc(s, c);
449 tok = isl_token_new(s->ctx, line, col, old_line != line);
452 tok->type = ISL_TOKEN_OR;
453 if ((c = isl_stream_getc(s)) != '|' && c != -1)
454 isl_stream_ungetc(s, c);
458 tok = isl_token_new(s->ctx, line, col, old_line != line);
461 tok->type = ISL_TOKEN_UNKNOWN;
468 struct isl_token *isl_stream_next_token(struct isl_stream *s)
470 return next_token(s, 0);
473 struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
475 return next_token(s, 1);
478 int isl_stream_eat_if_available(struct isl_stream *s, int type)
480 struct isl_token *tok;
482 tok = isl_stream_next_token(s);
485 if (tok->type == type) {
489 isl_stream_push_token(s, tok);
493 int isl_stream_next_token_is(struct isl_stream *s, int type)
495 struct isl_token *tok;
498 tok = isl_stream_next_token(s);
501 r = tok->type == type;
502 isl_stream_push_token(s, tok);
506 char *isl_stream_read_ident_if_available(struct isl_stream *s)
508 struct isl_token *tok;
510 tok = isl_stream_next_token(s);
513 if (tok->type == ISL_TOKEN_IDENT) {
514 char *ident = strdup(tok->u.s);
518 isl_stream_push_token(s, tok);
522 int isl_stream_eat(struct isl_stream *s, int type)
524 struct isl_token *tok;
526 tok = isl_stream_next_token(s);
529 if (tok->type == type) {
533 isl_stream_error(s, tok, "expecting other token");
534 isl_stream_push_token(s, tok);
538 int isl_stream_is_empty(struct isl_stream *s)
540 struct isl_token *tok;
542 tok = isl_stream_next_token(s);
547 isl_stream_push_token(s, tok);
551 static int free_keyword(void **p, void *user)
553 struct isl_keyword *keyword = *p;
561 void isl_stream_flush_tokens(struct isl_stream *s)
567 for (i = 0; i < s->n_token; ++i)
568 isl_token_free(s->tokens[i]);
572 void isl_stream_free(struct isl_stream *s)
577 if (s->n_token != 0) {
578 struct isl_token *tok = isl_stream_next_token(s);
579 isl_stream_error(s, tok, "unexpected token");
583 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
584 isl_hash_table_free(s->ctx, s->keywords);
586 isl_ctx_deref(s->ctx);