change isl_basic_map_empty interface for consistency
[isl.git] / isl_stream.c
blob9277e47669b23d845fadc12c69d2dd23edc46dd3
1 #include <ctype.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <isl_ctx.h>
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);
11 if (!tok)
12 return NULL;
13 tok->line = line;
14 tok->col = col;
15 tok->on_new_line = on_new_line;
16 return tok;
19 void isl_token_free(struct isl_token *tok)
21 if (!tok)
22 return;
23 if (tok->type == ISL_TOKEN_VALUE)
24 isl_int_clear(tok->u.v);
25 else if (tok->type == ISL_TOKEN_IDENT)
26 free(tok->u.s);
27 free(tok);
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);
35 if (tok) {
36 if (tok->type < 256)
37 fprintf(stderr, "got '%c'\n", tok->type);
38 else
39 fprintf(stderr, "got token type %d\n", tok->type);
43 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
45 int i;
46 struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
47 if (!s)
48 return NULL;
49 s->ctx = ctx;
50 isl_ctx_ref(s->ctx);
51 s->size = 256;
52 s->file = NULL;
53 s->str = NULL;
54 s->buffer = isl_alloc_array(ctx, char, s->size);
55 if (!s->buffer)
56 goto error;
57 s->len = 0;
58 s->line = 1;
59 s->col = 0;
60 s->eof = 0;
61 s->c = -1;
62 for (i = 0; i < 5; ++i)
63 s->tokens[i] = NULL;
64 s->n_token = 0;
65 return s;
66 error:
67 isl_stream_free(s);
68 return NULL;
71 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
73 struct isl_stream *s = isl_stream_new(ctx);
74 if (!s)
75 return NULL;
76 s->file = file;
77 return s;
80 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
82 struct isl_stream *s = isl_stream_new(ctx);
83 s->str = str;
84 return s;
87 static int isl_stream_getc(struct isl_stream *s)
89 int c;
90 if (s->eof)
91 return -1;
92 if (s->file)
93 c = fgetc(s->file);
94 else {
95 c = *s->str++;
96 if (c == '\0')
97 c = -1;
99 if (c == -1)
100 s->eof = 1;
101 if (!s->eof) {
102 if (s->c == '\n') {
103 s->line++;
104 s->col = 0;
105 } else
106 s->col++;
108 s->c = c;
109 return c;
112 static void isl_stream_ungetc(struct isl_stream *s, int c)
114 if (s->file)
115 ungetc(c, s->file);
116 else
117 --s->str;
118 s->c = -1;
121 static int isl_stream_push_char(struct isl_stream *s, int c)
123 if (s->len >= s->size) {
124 s->size = (3*s->size)/2;
125 s->buffer = isl_realloc_array(ctx, s->buffer, char, s->size);
126 if (!s->buffer)
127 return -1;
129 s->buffer[s->len++] = c;
130 return 0;
133 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
135 isl_assert(s->ctx, s->n_token < 5, return);
136 s->tokens[s->n_token++] = tok;
139 struct isl_token *isl_stream_next_token(struct isl_stream *s)
141 int c;
142 struct isl_token *tok = NULL;
143 int line, col;
144 int old_line = s->line;
146 if (s->n_token)
147 return s->tokens[--s->n_token];
149 s->len = 0;
151 /* skip spaces */
152 while ((c = isl_stream_getc(s)) != -1 && isspace(c))
153 /* nothing */
156 line = s->line;
157 col = s->col;
159 if (c == -1)
160 return NULL;
161 if (c == '(' ||
162 c == ')' ||
163 c == '+' ||
164 c == '/' ||
165 c == '*' ||
166 c == '^' ||
167 c == '=' ||
168 c == ',' ||
169 c == ':' ||
170 c == '[' ||
171 c == ']' ||
172 c == '{' ||
173 c == '}') {
174 tok = isl_token_new(s->ctx, line, col, old_line != line);
175 if (!tok)
176 return NULL;
177 tok->type = (enum isl_token_type)c;
178 return tok;
180 if (c == '-') {
181 int c;
182 if ((c = isl_stream_getc(s)) == '>') {
183 tok = isl_token_new(s->ctx, line, col, old_line != line);
184 if (!tok)
185 return NULL;
186 tok->type = ISL_TOKEN_TO;
187 return tok;
189 if (c != -1)
190 isl_stream_ungetc(s, c);
192 if (c == '-' || isdigit(c)) {
193 tok = isl_token_new(s->ctx, line, col, old_line != line);
194 if (!tok)
195 return NULL;
196 tok->type = ISL_TOKEN_VALUE;
197 isl_int_init(tok->u.v);
198 if (isl_stream_push_char(s, c))
199 goto error;
200 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
201 if (isl_stream_push_char(s, c))
202 goto error;
203 if (c != -1)
204 isl_stream_ungetc(s, c);
205 if (s->len == 1 && s->buffer[0] == '-')
206 isl_int_set_si(tok->u.v, -1);
207 else {
208 isl_stream_push_char(s, '\0');
209 isl_int_read(tok->u.v, s->buffer);
211 return tok;
213 if (isalpha(c)) {
214 tok = isl_token_new(s->ctx, line, col, old_line != line);
215 if (!tok)
216 return NULL;
217 isl_stream_push_char(s, c);
218 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
219 isl_stream_push_char(s, c);
220 if (c != -1)
221 isl_stream_ungetc(s, c);
222 isl_stream_push_char(s, '\0');
223 if (!strcasecmp(s->buffer, "exists"))
224 tok->type = ISL_TOKEN_EXISTS;
225 else {
226 tok->type = ISL_TOKEN_IDENT;
227 tok->u.s = strdup(s->buffer);
229 return tok;
231 if (c == '>') {
232 int c;
233 if ((c = isl_stream_getc(s)) == '=') {
234 tok = isl_token_new(s->ctx, line, col, old_line != line);
235 if (!tok)
236 return NULL;
237 tok->type = ISL_TOKEN_GE;
238 return tok;
240 if (c != -1)
241 isl_stream_ungetc(s, c);
243 if (c == '<') {
244 int c;
245 if ((c = isl_stream_getc(s)) == '=') {
246 tok = isl_token_new(s->ctx, line, col, old_line != line);
247 if (!tok)
248 return NULL;
249 tok->type = ISL_TOKEN_LE;
250 return tok;
252 if (c != -1)
253 isl_stream_ungetc(s, c);
255 if (c == '&') {
256 tok = isl_token_new(s->ctx, line, col, old_line != line);
257 if (!tok)
258 return NULL;
259 tok->type = ISL_TOKEN_AND;
260 if ((c = isl_stream_getc(s)) != '&' && c != -1)
261 isl_stream_ungetc(s, c);
262 return tok;
265 tok = isl_token_new(s->ctx, line, col, old_line != line);
266 if (!tok)
267 return NULL;
268 tok->type = ISL_TOKEN_UNKNOWN;
269 return tok;
270 error:
271 isl_token_free(tok);
272 return NULL;
275 int isl_stream_eat(struct isl_stream *s, int type)
277 struct isl_token *tok;
279 tok = isl_stream_next_token(s);
280 if (!tok)
281 return -1;
282 if (tok->type == type) {
283 isl_token_free(tok);
284 return 0;
286 isl_stream_error(s, tok, "expecting other token");
287 isl_stream_push_token(s, tok);
288 return -1;
291 void isl_stream_free(struct isl_stream *s)
293 if (!s)
294 return;
295 free(s->buffer);
296 if (s->n_token != 0) {
297 struct isl_token *tok = isl_stream_next_token(s);
298 isl_stream_error(s, tok, "unexpected token");
299 isl_token_free(tok);
301 isl_ctx_deref(s->ctx);
302 free(s);