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
13 #include <isl_stream_private.h>
16 #include <isl_val_private.h>
20 enum isl_token_type type
;
23 static int same_name(const void *entry
, const void *val
)
25 const struct isl_keyword
*keyword
= (const struct isl_keyword
*)entry
;
27 return !strcmp(keyword
->name
, val
);
30 enum isl_token_type
isl_stream_register_keyword(__isl_keep isl_stream
*s
,
33 struct isl_hash_table_entry
*entry
;
34 struct isl_keyword
*keyword
;
38 s
->keywords
= isl_hash_table_alloc(s
->ctx
, 10);
40 return ISL_TOKEN_ERROR
;
41 s
->next_type
= ISL_TOKEN_LAST
;
44 name_hash
= isl_hash_string(isl_hash_init(), name
);
46 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
,
49 return ISL_TOKEN_ERROR
;
51 keyword
= entry
->data
;
55 keyword
= isl_calloc_type(s
->ctx
, struct isl_keyword
);
57 return ISL_TOKEN_ERROR
;
58 keyword
->type
= s
->next_type
++;
59 keyword
->name
= strdup(name
);
62 return ISL_TOKEN_ERROR
;
64 entry
->data
= keyword
;
69 struct isl_token
*isl_token_new(isl_ctx
*ctx
,
70 int line
, int col
, unsigned on_new_line
)
72 struct isl_token
*tok
= isl_alloc_type(ctx
, struct isl_token
);
77 tok
->on_new_line
= on_new_line
;
83 /* Return the type of "tok".
85 int isl_token_get_type(struct isl_token
*tok
)
87 return tok
? tok
->type
: ISL_TOKEN_ERROR
;
90 /* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
92 __isl_give isl_val
*isl_token_get_val(isl_ctx
*ctx
, struct isl_token
*tok
)
96 if (tok
->type
!= ISL_TOKEN_VALUE
)
97 isl_die(ctx
, isl_error_invalid
, "not a value token",
100 return isl_val_int_from_isl_int(ctx
, tok
->u
.v
);
103 /* Given a token with a string representation, return a copy of this string.
105 __isl_give
char *isl_token_get_str(isl_ctx
*ctx
, struct isl_token
*tok
)
110 isl_die(ctx
, isl_error_invalid
,
111 "token does not have a string representation",
114 return strdup(tok
->u
.s
);
117 void isl_token_free(struct isl_token
*tok
)
121 if (tok
->type
== ISL_TOKEN_VALUE
)
122 isl_int_clear(tok
->u
.v
);
123 else if (tok
->type
== ISL_TOKEN_MAP
)
124 isl_map_free(tok
->u
.map
);
125 else if (tok
->type
== ISL_TOKEN_AFF
)
126 isl_pw_aff_free(tok
->u
.pwaff
);
132 void isl_stream_error(__isl_keep isl_stream
*s
, struct isl_token
*tok
,
135 int line
= tok
? tok
->line
: s
->line
;
136 int col
= tok
? tok
->col
: s
->col
;
137 fprintf(stderr
, "syntax error (%d, %d): %s\n", line
, col
, msg
);
140 fprintf(stderr
, "got '%c'\n", tok
->type
);
141 else if (tok
->type
== ISL_TOKEN_IDENT
)
142 fprintf(stderr
, "got ident '%s'\n", tok
->u
.s
);
143 else if (tok
->is_keyword
)
144 fprintf(stderr
, "got keyword '%s'\n", tok
->u
.s
);
145 else if (tok
->type
== ISL_TOKEN_VALUE
) {
146 fprintf(stderr
, "got value '");
147 isl_int_print(stderr
, tok
->u
.v
, 0);
148 fprintf(stderr
, "'\n");
149 } else if (tok
->type
== ISL_TOKEN_MAP
) {
151 fprintf(stderr
, "got map '");
152 p
= isl_printer_to_file(s
->ctx
, stderr
);
153 p
= isl_printer_print_map(p
, tok
->u
.map
);
155 fprintf(stderr
, "'\n");
156 } else if (tok
->type
== ISL_TOKEN_AFF
) {
158 fprintf(stderr
, "got affine expression '");
159 p
= isl_printer_to_file(s
->ctx
, stderr
);
160 p
= isl_printer_print_pw_aff(p
, tok
->u
.pwaff
);
162 fprintf(stderr
, "'\n");
164 fprintf(stderr
, "got token '%s'\n", tok
->u
.s
);
166 fprintf(stderr
, "got token type %d\n", tok
->type
);
170 static __isl_give isl_stream
* isl_stream_new(struct isl_ctx
*ctx
)
173 isl_stream
*s
= isl_calloc_type(ctx
, struct isl_stream
);
187 for (i
= 0; i
< 5; ++i
)
192 s
->buffer
= isl_alloc_array(ctx
, char, s
->size
);
201 __isl_give isl_stream
* isl_stream_new_file(struct isl_ctx
*ctx
, FILE *file
)
203 isl_stream
*s
= isl_stream_new(ctx
);
210 __isl_give isl_stream
* isl_stream_new_str(struct isl_ctx
*ctx
, const char *str
)
215 s
= isl_stream_new(ctx
);
222 /* Read a character from the stream and advance s->line and s->col
223 * to point to the next character.
225 static int stream_getc(__isl_keep isl_stream
*s
)
231 return s
->c
= s
->un
[--s
->n_un
];
241 else if (c
== '\n') {
250 static void isl_stream_ungetc(__isl_keep isl_stream
*s
, int c
)
252 isl_assert(s
->ctx
, s
->n_un
< 5, return);
253 s
->un
[s
->n_un
++] = c
;
257 /* Read a character from the stream, skipping pairs of '\\' and '\n'.
258 * Set s->start_line and s->start_col to the line and column
259 * of the returned character.
261 static int isl_stream_getc(__isl_keep isl_stream
*s
)
266 s
->start_line
= s
->line
;
267 s
->start_col
= s
->col
;
274 isl_stream_ungetc(s
, c
);
279 static int isl_stream_push_char(__isl_keep isl_stream
*s
, int c
)
281 if (s
->len
>= s
->size
) {
283 s
->size
= (3*s
->size
)/2;
284 buffer
= isl_realloc_array(s
->ctx
, s
->buffer
, char, s
->size
);
289 s
->buffer
[s
->len
++] = c
;
293 void isl_stream_push_token(__isl_keep isl_stream
*s
, struct isl_token
*tok
)
295 isl_assert(s
->ctx
, s
->n_token
< 5, return);
296 s
->tokens
[s
->n_token
++] = tok
;
299 static enum isl_token_type
check_keywords(__isl_keep isl_stream
*s
)
301 struct isl_hash_table_entry
*entry
;
302 struct isl_keyword
*keyword
;
305 if (!strcasecmp(s
->buffer
, "exists"))
306 return ISL_TOKEN_EXISTS
;
307 if (!strcasecmp(s
->buffer
, "and"))
308 return ISL_TOKEN_AND
;
309 if (!strcasecmp(s
->buffer
, "or"))
311 if (!strcasecmp(s
->buffer
, "implies"))
312 return ISL_TOKEN_IMPLIES
;
313 if (!strcasecmp(s
->buffer
, "not"))
314 return ISL_TOKEN_NOT
;
315 if (!strcasecmp(s
->buffer
, "infty"))
316 return ISL_TOKEN_INFTY
;
317 if (!strcasecmp(s
->buffer
, "infinity"))
318 return ISL_TOKEN_INFTY
;
319 if (!strcasecmp(s
->buffer
, "NaN"))
320 return ISL_TOKEN_NAN
;
321 if (!strcasecmp(s
->buffer
, "min"))
322 return ISL_TOKEN_MIN
;
323 if (!strcasecmp(s
->buffer
, "max"))
324 return ISL_TOKEN_MAX
;
325 if (!strcasecmp(s
->buffer
, "rat"))
326 return ISL_TOKEN_RAT
;
327 if (!strcasecmp(s
->buffer
, "true"))
328 return ISL_TOKEN_TRUE
;
329 if (!strcasecmp(s
->buffer
, "false"))
330 return ISL_TOKEN_FALSE
;
331 if (!strcasecmp(s
->buffer
, "ceild"))
332 return ISL_TOKEN_CEILD
;
333 if (!strcasecmp(s
->buffer
, "floord"))
334 return ISL_TOKEN_FLOORD
;
335 if (!strcasecmp(s
->buffer
, "mod"))
336 return ISL_TOKEN_MOD
;
337 if (!strcasecmp(s
->buffer
, "ceil"))
338 return ISL_TOKEN_CEIL
;
339 if (!strcasecmp(s
->buffer
, "floor"))
340 return ISL_TOKEN_FLOOR
;
343 return ISL_TOKEN_IDENT
;
345 name_hash
= isl_hash_string(isl_hash_init(), s
->buffer
);
346 entry
= isl_hash_table_find(s
->ctx
, s
->keywords
, name_hash
, same_name
,
349 keyword
= entry
->data
;
350 return keyword
->type
;
353 return ISL_TOKEN_IDENT
;
356 int isl_stream_skip_line(__isl_keep isl_stream
*s
)
360 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
364 return c
== -1 ? -1 : 0;
367 static struct isl_token
*next_token(__isl_keep isl_stream
*s
, int same_line
)
370 struct isl_token
*tok
= NULL
;
372 int old_line
= s
->last_line
;
375 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
377 return s
->tokens
[--s
->n_token
];
380 if (same_line
&& s
->c
== '\n')
385 /* skip spaces and comment lines */
386 while ((c
= isl_stream_getc(s
)) != -1) {
388 if (isl_stream_skip_line(s
) < 0)
393 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
397 line
= s
->start_line
;
400 if (c
== -1 || (same_line
&& c
== '\n'))
420 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
423 tok
->type
= (enum isl_token_type
)c
;
428 if ((c
= isl_stream_getc(s
)) == '>') {
429 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
432 tok
->u
.s
= strdup("->");
433 tok
->type
= ISL_TOKEN_TO
;
437 isl_stream_ungetc(s
, c
);
439 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
442 tok
->type
= (enum isl_token_type
) '-';
446 if (c
== '-' || isdigit(c
)) {
447 int minus
= c
== '-';
448 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
451 tok
->type
= ISL_TOKEN_VALUE
;
452 isl_int_init(tok
->u
.v
);
453 if (isl_stream_push_char(s
, c
))
455 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
456 if (isl_stream_push_char(s
, c
))
459 isl_stream_ungetc(s
, c
);
460 isl_stream_push_char(s
, '\0');
461 isl_int_read(tok
->u
.v
, s
->buffer
);
462 if (minus
&& isl_int_is_zero(tok
->u
.v
)) {
464 tok
->on_new_line
= 0;
465 isl_stream_push_token(s
, tok
);
466 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
469 tok
->type
= (enum isl_token_type
) '-';
473 if (isalpha(c
) || c
== '_') {
474 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
477 isl_stream_push_char(s
, c
);
478 while ((c
= isl_stream_getc(s
)) != -1 &&
479 (isalnum(c
) || c
== '_'))
480 isl_stream_push_char(s
, c
);
482 isl_stream_ungetc(s
, c
);
483 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
484 isl_stream_push_char(s
, c
);
486 isl_stream_ungetc(s
, c
);
487 isl_stream_push_char(s
, '\0');
488 tok
->type
= check_keywords(s
);
489 if (tok
->type
!= ISL_TOKEN_IDENT
)
491 tok
->u
.s
= strdup(s
->buffer
);
497 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
500 tok
->type
= ISL_TOKEN_STRING
;
502 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
503 isl_stream_push_char(s
, c
);
505 isl_stream_error(s
, NULL
, "unterminated string");
508 isl_stream_push_char(s
, '\0');
509 tok
->u
.s
= strdup(s
->buffer
);
514 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
517 if ((c
= isl_stream_getc(s
)) == '=') {
518 tok
->u
.s
= strdup("==");
519 tok
->type
= ISL_TOKEN_EQ_EQ
;
523 isl_stream_ungetc(s
, c
);
524 tok
->type
= (enum isl_token_type
) '=';
529 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
532 if ((c
= isl_stream_getc(s
)) == '=') {
533 tok
->u
.s
= strdup(":=");
534 tok
->type
= ISL_TOKEN_DEF
;
538 isl_stream_ungetc(s
, c
);
539 tok
->type
= (enum isl_token_type
) ':';
544 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
547 if ((c
= isl_stream_getc(s
)) == '=') {
548 tok
->u
.s
= strdup(">=");
549 tok
->type
= ISL_TOKEN_GE
;
551 } else if (c
== '>') {
552 if ((c
= isl_stream_getc(s
)) == '=') {
553 tok
->u
.s
= strdup(">>=");
554 tok
->type
= ISL_TOKEN_LEX_GE
;
557 tok
->u
.s
= strdup(">>");
558 tok
->type
= ISL_TOKEN_LEX_GT
;
560 tok
->u
.s
= strdup(">");
561 tok
->type
= ISL_TOKEN_GT
;
564 isl_stream_ungetc(s
, c
);
569 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
572 if ((c
= isl_stream_getc(s
)) == '=') {
573 tok
->u
.s
= strdup("<=");
574 tok
->type
= ISL_TOKEN_LE
;
576 } else if (c
== '<') {
577 if ((c
= isl_stream_getc(s
)) == '=') {
578 tok
->u
.s
= strdup("<<=");
579 tok
->type
= ISL_TOKEN_LEX_LE
;
582 tok
->u
.s
= strdup("<<");
583 tok
->type
= ISL_TOKEN_LEX_LT
;
585 tok
->u
.s
= strdup("<");
586 tok
->type
= ISL_TOKEN_LT
;
589 isl_stream_ungetc(s
, c
);
593 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
596 tok
->type
= ISL_TOKEN_AND
;
597 if ((c
= isl_stream_getc(s
)) != '&' && c
!= -1) {
598 tok
->u
.s
= strdup("&");
599 isl_stream_ungetc(s
, c
);
601 tok
->u
.s
= strdup("&&");
605 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
608 tok
->type
= ISL_TOKEN_OR
;
609 if ((c
= isl_stream_getc(s
)) != '|' && c
!= -1) {
610 tok
->u
.s
= strdup("|");
611 isl_stream_ungetc(s
, c
);
613 tok
->u
.s
= strdup("||");
617 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
620 if ((c
= isl_stream_getc(s
)) != '\\' && c
!= -1) {
621 tok
->type
= (enum isl_token_type
) '/';
622 isl_stream_ungetc(s
, c
);
624 tok
->u
.s
= strdup("/\\");
625 tok
->type
= ISL_TOKEN_AND
;
630 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
633 if ((c
= isl_stream_getc(s
)) != '/' && c
!= -1) {
634 tok
->type
= (enum isl_token_type
) '\\';
635 isl_stream_ungetc(s
, c
);
637 tok
->u
.s
= strdup("\\/");
638 tok
->type
= ISL_TOKEN_OR
;
643 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
646 if ((c
= isl_stream_getc(s
)) == '=') {
647 tok
->u
.s
= strdup("!=");
648 tok
->type
= ISL_TOKEN_NE
;
651 tok
->type
= ISL_TOKEN_NOT
;
652 tok
->u
.s
= strdup("!");
655 isl_stream_ungetc(s
, c
);
659 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
662 tok
->type
= ISL_TOKEN_UNKNOWN
;
669 struct isl_token
*isl_stream_next_token(__isl_keep isl_stream
*s
)
671 return next_token(s
, 0);
674 struct isl_token
*isl_stream_next_token_on_same_line(__isl_keep isl_stream
*s
)
676 return next_token(s
, 1);
679 int isl_stream_eat_if_available(__isl_keep isl_stream
*s
, int type
)
681 struct isl_token
*tok
;
683 tok
= isl_stream_next_token(s
);
686 if (tok
->type
== type
) {
690 isl_stream_push_token(s
, tok
);
694 int isl_stream_next_token_is(__isl_keep isl_stream
*s
, int type
)
696 struct isl_token
*tok
;
699 tok
= isl_stream_next_token(s
);
702 r
= tok
->type
== type
;
703 isl_stream_push_token(s
, tok
);
707 char *isl_stream_read_ident_if_available(__isl_keep isl_stream
*s
)
709 struct isl_token
*tok
;
711 tok
= isl_stream_next_token(s
);
714 if (tok
->type
== ISL_TOKEN_IDENT
) {
715 char *ident
= strdup(tok
->u
.s
);
719 isl_stream_push_token(s
, tok
);
723 int isl_stream_eat(__isl_keep isl_stream
*s
, int type
)
725 struct isl_token
*tok
;
727 tok
= isl_stream_next_token(s
);
730 if (tok
->type
== type
) {
734 isl_stream_error(s
, tok
, "expecting other token");
735 isl_stream_push_token(s
, tok
);
739 int isl_stream_is_empty(__isl_keep isl_stream
*s
)
741 struct isl_token
*tok
;
743 tok
= isl_stream_next_token(s
);
748 isl_stream_push_token(s
, tok
);
752 static isl_stat
free_keyword(void **p
, void *user
)
754 struct isl_keyword
*keyword
= *p
;
762 void isl_stream_flush_tokens(__isl_keep isl_stream
*s
)
768 for (i
= 0; i
< s
->n_token
; ++i
)
769 isl_token_free(s
->tokens
[i
]);
773 isl_ctx
*isl_stream_get_ctx(__isl_keep isl_stream
*s
)
775 return s
? s
->ctx
: NULL
;
778 void isl_stream_free(__isl_take isl_stream
*s
)
783 if (s
->n_token
!= 0) {
784 struct isl_token
*tok
= isl_stream_next_token(s
);
785 isl_stream_error(s
, tok
, "unexpected token");
789 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
790 isl_hash_table_free(s
->ctx
, s
->keywords
);
793 free(s
->yaml_indent
);
794 isl_ctx_deref(s
->ctx
);
798 /* Push "state" onto the stack of currently active YAML elements.
799 * The caller is responsible for setting the corresponding indentation.
800 * Return 0 on success and -1 on failure.
802 static int push_state(__isl_keep isl_stream
*s
, enum isl_yaml_state state
)
804 if (s
->yaml_size
< s
->yaml_depth
+ 1) {
806 enum isl_yaml_state
*state
;
808 state
= isl_realloc_array(s
->ctx
, s
->yaml_state
,
809 enum isl_yaml_state
, s
->yaml_depth
+ 1);
812 s
->yaml_state
= state
;
814 indent
= isl_realloc_array(s
->ctx
, s
->yaml_indent
,
815 int, s
->yaml_depth
+ 1);
818 s
->yaml_indent
= indent
;
820 s
->yaml_size
= s
->yaml_depth
+ 1;
823 s
->yaml_state
[s
->yaml_depth
] = state
;
829 /* Remove the innermost active YAML element from the stack.
830 * Return 0 on success and -1 on failure.
832 static int pop_state(__isl_keep isl_stream
*s
)
836 if (s
->yaml_depth
< 1)
837 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
838 "not in YAML construct", return -1);
845 /* Set the state of the innermost active YAML element to "state".
846 * Return 0 on success and -1 on failure.
848 static int update_state(__isl_keep isl_stream
*s
, enum isl_yaml_state state
)
852 if (s
->yaml_depth
< 1)
853 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
854 "not in YAML construct", return -1);
856 s
->yaml_state
[s
->yaml_depth
- 1] = state
;
861 /* Return the state of the innermost active YAML element.
862 * Return isl_yaml_none if we are not inside any YAML element.
864 static enum isl_yaml_state
current_state(__isl_keep isl_stream
*s
)
867 return isl_yaml_none
;
868 if (s
->yaml_depth
< 1)
869 return isl_yaml_none
;
870 return s
->yaml_state
[s
->yaml_depth
- 1];
873 /* Set the indentation of the innermost active YAML element to "indent".
874 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
875 * that the current elemient is in flow format.
877 static int set_yaml_indent(__isl_keep isl_stream
*s
, int indent
)
879 if (s
->yaml_depth
< 1)
880 isl_die(s
->ctx
, isl_error_internal
,
881 "not in YAML element", return -1);
883 s
->yaml_indent
[s
->yaml_depth
- 1] = indent
;
888 /* Return the indentation of the innermost active YAML element
891 static int get_yaml_indent(__isl_keep isl_stream
*s
)
893 if (s
->yaml_depth
< 1)
894 isl_die(s
->ctx
, isl_error_internal
,
895 "not in YAML element", return -1);
897 return s
->yaml_indent
[s
->yaml_depth
- 1];
900 /* Move to the next state at the innermost level.
901 * Return 1 if successful.
902 * Return 0 if we are at the end of the innermost level.
903 * Return -1 on error.
905 * If we are in state isl_yaml_mapping_key_start, then we have just
906 * started a mapping and we are expecting a key. If the mapping started
907 * with a '{', then we check if the next token is a '}'. If so,
908 * then the mapping is empty and there is no next state at this level.
909 * Otherwise, we assume that there is at least one key (the one from
910 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
912 * If we are in state isl_yaml_mapping_key, then the we expect a colon
913 * followed by a value, so there is always a next state unless
916 * If we are in state isl_yaml_mapping_val, then there may or may
917 * not be a subsequent key in the same mapping.
918 * In flow format, the next key is preceded by a comma.
919 * In block format, the next key has the same indentation as the first key.
920 * If the first token has a smaller indentation, then we have reached
921 * the end of the current mapping.
923 * If we are in state isl_yaml_sequence_start, then we have just
924 * started a sequence. If the sequence started with a '[',
925 * then we check if the next token is a ']'. If so, then the sequence
926 * is empty and there is no next state at this level.
927 * Otherwise, we assume that there is at least one element in the sequence
928 * (the one from which we derived the indentation in
929 * isl_stream_yaml_read_start_sequence.
931 * If we are in state isl_yaml_sequence, then there may or may
932 * not be a subsequent element in the same sequence.
933 * In flow format, the next element is preceded by a comma.
934 * In block format, the next element is introduced by a dash with
935 * the same indentation as that of the first element.
936 * If the first token is not a dash or if it has a smaller indentation,
937 * then we have reached the end of the current sequence.
939 int isl_stream_yaml_next(__isl_keep isl_stream
*s
)
941 struct isl_token
*tok
;
942 enum isl_yaml_state state
;
945 state
= current_state(s
);
946 if (state
== isl_yaml_none
)
947 isl_die(s
->ctx
, isl_error_invalid
,
948 "not in YAML element", return -1);
950 case isl_yaml_mapping_key_start
:
951 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
&&
952 isl_stream_next_token_is(s
, '}'))
954 if (update_state(s
, isl_yaml_mapping_key
) < 0)
957 case isl_yaml_mapping_key
:
958 tok
= isl_stream_next_token(s
);
961 isl_stream_error(s
, NULL
, "unexpected EOF");
964 if (tok
->type
== ':') {
966 if (update_state(s
, isl_yaml_mapping_val
) < 0)
970 isl_stream_error(s
, tok
, "expecting ':'");
971 isl_stream_push_token(s
, tok
);
973 case isl_yaml_mapping_val
:
974 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
975 if (!isl_stream_eat_if_available(s
, ','))
977 if (update_state(s
, isl_yaml_mapping_key
) < 0)
981 tok
= isl_stream_next_token(s
);
984 indent
= tok
->col
- 1;
985 isl_stream_push_token(s
, tok
);
986 if (indent
< get_yaml_indent(s
))
988 if (update_state(s
, isl_yaml_mapping_key
) < 0)
991 case isl_yaml_sequence_start
:
992 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
993 if (isl_stream_next_token_is(s
, ']'))
995 if (update_state(s
, isl_yaml_sequence
) < 0)
999 tok
= isl_stream_next_token(s
);
1002 isl_stream_error(s
, NULL
, "unexpected EOF");
1005 if (tok
->type
== '-') {
1006 isl_token_free(tok
);
1007 if (update_state(s
, isl_yaml_sequence
) < 0)
1011 isl_stream_error(s
, tok
, "expecting '-'");
1012 isl_stream_push_token(s
, tok
);
1014 case isl_yaml_sequence
:
1015 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
)
1016 return isl_stream_eat_if_available(s
, ',');
1017 tok
= isl_stream_next_token(s
);
1020 indent
= tok
->col
- 1;
1021 if (indent
< get_yaml_indent(s
) || tok
->type
!= '-') {
1022 isl_stream_push_token(s
, tok
);
1025 isl_token_free(tok
);
1028 isl_die(s
->ctx
, isl_error_internal
,
1029 "unexpected state", return 0);
1033 /* Start reading a YAML mapping.
1034 * Return 0 on success and -1 on error.
1036 * If the first token on the stream is a '{' then we remove this token
1037 * from the stream and keep track of the fact that the mapping
1038 * is given in flow format.
1039 * Otherwise, we assume the first token is the first key of the mapping and
1040 * keep track of its indentation, but keep the token on the stream.
1041 * In both cases, the next token we expect is the first key of the mapping.
1043 int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream
*s
)
1045 struct isl_token
*tok
;
1048 if (push_state(s
, isl_yaml_mapping_key_start
) < 0)
1051 tok
= isl_stream_next_token(s
);
1054 isl_stream_error(s
, NULL
, "unexpected EOF");
1057 if (isl_token_get_type(tok
) == '{') {
1058 isl_token_free(tok
);
1059 return set_yaml_indent(s
, ISL_YAML_INDENT_FLOW
);
1061 indent
= tok
->col
- 1;
1062 isl_stream_push_token(s
, tok
);
1064 return set_yaml_indent(s
, indent
);
1067 /* Finish reading a YAML mapping.
1068 * Return 0 on success and -1 on error.
1070 * If the mapping started with a '{', then we expect a '}' to close
1072 * Otherwise, we double-check that the next token (if any)
1073 * has a smaller indentation than that of the current mapping.
1075 int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream
*s
)
1077 struct isl_token
*tok
;
1080 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1081 if (isl_stream_eat(s
, '}') < 0)
1083 return pop_state(s
);
1086 tok
= isl_stream_next_token(s
);
1088 return pop_state(s
);
1090 indent
= tok
->col
- 1;
1091 isl_stream_push_token(s
, tok
);
1093 if (indent
>= get_yaml_indent(s
))
1094 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
1095 "mapping not finished", return -1);
1097 return pop_state(s
);
1100 /* Start reading a YAML sequence.
1101 * Return 0 on success and -1 on error.
1103 * If the first token on the stream is a '[' then we remove this token
1104 * from the stream and keep track of the fact that the sequence
1105 * is given in flow format.
1106 * Otherwise, we assume the first token is the dash that introduces
1107 * the first element of the sequence and keep track of its indentation,
1108 * but keep the token on the stream.
1109 * In both cases, the next token we expect is the first element
1112 int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream
*s
)
1114 struct isl_token
*tok
;
1117 if (push_state(s
, isl_yaml_sequence_start
) < 0)
1120 tok
= isl_stream_next_token(s
);
1123 isl_stream_error(s
, NULL
, "unexpected EOF");
1126 if (isl_token_get_type(tok
) == '[') {
1127 isl_token_free(tok
);
1128 return set_yaml_indent(s
, ISL_YAML_INDENT_FLOW
);
1130 indent
= tok
->col
- 1;
1131 isl_stream_push_token(s
, tok
);
1133 return set_yaml_indent(s
, indent
);
1136 /* Finish reading a YAML sequence.
1137 * Return 0 on success and -1 on error.
1139 * If the sequence started with a '[', then we expect a ']' to close
1141 * Otherwise, we double-check that the next token (if any)
1142 * is not a dash or that it has a smaller indentation than
1143 * that of the current sequence.
1145 int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream
*s
)
1147 struct isl_token
*tok
;
1151 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1152 if (isl_stream_eat(s
, ']') < 0)
1154 return pop_state(s
);
1157 tok
= isl_stream_next_token(s
);
1159 return pop_state(s
);
1161 indent
= tok
->col
- 1;
1162 dash
= tok
->type
== '-';
1163 isl_stream_push_token(s
, tok
);
1165 if (indent
>= get_yaml_indent(s
) && dash
)
1166 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
1167 "sequence not finished", return -1);
1169 return pop_state(s
);