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 isl_stream_error(s
, NULL
, "unexpected EOF");
733 if (tok
->type
== type
) {
737 isl_stream_error(s
, tok
, "expecting other token");
738 isl_stream_push_token(s
, tok
);
742 int isl_stream_is_empty(__isl_keep isl_stream
*s
)
744 struct isl_token
*tok
;
746 tok
= isl_stream_next_token(s
);
751 isl_stream_push_token(s
, tok
);
755 static isl_stat
free_keyword(void **p
, void *user
)
757 struct isl_keyword
*keyword
= *p
;
765 void isl_stream_flush_tokens(__isl_keep isl_stream
*s
)
771 for (i
= 0; i
< s
->n_token
; ++i
)
772 isl_token_free(s
->tokens
[i
]);
776 isl_ctx
*isl_stream_get_ctx(__isl_keep isl_stream
*s
)
778 return s
? s
->ctx
: NULL
;
781 void isl_stream_free(__isl_take isl_stream
*s
)
786 if (s
->n_token
!= 0) {
787 struct isl_token
*tok
= isl_stream_next_token(s
);
788 isl_stream_error(s
, tok
, "unexpected token");
792 isl_hash_table_foreach(s
->ctx
, s
->keywords
, &free_keyword
, NULL
);
793 isl_hash_table_free(s
->ctx
, s
->keywords
);
796 free(s
->yaml_indent
);
797 isl_ctx_deref(s
->ctx
);
801 /* Push "state" onto the stack of currently active YAML elements.
802 * The caller is responsible for setting the corresponding indentation.
803 * Return 0 on success and -1 on failure.
805 static int push_state(__isl_keep isl_stream
*s
, enum isl_yaml_state state
)
807 if (s
->yaml_size
< s
->yaml_depth
+ 1) {
809 enum isl_yaml_state
*state
;
811 state
= isl_realloc_array(s
->ctx
, s
->yaml_state
,
812 enum isl_yaml_state
, s
->yaml_depth
+ 1);
815 s
->yaml_state
= state
;
817 indent
= isl_realloc_array(s
->ctx
, s
->yaml_indent
,
818 int, s
->yaml_depth
+ 1);
821 s
->yaml_indent
= indent
;
823 s
->yaml_size
= s
->yaml_depth
+ 1;
826 s
->yaml_state
[s
->yaml_depth
] = state
;
832 /* Remove the innermost active YAML element from the stack.
833 * Return 0 on success and -1 on failure.
835 static int pop_state(__isl_keep isl_stream
*s
)
839 if (s
->yaml_depth
< 1)
840 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
841 "not in YAML construct", return -1);
848 /* Set the state of the innermost active YAML element to "state".
849 * Return 0 on success and -1 on failure.
851 static int update_state(__isl_keep isl_stream
*s
, enum isl_yaml_state state
)
855 if (s
->yaml_depth
< 1)
856 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
857 "not in YAML construct", return -1);
859 s
->yaml_state
[s
->yaml_depth
- 1] = state
;
864 /* Return the state of the innermost active YAML element.
865 * Return isl_yaml_none if we are not inside any YAML element.
867 static enum isl_yaml_state
current_state(__isl_keep isl_stream
*s
)
870 return isl_yaml_none
;
871 if (s
->yaml_depth
< 1)
872 return isl_yaml_none
;
873 return s
->yaml_state
[s
->yaml_depth
- 1];
876 /* Set the indentation of the innermost active YAML element to "indent".
877 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
878 * that the current elemient is in flow format.
880 static int set_yaml_indent(__isl_keep isl_stream
*s
, int indent
)
882 if (s
->yaml_depth
< 1)
883 isl_die(s
->ctx
, isl_error_internal
,
884 "not in YAML element", return -1);
886 s
->yaml_indent
[s
->yaml_depth
- 1] = indent
;
891 /* Return the indentation of the innermost active YAML element
894 static int get_yaml_indent(__isl_keep isl_stream
*s
)
896 if (s
->yaml_depth
< 1)
897 isl_die(s
->ctx
, isl_error_internal
,
898 "not in YAML element", return -1);
900 return s
->yaml_indent
[s
->yaml_depth
- 1];
903 /* Move to the next state at the innermost level.
904 * Return 1 if successful.
905 * Return 0 if we are at the end of the innermost level.
906 * Return -1 on error.
908 * If we are in state isl_yaml_mapping_key_start, then we have just
909 * started a mapping and we are expecting a key. If the mapping started
910 * with a '{', then we check if the next token is a '}'. If so,
911 * then the mapping is empty and there is no next state at this level.
912 * Otherwise, we assume that there is at least one key (the one from
913 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
915 * If we are in state isl_yaml_mapping_key, then the we expect a colon
916 * followed by a value, so there is always a next state unless
919 * If we are in state isl_yaml_mapping_val, then there may or may
920 * not be a subsequent key in the same mapping.
921 * In flow format, the next key is preceded by a comma.
922 * In block format, the next key has the same indentation as the first key.
923 * If the first token has a smaller indentation, then we have reached
924 * the end of the current mapping.
926 * If we are in state isl_yaml_sequence_start, then we have just
927 * started a sequence. If the sequence started with a '[',
928 * then we check if the next token is a ']'. If so, then the sequence
929 * is empty and there is no next state at this level.
930 * Otherwise, we assume that there is at least one element in the sequence
931 * (the one from which we derived the indentation in
932 * isl_stream_yaml_read_start_sequence.
934 * If we are in state isl_yaml_sequence, then there may or may
935 * not be a subsequent element in the same sequence.
936 * In flow format, the next element is preceded by a comma.
937 * In block format, the next element is introduced by a dash with
938 * the same indentation as that of the first element.
939 * If the first token is not a dash or if it has a smaller indentation,
940 * then we have reached the end of the current sequence.
942 int isl_stream_yaml_next(__isl_keep isl_stream
*s
)
944 struct isl_token
*tok
;
945 enum isl_yaml_state state
;
948 state
= current_state(s
);
949 if (state
== isl_yaml_none
)
950 isl_die(s
->ctx
, isl_error_invalid
,
951 "not in YAML element", return -1);
953 case isl_yaml_mapping_key_start
:
954 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
&&
955 isl_stream_next_token_is(s
, '}'))
957 if (update_state(s
, isl_yaml_mapping_key
) < 0)
960 case isl_yaml_mapping_key
:
961 tok
= isl_stream_next_token(s
);
964 isl_stream_error(s
, NULL
, "unexpected EOF");
967 if (tok
->type
== ':') {
969 if (update_state(s
, isl_yaml_mapping_val
) < 0)
973 isl_stream_error(s
, tok
, "expecting ':'");
974 isl_stream_push_token(s
, tok
);
976 case isl_yaml_mapping_val
:
977 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
978 if (!isl_stream_eat_if_available(s
, ','))
980 if (update_state(s
, isl_yaml_mapping_key
) < 0)
984 tok
= isl_stream_next_token(s
);
987 indent
= tok
->col
- 1;
988 isl_stream_push_token(s
, tok
);
989 if (indent
< get_yaml_indent(s
))
991 if (update_state(s
, isl_yaml_mapping_key
) < 0)
994 case isl_yaml_sequence_start
:
995 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
996 if (isl_stream_next_token_is(s
, ']'))
998 if (update_state(s
, isl_yaml_sequence
) < 0)
1002 tok
= isl_stream_next_token(s
);
1005 isl_stream_error(s
, NULL
, "unexpected EOF");
1008 if (tok
->type
== '-') {
1009 isl_token_free(tok
);
1010 if (update_state(s
, isl_yaml_sequence
) < 0)
1014 isl_stream_error(s
, tok
, "expecting '-'");
1015 isl_stream_push_token(s
, tok
);
1017 case isl_yaml_sequence
:
1018 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
)
1019 return isl_stream_eat_if_available(s
, ',');
1020 tok
= isl_stream_next_token(s
);
1023 indent
= tok
->col
- 1;
1024 if (indent
< get_yaml_indent(s
) || tok
->type
!= '-') {
1025 isl_stream_push_token(s
, tok
);
1028 isl_token_free(tok
);
1031 isl_die(s
->ctx
, isl_error_internal
,
1032 "unexpected state", return 0);
1036 /* Start reading a YAML mapping.
1037 * Return 0 on success and -1 on error.
1039 * If the first token on the stream is a '{' then we remove this token
1040 * from the stream and keep track of the fact that the mapping
1041 * is given in flow format.
1042 * Otherwise, we assume the first token is the first key of the mapping and
1043 * keep track of its indentation, but keep the token on the stream.
1044 * In both cases, the next token we expect is the first key of the mapping.
1046 int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream
*s
)
1048 struct isl_token
*tok
;
1051 if (push_state(s
, isl_yaml_mapping_key_start
) < 0)
1054 tok
= isl_stream_next_token(s
);
1057 isl_stream_error(s
, NULL
, "unexpected EOF");
1060 if (isl_token_get_type(tok
) == '{') {
1061 isl_token_free(tok
);
1062 return set_yaml_indent(s
, ISL_YAML_INDENT_FLOW
);
1064 indent
= tok
->col
- 1;
1065 isl_stream_push_token(s
, tok
);
1067 return set_yaml_indent(s
, indent
);
1070 /* Finish reading a YAML mapping.
1071 * Return 0 on success and -1 on error.
1073 * If the mapping started with a '{', then we expect a '}' to close
1075 * Otherwise, we double-check that the next token (if any)
1076 * has a smaller indentation than that of the current mapping.
1078 int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream
*s
)
1080 struct isl_token
*tok
;
1083 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1084 if (isl_stream_eat(s
, '}') < 0)
1086 return pop_state(s
);
1089 tok
= isl_stream_next_token(s
);
1091 return pop_state(s
);
1093 indent
= tok
->col
- 1;
1094 isl_stream_push_token(s
, tok
);
1096 if (indent
>= get_yaml_indent(s
))
1097 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
1098 "mapping not finished", return -1);
1100 return pop_state(s
);
1103 /* Start reading a YAML sequence.
1104 * Return 0 on success and -1 on error.
1106 * If the first token on the stream is a '[' then we remove this token
1107 * from the stream and keep track of the fact that the sequence
1108 * is given in flow format.
1109 * Otherwise, we assume the first token is the dash that introduces
1110 * the first element of the sequence and keep track of its indentation,
1111 * but keep the token on the stream.
1112 * In both cases, the next token we expect is the first element
1115 int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream
*s
)
1117 struct isl_token
*tok
;
1120 if (push_state(s
, isl_yaml_sequence_start
) < 0)
1123 tok
= isl_stream_next_token(s
);
1126 isl_stream_error(s
, NULL
, "unexpected EOF");
1129 if (isl_token_get_type(tok
) == '[') {
1130 isl_token_free(tok
);
1131 return set_yaml_indent(s
, ISL_YAML_INDENT_FLOW
);
1133 indent
= tok
->col
- 1;
1134 isl_stream_push_token(s
, tok
);
1136 return set_yaml_indent(s
, indent
);
1139 /* Finish reading a YAML sequence.
1140 * Return 0 on success and -1 on error.
1142 * If the sequence started with a '[', then we expect a ']' to close
1144 * Otherwise, we double-check that the next token (if any)
1145 * is not a dash or that it has a smaller indentation than
1146 * that of the current sequence.
1148 int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream
*s
)
1150 struct isl_token
*tok
;
1154 if (get_yaml_indent(s
) == ISL_YAML_INDENT_FLOW
) {
1155 if (isl_stream_eat(s
, ']') < 0)
1157 return pop_state(s
);
1160 tok
= isl_stream_next_token(s
);
1162 return pop_state(s
);
1164 indent
= tok
->col
- 1;
1165 dash
= tok
->type
== '-';
1166 isl_stream_push_token(s
, tok
);
1168 if (indent
>= get_yaml_indent(s
) && dash
)
1169 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
1170 "sequence not finished", return -1);
1172 return pop_state(s
);