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"
16 static struct isl_token
*isl_token_new(struct isl_ctx
*ctx
,
17 int line
, int col
, unsigned on_new_line
)
19 struct isl_token
*tok
= isl_alloc_type(ctx
, struct isl_token
);
24 tok
->on_new_line
= on_new_line
;
28 void isl_token_free(struct isl_token
*tok
)
32 if (tok
->type
== ISL_TOKEN_VALUE
)
33 isl_int_clear(tok
->u
.v
);
34 else if (tok
->type
== ISL_TOKEN_IDENT
)
39 void isl_stream_error(struct isl_stream
*s
, struct isl_token
*tok
, char *msg
)
41 int line
= tok
? tok
->line
: s
->line
;
42 int col
= tok
? tok
->col
: s
->col
;
43 fprintf(stderr
, "syntax error (%d, %d): %s\n", line
, col
, msg
);
46 fprintf(stderr
, "got '%c'\n", tok
->type
);
48 fprintf(stderr
, "got token type %d\n", tok
->type
);
52 static struct isl_stream
* isl_stream_new(struct isl_ctx
*ctx
)
55 struct isl_stream
*s
= isl_alloc_type(ctx
, struct isl_stream
);
63 s
->buffer
= isl_alloc_array(ctx
, char, s
->size
);
71 for (i
= 0; i
< 5; ++i
)
80 struct isl_stream
* isl_stream_new_file(struct isl_ctx
*ctx
, FILE *file
)
82 struct isl_stream
*s
= isl_stream_new(ctx
);
89 struct isl_stream
* isl_stream_new_str(struct isl_ctx
*ctx
, const char *str
)
91 struct isl_stream
*s
= isl_stream_new(ctx
);
96 static int isl_stream_getc(struct isl_stream
*s
)
121 static void isl_stream_ungetc(struct isl_stream
*s
, int c
)
130 static int isl_stream_push_char(struct isl_stream
*s
, int c
)
132 if (s
->len
>= s
->size
) {
133 s
->size
= (3*s
->size
)/2;
134 s
->buffer
= isl_realloc_array(ctx
, s
->buffer
, char, s
->size
);
138 s
->buffer
[s
->len
++] = c
;
142 void isl_stream_push_token(struct isl_stream
*s
, struct isl_token
*tok
)
144 isl_assert(s
->ctx
, s
->n_token
< 5, return);
145 s
->tokens
[s
->n_token
++] = tok
;
148 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
151 struct isl_token
*tok
= NULL
;
153 int old_line
= s
->line
;
156 return s
->tokens
[--s
->n_token
];
160 /* skip spaces and comment lines */
161 while ((c
= isl_stream_getc(s
)) != -1) {
163 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
168 } else if (!isspace(c
))
190 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
193 tok
->type
= (enum isl_token_type
)c
;
198 if ((c
= isl_stream_getc(s
)) == '>') {
199 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
202 tok
->type
= ISL_TOKEN_TO
;
206 isl_stream_ungetc(s
, c
);
208 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
211 tok
->type
= (enum isl_token_type
) '-';
215 if (c
== '-' || isdigit(c
)) {
216 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
219 tok
->type
= ISL_TOKEN_VALUE
;
220 isl_int_init(tok
->u
.v
);
221 if (isl_stream_push_char(s
, c
))
223 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
224 if (isl_stream_push_char(s
, c
))
227 isl_stream_ungetc(s
, c
);
228 isl_stream_push_char(s
, '\0');
229 isl_int_read(tok
->u
.v
, s
->buffer
);
233 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
236 isl_stream_push_char(s
, c
);
237 while ((c
= isl_stream_getc(s
)) != -1 && isalnum(c
))
238 isl_stream_push_char(s
, c
);
240 isl_stream_ungetc(s
, c
);
241 isl_stream_push_char(s
, '\0');
242 if (!strcasecmp(s
->buffer
, "exists"))
243 tok
->type
= ISL_TOKEN_EXISTS
;
244 else if (!strcasecmp(s
->buffer
, "and"))
245 tok
->type
= ISL_TOKEN_AND
;
246 else if (!strcasecmp(s
->buffer
, "or"))
247 tok
->type
= ISL_TOKEN_OR
;
249 tok
->type
= ISL_TOKEN_IDENT
;
250 tok
->u
.s
= strdup(s
->buffer
);
256 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
259 if ((c
= isl_stream_getc(s
)) == '=') {
260 tok
->type
= ISL_TOKEN_GE
;
264 isl_stream_ungetc(s
, c
);
265 tok
->type
= ISL_TOKEN_GT
;
270 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
273 if ((c
= isl_stream_getc(s
)) == '=') {
274 tok
->type
= ISL_TOKEN_LE
;
278 isl_stream_ungetc(s
, c
);
279 tok
->type
= ISL_TOKEN_LT
;
283 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
286 tok
->type
= ISL_TOKEN_AND
;
287 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1)
288 isl_stream_ungetc(s
, c
);
292 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
295 tok
->type
= ISL_TOKEN_OR
;
296 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1)
297 isl_stream_ungetc(s
, c
);
301 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
304 tok
->type
= ISL_TOKEN_UNKNOWN
;
311 int isl_stream_eat(struct isl_stream
*s
, int type
)
313 struct isl_token
*tok
;
315 tok
= isl_stream_next_token(s
);
318 if (tok
->type
== type
) {
322 isl_stream_error(s
, tok
, "expecting other token");
323 isl_stream_push_token(s
, tok
);
327 void isl_stream_free(struct isl_stream
*s
)
332 if (s
->n_token
!= 0) {
333 struct isl_token
*tok
= isl_stream_next_token(s
);
334 isl_stream_error(s
, tok
, "unexpected token");
337 isl_ctx_deref(s
->ctx
);