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
14 #include <isl_stream_private.h>
17 #include <isl_val_private.h>
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
,
34 struct isl_hash_table_entry
*entry
;
35 struct isl_keyword
*keyword
;
39 s
->keywords
= isl_hash_table_alloc(s
->ctx
, 10);
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
,
50 return ISL_TOKEN_ERROR
;
52 keyword
= entry
->data
;
56 keyword
= isl_calloc_type(s
->ctx
, struct isl_keyword
);
58 return ISL_TOKEN_ERROR
;
59 keyword
->type
= s
->next_type
++;
60 keyword
->name
= strdup(name
);
63 return ISL_TOKEN_ERROR
;
65 entry
->data
= keyword
;
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
);
78 tok
->on_new_line
= on_new_line
;
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
)
97 if (tok
->type
!= ISL_TOKEN_VALUE
)
98 isl_die(ctx
, isl_error_invalid
, "not a value token",
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
)
111 isl_die(ctx
, isl_error_invalid
,
112 "token does not have a string representation",
115 return strdup(tok
->u
.s
);
118 void isl_token_free(struct isl_token
*tok
)
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
);
133 void isl_stream_error(__isl_keep isl_stream
*s
, struct isl_token
*tok
,
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
);
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
) {
152 fprintf(stderr
, "got map '");
153 p
= isl_printer_to_file(s
->ctx
, stderr
);
154 p
= isl_printer_print_map(p
, tok
->u
.map
);
156 fprintf(stderr
, "'\n");
157 } else if (tok
->type
== ISL_TOKEN_AFF
) {
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
);
163 fprintf(stderr
, "'\n");
165 fprintf(stderr
, "got token '%s'\n", tok
->u
.s
);
167 fprintf(stderr
, "got token type %d\n", tok
->type
);
171 static __isl_give isl_stream
* isl_stream_new(struct isl_ctx
*ctx
)
174 isl_stream
*s
= isl_calloc_type(ctx
, struct isl_stream
);
188 for (i
= 0; i
< 5; ++i
)
193 s
->buffer
= isl_alloc_array(ctx
, char, s
->size
);
202 __isl_give isl_stream
* isl_stream_new_file(struct isl_ctx
*ctx
, FILE *file
)
204 isl_stream
*s
= isl_stream_new(ctx
);
211 __isl_give isl_stream
* isl_stream_new_str(struct isl_ctx
*ctx
, const char *str
)
216 s
= isl_stream_new(ctx
);
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
)
232 return s
->c
= s
->un
[--s
->n_un
];
242 else if (c
== '\n') {
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
;
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
)
267 s
->start_line
= s
->line
;
268 s
->start_col
= s
->col
;
275 isl_stream_ungetc(s
, c
);
280 static int isl_stream_push_char(__isl_keep isl_stream
*s
, int c
)
282 if (s
->len
>= s
->size
) {
284 s
->size
= (3*s
->size
)/2;
285 buffer
= isl_realloc_array(s
->ctx
, s
->buffer
, char, s
->size
);
290 s
->buffer
[s
->len
++] = c
;
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
;
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"))
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
;
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
,
350 keyword
= entry
->data
;
351 return keyword
->type
;
354 return ISL_TOKEN_IDENT
;
357 int isl_stream_skip_line(__isl_keep isl_stream
*s
)
361 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '\n')
365 return c
== -1 ? -1 : 0;
368 static struct isl_token
*next_token(__isl_keep isl_stream
*s
, int same_line
)
371 struct isl_token
*tok
= NULL
;
373 int old_line
= s
->last_line
;
376 if (same_line
&& s
->tokens
[s
->n_token
- 1]->on_new_line
)
378 return s
->tokens
[--s
->n_token
];
381 if (same_line
&& s
->c
== '\n')
386 /* skip spaces and comment lines */
387 while ((c
= isl_stream_getc(s
)) != -1) {
389 if (isl_stream_skip_line(s
) < 0)
394 } else if (!isspace(c
) || (same_line
&& c
== '\n'))
398 line
= s
->start_line
;
401 if (c
== -1 || (same_line
&& c
== '\n'))
421 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
424 tok
->type
= (enum isl_token_type
)c
;
429 if ((c
= isl_stream_getc(s
)) == '>') {
430 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
433 tok
->u
.s
= strdup("->");
434 tok
->type
= ISL_TOKEN_TO
;
438 isl_stream_ungetc(s
, c
);
440 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
443 tok
->type
= (enum isl_token_type
) '-';
447 if (c
== '-' || isdigit(c
)) {
448 int minus
= c
== '-';
449 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
452 tok
->type
= ISL_TOKEN_VALUE
;
453 isl_int_init(tok
->u
.v
);
454 if (isl_stream_push_char(s
, c
))
456 while ((c
= isl_stream_getc(s
)) != -1 && isdigit(c
))
457 if (isl_stream_push_char(s
, c
))
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
)) {
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
);
470 tok
->type
= (enum isl_token_type
) '-';
474 if (isalpha(c
) || c
== '_') {
475 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
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
);
483 isl_stream_ungetc(s
, c
);
484 while ((c
= isl_stream_getc(s
)) != -1 && c
== '\'')
485 isl_stream_push_char(s
, c
);
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
)
492 tok
->u
.s
= strdup(s
->buffer
);
498 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
501 tok
->type
= ISL_TOKEN_STRING
;
503 while ((c
= isl_stream_getc(s
)) != -1 && c
!= '"' && c
!= '\n')
504 isl_stream_push_char(s
, c
);
506 isl_stream_error(s
, NULL
, "unterminated string");
509 isl_stream_push_char(s
, '\0');
510 tok
->u
.s
= strdup(s
->buffer
);
515 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
518 if ((c
= isl_stream_getc(s
)) == '=') {
519 tok
->u
.s
= strdup("==");
520 tok
->type
= ISL_TOKEN_EQ_EQ
;
524 isl_stream_ungetc(s
, c
);
525 tok
->type
= (enum isl_token_type
) '=';
530 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
533 if ((c
= isl_stream_getc(s
)) == '=') {
534 tok
->u
.s
= strdup(":=");
535 tok
->type
= ISL_TOKEN_DEF
;
539 isl_stream_ungetc(s
, c
);
540 tok
->type
= (enum isl_token_type
) ':';
545 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
548 if ((c
= isl_stream_getc(s
)) == '=') {
549 tok
->u
.s
= strdup(">=");
550 tok
->type
= ISL_TOKEN_GE
;
552 } else if (c
== '>') {
553 if ((c
= isl_stream_getc(s
)) == '=') {
554 tok
->u
.s
= strdup(">>=");
555 tok
->type
= ISL_TOKEN_LEX_GE
;
558 tok
->u
.s
= strdup(">>");
559 tok
->type
= ISL_TOKEN_LEX_GT
;
561 tok
->u
.s
= strdup(">");
562 tok
->type
= ISL_TOKEN_GT
;
565 isl_stream_ungetc(s
, c
);
570 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
573 if ((c
= isl_stream_getc(s
)) == '=') {
574 tok
->u
.s
= strdup("<=");
575 tok
->type
= ISL_TOKEN_LE
;
577 } else if (c
== '<') {
578 if ((c
= isl_stream_getc(s
)) == '=') {
579 tok
->u
.s
= strdup("<<=");
580 tok
->type
= ISL_TOKEN_LEX_LE
;
583 tok
->u
.s
= strdup("<<");
584 tok
->type
= ISL_TOKEN_LEX_LT
;
586 tok
->u
.s
= strdup("<");
587 tok
->type
= ISL_TOKEN_LT
;
590 isl_stream_ungetc(s
, c
);
594 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
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
);
602 tok
->u
.s
= strdup("&&");
606 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
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
);
614 tok
->u
.s
= strdup("||");
618 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
621 if ((c
= isl_stream_getc(s
)) != '\\' && c
!= -1) {
622 tok
->type
= (enum isl_token_type
) '/';
623 isl_stream_ungetc(s
, c
);
625 tok
->u
.s
= strdup("/\\");
626 tok
->type
= ISL_TOKEN_AND
;
631 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
634 if ((c
= isl_stream_getc(s
)) != '/' && c
!= -1) {
635 tok
->type
= (enum isl_token_type
) '\\';
636 isl_stream_ungetc(s
, c
);
638 tok
->u
.s
= strdup("\\/");
639 tok
->type
= ISL_TOKEN_OR
;
644 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
647 if ((c
= isl_stream_getc(s
)) == '=') {
648 tok
->u
.s
= strdup("!=");
649 tok
->type
= ISL_TOKEN_NE
;
652 tok
->type
= ISL_TOKEN_NOT
;
653 tok
->u
.s
= strdup("!");
656 isl_stream_ungetc(s
, c
);
660 tok
= isl_token_new(s
->ctx
, line
, col
, old_line
!= line
);
663 tok
->type
= ISL_TOKEN_UNKNOWN
;
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
);
687 if (tok
->type
== type
) {
691 isl_stream_push_token(s
, tok
);
695 int isl_stream_next_token_is(__isl_keep isl_stream
*s
, int type
)
697 struct isl_token
*tok
;
700 tok
= isl_stream_next_token(s
);
703 r
= tok
->type
== type
;
704 isl_stream_push_token(s
, tok
);
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
);
715 if (tok
->type
== ISL_TOKEN_IDENT
) {
716 char *ident
= strdup(tok
->u
.s
);
720 isl_stream_push_token(s
, tok
);
724 int isl_stream_eat(__isl_keep isl_stream
*s
, int type
)
726 struct isl_token
*tok
;
728 tok
= isl_stream_next_token(s
);
731 if (tok
->type
== type
) {
735 isl_stream_error(s
, tok
, "expecting other token");
736 isl_stream_push_token(s
, tok
);
740 int isl_stream_is_empty(__isl_keep isl_stream
*s
)
742 struct isl_token
*tok
;
744 tok
= isl_stream_next_token(s
);
749 isl_stream_push_token(s
, tok
);
753 static int free_keyword(void **p
, void *user
)
755 struct isl_keyword
*keyword
= *p
;
763 void isl_stream_flush_tokens(__isl_keep isl_stream
*s
)
769 for (i
= 0; i
< s
->n_token
; ++i
)
770 isl_token_free(s
->tokens
[i
]);
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
)
784 if (s
->n_token
!= 0) {
785 struct isl_token
*tok
= isl_stream_next_token(s
);
786 isl_stream_error(s
, tok
, "unexpected token");
790 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
791 isl_hash_table_free(s
->ctx
, s
->keywords
);
794 free(s
->yaml_indent
);
795 isl_ctx_deref(s
->ctx
);
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) {
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);
813 s
->yaml_state
= state
;
815 indent
= isl_realloc_array(s
->ctx
, s
->yaml_indent
,
816 int, s
->yaml_depth
+ 1);
819 s
->yaml_indent
= indent
;
821 s
->yaml_size
= s
->yaml_depth
+ 1;
824 s
->yaml_state
[s
->yaml_depth
] = state
;
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
)
837 if (s
->yaml_depth
< 1)
838 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
839 "not in YAML construct", return -1);
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
)
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
;
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
)
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
;
889 /* Return the indentation of the innermost active YAML element
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
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
;
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);
951 case isl_yaml_mapping_key_start
:
952 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
&&
953 isl_stream_next_token_is(s
, '}'))
955 if (update_state(s
, isl_yaml_mapping_key
) < 0)
958 case isl_yaml_mapping_key
:
959 tok
= isl_stream_next_token(s
);
962 isl_stream_error(s
, NULL
, "unexpected EOF");
965 if (tok
->type
== ':') {
967 if (update_state(s
, isl_yaml_mapping_val
) < 0)
971 isl_stream_error(s
, tok
, "expecting ':'");
972 isl_stream_push_token(s
, tok
);
974 case isl_yaml_mapping_val
:
975 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
976 if (!isl_stream_eat_if_available(s
, ','))
978 if (update_state(s
, isl_yaml_mapping_key
) < 0)
982 tok
= isl_stream_next_token(s
);
985 indent
= tok
->col
- 1;
986 isl_stream_push_token(s
, tok
);
987 if (indent
< get_yaml_indent(s
))
989 if (update_state(s
, isl_yaml_mapping_key
) < 0)
992 case isl_yaml_sequence_start
:
993 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
994 if (isl_stream_next_token_is(s
, ']'))
996 if (update_state(s
, isl_yaml_sequence
) < 0)
1000 tok
= isl_stream_next_token(s
);
1003 isl_stream_error(s
, NULL
, "unexpected EOF");
1006 if (tok
->type
== '-') {
1007 isl_token_free(tok
);
1008 if (update_state(s
, isl_yaml_sequence
) < 0)
1012 isl_stream_error(s
, tok
, "expecting '-'");
1013 isl_stream_push_token(s
, tok
);
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
);
1021 indent
= tok
->col
- 1;
1022 if (indent
< get_yaml_indent(s
) || tok
->type
!= '-') {
1023 isl_stream_push_token(s
, tok
);
1026 isl_token_free(tok
);
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
;
1049 if (push_state(s
, isl_yaml_mapping_key_start
) < 0)
1052 tok
= isl_stream_next_token(s
);
1055 isl_stream_error(s
, NULL
, "unexpected EOF");
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
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
;
1081 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1082 if (isl_stream_eat(s
, '}') < 0)
1084 return pop_state(s
);
1087 tok
= isl_stream_next_token(s
);
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
1113 int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream
*s
)
1115 struct isl_token
*tok
;
1118 if (push_state(s
, isl_yaml_sequence_start
) < 0)
1121 tok
= isl_stream_next_token(s
);
1124 isl_stream_error(s
, NULL
, "unexpected EOF");
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
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
;
1152 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1153 if (isl_stream_eat(s
, ']') < 0)
1155 return pop_state(s
);
1158 tok
= isl_stream_next_token(s
);
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
);