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
);
98 else if (tok
->type
== ISL_TOKEN_IDENT
)
99 fprintf(stderr
, "got ident '%s'\n", tok
->u
.s
);
101 fprintf(stderr
, "got token type %d\n", tok
->type
);
105 static struct isl_stream
* isl_stream_new(struct isl_ctx
*ctx
)
108 struct isl_stream
*s
= isl_alloc_type(ctx
, struct isl_stream
);
120 for (i
= 0; i
< 5; ++i
)
125 s
->buffer
= isl_alloc_array(ctx
, char, s
->size
);
134 struct isl_stream
* isl_stream_new_file(struct isl_ctx
*ctx
, FILE *file
)
136 struct isl_stream
*s
= isl_stream_new(ctx
);
143 struct isl_stream
* isl_stream_new_str(struct isl_ctx
*ctx
, const char *str
)
145 struct isl_stream
*s
= isl_stream_new(ctx
);
152 static int isl_stream_getc(struct isl_stream
*s
)
177 static void isl_stream_ungetc(struct isl_stream
*s
, int c
)
186 static int isl_stream_push_char(struct isl_stream
*s
, int c
)
188 if (s
->len
>= s
->size
) {
189 s
->size
= (3*s
->size
)/2;
190 s
->buffer
= isl_realloc_array(s
->ctx
, s
->buffer
, char, s
->size
);
194 s
->buffer
[s
->len
++] = c
;
198 void isl_stream_push_token(struct isl_stream
*s
, struct isl_token
*tok
)
200 isl_assert(s
->ctx
, s
->n_token
< 5, return);
201 s
->tokens
[s
->n_token
++] = tok
;
204 static enum isl_token_type
check_keywords(struct isl_stream
*s
)
206 struct isl_hash_table_entry
*entry
;
207 struct isl_keyword
*keyword
;
210 if (!strcasecmp(s
->buffer
, "exists"))
211 return ISL_TOKEN_EXISTS
;
212 if (!strcasecmp(s
->buffer
, "and"))
213 return ISL_TOKEN_AND
;
214 if (!strcasecmp(s
->buffer
, "or"))
216 if (!strcasecmp(s
->buffer
, "infty"))
217 return ISL_TOKEN_INFTY
;
218 if (!strcasecmp(s
->buffer
, "infinity"))
219 return ISL_TOKEN_INFTY
;
220 if (!strcasecmp(s
->buffer
, "NaN"))
221 return ISL_TOKEN_NAN
;
224 return ISL_TOKEN_IDENT
;
226 name_hash
= isl_hash_string(isl_hash_init(), s
->buffer
);
227 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
, same_name
,
230 keyword
= entry
->data
;
231 return keyword
->type
;
234 return ISL_TOKEN_IDENT
;
237 int isl_stream_skip_line(struct isl_stream
*s
)
241 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
245 return c
== -1 ? -1 : 0;
248 static struct isl_token
*next_token(struct isl_stream
*s
, int same_line
)
251 struct isl_token
*tok
= NULL
;
253 int old_line
= s
->line
;
256 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
258 return s
->tokens
[--s
->n_token
];
261 if (same_line
&& s
->c
== '\n')
266 /* skip spaces and comment lines */
267 while ((c
= isl_stream_getc(s
)) != -1) {
269 if (isl_stream_skip_line(s
) < 0)
274 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
281 if (c
== -1 || (same_line
&& c
== '\n'))
299 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
302 tok
->type
= (enum isl_token_type
)c
;
307 if ((c
= isl_stream_getc(s
)) == '>') {
308 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
311 tok
->type
= ISL_TOKEN_TO
;
315 isl_stream_ungetc(s
, c
);
317 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
320 tok
->type
= (enum isl_token_type
) '-';
324 if (c
== '-' || isdigit(c
)) {
325 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
328 tok
->type
= ISL_TOKEN_VALUE
;
329 isl_int_init(tok
->u
.v
);
330 if (isl_stream_push_char(s
, c
))
332 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
333 if (isl_stream_push_char(s
, c
))
336 isl_stream_ungetc(s
, c
);
337 isl_stream_push_char(s
, '\0');
338 isl_int_read(tok
->u
.v
, s
->buffer
);
342 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
345 isl_stream_push_char(s
, c
);
346 while ((c
= isl_stream_getc(s
)) != -1 &&
347 (isalnum(c
) || c
== '_'))
348 isl_stream_push_char(s
, c
);
350 isl_stream_ungetc(s
, c
);
351 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
352 isl_stream_push_char(s
, c
);
354 isl_stream_ungetc(s
, c
);
355 isl_stream_push_char(s
, '\0');
356 tok
->type
= check_keywords(s
);
357 if (tok
->type
== ISL_TOKEN_IDENT
)
358 tok
->u
.s
= strdup(s
->buffer
);
362 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
365 tok
->type
= ISL_TOKEN_STRING
;
367 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
368 isl_stream_push_char(s
, c
);
370 isl_stream_error(s
, NULL
, "unterminated string");
373 isl_stream_push_char(s
, '\0');
374 tok
->u
.s
= strdup(s
->buffer
);
379 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
382 if ((c
= isl_stream_getc(s
)) == '=') {
383 tok
->type
= ISL_TOKEN_DEF
;
387 isl_stream_ungetc(s
, c
);
388 tok
->type
= (enum isl_token_type
) ':';
393 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
396 if ((c
= isl_stream_getc(s
)) == '=') {
397 tok
->type
= ISL_TOKEN_GE
;
399 } else if (c
== '>') {
400 if ((c
= isl_stream_getc(s
)) == '=') {
401 tok
->type
= ISL_TOKEN_LEX_GE
;
404 tok
->type
= ISL_TOKEN_LEX_GT
;
406 tok
->type
= ISL_TOKEN_GT
;
408 isl_stream_ungetc(s
, c
);
413 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
416 if ((c
= isl_stream_getc(s
)) == '=') {
417 tok
->type
= ISL_TOKEN_LE
;
419 } else if (c
== '<') {
420 if ((c
= isl_stream_getc(s
)) == '=') {
421 tok
->type
= ISL_TOKEN_LEX_LE
;
424 tok
->type
= ISL_TOKEN_LEX_LT
;
426 tok
->type
= ISL_TOKEN_LT
;
428 isl_stream_ungetc(s
, c
);
432 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
435 tok
->type
= ISL_TOKEN_AND
;
436 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1)
437 isl_stream_ungetc(s
, c
);
441 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
444 tok
->type
= ISL_TOKEN_OR
;
445 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1)
446 isl_stream_ungetc(s
, c
);
450 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
453 tok
->type
= ISL_TOKEN_UNKNOWN
;
460 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
462 return next_token(s
, 0);
465 struct isl_token
*isl_stream_next_token_on_same_line(struct isl_stream
*s
)
467 return next_token(s
, 1);
470 int isl_stream_eat_if_available(struct isl_stream
*s
, int type
)
472 struct isl_token
*tok
;
474 tok
= isl_stream_next_token(s
);
477 if (tok
->type
== type
) {
481 isl_stream_push_token(s
, tok
);
485 int isl_stream_next_token_is(struct isl_stream
*s
, int type
)
487 struct isl_token
*tok
;
490 tok
= isl_stream_next_token(s
);
493 r
= tok
->type
== type
;
494 isl_stream_push_token(s
, tok
);
498 char *isl_stream_read_ident_if_available(struct isl_stream
*s
)
500 struct isl_token
*tok
;
502 tok
= isl_stream_next_token(s
);
505 if (tok
->type
== ISL_TOKEN_IDENT
) {
506 char *ident
= strdup(tok
->u
.s
);
510 isl_stream_push_token(s
, tok
);
514 int isl_stream_eat(struct isl_stream
*s
, int type
)
516 struct isl_token
*tok
;
518 tok
= isl_stream_next_token(s
);
521 if (tok
->type
== type
) {
525 isl_stream_error(s
, tok
, "expecting other token");
526 isl_stream_push_token(s
, tok
);
530 int isl_stream_is_empty(struct isl_stream
*s
)
532 struct isl_token
*tok
;
534 tok
= isl_stream_next_token(s
);
539 isl_stream_push_token(s
, tok
);
543 static int free_keyword(void **p
, void *user
)
545 struct isl_keyword
*keyword
= *p
;
553 void isl_stream_flush_tokens(struct isl_stream
*s
)
559 for (i
= 0; i
< s
->n_token
; ++i
)
560 isl_token_free(s
->tokens
[i
]);
564 void isl_stream_free(struct isl_stream
*s
)
569 if (s
->n_token
!= 0) {
570 struct isl_token
*tok
= isl_stream_next_token(s
);
571 isl_stream_error(s
, tok
, "unexpected token");
575 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
576 isl_hash_table_free(s
->ctx
, s
->keywords
);
578 isl_ctx_deref(s
->ctx
);