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 static struct isl_token
*next_token(struct isl_stream
*s
, int same_line
)
151 struct isl_token
*tok
= NULL
;
153 int old_line
= s
->line
;
156 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
158 return s
->tokens
[--s
->n_token
];
161 if (same_line
&& s
->c
== '\n')
166 /* skip spaces and comment lines */
167 while ((c
= isl_stream_getc(s
)) != -1) {
169 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
172 if (c
== -1 || (same_line
&& c
== '\n'))
174 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
181 if (c
== -1 || (same_line
&& c
== '\n'))
197 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
200 tok
->type
= (enum isl_token_type
)c
;
205 if ((c
= isl_stream_getc(s
)) == '>') {
206 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
209 tok
->type
= ISL_TOKEN_TO
;
213 isl_stream_ungetc(s
, c
);
215 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
218 tok
->type
= (enum isl_token_type
) '-';
222 if (c
== '-' || isdigit(c
)) {
223 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
226 tok
->type
= ISL_TOKEN_VALUE
;
227 isl_int_init(tok
->u
.v
);
228 if (isl_stream_push_char(s
, c
))
230 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
231 if (isl_stream_push_char(s
, c
))
234 isl_stream_ungetc(s
, c
);
235 isl_stream_push_char(s
, '\0');
236 isl_int_read(tok
->u
.v
, s
->buffer
);
240 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
243 isl_stream_push_char(s
, c
);
244 while ((c
= isl_stream_getc(s
)) != -1 && isalnum(c
))
245 isl_stream_push_char(s
, c
);
247 isl_stream_ungetc(s
, c
);
248 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
249 isl_stream_push_char(s
, c
);
251 isl_stream_ungetc(s
, c
);
252 isl_stream_push_char(s
, '\0');
253 if (!strcasecmp(s
->buffer
, "exists"))
254 tok
->type
= ISL_TOKEN_EXISTS
;
255 else if (!strcasecmp(s
->buffer
, "and"))
256 tok
->type
= ISL_TOKEN_AND
;
257 else if (!strcasecmp(s
->buffer
, "or"))
258 tok
->type
= ISL_TOKEN_OR
;
260 tok
->type
= ISL_TOKEN_IDENT
;
261 tok
->u
.s
= strdup(s
->buffer
);
267 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
270 if ((c
= isl_stream_getc(s
)) == '=') {
271 tok
->type
= ISL_TOKEN_GE
;
275 isl_stream_ungetc(s
, c
);
276 tok
->type
= ISL_TOKEN_GT
;
281 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
284 if ((c
= isl_stream_getc(s
)) == '=') {
285 tok
->type
= ISL_TOKEN_LE
;
289 isl_stream_ungetc(s
, c
);
290 tok
->type
= ISL_TOKEN_LT
;
294 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
297 tok
->type
= ISL_TOKEN_AND
;
298 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1)
299 isl_stream_ungetc(s
, c
);
303 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
306 tok
->type
= ISL_TOKEN_OR
;
307 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1)
308 isl_stream_ungetc(s
, c
);
312 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
315 tok
->type
= ISL_TOKEN_UNKNOWN
;
322 struct isl_token
*isl_stream_next_token(struct isl_stream
*s
)
324 return next_token(s
, 0);
327 struct isl_token
*isl_stream_next_token_on_same_line(struct isl_stream
*s
)
329 return next_token(s
, 1);
332 int isl_stream_eat(struct isl_stream
*s
, int type
)
334 struct isl_token
*tok
;
336 tok
= isl_stream_next_token(s
);
339 if (tok
->type
== type
) {
343 isl_stream_error(s
, tok
, "expecting other token");
344 isl_stream_push_token(s
, tok
);
348 void isl_stream_free(struct isl_stream
*s
)
353 if (s
->n_token
!= 0) {
354 struct isl_token
*tok
= isl_stream_next_token(s
);
355 isl_stream_error(s
, tok
, "unexpected token");
358 isl_ctx_deref(s
->ctx
);