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
);
131 for (i
= 0; i
< 5; ++i
)
136 s
->buffer
= isl_alloc_array(ctx
, char, s
->size
);
145 struct isl_stream
* isl_stream_new_file(struct isl_ctx
*ctx
, FILE *file
)
147 struct isl_stream
*s
= isl_stream_new(ctx
);
154 struct isl_stream
* isl_stream_new_str(struct isl_ctx
*ctx
, const char *str
)
156 struct isl_stream
*s
= isl_stream_new(ctx
);
163 static int stream_getc(struct isl_stream
*s
)
169 return s
->c
= s
->un
[--s
->n_un
];
190 static void isl_stream_ungetc(struct isl_stream
*s
, int c
)
192 isl_assert(s
->ctx
, s
->n_un
< 5, return);
193 s
->un
[s
->n_un
++] = c
;
197 static int isl_stream_getc(struct isl_stream
*s
)
208 isl_stream_ungetc(s
, c
);
213 static int isl_stream_push_char(struct isl_stream
*s
, int c
)
215 if (s
->len
>= s
->size
) {
216 s
->size
= (3*s
->size
)/2;
217 s
->buffer
= isl_realloc_array(s
->ctx
, s
->buffer
, char, s
->size
);
221 s
->buffer
[s
->len
++] = c
;
225 void isl_stream_push_token(struct isl_stream
*s
, struct isl_token
*tok
)
227 isl_assert(s
->ctx
, s
->n_token
< 5, return);
228 s
->tokens
[s
->n_token
++] = tok
;
231 static enum isl_token_type
check_keywords(struct isl_stream
*s
)
233 struct isl_hash_table_entry
*entry
;
234 struct isl_keyword
*keyword
;
237 if (!strcasecmp(s
->buffer
, "exists"))
238 return ISL_TOKEN_EXISTS
;
239 if (!strcasecmp(s
->buffer
, "and"))
240 return ISL_TOKEN_AND
;
241 if (!strcasecmp(s
->buffer
, "or"))
243 if (!strcasecmp(s
->buffer
, "not"))
244 return ISL_TOKEN_NOT
;
245 if (!strcasecmp(s
->buffer
, "infty"))
246 return ISL_TOKEN_INFTY
;
247 if (!strcasecmp(s
->buffer
, "infinity"))
248 return ISL_TOKEN_INFTY
;
249 if (!strcasecmp(s
->buffer
, "NaN"))
250 return ISL_TOKEN_NAN
;
251 if (!strcasecmp(s
->buffer
, "max"))
252 return ISL_TOKEN_MAX
;
253 if (!strcasecmp(s
->buffer
, "rat"))
254 return ISL_TOKEN_RAT
;
255 if (!strcasecmp(s
->buffer
, "true"))
256 return ISL_TOKEN_TRUE
;
257 if (!strcasecmp(s
->buffer
, "false"))
258 return ISL_TOKEN_FALSE
;
261 return ISL_TOKEN_IDENT
;
263 name_hash
= isl_hash_string(isl_hash_init(), s
->buffer
);
264 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
, same_name
,
267 keyword
= entry
->data
;
268 return keyword
->type
;
271 return ISL_TOKEN_IDENT
;
274 int isl_stream_skip_line(struct isl_stream
*s
)
278 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
282 return c
== -1 ? -1 : 0;
285 static struct isl_token
*next_token(struct isl_stream
*s
, int same_line
)
288 struct isl_token
*tok
= NULL
;
290 int old_line
= s
->line
;
293 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
295 return s
->tokens
[--s
->n_token
];
298 if (same_line
&& s
->c
== '\n')
303 /* skip spaces and comment lines */
304 while ((c
= isl_stream_getc(s
)) != -1) {
306 if (isl_stream_skip_line(s
) < 0)
311 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
318 if (c
== -1 || (same_line
&& c
== '\n'))
336 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
339 tok
->type
= (enum isl_token_type
)c
;
344 if ((c
= isl_stream_getc(s
)) == '>') {
345 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
348 tok
->u
.s
= strdup("->");
349 tok
->type
= ISL_TOKEN_TO
;
353 isl_stream_ungetc(s
, c
);
355 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
358 tok
->type
= (enum isl_token_type
) '-';
362 if (c
== '-' || isdigit(c
)) {
363 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
366 tok
->type
= ISL_TOKEN_VALUE
;
367 isl_int_init(tok
->u
.v
);
368 if (isl_stream_push_char(s
, c
))
370 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
371 if (isl_stream_push_char(s
, c
))
374 isl_stream_ungetc(s
, c
);
375 isl_stream_push_char(s
, '\0');
376 isl_int_read(tok
->u
.v
, s
->buffer
);
379 if (isalpha(c
) || c
== '_') {
380 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
383 isl_stream_push_char(s
, c
);
384 while ((c
= isl_stream_getc(s
)) != -1 &&
385 (isalnum(c
) || c
== '_'))
386 isl_stream_push_char(s
, c
);
388 isl_stream_ungetc(s
, c
);
389 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
390 isl_stream_push_char(s
, c
);
392 isl_stream_ungetc(s
, c
);
393 isl_stream_push_char(s
, '\0');
394 tok
->type
= check_keywords(s
);
395 if (tok
->type
!= ISL_TOKEN_IDENT
)
397 tok
->u
.s
= strdup(s
->buffer
);
403 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
406 tok
->type
= ISL_TOKEN_STRING
;
408 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
409 isl_stream_push_char(s
, c
);
411 isl_stream_error(s
, NULL
, "unterminated string");
414 isl_stream_push_char(s
, '\0');
415 tok
->u
.s
= strdup(s
->buffer
);
420 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
423 if ((c
= isl_stream_getc(s
)) == '=') {
424 tok
->u
.s
= strdup(":=");
425 tok
->type
= ISL_TOKEN_DEF
;
429 isl_stream_ungetc(s
, c
);
430 tok
->type
= (enum isl_token_type
) ':';
435 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
438 if ((c
= isl_stream_getc(s
)) == '=') {
439 tok
->u
.s
= strdup(">=");
440 tok
->type
= ISL_TOKEN_GE
;
442 } else if (c
== '>') {
443 if ((c
= isl_stream_getc(s
)) == '=') {
444 tok
->u
.s
= strdup(">>=");
445 tok
->type
= ISL_TOKEN_LEX_GE
;
448 tok
->u
.s
= strdup(">>");
449 tok
->type
= ISL_TOKEN_LEX_GT
;
451 tok
->u
.s
= strdup(">");
452 tok
->type
= ISL_TOKEN_GT
;
455 isl_stream_ungetc(s
, c
);
460 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
463 if ((c
= isl_stream_getc(s
)) == '=') {
464 tok
->u
.s
= strdup("<=");
465 tok
->type
= ISL_TOKEN_LE
;
467 } else if (c
== '<') {
468 if ((c
= isl_stream_getc(s
)) == '=') {
469 tok
->u
.s
= strdup("<<=");
470 tok
->type
= ISL_TOKEN_LEX_LE
;
473 tok
->u
.s
= strdup("<<");
474 tok
->type
= ISL_TOKEN_LEX_LT
;
476 tok
->u
.s
= strdup("<");
477 tok
->type
= ISL_TOKEN_LT
;
480 isl_stream_ungetc(s
, c
);
484 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
487 tok
->type
= ISL_TOKEN_AND
;
488 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1) {
489 tok
->u
.s
= strdup("&");
490 isl_stream_ungetc(s
, c
);
492 tok
->u
.s
= strdup("&&");
496 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
499 tok
->type
= ISL_TOKEN_OR
;
500 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1) {
501 tok
->u
.s
= strdup("|");
502 isl_stream_ungetc(s
, c
);
504 tok
->u
.s
= strdup("||");
508 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
511 if ((c
= isl_stream_getc(s
)) != '\\' && c
!= -1) {
512 tok
->type
= (enum isl_token_type
) '/';
513 isl_stream_ungetc(s
, c
);
515 tok
->u
.s
= strdup("/\\");
516 tok
->type
= ISL_TOKEN_AND
;
521 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
524 if ((c
= isl_stream_getc(s
)) != '/' && c
!= -1) {
525 tok
->type
= (enum isl_token_type
) '\\';
526 isl_stream_ungetc(s
, c
);
528 tok
->u
.s
= strdup("\\/");
529 tok
->type
= ISL_TOKEN_OR
;
534 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
537 tok
->type
= ISL_TOKEN_NOT
;
538 tok
->u
.s
= strdup("!");
542 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
545 tok
->type
= ISL_TOKEN_UNKNOWN
;
552 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
554 return next_token(s
, 0);
557 struct isl_token
*isl_stream_next_token_on_same_line(struct isl_stream
*s
)
559 return next_token(s
, 1);
562 int isl_stream_eat_if_available(struct isl_stream
*s
, int type
)
564 struct isl_token
*tok
;
566 tok
= isl_stream_next_token(s
);
569 if (tok
->type
== type
) {
573 isl_stream_push_token(s
, tok
);
577 int isl_stream_next_token_is(struct isl_stream
*s
, int type
)
579 struct isl_token
*tok
;
582 tok
= isl_stream_next_token(s
);
585 r
= tok
->type
== type
;
586 isl_stream_push_token(s
, tok
);
590 char *isl_stream_read_ident_if_available(struct isl_stream
*s
)
592 struct isl_token
*tok
;
594 tok
= isl_stream_next_token(s
);
597 if (tok
->type
== ISL_TOKEN_IDENT
) {
598 char *ident
= strdup(tok
->u
.s
);
602 isl_stream_push_token(s
, tok
);
606 int isl_stream_eat(struct isl_stream
*s
, int type
)
608 struct isl_token
*tok
;
610 tok
= isl_stream_next_token(s
);
613 if (tok
->type
== type
) {
617 isl_stream_error(s
, tok
, "expecting other token");
618 isl_stream_push_token(s
, tok
);
622 int isl_stream_is_empty(struct isl_stream
*s
)
624 struct isl_token
*tok
;
626 tok
= isl_stream_next_token(s
);
631 isl_stream_push_token(s
, tok
);
635 static int free_keyword(void **p
, void *user
)
637 struct isl_keyword
*keyword
= *p
;
645 void isl_stream_flush_tokens(struct isl_stream
*s
)
651 for (i
= 0; i
< s
->n_token
; ++i
)
652 isl_token_free(s
->tokens
[i
]);
656 void isl_stream_free(struct isl_stream
*s
)
661 if (s
->n_token
!= 0) {
662 struct isl_token
*tok
= isl_stream_next_token(s
);
663 isl_stream_error(s
, tok
, "unexpected token");
667 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
668 isl_hash_table_free(s
->ctx
, s
->keywords
);
670 isl_ctx_deref(s
->ctx
);