isl_stream.c: extracted from isl_input_omega.c
[platform/upstream/isl.git] / isl_stream.c
1 #include <ctype.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <isl_ctx.h>
5 #include "isl_stream.h"
6
7 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
8         int line, int col, unsigned on_new_line)
9 {
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;
17 }
18
19 void isl_token_free(struct isl_token *tok)
20 {
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);
28 }
29
30 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
31 {
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);
40         }
41 }
42
43 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
44 {
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         s->size = 256;
51         s->file = NULL;
52         s->str = NULL;
53         s->buffer = isl_alloc_array(ctx, char, s->size);
54         if (!s->buffer)
55                 goto error;
56         s->len = 0;
57         s->line = 1;
58         s->col = 0;
59         s->eof = 0;
60         s->c = -1;
61         for (i = 0; i < 5; ++i)
62                 s->tokens[i] = NULL;
63         s->n_token = 0;
64         return s;
65 error:
66         isl_stream_free(s);
67         return NULL;
68 }
69
70 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
71 {
72         struct isl_stream *s = isl_stream_new(ctx);
73         if (!s)
74                 return NULL;
75         s->file = file;
76         return s;
77 }
78
79 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
80 {
81     struct isl_stream *s = isl_stream_new(ctx);
82     s->str = str;
83     return s;
84 }
85
86 static int isl_stream_getc(struct isl_stream *s)
87 {
88         int c;
89         if (s->eof)
90                 return -1;
91         if (s->file)
92                 c = fgetc(s->file);
93         else {
94                 c = *s->str++;
95                 if (c == '\0')
96                         c = -1;
97         }
98         if (c == -1)
99                 s->eof = 1;
100         if (!s->eof) {
101                 if (s->c == '\n') {
102                         s->line++;
103                         s->col = 0;
104                 } else
105                         s->col++;
106         }
107         s->c = c;
108         return c;
109 }
110
111 static void isl_stream_ungetc(struct isl_stream *s, int c)
112 {
113         if (s->file)
114                 ungetc(c, s->file);
115         else
116                 --s->str;
117         s->c = -1;
118 }
119
120 static int isl_stream_push_char(struct isl_stream *s, int c)
121 {
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);
125                 if (!s->buffer)
126                         return -1;
127         }
128         s->buffer[s->len++] = c;
129         return 0;
130 }
131
132 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
133 {
134         isl_assert(s->ctx, s->n_token < 5, return);
135         s->tokens[s->n_token++] = tok;
136 }
137
138 struct isl_token *isl_stream_next_token(struct isl_stream *s)
139 {
140         int c;
141         struct isl_token *tok = NULL;
142         int line, col;
143         int old_line = s->line;
144
145         if (s->n_token)
146                 return s->tokens[--s->n_token];
147
148         s->len = 0;
149
150         /* skip spaces */
151         while ((c = isl_stream_getc(s)) != -1 && isspace(c))
152                 /* nothing */
153                 ;
154
155         line = s->line;
156         col = s->col;
157
158         if (c == -1)
159                 return NULL;
160         if (c == '(' ||
161             c == ')' ||
162             c == '+' ||
163             c == '/' ||
164             c == '*' ||
165             c == '^' ||
166             c == '=' ||
167             c == ',' ||
168             c == ':' ||
169             c == '[' ||
170             c == ']' ||
171             c == '{' ||
172             c == '}') {
173                 tok = isl_token_new(s->ctx, line, col, old_line != line);
174                 if (!tok)
175                         return NULL;
176                 tok->type = (enum isl_token_type)c;
177                 return tok;
178         }
179         if (c == '-') {
180                 int c;
181                 if ((c = isl_stream_getc(s)) == '>') {
182                         tok = isl_token_new(s->ctx, line, col, old_line != line);
183                         if (!tok)
184                                 return NULL;
185                         tok->type = ISL_TOKEN_TO;
186                         return tok;
187                 }
188                 if (c != -1)
189                         isl_stream_ungetc(s, c);
190         }
191         if (c == '-' || isdigit(c)) {
192                 tok = isl_token_new(s->ctx, line, col, old_line != line);
193                 if (!tok)
194                         return NULL;
195                 tok->type = ISL_TOKEN_VALUE;
196                 isl_int_init(tok->u.v);
197                 if (isl_stream_push_char(s, c))
198                         goto error;
199                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
200                         if (isl_stream_push_char(s, c))
201                                 goto error;
202                 if (c != -1)
203                         isl_stream_ungetc(s, c);
204                 if (s->len == 1 && s->buffer[0] == '-')
205                         isl_int_set_si(tok->u.v, -1);
206                 else {
207                         isl_stream_push_char(s, '\0');
208                         isl_int_read(tok->u.v, s->buffer);
209                 }
210                 return tok;
211         }
212         if (isalpha(c)) {
213                 tok = isl_token_new(s->ctx, line, col, old_line != line);
214                 if (!tok)
215                         return NULL;
216                 isl_stream_push_char(s, c);
217                 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
218                         isl_stream_push_char(s, c);
219                 if (c != -1)
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;
224                 else {
225                         tok->type = ISL_TOKEN_IDENT;
226                         tok->u.s = strdup(s->buffer);
227                 }
228                 return tok;
229         }
230         if (c == '>') {
231                 int c;
232                 if ((c = isl_stream_getc(s)) == '=') {
233                         tok = isl_token_new(s->ctx, line, col, old_line != line);
234                         if (!tok)
235                                 return NULL;
236                         tok->type = ISL_TOKEN_GE;
237                         return tok;
238                 }
239                 if (c != -1)
240                         isl_stream_ungetc(s, c);
241         }
242         if (c == '<') {
243                 int c;
244                 if ((c = isl_stream_getc(s)) == '=') {
245                         tok = isl_token_new(s->ctx, line, col, old_line != line);
246                         if (!tok)
247                                 return NULL;
248                         tok->type = ISL_TOKEN_LE;
249                         return tok;
250                 }
251                 if (c != -1)
252                         isl_stream_ungetc(s, c);
253         }
254         if (c == '&') {
255                 tok = isl_token_new(s->ctx, line, col, old_line != line);
256                 if (!tok)
257                         return NULL;
258                 tok->type = ISL_TOKEN_AND;
259                 if ((c = isl_stream_getc(s)) != '&' && c != -1)
260                         isl_stream_ungetc(s, c);
261                 return tok;
262         }
263
264         tok = isl_token_new(s->ctx, line, col, old_line != line);
265         if (!tok)
266                 return NULL;
267         tok->type = ISL_TOKEN_UNKNOWN;
268         return tok;
269 error:
270         isl_token_free(tok);
271         return NULL;
272 }
273
274 int isl_stream_eat(struct isl_stream *s, int type)
275 {
276         struct isl_token *tok;
277
278         tok = isl_stream_next_token(s);
279         if (!tok)
280                 return -1;
281         if (tok->type == type) {
282                 isl_token_free(tok);
283                 return 0;
284         }
285         isl_stream_error(s, tok, "expecting other token");
286         isl_stream_push_token(s, tok);
287         return -1;
288 }
289
290 void isl_stream_free(struct isl_stream *s)
291 {
292         if (!s)
293                 return;
294         free(s->buffer);
295         if (s->n_token != 0) {
296                 struct isl_token *tok = isl_stream_next_token(s);
297                 isl_stream_error(s, tok, "unexpected token");
298                 isl_token_free(tok);
299         }
300         free(s);
301 }