5 #include "isl_stream.h"
7 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
8 int line, int col, unsigned on_new_line)
10 struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
15 tok->on_new_line = on_new_line;
19 void isl_token_free(struct isl_token *tok)
23 if (tok->type == ISL_TOKEN_VALUE)
24 isl_int_clear(tok->u.v);
25 else if (tok->type == ISL_TOKEN_IDENT)
30 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
32 int line = tok ? tok->line : s->line;
33 int col = tok ? tok->col : s->col;
34 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
37 fprintf(stderr, "got '%c'\n", tok->type);
39 fprintf(stderr, "got token type %d\n", tok->type);
43 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
46 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
53 s->buffer = isl_alloc_array(ctx, char, s->size);
61 for (i = 0; i < 5; ++i)
70 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
72 struct isl_stream *s = isl_stream_new(ctx);
79 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
81 struct isl_stream *s = isl_stream_new(ctx);
86 static int isl_stream_getc(struct isl_stream *s)
111 static void isl_stream_ungetc(struct isl_stream *s, int c)
120 static int isl_stream_push_char(struct isl_stream *s, int c)
122 if (s->len >= s->size) {
123 s->size = (3*s->size)/2;
124 s->buffer = isl_realloc_array(ctx, s->buffer, char, s->size);
128 s->buffer[s->len++] = c;
132 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
134 isl_assert(s->ctx, s->n_token < 5, return);
135 s->tokens[s->n_token++] = tok;
138 struct isl_token *isl_stream_next_token(struct isl_stream *s)
141 struct isl_token *tok = NULL;
143 int old_line = s->line;
146 return s->tokens[--s->n_token];
151 while ((c = isl_stream_getc(s)) != -1 && isspace(c))
173 tok = isl_token_new(s->ctx, line, col, old_line != line);
176 tok->type = (enum isl_token_type)c;
181 if ((c = isl_stream_getc(s)) == '>') {
182 tok = isl_token_new(s->ctx, line, col, old_line != line);
185 tok->type = ISL_TOKEN_TO;
189 isl_stream_ungetc(s, c);
191 if (c == '-' || isdigit(c)) {
192 tok = isl_token_new(s->ctx, line, col, old_line != line);
195 tok->type = ISL_TOKEN_VALUE;
196 isl_int_init(tok->u.v);
197 if (isl_stream_push_char(s, c))
199 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
200 if (isl_stream_push_char(s, c))
203 isl_stream_ungetc(s, c);
204 if (s->len == 1 && s->buffer[0] == '-')
205 isl_int_set_si(tok->u.v, -1);
207 isl_stream_push_char(s, '\0');
208 isl_int_read(tok->u.v, s->buffer);
213 tok = isl_token_new(s->ctx, line, col, old_line != line);
216 isl_stream_push_char(s, c);
217 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
218 isl_stream_push_char(s, c);
220 isl_stream_ungetc(s, c);
221 isl_stream_push_char(s, '\0');
222 if (!strcasecmp(s->buffer, "exists"))
223 tok->type = ISL_TOKEN_EXISTS;
225 tok->type = ISL_TOKEN_IDENT;
226 tok->u.s = strdup(s->buffer);
232 if ((c = isl_stream_getc(s)) == '=') {
233 tok = isl_token_new(s->ctx, line, col, old_line != line);
236 tok->type = ISL_TOKEN_GE;
240 isl_stream_ungetc(s, c);
244 if ((c = isl_stream_getc(s)) == '=') {
245 tok = isl_token_new(s->ctx, line, col, old_line != line);
248 tok->type = ISL_TOKEN_LE;
252 isl_stream_ungetc(s, c);
255 tok = isl_token_new(s->ctx, line, col, old_line != line);
258 tok->type = ISL_TOKEN_AND;
259 if ((c = isl_stream_getc(s)) != '&' && c != -1)
260 isl_stream_ungetc(s, c);
264 tok = isl_token_new(s->ctx, line, col, old_line != line);
267 tok->type = ISL_TOKEN_UNKNOWN;
274 int isl_stream_eat(struct isl_stream *s, int type)
276 struct isl_token *tok;
278 tok = isl_stream_next_token(s);
281 if (tok->type == type) {
285 isl_stream_error(s, tok, "expecting other token");
286 isl_stream_push_token(s, tok);
290 void isl_stream_free(struct isl_stream *s)
295 if (s->n_token != 0) {
296 struct isl_token *tok = isl_stream_next_token(s);
297 isl_stream_error(s, tok, "unexpected token");