tokenizer: accept "and" keyword
[platform/upstream/isl.git] / isl_stream.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <ctype.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <isl_ctx.h>
14 #include "isl_stream.h"
15
16 static struct isl_token *isl_token_new(struct isl_ctx *ctx,
17         int line, int col, unsigned on_new_line)
18 {
19         struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
20         if (!tok)
21                 return NULL;
22         tok->line = line;
23         tok->col = col;
24         tok->on_new_line = on_new_line;
25         return tok;
26 }
27
28 void isl_token_free(struct isl_token *tok)
29 {
30         if (!tok)
31                 return;
32         if (tok->type == ISL_TOKEN_VALUE)
33                 isl_int_clear(tok->u.v);
34         else if (tok->type == ISL_TOKEN_IDENT)
35                 free(tok->u.s);
36         free(tok);
37 }
38
39 void isl_stream_error(struct isl_stream *s, struct isl_token *tok, char *msg)
40 {
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);
44         if (tok) {
45                 if (tok->type < 256)
46                         fprintf(stderr, "got '%c'\n", tok->type);
47                 else
48                         fprintf(stderr, "got token type %d\n", tok->type);
49         }
50 }
51
52 static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
53 {
54         int i;
55         struct isl_stream *s = isl_alloc_type(ctx, struct isl_stream);
56         if (!s)
57                 return NULL;
58         s->ctx = ctx;
59         isl_ctx_ref(s->ctx);
60         s->size = 256;
61         s->file = NULL;
62         s->str = NULL;
63         s->buffer = isl_alloc_array(ctx, char, s->size);
64         if (!s->buffer)
65                 goto error;
66         s->len = 0;
67         s->line = 1;
68         s->col = 0;
69         s->eof = 0;
70         s->c = -1;
71         for (i = 0; i < 5; ++i)
72                 s->tokens[i] = NULL;
73         s->n_token = 0;
74         return s;
75 error:
76         isl_stream_free(s);
77         return NULL;
78 }
79
80 struct isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
81 {
82         struct isl_stream *s = isl_stream_new(ctx);
83         if (!s)
84                 return NULL;
85         s->file = file;
86         return s;
87 }
88
89 struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
90 {
91     struct isl_stream *s = isl_stream_new(ctx);
92     s->str = str;
93     return s;
94 }
95
96 static int isl_stream_getc(struct isl_stream *s)
97 {
98         int c;
99         if (s->eof)
100                 return -1;
101         if (s->file)
102                 c = fgetc(s->file);
103         else {
104                 c = *s->str++;
105                 if (c == '\0')
106                         c = -1;
107         }
108         if (c == -1)
109                 s->eof = 1;
110         if (!s->eof) {
111                 if (s->c == '\n') {
112                         s->line++;
113                         s->col = 0;
114                 } else
115                         s->col++;
116         }
117         s->c = c;
118         return c;
119 }
120
121 static void isl_stream_ungetc(struct isl_stream *s, int c)
122 {
123         if (s->file)
124                 ungetc(c, s->file);
125         else
126                 --s->str;
127         s->c = -1;
128 }
129
130 static int isl_stream_push_char(struct isl_stream *s, int c)
131 {
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);
135                 if (!s->buffer)
136                         return -1;
137         }
138         s->buffer[s->len++] = c;
139         return 0;
140 }
141
142 void isl_stream_push_token(struct isl_stream *s, struct isl_token *tok)
143 {
144         isl_assert(s->ctx, s->n_token < 5, return);
145         s->tokens[s->n_token++] = tok;
146 }
147
148 struct isl_token *isl_stream_next_token(struct isl_stream *s)
149 {
150         int c;
151         struct isl_token *tok = NULL;
152         int line, col;
153         int old_line = s->line;
154
155         if (s->n_token)
156                 return s->tokens[--s->n_token];
157
158         s->len = 0;
159
160         /* skip spaces and comment lines */
161         while ((c = isl_stream_getc(s)) != -1) {
162                 if (c == '#') {
163                         while ((c = isl_stream_getc(s)) != -1 && c != '\n')
164                                 /* nothing */
165                                 ;
166                         if (c == -1)
167                                 break;
168                 } else if (!isspace(c))
169                         break;
170         }
171
172         line = s->line;
173         col = s->col;
174
175         if (c == -1)
176                 return NULL;
177         if (c == '(' ||
178             c == ')' ||
179             c == '+' ||
180             c == '/' ||
181             c == '*' ||
182             c == '^' ||
183             c == '=' ||
184             c == ',' ||
185             c == ':' ||
186             c == '[' ||
187             c == ']' ||
188             c == '{' ||
189             c == '}') {
190                 tok = isl_token_new(s->ctx, line, col, old_line != line);
191                 if (!tok)
192                         return NULL;
193                 tok->type = (enum isl_token_type)c;
194                 return tok;
195         }
196         if (c == '-') {
197                 int c;
198                 if ((c = isl_stream_getc(s)) == '>') {
199                         tok = isl_token_new(s->ctx, line, col, old_line != line);
200                         if (!tok)
201                                 return NULL;
202                         tok->type = ISL_TOKEN_TO;
203                         return tok;
204                 }
205                 if (c != -1)
206                         isl_stream_ungetc(s, c);
207         }
208         if (c == '-' || isdigit(c)) {
209                 tok = isl_token_new(s->ctx, line, col, old_line != line);
210                 if (!tok)
211                         return NULL;
212                 tok->type = ISL_TOKEN_VALUE;
213                 isl_int_init(tok->u.v);
214                 if (isl_stream_push_char(s, c))
215                         goto error;
216                 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
217                         if (isl_stream_push_char(s, c))
218                                 goto error;
219                 if (c != -1)
220                         isl_stream_ungetc(s, c);
221                 if (s->len == 1 && s->buffer[0] == '-')
222                         isl_int_set_si(tok->u.v, -1);
223                 else {
224                         isl_stream_push_char(s, '\0');
225                         isl_int_read(tok->u.v, s->buffer);
226                 }
227                 return tok;
228         }
229         if (isalpha(c)) {
230                 tok = isl_token_new(s->ctx, line, col, old_line != line);
231                 if (!tok)
232                         return NULL;
233                 isl_stream_push_char(s, c);
234                 while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
235                         isl_stream_push_char(s, c);
236                 if (c != -1)
237                         isl_stream_ungetc(s, c);
238                 isl_stream_push_char(s, '\0');
239                 if (!strcasecmp(s->buffer, "exists"))
240                         tok->type = ISL_TOKEN_EXISTS;
241                 else if (!strcasecmp(s->buffer, "and"))
242                         tok->type = ISL_TOKEN_AND;
243                 else {
244                         tok->type = ISL_TOKEN_IDENT;
245                         tok->u.s = strdup(s->buffer);
246                 }
247                 return tok;
248         }
249         if (c == '>') {
250                 int c;
251                 if ((c = isl_stream_getc(s)) == '=') {
252                         tok = isl_token_new(s->ctx, line, col, old_line != line);
253                         if (!tok)
254                                 return NULL;
255                         tok->type = ISL_TOKEN_GE;
256                         return tok;
257                 }
258                 if (c != -1)
259                         isl_stream_ungetc(s, c);
260         }
261         if (c == '<') {
262                 int c;
263                 if ((c = isl_stream_getc(s)) == '=') {
264                         tok = isl_token_new(s->ctx, line, col, old_line != line);
265                         if (!tok)
266                                 return NULL;
267                         tok->type = ISL_TOKEN_LE;
268                         return tok;
269                 }
270                 if (c != -1)
271                         isl_stream_ungetc(s, c);
272         }
273         if (c == '&') {
274                 tok = isl_token_new(s->ctx, line, col, old_line != line);
275                 if (!tok)
276                         return NULL;
277                 tok->type = ISL_TOKEN_AND;
278                 if ((c = isl_stream_getc(s)) != '&' && c != -1)
279                         isl_stream_ungetc(s, c);
280                 return tok;
281         }
282
283         tok = isl_token_new(s->ctx, line, col, old_line != line);
284         if (!tok)
285                 return NULL;
286         tok->type = ISL_TOKEN_UNKNOWN;
287         return tok;
288 error:
289         isl_token_free(tok);
290         return NULL;
291 }
292
293 int isl_stream_eat(struct isl_stream *s, int type)
294 {
295         struct isl_token *tok;
296
297         tok = isl_stream_next_token(s);
298         if (!tok)
299                 return -1;
300         if (tok->type == type) {
301                 isl_token_free(tok);
302                 return 0;
303         }
304         isl_stream_error(s, tok, "expecting other token");
305         isl_stream_push_token(s, tok);
306         return -1;
307 }
308
309 void isl_stream_free(struct isl_stream *s)
310 {
311         if (!s)
312                 return;
313         free(s->buffer);
314         if (s->n_token != 0) {
315                 struct isl_token *tok = isl_stream_next_token(s);
316                 isl_stream_error(s, tok, "unexpected token");
317                 isl_token_free(tok);
318         }
319         isl_ctx_deref(s->ctx);
320         free(s);
321 }