add isl_schedule_node_foreach_ancestor_top_down
[isl.git] / isl_stream.c
blob0957751fe43097952d7e59bc1d570ad8f9a7a75e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <ctype.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <isl/ctx.h>
14 #include <isl_stream_private.h>
15 #include <isl/map.h>
16 #include <isl/aff.h>
17 #include <isl_val_private.h>
19 struct isl_keyword {
20 char *name;
21 enum isl_token_type type;
24 static int same_name(const void *entry, const void *val)
26 const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
28 return !strcmp(keyword->name, val);
31 enum isl_token_type isl_stream_register_keyword(__isl_keep isl_stream *s,
32 const char *name)
34 struct isl_hash_table_entry *entry;
35 struct isl_keyword *keyword;
36 uint32_t name_hash;
38 if (!s->keywords) {
39 s->keywords = isl_hash_table_alloc(s->ctx, 10);
40 if (!s->keywords)
41 return ISL_TOKEN_ERROR;
42 s->next_type = ISL_TOKEN_LAST;
45 name_hash = isl_hash_string(isl_hash_init(), name);
47 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
48 same_name, name, 1);
49 if (!entry)
50 return ISL_TOKEN_ERROR;
51 if (entry->data) {
52 keyword = entry->data;
53 return keyword->type;
56 keyword = isl_calloc_type(s->ctx, struct isl_keyword);
57 if (!keyword)
58 return ISL_TOKEN_ERROR;
59 keyword->type = s->next_type++;
60 keyword->name = strdup(name);
61 if (!keyword->name) {
62 free(keyword);
63 return ISL_TOKEN_ERROR;
65 entry->data = keyword;
67 return keyword->type;
70 struct isl_token *isl_token_new(isl_ctx *ctx,
71 int line, int col, unsigned on_new_line)
73 struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
74 if (!tok)
75 return NULL;
76 tok->line = line;
77 tok->col = col;
78 tok->on_new_line = on_new_line;
79 tok->is_keyword = 0;
80 tok->u.s = NULL;
81 return tok;
84 /* Return the type of "tok".
86 int isl_token_get_type(struct isl_token *tok)
88 return tok ? tok->type : ISL_TOKEN_ERROR;
91 /* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
93 __isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok)
95 if (!tok)
96 return NULL;
97 if (tok->type != ISL_TOKEN_VALUE)
98 isl_die(ctx, isl_error_invalid, "not a value token",
99 return NULL);
101 return isl_val_int_from_isl_int(ctx, tok->u.v);
104 /* Given a token with a string representation, return a copy of this string.
106 __isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok)
108 if (!tok)
109 return NULL;
110 if (!tok->u.s)
111 isl_die(ctx, isl_error_invalid,
112 "token does not have a string representation",
113 return NULL);
115 return strdup(tok->u.s);
118 void isl_token_free(struct isl_token *tok)
120 if (!tok)
121 return;
122 if (tok->type == ISL_TOKEN_VALUE)
123 isl_int_clear(tok->u.v);
124 else if (tok->type == ISL_TOKEN_MAP)
125 isl_map_free(tok->u.map);
126 else if (tok->type == ISL_TOKEN_AFF)
127 isl_pw_aff_free(tok->u.pwaff);
128 else
129 free(tok->u.s);
130 free(tok);
133 void isl_stream_error(__isl_keep isl_stream *s, struct isl_token *tok,
134 char *msg)
136 int line = tok ? tok->line : s->line;
137 int col = tok ? tok->col : s->col;
138 fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
139 if (tok) {
140 if (tok->type < 256)
141 fprintf(stderr, "got '%c'\n", tok->type);
142 else if (tok->type == ISL_TOKEN_IDENT)
143 fprintf(stderr, "got ident '%s'\n", tok->u.s);
144 else if (tok->is_keyword)
145 fprintf(stderr, "got keyword '%s'\n", tok->u.s);
146 else if (tok->type == ISL_TOKEN_VALUE) {
147 fprintf(stderr, "got value '");
148 isl_int_print(stderr, tok->u.v, 0);
149 fprintf(stderr, "'\n");
150 } else if (tok->type == ISL_TOKEN_MAP) {
151 isl_printer *p;
152 fprintf(stderr, "got map '");
153 p = isl_printer_to_file(s->ctx, stderr);
154 p = isl_printer_print_map(p, tok->u.map);
155 isl_printer_free(p);
156 fprintf(stderr, "'\n");
157 } else if (tok->type == ISL_TOKEN_AFF) {
158 isl_printer *p;
159 fprintf(stderr, "got affine expression '");
160 p = isl_printer_to_file(s->ctx, stderr);
161 p = isl_printer_print_pw_aff(p, tok->u.pwaff);
162 isl_printer_free(p);
163 fprintf(stderr, "'\n");
164 } else if (tok->u.s)
165 fprintf(stderr, "got token '%s'\n", tok->u.s);
166 else
167 fprintf(stderr, "got token type %d\n", tok->type);
171 static __isl_give isl_stream* isl_stream_new(struct isl_ctx *ctx)
173 int i;
174 isl_stream *s = isl_calloc_type(ctx, struct isl_stream);
175 if (!s)
176 return NULL;
177 s->ctx = ctx;
178 isl_ctx_ref(s->ctx);
179 s->file = NULL;
180 s->str = NULL;
181 s->len = 0;
182 s->line = 1;
183 s->col = 1;
184 s->eof = 0;
185 s->last_line = 0;
186 s->c = -1;
187 s->n_un = 0;
188 for (i = 0; i < 5; ++i)
189 s->tokens[i] = NULL;
190 s->n_token = 0;
191 s->keywords = NULL;
192 s->size = 256;
193 s->buffer = isl_alloc_array(ctx, char, s->size);
194 if (!s->buffer)
195 goto error;
196 return s;
197 error:
198 isl_stream_free(s);
199 return NULL;
202 __isl_give isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
204 isl_stream *s = isl_stream_new(ctx);
205 if (!s)
206 return NULL;
207 s->file = file;
208 return s;
211 __isl_give isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
213 isl_stream *s;
214 if (!str)
215 return NULL;
216 s = isl_stream_new(ctx);
217 if (!s)
218 return NULL;
219 s->str = str;
220 return s;
223 /* Read a character from the stream and advance s->line and s->col
224 * to point to the next character.
226 static int stream_getc(__isl_keep isl_stream *s)
228 int c;
229 if (s->eof)
230 return -1;
231 if (s->n_un)
232 return s->c = s->un[--s->n_un];
233 if (s->file)
234 c = fgetc(s->file);
235 else {
236 c = *s->str++;
237 if (c == '\0')
238 c = -1;
240 if (c == -1)
241 s->eof = 1;
242 else if (c == '\n') {
243 s->line++;
244 s->col = 1;
245 } else
246 s->col++;
247 s->c = c;
248 return c;
251 static void isl_stream_ungetc(__isl_keep isl_stream *s, int c)
253 isl_assert(s->ctx, s->n_un < 5, return);
254 s->un[s->n_un++] = c;
255 s->c = -1;
258 /* Read a character from the stream, skipping pairs of '\\' and '\n'.
259 * Set s->start_line and s->start_col to the line and column
260 * of the returned character.
262 static int isl_stream_getc(__isl_keep isl_stream *s)
264 int c;
266 do {
267 s->start_line = s->line;
268 s->start_col = s->col;
269 c = stream_getc(s);
270 if (c != '\\')
271 return c;
272 c = stream_getc(s);
273 } while (c == '\n');
275 isl_stream_ungetc(s, c);
277 return '\\';
280 static int isl_stream_push_char(__isl_keep isl_stream *s, int c)
282 if (s->len >= s->size) {
283 char *buffer;
284 s->size = (3*s->size)/2;
285 buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
286 if (!buffer)
287 return -1;
288 s->buffer = buffer;
290 s->buffer[s->len++] = c;
291 return 0;
294 void isl_stream_push_token(__isl_keep isl_stream *s, struct isl_token *tok)
296 isl_assert(s->ctx, s->n_token < 5, return);
297 s->tokens[s->n_token++] = tok;
300 static enum isl_token_type check_keywords(__isl_keep isl_stream *s)
302 struct isl_hash_table_entry *entry;
303 struct isl_keyword *keyword;
304 uint32_t name_hash;
306 if (!strcasecmp(s->buffer, "exists"))
307 return ISL_TOKEN_EXISTS;
308 if (!strcasecmp(s->buffer, "and"))
309 return ISL_TOKEN_AND;
310 if (!strcasecmp(s->buffer, "or"))
311 return ISL_TOKEN_OR;
312 if (!strcasecmp(s->buffer, "implies"))
313 return ISL_TOKEN_IMPLIES;
314 if (!strcasecmp(s->buffer, "not"))
315 return ISL_TOKEN_NOT;
316 if (!strcasecmp(s->buffer, "infty"))
317 return ISL_TOKEN_INFTY;
318 if (!strcasecmp(s->buffer, "infinity"))
319 return ISL_TOKEN_INFTY;
320 if (!strcasecmp(s->buffer, "NaN"))
321 return ISL_TOKEN_NAN;
322 if (!strcasecmp(s->buffer, "min"))
323 return ISL_TOKEN_MIN;
324 if (!strcasecmp(s->buffer, "max"))
325 return ISL_TOKEN_MAX;
326 if (!strcasecmp(s->buffer, "rat"))
327 return ISL_TOKEN_RAT;
328 if (!strcasecmp(s->buffer, "true"))
329 return ISL_TOKEN_TRUE;
330 if (!strcasecmp(s->buffer, "false"))
331 return ISL_TOKEN_FALSE;
332 if (!strcasecmp(s->buffer, "ceild"))
333 return ISL_TOKEN_CEILD;
334 if (!strcasecmp(s->buffer, "floord"))
335 return ISL_TOKEN_FLOORD;
336 if (!strcasecmp(s->buffer, "mod"))
337 return ISL_TOKEN_MOD;
338 if (!strcasecmp(s->buffer, "ceil"))
339 return ISL_TOKEN_CEIL;
340 if (!strcasecmp(s->buffer, "floor"))
341 return ISL_TOKEN_FLOOR;
343 if (!s->keywords)
344 return ISL_TOKEN_IDENT;
346 name_hash = isl_hash_string(isl_hash_init(), s->buffer);
347 entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
348 s->buffer, 0);
349 if (entry) {
350 keyword = entry->data;
351 return keyword->type;
354 return ISL_TOKEN_IDENT;
357 int isl_stream_skip_line(__isl_keep isl_stream *s)
359 int c;
361 while ((c = isl_stream_getc(s)) != -1 && c != '\n')
362 /* nothing */
365 return c == -1 ? -1 : 0;
368 static struct isl_token *next_token(__isl_keep isl_stream *s, int same_line)
370 int c;
371 struct isl_token *tok = NULL;
372 int line, col;
373 int old_line = s->last_line;
375 if (s->n_token) {
376 if (same_line && s->tokens[s->n_token - 1]->on_new_line)
377 return NULL;
378 return s->tokens[--s->n_token];
381 if (same_line && s->c == '\n')
382 return NULL;
384 s->len = 0;
386 /* skip spaces and comment lines */
387 while ((c = isl_stream_getc(s)) != -1) {
388 if (c == '#') {
389 if (isl_stream_skip_line(s) < 0)
390 break;
391 c = '\n';
392 if (same_line)
393 break;
394 } else if (!isspace(c) || (same_line && c == '\n'))
395 break;
398 line = s->start_line;
399 col = s->start_col;
401 if (c == -1 || (same_line && c == '\n'))
402 return NULL;
403 s->last_line = line;
405 if (c == '(' ||
406 c == ')' ||
407 c == '+' ||
408 c == '*' ||
409 c == '%' ||
410 c == '?' ||
411 c == '^' ||
412 c == '@' ||
413 c == '$' ||
414 c == ',' ||
415 c == '.' ||
416 c == ';' ||
417 c == '[' ||
418 c == ']' ||
419 c == '{' ||
420 c == '}') {
421 tok = isl_token_new(s->ctx, line, col, old_line != line);
422 if (!tok)
423 return NULL;
424 tok->type = (enum isl_token_type)c;
425 return tok;
427 if (c == '-') {
428 int c;
429 if ((c = isl_stream_getc(s)) == '>') {
430 tok = isl_token_new(s->ctx, line, col, old_line != line);
431 if (!tok)
432 return NULL;
433 tok->u.s = strdup("->");
434 tok->type = ISL_TOKEN_TO;
435 return tok;
437 if (c != -1)
438 isl_stream_ungetc(s, c);
439 if (!isdigit(c)) {
440 tok = isl_token_new(s->ctx, line, col, old_line != line);
441 if (!tok)
442 return NULL;
443 tok->type = (enum isl_token_type) '-';
444 return tok;
447 if (c == '-' || isdigit(c)) {
448 int minus = c == '-';
449 tok = isl_token_new(s->ctx, line, col, old_line != line);
450 if (!tok)
451 return NULL;
452 tok->type = ISL_TOKEN_VALUE;
453 isl_int_init(tok->u.v);
454 if (isl_stream_push_char(s, c))
455 goto error;
456 while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
457 if (isl_stream_push_char(s, c))
458 goto error;
459 if (c != -1)
460 isl_stream_ungetc(s, c);
461 isl_stream_push_char(s, '\0');
462 isl_int_read(tok->u.v, s->buffer);
463 if (minus && isl_int_is_zero(tok->u.v)) {
464 tok->col++;
465 tok->on_new_line = 0;
466 isl_stream_push_token(s, tok);
467 tok = isl_token_new(s->ctx, line, col, old_line != line);
468 if (!tok)
469 return NULL;
470 tok->type = (enum isl_token_type) '-';
472 return tok;
474 if (isalpha(c) || c == '_') {
475 tok = isl_token_new(s->ctx, line, col, old_line != line);
476 if (!tok)
477 return NULL;
478 isl_stream_push_char(s, c);
479 while ((c = isl_stream_getc(s)) != -1 &&
480 (isalnum(c) || c == '_'))
481 isl_stream_push_char(s, c);
482 if (c != -1)
483 isl_stream_ungetc(s, c);
484 while ((c = isl_stream_getc(s)) != -1 && c == '\'')
485 isl_stream_push_char(s, c);
486 if (c != -1)
487 isl_stream_ungetc(s, c);
488 isl_stream_push_char(s, '\0');
489 tok->type = check_keywords(s);
490 if (tok->type != ISL_TOKEN_IDENT)
491 tok->is_keyword = 1;
492 tok->u.s = strdup(s->buffer);
493 if (!tok->u.s)
494 goto error;
495 return tok;
497 if (c == '"') {
498 tok = isl_token_new(s->ctx, line, col, old_line != line);
499 if (!tok)
500 return NULL;
501 tok->type = ISL_TOKEN_STRING;
502 tok->u.s = NULL;
503 while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
504 isl_stream_push_char(s, c);
505 if (c != '"') {
506 isl_stream_error(s, NULL, "unterminated string");
507 goto error;
509 isl_stream_push_char(s, '\0');
510 tok->u.s = strdup(s->buffer);
511 return tok;
513 if (c == '=') {
514 int c;
515 tok = isl_token_new(s->ctx, line, col, old_line != line);
516 if (!tok)
517 return NULL;
518 if ((c = isl_stream_getc(s)) == '=') {
519 tok->u.s = strdup("==");
520 tok->type = ISL_TOKEN_EQ_EQ;
521 return tok;
523 if (c != -1)
524 isl_stream_ungetc(s, c);
525 tok->type = (enum isl_token_type) '=';
526 return tok;
528 if (c == ':') {
529 int c;
530 tok = isl_token_new(s->ctx, line, col, old_line != line);
531 if (!tok)
532 return NULL;
533 if ((c = isl_stream_getc(s)) == '=') {
534 tok->u.s = strdup(":=");
535 tok->type = ISL_TOKEN_DEF;
536 return tok;
538 if (c != -1)
539 isl_stream_ungetc(s, c);
540 tok->type = (enum isl_token_type) ':';
541 return tok;
543 if (c == '>') {
544 int c;
545 tok = isl_token_new(s->ctx, line, col, old_line != line);
546 if (!tok)
547 return NULL;
548 if ((c = isl_stream_getc(s)) == '=') {
549 tok->u.s = strdup(">=");
550 tok->type = ISL_TOKEN_GE;
551 return tok;
552 } else if (c == '>') {
553 if ((c = isl_stream_getc(s)) == '=') {
554 tok->u.s = strdup(">>=");
555 tok->type = ISL_TOKEN_LEX_GE;
556 return tok;
558 tok->u.s = strdup(">>");
559 tok->type = ISL_TOKEN_LEX_GT;
560 } else {
561 tok->u.s = strdup(">");
562 tok->type = ISL_TOKEN_GT;
564 if (c != -1)
565 isl_stream_ungetc(s, c);
566 return tok;
568 if (c == '<') {
569 int c;
570 tok = isl_token_new(s->ctx, line, col, old_line != line);
571 if (!tok)
572 return NULL;
573 if ((c = isl_stream_getc(s)) == '=') {
574 tok->u.s = strdup("<=");
575 tok->type = ISL_TOKEN_LE;
576 return tok;
577 } else if (c == '<') {
578 if ((c = isl_stream_getc(s)) == '=') {
579 tok->u.s = strdup("<<=");
580 tok->type = ISL_TOKEN_LEX_LE;
581 return tok;
583 tok->u.s = strdup("<<");
584 tok->type = ISL_TOKEN_LEX_LT;
585 } else {
586 tok->u.s = strdup("<");
587 tok->type = ISL_TOKEN_LT;
589 if (c != -1)
590 isl_stream_ungetc(s, c);
591 return tok;
593 if (c == '&') {
594 tok = isl_token_new(s->ctx, line, col, old_line != line);
595 if (!tok)
596 return NULL;
597 tok->type = ISL_TOKEN_AND;
598 if ((c = isl_stream_getc(s)) != '&' && c != -1) {
599 tok->u.s = strdup("&");
600 isl_stream_ungetc(s, c);
601 } else
602 tok->u.s = strdup("&&");
603 return tok;
605 if (c == '|') {
606 tok = isl_token_new(s->ctx, line, col, old_line != line);
607 if (!tok)
608 return NULL;
609 tok->type = ISL_TOKEN_OR;
610 if ((c = isl_stream_getc(s)) != '|' && c != -1) {
611 tok->u.s = strdup("|");
612 isl_stream_ungetc(s, c);
613 } else
614 tok->u.s = strdup("||");
615 return tok;
617 if (c == '/') {
618 tok = isl_token_new(s->ctx, line, col, old_line != line);
619 if (!tok)
620 return NULL;
621 if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
622 tok->type = (enum isl_token_type) '/';
623 isl_stream_ungetc(s, c);
624 } else {
625 tok->u.s = strdup("/\\");
626 tok->type = ISL_TOKEN_AND;
628 return tok;
630 if (c == '\\') {
631 tok = isl_token_new(s->ctx, line, col, old_line != line);
632 if (!tok)
633 return NULL;
634 if ((c = isl_stream_getc(s)) != '/' && c != -1) {
635 tok->type = (enum isl_token_type) '\\';
636 isl_stream_ungetc(s, c);
637 } else {
638 tok->u.s = strdup("\\/");
639 tok->type = ISL_TOKEN_OR;
641 return tok;
643 if (c == '!') {
644 tok = isl_token_new(s->ctx, line, col, old_line != line);
645 if (!tok)
646 return NULL;
647 if ((c = isl_stream_getc(s)) == '=') {
648 tok->u.s = strdup("!=");
649 tok->type = ISL_TOKEN_NE;
650 return tok;
651 } else {
652 tok->type = ISL_TOKEN_NOT;
653 tok->u.s = strdup("!");
655 if (c != -1)
656 isl_stream_ungetc(s, c);
657 return tok;
660 tok = isl_token_new(s->ctx, line, col, old_line != line);
661 if (!tok)
662 return NULL;
663 tok->type = ISL_TOKEN_UNKNOWN;
664 return tok;
665 error:
666 isl_token_free(tok);
667 return NULL;
670 struct isl_token *isl_stream_next_token(__isl_keep isl_stream *s)
672 return next_token(s, 0);
675 struct isl_token *isl_stream_next_token_on_same_line(__isl_keep isl_stream *s)
677 return next_token(s, 1);
680 int isl_stream_eat_if_available(__isl_keep isl_stream *s, int type)
682 struct isl_token *tok;
684 tok = isl_stream_next_token(s);
685 if (!tok)
686 return 0;
687 if (tok->type == type) {
688 isl_token_free(tok);
689 return 1;
691 isl_stream_push_token(s, tok);
692 return 0;
695 int isl_stream_next_token_is(__isl_keep isl_stream *s, int type)
697 struct isl_token *tok;
698 int r;
700 tok = isl_stream_next_token(s);
701 if (!tok)
702 return 0;
703 r = tok->type == type;
704 isl_stream_push_token(s, tok);
705 return r;
708 char *isl_stream_read_ident_if_available(__isl_keep isl_stream *s)
710 struct isl_token *tok;
712 tok = isl_stream_next_token(s);
713 if (!tok)
714 return NULL;
715 if (tok->type == ISL_TOKEN_IDENT) {
716 char *ident = strdup(tok->u.s);
717 isl_token_free(tok);
718 return ident;
720 isl_stream_push_token(s, tok);
721 return NULL;
724 int isl_stream_eat(__isl_keep isl_stream *s, int type)
726 struct isl_token *tok;
728 tok = isl_stream_next_token(s);
729 if (!tok)
730 return -1;
731 if (tok->type == type) {
732 isl_token_free(tok);
733 return 0;
735 isl_stream_error(s, tok, "expecting other token");
736 isl_stream_push_token(s, tok);
737 return -1;
740 int isl_stream_is_empty(__isl_keep isl_stream *s)
742 struct isl_token *tok;
744 tok = isl_stream_next_token(s);
746 if (!tok)
747 return 1;
749 isl_stream_push_token(s, tok);
750 return 0;
753 static int free_keyword(void **p, void *user)
755 struct isl_keyword *keyword = *p;
757 free(keyword->name);
758 free(keyword);
760 return 0;
763 void isl_stream_flush_tokens(__isl_keep isl_stream *s)
765 int i;
767 if (!s)
768 return;
769 for (i = 0; i < s->n_token; ++i)
770 isl_token_free(s->tokens[i]);
771 s->n_token = 0;
774 isl_ctx *isl_stream_get_ctx(__isl_keep isl_stream *s)
776 return s ? s->ctx : NULL;
779 void isl_stream_free(__isl_take isl_stream *s)
781 if (!s)
782 return;
783 free(s->buffer);
784 if (s->n_token != 0) {
785 struct isl_token *tok = isl_stream_next_token(s);
786 isl_stream_error(s, tok, "unexpected token");
787 isl_token_free(tok);
789 if (s->keywords) {
790 isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
791 isl_hash_table_free(s->ctx, s->keywords);
793 free(s->yaml_state);
794 free(s->yaml_indent);
795 isl_ctx_deref(s->ctx);
796 free(s);
799 /* Push "state" onto the stack of currently active YAML elements.
800 * The caller is responsible for setting the corresponding indentation.
801 * Return 0 on success and -1 on failure.
803 static int push_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
805 if (s->yaml_size < s->yaml_depth + 1) {
806 int *indent;
807 enum isl_yaml_state *state;
809 state = isl_realloc_array(s->ctx, s->yaml_state,
810 enum isl_yaml_state, s->yaml_depth + 1);
811 if (!state)
812 return -1;
813 s->yaml_state = state;
815 indent = isl_realloc_array(s->ctx, s->yaml_indent,
816 int, s->yaml_depth + 1);
817 if (!indent)
818 return -1;
819 s->yaml_indent = indent;
821 s->yaml_size = s->yaml_depth + 1;
824 s->yaml_state[s->yaml_depth] = state;
825 s->yaml_depth++;
827 return 0;
830 /* Remove the innermost active YAML element from the stack.
831 * Return 0 on success and -1 on failure.
833 static int pop_state(__isl_keep isl_stream *s)
835 if (!s)
836 return -1;
837 if (s->yaml_depth < 1)
838 isl_die(isl_stream_get_ctx(s), isl_error_invalid,
839 "not in YAML construct", return -1);
841 s->yaml_depth--;
843 return 0;
846 /* Set the state of the innermost active YAML element to "state".
847 * Return 0 on success and -1 on failure.
849 static int update_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
851 if (!s)
852 return -1;
853 if (s->yaml_depth < 1)
854 isl_die(isl_stream_get_ctx(s), isl_error_invalid,
855 "not in YAML construct", return -1);
857 s->yaml_state[s->yaml_depth - 1] = state;
859 return 0;
862 /* Return the state of the innermost active YAML element.
863 * Return isl_yaml_none if we are not inside any YAML element.
865 static enum isl_yaml_state current_state(__isl_keep isl_stream *s)
867 if (!s)
868 return isl_yaml_none;
869 if (s->yaml_depth < 1)
870 return isl_yaml_none;
871 return s->yaml_state[s->yaml_depth - 1];
874 /* Set the indentation of the innermost active YAML element to "indent".
875 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
876 * that the current elemient is in flow format.
878 static int set_yaml_indent(__isl_keep isl_stream *s, int indent)
880 if (s->yaml_depth < 1)
881 isl_die(s->ctx, isl_error_internal,
882 "not in YAML element", return -1);
884 s->yaml_indent[s->yaml_depth - 1] = indent;
886 return 0;
889 /* Return the indentation of the innermost active YAML element
890 * of -1 on error.
892 static int get_yaml_indent(__isl_keep isl_stream *s)
894 if (s->yaml_depth < 1)
895 isl_die(s->ctx, isl_error_internal,
896 "not in YAML element", return -1);
898 return s->yaml_indent[s->yaml_depth - 1];
901 /* Move to the next state at the innermost level.
902 * Return 1 if successful.
903 * Return 0 if we are at the end of the innermost level.
904 * Return -1 on error.
906 * If we are in state isl_yaml_mapping_key_start, then we have just
907 * started a mapping and we are expecting a key. If the mapping started
908 * with a '{', then we check if the next token is a '}'. If so,
909 * then the mapping is empty and there is no next state at this level.
910 * Otherwise, we assume that there is at least one key (the one from
911 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
913 * If we are in state isl_yaml_mapping_key, then the we expect a colon
914 * followed by a value, so there is always a next state unless
915 * some error occurs.
917 * If we are in state isl_yaml_mapping_val, then there may or may
918 * not be a subsequent key in the same mapping.
919 * In flow format, the next key is preceded by a comma.
920 * In block format, the next key has the same indentation as the first key.
921 * If the first token has a smaller indentation, then we have reached
922 * the end of the current mapping.
924 * If we are in state isl_yaml_sequence_start, then we have just
925 * started a sequence. If the sequence started with a '[',
926 * then we check if the next token is a ']'. If so, then the sequence
927 * is empty and there is no next state at this level.
928 * Otherwise, we assume that there is at least one element in the sequence
929 * (the one from which we derived the indentation in
930 * isl_stream_yaml_read_start_sequence.
932 * If we are in state isl_yaml_sequence, then there may or may
933 * not be a subsequent element in the same sequence.
934 * In flow format, the next element is preceded by a comma.
935 * In block format, the next element is introduced by a dash with
936 * the same indentation as that of the first element.
937 * If the first token is not a dash or if it has a smaller indentation,
938 * then we have reached the end of the current sequence.
940 int isl_stream_yaml_next(__isl_keep isl_stream *s)
942 struct isl_token *tok;
943 enum isl_yaml_state state;
944 int indent;
946 state = current_state(s);
947 if (state == isl_yaml_none)
948 isl_die(s->ctx, isl_error_invalid,
949 "not in YAML element", return -1);
950 switch (state) {
951 case isl_yaml_mapping_key_start:
952 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW &&
953 isl_stream_next_token_is(s, '}'))
954 return 0;
955 if (update_state(s, isl_yaml_mapping_key) < 0)
956 return -1;
957 return 1;
958 case isl_yaml_mapping_key:
959 tok = isl_stream_next_token(s);
960 if (!tok) {
961 if (s->eof)
962 isl_stream_error(s, NULL, "unexpected EOF");
963 return -1;
965 if (tok->type == ':') {
966 isl_token_free(tok);
967 if (update_state(s, isl_yaml_mapping_val) < 0)
968 return -1;
969 return 1;
971 isl_stream_error(s, tok, "expecting ':'");
972 isl_stream_push_token(s, tok);
973 return -1;
974 case isl_yaml_mapping_val:
975 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
976 if (!isl_stream_eat_if_available(s, ','))
977 return 0;
978 if (update_state(s, isl_yaml_mapping_key) < 0)
979 return -1;
980 return 1;
982 tok = isl_stream_next_token(s);
983 if (!tok)
984 return 0;
985 indent = tok->col - 1;
986 isl_stream_push_token(s, tok);
987 if (indent < get_yaml_indent(s))
988 return 0;
989 if (update_state(s, isl_yaml_mapping_key) < 0)
990 return -1;
991 return 1;
992 case isl_yaml_sequence_start:
993 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
994 if (isl_stream_next_token_is(s, ']'))
995 return 0;
996 if (update_state(s, isl_yaml_sequence) < 0)
997 return -1;
998 return 1;
1000 tok = isl_stream_next_token(s);
1001 if (!tok) {
1002 if (s->eof)
1003 isl_stream_error(s, NULL, "unexpected EOF");
1004 return -1;
1006 if (tok->type == '-') {
1007 isl_token_free(tok);
1008 if (update_state(s, isl_yaml_sequence) < 0)
1009 return -1;
1010 return 1;
1012 isl_stream_error(s, tok, "expecting '-'");
1013 isl_stream_push_token(s, tok);
1014 return 0;
1015 case isl_yaml_sequence:
1016 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW)
1017 return isl_stream_eat_if_available(s, ',');
1018 tok = isl_stream_next_token(s);
1019 if (!tok)
1020 return 0;
1021 indent = tok->col - 1;
1022 if (indent < get_yaml_indent(s) || tok->type != '-') {
1023 isl_stream_push_token(s, tok);
1024 return 0;
1026 isl_token_free(tok);
1027 return 1;
1028 default:
1029 isl_die(s->ctx, isl_error_internal,
1030 "unexpected state", return 0);
1034 /* Start reading a YAML mapping.
1035 * Return 0 on success and -1 on error.
1037 * If the first token on the stream is a '{' then we remove this token
1038 * from the stream and keep track of the fact that the mapping
1039 * is given in flow format.
1040 * Otherwise, we assume the first token is the first key of the mapping and
1041 * keep track of its indentation, but keep the token on the stream.
1042 * In both cases, the next token we expect is the first key of the mapping.
1044 int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream *s)
1046 struct isl_token *tok;
1047 int indent;
1049 if (push_state(s, isl_yaml_mapping_key_start) < 0)
1050 return -1;
1052 tok = isl_stream_next_token(s);
1053 if (!tok) {
1054 if (s->eof)
1055 isl_stream_error(s, NULL, "unexpected EOF");
1056 return -1;
1058 if (isl_token_get_type(tok) == '{') {
1059 isl_token_free(tok);
1060 return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1062 indent = tok->col - 1;
1063 isl_stream_push_token(s, tok);
1065 return set_yaml_indent(s, indent);
1068 /* Finish reading a YAML mapping.
1069 * Return 0 on success and -1 on error.
1071 * If the mapping started with a '{', then we expect a '}' to close
1072 * the mapping.
1073 * Otherwise, we double-check that the next token (if any)
1074 * has a smaller indentation than that of the current mapping.
1076 int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream *s)
1078 struct isl_token *tok;
1079 int indent;
1081 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1082 if (isl_stream_eat(s, '}') < 0)
1083 return -1;
1084 return pop_state(s);
1087 tok = isl_stream_next_token(s);
1088 if (!tok)
1089 return pop_state(s);
1091 indent = tok->col - 1;
1092 isl_stream_push_token(s, tok);
1094 if (indent >= get_yaml_indent(s))
1095 isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1096 "mapping not finished", return -1);
1098 return pop_state(s);
1101 /* Start reading a YAML sequence.
1102 * Return 0 on success and -1 on error.
1104 * If the first token on the stream is a '[' then we remove this token
1105 * from the stream and keep track of the fact that the sequence
1106 * is given in flow format.
1107 * Otherwise, we assume the first token is the dash that introduces
1108 * the first element of the sequence and keep track of its indentation,
1109 * but keep the token on the stream.
1110 * In both cases, the next token we expect is the first element
1111 * of the sequence.
1113 int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream *s)
1115 struct isl_token *tok;
1116 int indent;
1118 if (push_state(s, isl_yaml_sequence_start) < 0)
1119 return -1;
1121 tok = isl_stream_next_token(s);
1122 if (!tok) {
1123 if (s->eof)
1124 isl_stream_error(s, NULL, "unexpected EOF");
1125 return -1;
1127 if (isl_token_get_type(tok) == '[') {
1128 isl_token_free(tok);
1129 return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
1131 indent = tok->col - 1;
1132 isl_stream_push_token(s, tok);
1134 return set_yaml_indent(s, indent);
1137 /* Finish reading a YAML sequence.
1138 * Return 0 on success and -1 on error.
1140 * If the sequence started with a '[', then we expect a ']' to close
1141 * the sequence.
1142 * Otherwise, we double-check that the next token (if any)
1143 * is not a dash or that it has a smaller indentation than
1144 * that of the current sequence.
1146 int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream *s)
1148 struct isl_token *tok;
1149 int indent;
1150 int dash;
1152 if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
1153 if (isl_stream_eat(s, ']') < 0)
1154 return -1;
1155 return pop_state(s);
1158 tok = isl_stream_next_token(s);
1159 if (!tok)
1160 return pop_state(s);
1162 indent = tok->col - 1;
1163 dash = tok->type == '-';
1164 isl_stream_push_token(s, tok);
1166 if (indent >= get_yaml_indent(s) && dash)
1167 isl_die(isl_stream_get_ctx(s), isl_error_invalid,
1168 "sequence not finished", return -1);
1170 return pop_state(s);