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
;
222 if (!strcasecmp(s
->buffer
, "max"))
223 return ISL_TOKEN_MAX
;
226 return ISL_TOKEN_IDENT
;
228 name_hash
= isl_hash_string(isl_hash_init(), s
->buffer
);
229 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
, same_name
,
232 keyword
= entry
->data
;
233 return keyword
->type
;
236 return ISL_TOKEN_IDENT
;
239 int isl_stream_skip_line(struct isl_stream
*s
)
243 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
247 return c
== -1 ? -1 : 0;
250 static struct isl_token
*next_token(struct isl_stream
*s
, int same_line
)
253 struct isl_token
*tok
= NULL
;
255 int old_line
= s
->line
;
258 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
260 return s
->tokens
[--s
->n_token
];
263 if (same_line
&& s
->c
== '\n')
268 /* skip spaces and comment lines */
269 while ((c
= isl_stream_getc(s
)) != -1) {
271 if (isl_stream_skip_line(s
) < 0)
276 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
283 if (c
== -1 || (same_line
&& c
== '\n'))
301 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
304 tok
->type
= (enum isl_token_type
)c
;
309 if ((c
= isl_stream_getc(s
)) == '>') {
310 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
313 tok
->type
= ISL_TOKEN_TO
;
317 isl_stream_ungetc(s
, c
);
319 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
322 tok
->type
= (enum isl_token_type
) '-';
326 if (c
== '-' || isdigit(c
)) {
327 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
330 tok
->type
= ISL_TOKEN_VALUE
;
331 isl_int_init(tok
->u
.v
);
332 if (isl_stream_push_char(s
, c
))
334 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
335 if (isl_stream_push_char(s
, c
))
338 isl_stream_ungetc(s
, c
);
339 isl_stream_push_char(s
, '\0');
340 isl_int_read(tok
->u
.v
, s
->buffer
);
343 if (isalpha(c
) || c
== '_') {
344 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
347 isl_stream_push_char(s
, c
);
348 while ((c
= isl_stream_getc(s
)) != -1 &&
349 (isalnum(c
) || c
== '_'))
350 isl_stream_push_char(s
, c
);
352 isl_stream_ungetc(s
, c
);
353 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
354 isl_stream_push_char(s
, c
);
356 isl_stream_ungetc(s
, c
);
357 isl_stream_push_char(s
, '\0');
358 tok
->type
= check_keywords(s
);
359 if (tok
->type
== ISL_TOKEN_IDENT
)
360 tok
->u
.s
= strdup(s
->buffer
);
364 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
367 tok
->type
= ISL_TOKEN_STRING
;
369 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
370 isl_stream_push_char(s
, c
);
372 isl_stream_error(s
, NULL
, "unterminated string");
375 isl_stream_push_char(s
, '\0');
376 tok
->u
.s
= strdup(s
->buffer
);
381 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
384 if ((c
= isl_stream_getc(s
)) == '=') {
385 tok
->type
= ISL_TOKEN_DEF
;
389 isl_stream_ungetc(s
, c
);
390 tok
->type
= (enum isl_token_type
) ':';
395 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
398 if ((c
= isl_stream_getc(s
)) == '=') {
399 tok
->type
= ISL_TOKEN_GE
;
401 } else if (c
== '>') {
402 if ((c
= isl_stream_getc(s
)) == '=') {
403 tok
->type
= ISL_TOKEN_LEX_GE
;
406 tok
->type
= ISL_TOKEN_LEX_GT
;
408 tok
->type
= ISL_TOKEN_GT
;
410 isl_stream_ungetc(s
, c
);
415 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
418 if ((c
= isl_stream_getc(s
)) == '=') {
419 tok
->type
= ISL_TOKEN_LE
;
421 } else if (c
== '<') {
422 if ((c
= isl_stream_getc(s
)) == '=') {
423 tok
->type
= ISL_TOKEN_LEX_LE
;
426 tok
->type
= ISL_TOKEN_LEX_LT
;
428 tok
->type
= ISL_TOKEN_LT
;
430 isl_stream_ungetc(s
, c
);
434 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
437 tok
->type
= ISL_TOKEN_AND
;
438 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1)
439 isl_stream_ungetc(s
, c
);
443 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
446 tok
->type
= ISL_TOKEN_OR
;
447 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1)
448 isl_stream_ungetc(s
, c
);
452 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
455 tok
->type
= ISL_TOKEN_UNKNOWN
;
462 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
464 return next_token(s
, 0);
467 struct isl_token
*isl_stream_next_token_on_same_line(struct isl_stream
*s
)
469 return next_token(s
, 1);
472 int isl_stream_eat_if_available(struct isl_stream
*s
, int type
)
474 struct isl_token
*tok
;
476 tok
= isl_stream_next_token(s
);
479 if (tok
->type
== type
) {
483 isl_stream_push_token(s
, tok
);
487 int isl_stream_next_token_is(struct isl_stream
*s
, int type
)
489 struct isl_token
*tok
;
492 tok
= isl_stream_next_token(s
);
495 r
= tok
->type
== type
;
496 isl_stream_push_token(s
, tok
);
500 char *isl_stream_read_ident_if_available(struct isl_stream
*s
)
502 struct isl_token
*tok
;
504 tok
= isl_stream_next_token(s
);
507 if (tok
->type
== ISL_TOKEN_IDENT
) {
508 char *ident
= strdup(tok
->u
.s
);
512 isl_stream_push_token(s
, tok
);
516 int isl_stream_eat(struct isl_stream
*s
, int type
)
518 struct isl_token
*tok
;
520 tok
= isl_stream_next_token(s
);
523 if (tok
->type
== type
) {
527 isl_stream_error(s
, tok
, "expecting other token");
528 isl_stream_push_token(s
, tok
);
532 int isl_stream_is_empty(struct isl_stream
*s
)
534 struct isl_token
*tok
;
536 tok
= isl_stream_next_token(s
);
541 isl_stream_push_token(s
, tok
);
545 static int free_keyword(void **p
, void *user
)
547 struct isl_keyword
*keyword
= *p
;
555 void isl_stream_flush_tokens(struct isl_stream
*s
)
561 for (i
= 0; i
< s
->n_token
; ++i
)
562 isl_token_free(s
->tokens
[i
]);
566 void isl_stream_free(struct isl_stream
*s
)
571 if (s
->n_token
!= 0) {
572 struct isl_token
*tok
= isl_stream_next_token(s
);
573 isl_stream_error(s
, tok
, "unexpected token");
577 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
578 isl_hash_table_free(s
->ctx
, s
->keywords
);
580 isl_ctx_deref(s
->ctx
);