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
) {
217 s
->size
= (3*s
->size
)/2;
218 buffer
= isl_realloc_array(s
->ctx
, s
->buffer
, char, s
->size
);
223 s
->buffer
[s
->len
++] = c
;
227 void isl_stream_push_token(struct isl_stream
*s
, struct isl_token
*tok
)
229 isl_assert(s
->ctx
, s
->n_token
< 5, return);
230 s
->tokens
[s
->n_token
++] = tok
;
233 static enum isl_token_type
check_keywords(struct isl_stream
*s
)
235 struct isl_hash_table_entry
*entry
;
236 struct isl_keyword
*keyword
;
239 if (!strcasecmp(s
->buffer
, "exists"))
240 return ISL_TOKEN_EXISTS
;
241 if (!strcasecmp(s
->buffer
, "and"))
242 return ISL_TOKEN_AND
;
243 if (!strcasecmp(s
->buffer
, "or"))
245 if (!strcasecmp(s
->buffer
, "not"))
246 return ISL_TOKEN_NOT
;
247 if (!strcasecmp(s
->buffer
, "infty"))
248 return ISL_TOKEN_INFTY
;
249 if (!strcasecmp(s
->buffer
, "infinity"))
250 return ISL_TOKEN_INFTY
;
251 if (!strcasecmp(s
->buffer
, "NaN"))
252 return ISL_TOKEN_NAN
;
253 if (!strcasecmp(s
->buffer
, "max"))
254 return ISL_TOKEN_MAX
;
255 if (!strcasecmp(s
->buffer
, "rat"))
256 return ISL_TOKEN_RAT
;
257 if (!strcasecmp(s
->buffer
, "true"))
258 return ISL_TOKEN_TRUE
;
259 if (!strcasecmp(s
->buffer
, "false"))
260 return ISL_TOKEN_FALSE
;
263 return ISL_TOKEN_IDENT
;
265 name_hash
= isl_hash_string(isl_hash_init(), s
->buffer
);
266 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
, same_name
,
269 keyword
= entry
->data
;
270 return keyword
->type
;
273 return ISL_TOKEN_IDENT
;
276 int isl_stream_skip_line(struct isl_stream
*s
)
280 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
284 return c
== -1 ? -1 : 0;
287 static struct isl_token
*next_token(struct isl_stream
*s
, int same_line
)
290 struct isl_token
*tok
= NULL
;
292 int old_line
= s
->line
;
295 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
297 return s
->tokens
[--s
->n_token
];
300 if (same_line
&& s
->c
== '\n')
305 /* skip spaces and comment lines */
306 while ((c
= isl_stream_getc(s
)) != -1) {
308 if (isl_stream_skip_line(s
) < 0)
313 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
320 if (c
== -1 || (same_line
&& c
== '\n'))
338 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
341 tok
->type
= (enum isl_token_type
)c
;
346 if ((c
= isl_stream_getc(s
)) == '>') {
347 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
350 tok
->u
.s
= strdup("->");
351 tok
->type
= ISL_TOKEN_TO
;
355 isl_stream_ungetc(s
, c
);
357 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
360 tok
->type
= (enum isl_token_type
) '-';
364 if (c
== '-' || isdigit(c
)) {
365 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
368 tok
->type
= ISL_TOKEN_VALUE
;
369 isl_int_init(tok
->u
.v
);
370 if (isl_stream_push_char(s
, c
))
372 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
373 if (isl_stream_push_char(s
, c
))
376 isl_stream_ungetc(s
, c
);
377 isl_stream_push_char(s
, '\0');
378 isl_int_read(tok
->u
.v
, s
->buffer
);
381 if (isalpha(c
) || c
== '_') {
382 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
385 isl_stream_push_char(s
, c
);
386 while ((c
= isl_stream_getc(s
)) != -1 &&
387 (isalnum(c
) || c
== '_'))
388 isl_stream_push_char(s
, c
);
390 isl_stream_ungetc(s
, c
);
391 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
392 isl_stream_push_char(s
, c
);
394 isl_stream_ungetc(s
, c
);
395 isl_stream_push_char(s
, '\0');
396 tok
->type
= check_keywords(s
);
397 if (tok
->type
!= ISL_TOKEN_IDENT
)
399 tok
->u
.s
= strdup(s
->buffer
);
405 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
408 tok
->type
= ISL_TOKEN_STRING
;
410 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
411 isl_stream_push_char(s
, c
);
413 isl_stream_error(s
, NULL
, "unterminated string");
416 isl_stream_push_char(s
, '\0');
417 tok
->u
.s
= strdup(s
->buffer
);
422 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
425 if ((c
= isl_stream_getc(s
)) == '=') {
426 tok
->u
.s
= strdup(":=");
427 tok
->type
= ISL_TOKEN_DEF
;
431 isl_stream_ungetc(s
, c
);
432 tok
->type
= (enum isl_token_type
) ':';
437 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
440 if ((c
= isl_stream_getc(s
)) == '=') {
441 tok
->u
.s
= strdup(">=");
442 tok
->type
= ISL_TOKEN_GE
;
444 } else if (c
== '>') {
445 if ((c
= isl_stream_getc(s
)) == '=') {
446 tok
->u
.s
= strdup(">>=");
447 tok
->type
= ISL_TOKEN_LEX_GE
;
450 tok
->u
.s
= strdup(">>");
451 tok
->type
= ISL_TOKEN_LEX_GT
;
453 tok
->u
.s
= strdup(">");
454 tok
->type
= ISL_TOKEN_GT
;
457 isl_stream_ungetc(s
, c
);
462 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
465 if ((c
= isl_stream_getc(s
)) == '=') {
466 tok
->u
.s
= strdup("<=");
467 tok
->type
= ISL_TOKEN_LE
;
469 } else if (c
== '<') {
470 if ((c
= isl_stream_getc(s
)) == '=') {
471 tok
->u
.s
= strdup("<<=");
472 tok
->type
= ISL_TOKEN_LEX_LE
;
475 tok
->u
.s
= strdup("<<");
476 tok
->type
= ISL_TOKEN_LEX_LT
;
478 tok
->u
.s
= strdup("<");
479 tok
->type
= ISL_TOKEN_LT
;
482 isl_stream_ungetc(s
, c
);
486 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
489 tok
->type
= ISL_TOKEN_AND
;
490 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1) {
491 tok
->u
.s
= strdup("&");
492 isl_stream_ungetc(s
, c
);
494 tok
->u
.s
= strdup("&&");
498 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
501 tok
->type
= ISL_TOKEN_OR
;
502 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1) {
503 tok
->u
.s
= strdup("|");
504 isl_stream_ungetc(s
, c
);
506 tok
->u
.s
= strdup("||");
510 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
513 if ((c
= isl_stream_getc(s
)) != '\\' && c
!= -1) {
514 tok
->type
= (enum isl_token_type
) '/';
515 isl_stream_ungetc(s
, c
);
517 tok
->u
.s
= strdup("/\\");
518 tok
->type
= ISL_TOKEN_AND
;
523 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
526 if ((c
= isl_stream_getc(s
)) != '/' && c
!= -1) {
527 tok
->type
= (enum isl_token_type
) '\\';
528 isl_stream_ungetc(s
, c
);
530 tok
->u
.s
= strdup("\\/");
531 tok
->type
= ISL_TOKEN_OR
;
536 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
539 tok
->type
= ISL_TOKEN_NOT
;
540 tok
->u
.s
= strdup("!");
544 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
547 tok
->type
= ISL_TOKEN_UNKNOWN
;
554 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
556 return next_token(s
, 0);
559 struct isl_token
*isl_stream_next_token_on_same_line(struct isl_stream
*s
)
561 return next_token(s
, 1);
564 int isl_stream_eat_if_available(struct isl_stream
*s
, int type
)
566 struct isl_token
*tok
;
568 tok
= isl_stream_next_token(s
);
571 if (tok
->type
== type
) {
575 isl_stream_push_token(s
, tok
);
579 int isl_stream_next_token_is(struct isl_stream
*s
, int type
)
581 struct isl_token
*tok
;
584 tok
= isl_stream_next_token(s
);
587 r
= tok
->type
== type
;
588 isl_stream_push_token(s
, tok
);
592 char *isl_stream_read_ident_if_available(struct isl_stream
*s
)
594 struct isl_token
*tok
;
596 tok
= isl_stream_next_token(s
);
599 if (tok
->type
== ISL_TOKEN_IDENT
) {
600 char *ident
= strdup(tok
->u
.s
);
604 isl_stream_push_token(s
, tok
);
608 int isl_stream_eat(struct isl_stream
*s
, int type
)
610 struct isl_token
*tok
;
612 tok
= isl_stream_next_token(s
);
615 if (tok
->type
== type
) {
619 isl_stream_error(s
, tok
, "expecting other token");
620 isl_stream_push_token(s
, tok
);
624 int isl_stream_is_empty(struct isl_stream
*s
)
626 struct isl_token
*tok
;
628 tok
= isl_stream_next_token(s
);
633 isl_stream_push_token(s
, tok
);
637 static int free_keyword(void **p
, void *user
)
639 struct isl_keyword
*keyword
= *p
;
647 void isl_stream_flush_tokens(struct isl_stream
*s
)
653 for (i
= 0; i
< s
->n_token
; ++i
)
654 isl_token_free(s
->tokens
[i
]);
658 void isl_stream_free(struct isl_stream
*s
)
663 if (s
->n_token
!= 0) {
664 struct isl_token
*tok
= isl_stream_next_token(s
);
665 isl_stream_error(s
, tok
, "unexpected token");
669 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
670 isl_hash_table_free(s
->ctx
, s
->keywords
);
672 isl_ctx_deref(s
->ctx
);