Blame isl-0.16.1/isl_stream.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
Packit fb9d21
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <ctype.h>
Packit fb9d21
#include <string.h>
Packit fb9d21
#include <isl/ctx.h>
Packit fb9d21
#include <isl_stream_private.h>
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl/aff.h>
Packit fb9d21
#include <isl_val_private.h>
Packit fb9d21
Packit fb9d21
struct isl_keyword {
Packit fb9d21
	char			*name;
Packit fb9d21
	enum isl_token_type	type;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static int same_name(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
Packit fb9d21
Packit fb9d21
	return !strcmp(keyword->name, val);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
enum isl_token_type isl_stream_register_keyword(__isl_keep isl_stream *s,
Packit fb9d21
	const char *name)
Packit fb9d21
{
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
	struct isl_keyword *keyword;
Packit fb9d21
	uint32_t name_hash;
Packit fb9d21
Packit fb9d21
	if (!s->keywords) {
Packit fb9d21
		s->keywords = isl_hash_table_alloc(s->ctx, 10);
Packit fb9d21
		if (!s->keywords)
Packit fb9d21
			return ISL_TOKEN_ERROR;
Packit fb9d21
		s->next_type = ISL_TOKEN_LAST;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	name_hash = isl_hash_string(isl_hash_init(), name);
Packit fb9d21
Packit fb9d21
	entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
Packit fb9d21
					same_name, name, 1);
Packit fb9d21
	if (!entry)
Packit fb9d21
		return ISL_TOKEN_ERROR;
Packit fb9d21
	if (entry->data) {
Packit fb9d21
		keyword = entry->data;
Packit fb9d21
		return keyword->type;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	keyword = isl_calloc_type(s->ctx, struct isl_keyword);
Packit fb9d21
	if (!keyword)
Packit fb9d21
		return ISL_TOKEN_ERROR;
Packit fb9d21
	keyword->type = s->next_type++;
Packit fb9d21
	keyword->name = strdup(name);
Packit fb9d21
	if (!keyword->name) {
Packit fb9d21
		free(keyword);
Packit fb9d21
		return ISL_TOKEN_ERROR;
Packit fb9d21
	}
Packit fb9d21
	entry->data = keyword;
Packit fb9d21
Packit fb9d21
	return keyword->type;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_token *isl_token_new(isl_ctx *ctx,
Packit fb9d21
	int line, int col, unsigned on_new_line)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return NULL;
Packit fb9d21
	tok->line = line;
Packit fb9d21
	tok->col = col;
Packit fb9d21
	tok->on_new_line = on_new_line;
Packit fb9d21
	tok->is_keyword = 0;
Packit fb9d21
	tok->u.s = NULL;
Packit fb9d21
	return tok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the type of "tok".
Packit fb9d21
 */
Packit fb9d21
int isl_token_get_type(struct isl_token *tok)
Packit fb9d21
{
Packit fb9d21
	return tok ? tok->type : ISL_TOKEN_ERROR;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a token of type ISL_TOKEN_VALUE, return the value it represents.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_val *isl_token_get_val(isl_ctx *ctx, struct isl_token *tok)
Packit fb9d21
{
Packit fb9d21
	if (!tok)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (tok->type != ISL_TOKEN_VALUE)
Packit fb9d21
		isl_die(ctx, isl_error_invalid, "not a value token",
Packit fb9d21
			return NULL);
Packit fb9d21
Packit fb9d21
	return isl_val_int_from_isl_int(ctx, tok->u.v);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a token with a string representation, return a copy of this string.
Packit fb9d21
 */
Packit fb9d21
__isl_give char *isl_token_get_str(isl_ctx *ctx, struct isl_token *tok)
Packit fb9d21
{
Packit fb9d21
	if (!tok)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (!tok->u.s)
Packit fb9d21
		isl_die(ctx, isl_error_invalid,
Packit fb9d21
			"token does not have a string representation",
Packit fb9d21
			return NULL);
Packit fb9d21
Packit fb9d21
	return strdup(tok->u.s);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_token_free(struct isl_token *tok)
Packit fb9d21
{
Packit fb9d21
	if (!tok)
Packit fb9d21
		return;
Packit fb9d21
	if (tok->type == ISL_TOKEN_VALUE)
Packit fb9d21
		isl_int_clear(tok->u.v);
Packit fb9d21
	else if (tok->type == ISL_TOKEN_MAP)
Packit fb9d21
		isl_map_free(tok->u.map);
Packit fb9d21
	else if (tok->type == ISL_TOKEN_AFF)
Packit fb9d21
		isl_pw_aff_free(tok->u.pwaff);
Packit fb9d21
	else
Packit fb9d21
		free(tok->u.s);
Packit fb9d21
	free(tok);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_stream_error(__isl_keep isl_stream *s, struct isl_token *tok,
Packit fb9d21
	char *msg)
Packit fb9d21
{
Packit fb9d21
	int line = tok ? tok->line : s->line;
Packit fb9d21
	int col = tok ? tok->col : s->col;
Packit fb9d21
	fprintf(stderr, "syntax error (%d, %d): %s\n", line, col, msg);
Packit fb9d21
	if (tok) {
Packit fb9d21
		if (tok->type < 256)
Packit fb9d21
			fprintf(stderr, "got '%c'\n", tok->type);
Packit fb9d21
		else if (tok->type == ISL_TOKEN_IDENT)
Packit fb9d21
			fprintf(stderr, "got ident '%s'\n", tok->u.s);
Packit fb9d21
		else if (tok->is_keyword)
Packit fb9d21
			fprintf(stderr, "got keyword '%s'\n", tok->u.s);
Packit fb9d21
		else if (tok->type == ISL_TOKEN_VALUE) {
Packit fb9d21
			fprintf(stderr, "got value '");
Packit fb9d21
			isl_int_print(stderr, tok->u.v, 0);
Packit fb9d21
			fprintf(stderr, "'\n");
Packit fb9d21
		} else if (tok->type == ISL_TOKEN_MAP) {
Packit fb9d21
			isl_printer *p;
Packit fb9d21
			fprintf(stderr, "got map '");
Packit fb9d21
			p = isl_printer_to_file(s->ctx, stderr);
Packit fb9d21
			p = isl_printer_print_map(p, tok->u.map);
Packit fb9d21
			isl_printer_free(p);
Packit fb9d21
			fprintf(stderr, "'\n");
Packit fb9d21
		} else if (tok->type == ISL_TOKEN_AFF) {
Packit fb9d21
			isl_printer *p;
Packit fb9d21
			fprintf(stderr, "got affine expression '");
Packit fb9d21
			p = isl_printer_to_file(s->ctx, stderr);
Packit fb9d21
			p = isl_printer_print_pw_aff(p, tok->u.pwaff);
Packit fb9d21
			isl_printer_free(p);
Packit fb9d21
			fprintf(stderr, "'\n");
Packit fb9d21
		} else if (tok->u.s)
Packit fb9d21
			fprintf(stderr, "got token '%s'\n", tok->u.s);
Packit fb9d21
		else
Packit fb9d21
			fprintf(stderr, "got token type %d\n", tok->type);
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static __isl_give isl_stream* isl_stream_new(struct isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	isl_stream *s = isl_calloc_type(ctx, struct isl_stream);
Packit fb9d21
	if (!s)
Packit fb9d21
		return NULL;
Packit fb9d21
	s->ctx = ctx;
Packit fb9d21
	isl_ctx_ref(s->ctx);
Packit fb9d21
	s->file = NULL;
Packit fb9d21
	s->str = NULL;
Packit fb9d21
	s->len = 0;
Packit fb9d21
	s->line = 1;
Packit fb9d21
	s->col = 1;
Packit fb9d21
	s->eof = 0;
Packit fb9d21
	s->last_line = 0;
Packit fb9d21
	s->c = -1;
Packit fb9d21
	s->n_un = 0;
Packit fb9d21
	for (i = 0; i < 5; ++i)
Packit fb9d21
		s->tokens[i] = NULL;
Packit fb9d21
	s->n_token = 0;
Packit fb9d21
	s->keywords = NULL;
Packit fb9d21
	s->size = 256;
Packit fb9d21
	s->buffer = isl_alloc_array(ctx, char, s->size);
Packit fb9d21
	if (!s->buffer)
Packit fb9d21
		goto error;
Packit fb9d21
	return s;
Packit fb9d21
error:
Packit fb9d21
	isl_stream_free(s);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_stream* isl_stream_new_file(struct isl_ctx *ctx, FILE *file)
Packit fb9d21
{
Packit fb9d21
	isl_stream *s = isl_stream_new(ctx);
Packit fb9d21
	if (!s)
Packit fb9d21
		return NULL;
Packit fb9d21
	s->file = file;
Packit fb9d21
	return s;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
Packit fb9d21
{
Packit fb9d21
	isl_stream *s;
Packit fb9d21
	if (!str)
Packit fb9d21
		return NULL;
Packit fb9d21
	s = isl_stream_new(ctx);
Packit fb9d21
	if (!s)
Packit fb9d21
		return NULL;
Packit fb9d21
	s->str = str;
Packit fb9d21
	return s;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Read a character from the stream and advance s->line and s->col
Packit fb9d21
 * to point to the next character.
Packit fb9d21
 */
Packit fb9d21
static int stream_getc(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	int c;
Packit fb9d21
	if (s->eof)
Packit fb9d21
		return -1;
Packit fb9d21
	if (s->n_un)
Packit fb9d21
		return s->c = s->un[--s->n_un];
Packit fb9d21
	if (s->file)
Packit fb9d21
		c = fgetc(s->file);
Packit fb9d21
	else {
Packit fb9d21
		c = *s->str++;
Packit fb9d21
		if (c == '\0')
Packit fb9d21
			c = -1;
Packit fb9d21
	}
Packit fb9d21
	if (c == -1)
Packit fb9d21
		s->eof = 1;
Packit fb9d21
	else if (c == '\n') {
Packit fb9d21
		s->line++;
Packit fb9d21
		s->col = 1;
Packit fb9d21
	} else
Packit fb9d21
		s->col++;
Packit fb9d21
	s->c = c;
Packit fb9d21
	return c;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static void isl_stream_ungetc(__isl_keep isl_stream *s, int c)
Packit fb9d21
{
Packit fb9d21
	isl_assert(s->ctx, s->n_un < 5, return);
Packit fb9d21
	s->un[s->n_un++] = c;
Packit fb9d21
	s->c = -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Read a character from the stream, skipping pairs of '\\' and '\n'.
Packit fb9d21
 * Set s->start_line and s->start_col to the line and column
Packit fb9d21
 * of the returned character.
Packit fb9d21
 */
Packit fb9d21
static int isl_stream_getc(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	int c;
Packit fb9d21
Packit fb9d21
	do {
Packit fb9d21
		s->start_line = s->line;
Packit fb9d21
		s->start_col = s->col;
Packit fb9d21
		c = stream_getc(s);
Packit fb9d21
		if (c != '\\')
Packit fb9d21
			return c;
Packit fb9d21
		c = stream_getc(s);
Packit fb9d21
	} while (c == '\n');
Packit fb9d21
Packit fb9d21
	isl_stream_ungetc(s, c);
Packit fb9d21
Packit fb9d21
	return '\\';
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static int isl_stream_push_char(__isl_keep isl_stream *s, int c)
Packit fb9d21
{
Packit fb9d21
	if (s->len >= s->size) {
Packit fb9d21
		char *buffer;
Packit fb9d21
		s->size = (3*s->size)/2;
Packit fb9d21
		buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
Packit fb9d21
		if (!buffer)
Packit fb9d21
			return -1;
Packit fb9d21
		s->buffer = buffer;
Packit fb9d21
	}
Packit fb9d21
	s->buffer[s->len++] = c;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_stream_push_token(__isl_keep isl_stream *s, struct isl_token *tok)
Packit fb9d21
{
Packit fb9d21
	isl_assert(s->ctx, s->n_token < 5, return);
Packit fb9d21
	s->tokens[s->n_token++] = tok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static enum isl_token_type check_keywords(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
	struct isl_keyword *keyword;
Packit fb9d21
	uint32_t name_hash;
Packit fb9d21
Packit fb9d21
	if (!strcasecmp(s->buffer, "exists"))
Packit fb9d21
		return ISL_TOKEN_EXISTS;
Packit fb9d21
	if (!strcasecmp(s->buffer, "and"))
Packit fb9d21
		return ISL_TOKEN_AND;
Packit fb9d21
	if (!strcasecmp(s->buffer, "or"))
Packit fb9d21
		return ISL_TOKEN_OR;
Packit fb9d21
	if (!strcasecmp(s->buffer, "implies"))
Packit fb9d21
		return ISL_TOKEN_IMPLIES;
Packit fb9d21
	if (!strcasecmp(s->buffer, "not"))
Packit fb9d21
		return ISL_TOKEN_NOT;
Packit fb9d21
	if (!strcasecmp(s->buffer, "infty"))
Packit fb9d21
		return ISL_TOKEN_INFTY;
Packit fb9d21
	if (!strcasecmp(s->buffer, "infinity"))
Packit fb9d21
		return ISL_TOKEN_INFTY;
Packit fb9d21
	if (!strcasecmp(s->buffer, "NaN"))
Packit fb9d21
		return ISL_TOKEN_NAN;
Packit fb9d21
	if (!strcasecmp(s->buffer, "min"))
Packit fb9d21
		return ISL_TOKEN_MIN;
Packit fb9d21
	if (!strcasecmp(s->buffer, "max"))
Packit fb9d21
		return ISL_TOKEN_MAX;
Packit fb9d21
	if (!strcasecmp(s->buffer, "rat"))
Packit fb9d21
		return ISL_TOKEN_RAT;
Packit fb9d21
	if (!strcasecmp(s->buffer, "true"))
Packit fb9d21
		return ISL_TOKEN_TRUE;
Packit fb9d21
	if (!strcasecmp(s->buffer, "false"))
Packit fb9d21
		return ISL_TOKEN_FALSE;
Packit fb9d21
	if (!strcasecmp(s->buffer, "ceild"))
Packit fb9d21
		return ISL_TOKEN_CEILD;
Packit fb9d21
	if (!strcasecmp(s->buffer, "floord"))
Packit fb9d21
		return ISL_TOKEN_FLOORD;
Packit fb9d21
	if (!strcasecmp(s->buffer, "mod"))
Packit fb9d21
		return ISL_TOKEN_MOD;
Packit fb9d21
	if (!strcasecmp(s->buffer, "ceil"))
Packit fb9d21
		return ISL_TOKEN_CEIL;
Packit fb9d21
	if (!strcasecmp(s->buffer, "floor"))
Packit fb9d21
		return ISL_TOKEN_FLOOR;
Packit fb9d21
Packit fb9d21
	if (!s->keywords)
Packit fb9d21
		return ISL_TOKEN_IDENT;
Packit fb9d21
Packit fb9d21
	name_hash = isl_hash_string(isl_hash_init(), s->buffer);
Packit fb9d21
	entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
Packit fb9d21
					s->buffer, 0);
Packit fb9d21
	if (entry) {
Packit fb9d21
		keyword = entry->data;
Packit fb9d21
		return keyword->type;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return ISL_TOKEN_IDENT;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_stream_skip_line(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	int c;
Packit fb9d21
Packit fb9d21
	while ((c = isl_stream_getc(s)) != -1 && c != '\n')
Packit fb9d21
		/* nothing */
Packit fb9d21
		;
Packit fb9d21
Packit fb9d21
	return c == -1 ? -1 : 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_token *next_token(__isl_keep isl_stream *s, int same_line)
Packit fb9d21
{
Packit fb9d21
	int c;
Packit fb9d21
	struct isl_token *tok = NULL;
Packit fb9d21
	int line, col;
Packit fb9d21
	int old_line = s->last_line;
Packit fb9d21
Packit fb9d21
	if (s->n_token) {
Packit fb9d21
		if (same_line && s->tokens[s->n_token - 1]->on_new_line)
Packit fb9d21
			return NULL;
Packit fb9d21
		return s->tokens[--s->n_token];
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (same_line && s->c == '\n')
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	s->len = 0;
Packit fb9d21
Packit fb9d21
	/* skip spaces and comment lines */
Packit fb9d21
	while ((c = isl_stream_getc(s)) != -1) {
Packit fb9d21
		if (c == '#') {
Packit fb9d21
			if (isl_stream_skip_line(s) < 0)
Packit fb9d21
				break;
Packit fb9d21
			c = '\n';
Packit fb9d21
			if (same_line)
Packit fb9d21
				break;
Packit fb9d21
		} else if (!isspace(c) || (same_line && c == '\n'))
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	line = s->start_line;
Packit fb9d21
	col = s->start_col;
Packit fb9d21
Packit fb9d21
	if (c == -1 || (same_line && c == '\n'))
Packit fb9d21
		return NULL;
Packit fb9d21
	s->last_line = line;
Packit fb9d21
Packit fb9d21
	if (c == '(' ||
Packit fb9d21
	    c == ')' ||
Packit fb9d21
	    c == '+' ||
Packit fb9d21
	    c == '*' ||
Packit fb9d21
	    c == '%' ||
Packit fb9d21
	    c == '?' ||
Packit fb9d21
	    c == '^' ||
Packit fb9d21
	    c == '@' ||
Packit fb9d21
	    c == '$' ||
Packit fb9d21
	    c == ',' ||
Packit fb9d21
	    c == '.' ||
Packit fb9d21
	    c == ';' ||
Packit fb9d21
	    c == '[' ||
Packit fb9d21
	    c == ']' ||
Packit fb9d21
	    c == '{' ||
Packit fb9d21
	    c == '}') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		tok->type = (enum isl_token_type)c;
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '-') {
Packit fb9d21
		int c;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '>') {
Packit fb9d21
			tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
			if (!tok)
Packit fb9d21
				return NULL;
Packit fb9d21
			tok->u.s = strdup("->");
Packit fb9d21
			tok->type = ISL_TOKEN_TO;
Packit fb9d21
			return tok;
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		if (!isdigit(c)) {
Packit fb9d21
			tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
			if (!tok)
Packit fb9d21
				return NULL;
Packit fb9d21
			tok->type = (enum isl_token_type) '-';
Packit fb9d21
			return tok;
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
	if (c == '-' || isdigit(c)) {
Packit fb9d21
		int minus = c == '-';
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		tok->type = ISL_TOKEN_VALUE;
Packit fb9d21
		isl_int_init(tok->u.v);
Packit fb9d21
		if (isl_stream_push_char(s, c))
Packit fb9d21
			goto error;
Packit fb9d21
		while ((c = isl_stream_getc(s)) != -1 && isdigit(c))
Packit fb9d21
			if (isl_stream_push_char(s, c))
Packit fb9d21
				goto error;
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		isl_stream_push_char(s, '\0');
Packit fb9d21
		isl_int_read(tok->u.v, s->buffer);
Packit fb9d21
		if (minus && isl_int_is_zero(tok->u.v)) {
Packit fb9d21
			tok->col++;
Packit fb9d21
			tok->on_new_line = 0;
Packit fb9d21
			isl_stream_push_token(s, tok);
Packit fb9d21
			tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
			if (!tok)
Packit fb9d21
				return NULL;
Packit fb9d21
			tok->type = (enum isl_token_type) '-';
Packit fb9d21
		}
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (isalpha(c) || c == '_') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		isl_stream_push_char(s, c);
Packit fb9d21
		while ((c = isl_stream_getc(s)) != -1 &&
Packit fb9d21
				(isalnum(c) || c == '_'))
Packit fb9d21
			isl_stream_push_char(s, c);
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		while ((c = isl_stream_getc(s)) != -1 && c == '\'')
Packit fb9d21
			isl_stream_push_char(s, c);
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		isl_stream_push_char(s, '\0');
Packit fb9d21
		tok->type = check_keywords(s);
Packit fb9d21
		if (tok->type != ISL_TOKEN_IDENT)
Packit fb9d21
			tok->is_keyword = 1;
Packit fb9d21
		tok->u.s = strdup(s->buffer);
Packit fb9d21
		if (!tok->u.s)
Packit fb9d21
			goto error;
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '"') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		tok->type = ISL_TOKEN_STRING;
Packit fb9d21
		tok->u.s = NULL;
Packit fb9d21
		while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
Packit fb9d21
			isl_stream_push_char(s, c);
Packit fb9d21
		if (c != '"') {
Packit fb9d21
			isl_stream_error(s, NULL, "unterminated string");
Packit fb9d21
			goto error;
Packit fb9d21
		}
Packit fb9d21
		isl_stream_push_char(s, '\0');
Packit fb9d21
		tok->u.s = strdup(s->buffer);
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '=') {
Packit fb9d21
		int c;
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
			tok->u.s = strdup("==");
Packit fb9d21
			tok->type = ISL_TOKEN_EQ_EQ;
Packit fb9d21
			return tok;
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		tok->type = (enum isl_token_type) '=';
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == ':') {
Packit fb9d21
		int c;
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
			tok->u.s = strdup(":=");
Packit fb9d21
			tok->type = ISL_TOKEN_DEF;
Packit fb9d21
			return tok;
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		tok->type = (enum isl_token_type) ':';
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '>') {
Packit fb9d21
		int c;
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
			tok->u.s = strdup(">=");
Packit fb9d21
			tok->type = ISL_TOKEN_GE;
Packit fb9d21
			return tok;
Packit fb9d21
		} else if (c == '>') {
Packit fb9d21
			if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
				tok->u.s = strdup(">>=");
Packit fb9d21
				tok->type = ISL_TOKEN_LEX_GE;
Packit fb9d21
				return tok;
Packit fb9d21
			}
Packit fb9d21
			tok->u.s = strdup(">>");
Packit fb9d21
			tok->type = ISL_TOKEN_LEX_GT;
Packit fb9d21
		} else {
Packit fb9d21
			tok->u.s = strdup(">");
Packit fb9d21
			tok->type = ISL_TOKEN_GT;
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '<') {
Packit fb9d21
		int c;
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
			tok->u.s = strdup("<=");
Packit fb9d21
			tok->type = ISL_TOKEN_LE;
Packit fb9d21
			return tok;
Packit fb9d21
		} else if (c == '<') {
Packit fb9d21
			if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
				tok->u.s = strdup("<<=");
Packit fb9d21
				tok->type = ISL_TOKEN_LEX_LE;
Packit fb9d21
				return tok;
Packit fb9d21
			}
Packit fb9d21
			tok->u.s = strdup("<<");
Packit fb9d21
			tok->type = ISL_TOKEN_LEX_LT;
Packit fb9d21
		} else {
Packit fb9d21
			tok->u.s = strdup("<");
Packit fb9d21
			tok->type = ISL_TOKEN_LT;
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '&') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		tok->type = ISL_TOKEN_AND;
Packit fb9d21
		if ((c = isl_stream_getc(s)) != '&' && c != -1) {
Packit fb9d21
			tok->u.s = strdup("&";;
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		} else
Packit fb9d21
			tok->u.s = strdup("&&";;
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '|') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		tok->type = ISL_TOKEN_OR;
Packit fb9d21
		if ((c = isl_stream_getc(s)) != '|' && c != -1) {
Packit fb9d21
			tok->u.s = strdup("|");
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		} else
Packit fb9d21
			tok->u.s = strdup("||");
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '/') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
Packit fb9d21
			tok->type = (enum isl_token_type) '/';
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		} else {
Packit fb9d21
			tok->u.s = strdup("/\\");
Packit fb9d21
			tok->type = ISL_TOKEN_AND;
Packit fb9d21
		}
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '\\') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) != '/' && c != -1) {
Packit fb9d21
			tok->type = (enum isl_token_type) '\\';
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		} else {
Packit fb9d21
			tok->u.s = strdup("\\/");
Packit fb9d21
			tok->type = ISL_TOKEN_OR;
Packit fb9d21
		}
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
	if (c == '!') {
Packit fb9d21
		tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return NULL;
Packit fb9d21
		if ((c = isl_stream_getc(s)) == '=') {
Packit fb9d21
			tok->u.s = strdup("!=");
Packit fb9d21
			tok->type = ISL_TOKEN_NE;
Packit fb9d21
			return tok;
Packit fb9d21
		} else {
Packit fb9d21
			tok->type = ISL_TOKEN_NOT;
Packit fb9d21
			tok->u.s = strdup("!");
Packit fb9d21
		}
Packit fb9d21
		if (c != -1)
Packit fb9d21
			isl_stream_ungetc(s, c);
Packit fb9d21
		return tok;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	tok = isl_token_new(s->ctx, line, col, old_line != line);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return NULL;
Packit fb9d21
	tok->type = ISL_TOKEN_UNKNOWN;
Packit fb9d21
	return tok;
Packit fb9d21
error:
Packit fb9d21
	isl_token_free(tok);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_token *isl_stream_next_token(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	return next_token(s, 0);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_token *isl_stream_next_token_on_same_line(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	return next_token(s, 1);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_stream_eat_if_available(__isl_keep isl_stream *s, int type)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return 0;
Packit fb9d21
	if (tok->type == type) {
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return 1;
Packit fb9d21
	}
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_stream_next_token_is(__isl_keep isl_stream *s, int type)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	int r;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return 0;
Packit fb9d21
	r = tok->type == type;
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
	return r;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
char *isl_stream_read_ident_if_available(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (tok->type == ISL_TOKEN_IDENT) {
Packit fb9d21
		char *ident = strdup(tok->u.s);
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return ident;
Packit fb9d21
	}
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_stream_eat(__isl_keep isl_stream *s, int type)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return -1;
Packit fb9d21
	if (tok->type == type) {
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
	isl_stream_error(s, tok, "expecting other token");
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_stream_is_empty(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
Packit fb9d21
	if (!tok)
Packit fb9d21
		return 1;
Packit fb9d21
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static isl_stat free_keyword(void **p, void *user)
Packit fb9d21
{
Packit fb9d21
	struct isl_keyword *keyword = *p;
Packit fb9d21
Packit fb9d21
	free(keyword->name);
Packit fb9d21
	free(keyword);
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_stream_flush_tokens(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!s)
Packit fb9d21
		return;
Packit fb9d21
	for (i = 0; i < s->n_token; ++i)
Packit fb9d21
		isl_token_free(s->tokens[i]);
Packit fb9d21
	s->n_token = 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_stream_get_ctx(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	return s ? s->ctx : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_stream_free(__isl_take isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	if (!s)
Packit fb9d21
		return;
Packit fb9d21
	free(s->buffer);
Packit fb9d21
	if (s->n_token != 0) {
Packit fb9d21
		struct isl_token *tok = isl_stream_next_token(s);
Packit fb9d21
		isl_stream_error(s, tok, "unexpected token");
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
	}
Packit fb9d21
	if (s->keywords) {
Packit fb9d21
		isl_hash_table_foreach(s->ctx, s->keywords, &free_keyword, NULL);
Packit fb9d21
		isl_hash_table_free(s->ctx, s->keywords);
Packit fb9d21
	}
Packit fb9d21
	free(s->yaml_state);
Packit fb9d21
	free(s->yaml_indent);
Packit fb9d21
	isl_ctx_deref(s->ctx);
Packit fb9d21
	free(s);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Push "state" onto the stack of currently active YAML elements.
Packit fb9d21
 * The caller is responsible for setting the corresponding indentation.
Packit fb9d21
 * Return 0 on success and -1 on failure.
Packit fb9d21
 */
Packit fb9d21
static int push_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
Packit fb9d21
{
Packit fb9d21
	if (s->yaml_size < s->yaml_depth + 1) {
Packit fb9d21
		int *indent;
Packit fb9d21
		enum isl_yaml_state *state;
Packit fb9d21
Packit fb9d21
		state = isl_realloc_array(s->ctx, s->yaml_state,
Packit fb9d21
					enum isl_yaml_state, s->yaml_depth + 1);
Packit fb9d21
		if (!state)
Packit fb9d21
			return -1;
Packit fb9d21
		s->yaml_state = state;
Packit fb9d21
Packit fb9d21
		indent = isl_realloc_array(s->ctx, s->yaml_indent,
Packit fb9d21
					int, s->yaml_depth + 1);
Packit fb9d21
		if (!indent)
Packit fb9d21
			return -1;
Packit fb9d21
		s->yaml_indent = indent;
Packit fb9d21
Packit fb9d21
		s->yaml_size = s->yaml_depth + 1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	s->yaml_state[s->yaml_depth] = state;
Packit fb9d21
	s->yaml_depth++;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Remove the innermost active YAML element from the stack.
Packit fb9d21
 * Return 0 on success and -1 on failure.
Packit fb9d21
 */
Packit fb9d21
static int pop_state(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	if (!s)
Packit fb9d21
		return -1;
Packit fb9d21
	if (s->yaml_depth < 1)
Packit fb9d21
		isl_die(isl_stream_get_ctx(s), isl_error_invalid,
Packit fb9d21
			"not in YAML construct", return -1);
Packit fb9d21
Packit fb9d21
	s->yaml_depth--;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the state of the innermost active YAML element to "state".
Packit fb9d21
 * Return 0 on success and -1 on failure.
Packit fb9d21
 */
Packit fb9d21
static int update_state(__isl_keep isl_stream *s, enum isl_yaml_state state)
Packit fb9d21
{
Packit fb9d21
	if (!s)
Packit fb9d21
		return -1;
Packit fb9d21
	if (s->yaml_depth < 1)
Packit fb9d21
		isl_die(isl_stream_get_ctx(s), isl_error_invalid,
Packit fb9d21
			"not in YAML construct", return -1);
Packit fb9d21
Packit fb9d21
	s->yaml_state[s->yaml_depth - 1] = state;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the state of the innermost active YAML element.
Packit fb9d21
 * Return isl_yaml_none if we are not inside any YAML element.
Packit fb9d21
 */
Packit fb9d21
static enum isl_yaml_state current_state(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	if (!s)
Packit fb9d21
		return isl_yaml_none;
Packit fb9d21
	if (s->yaml_depth < 1)
Packit fb9d21
		return isl_yaml_none;
Packit fb9d21
	return s->yaml_state[s->yaml_depth - 1];
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the indentation of the innermost active YAML element to "indent".
Packit fb9d21
 * If "indent" is equal to ISL_YAML_INDENT_FLOW, then this means
Packit fb9d21
 * that the current elemient is in flow format.
Packit fb9d21
 */
Packit fb9d21
static int set_yaml_indent(__isl_keep isl_stream *s, int indent)
Packit fb9d21
{
Packit fb9d21
	if (s->yaml_depth < 1)
Packit fb9d21
		isl_die(s->ctx, isl_error_internal,
Packit fb9d21
			"not in YAML element", return -1);
Packit fb9d21
Packit fb9d21
	s->yaml_indent[s->yaml_depth - 1] = indent;
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the indentation of the innermost active YAML element
Packit fb9d21
 * of -1 on error.
Packit fb9d21
 */
Packit fb9d21
static int get_yaml_indent(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	if (s->yaml_depth < 1)
Packit fb9d21
		isl_die(s->ctx, isl_error_internal,
Packit fb9d21
			"not in YAML element", return -1);
Packit fb9d21
Packit fb9d21
	return s->yaml_indent[s->yaml_depth - 1];
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Move to the next state at the innermost level.
Packit fb9d21
 * Return 1 if successful.
Packit fb9d21
 * Return 0 if we are at the end of the innermost level.
Packit fb9d21
 * Return -1 on error.
Packit fb9d21
 *
Packit fb9d21
 * If we are in state isl_yaml_mapping_key_start, then we have just
Packit fb9d21
 * started a mapping and we are expecting a key.  If the mapping started
Packit fb9d21
 * with a '{', then we check if the next token is a '}'.  If so,
Packit fb9d21
 * then the mapping is empty and there is no next state at this level.
Packit fb9d21
 * Otherwise, we assume that there is at least one key (the one from
Packit fb9d21
 * which we derived the indentation in isl_stream_yaml_read_start_mapping.
Packit fb9d21
 *
Packit fb9d21
 * If we are in state isl_yaml_mapping_key, then the we expect a colon
Packit fb9d21
 * followed by a value, so there is always a next state unless
Packit fb9d21
 * some error occurs.
Packit fb9d21
 *
Packit fb9d21
 * If we are in state isl_yaml_mapping_val, then there may or may
Packit fb9d21
 * not be a subsequent key in the same mapping.
Packit fb9d21
 * In flow format, the next key is preceded by a comma.
Packit fb9d21
 * In block format, the next key has the same indentation as the first key.
Packit fb9d21
 * If the first token has a smaller indentation, then we have reached
Packit fb9d21
 * the end of the current mapping.
Packit fb9d21
 *
Packit fb9d21
 * If we are in state isl_yaml_sequence_start, then we have just
Packit fb9d21
 * started a sequence.  If the sequence started with a '[',
Packit fb9d21
 * then we check if the next token is a ']'.  If so, then the sequence
Packit fb9d21
 * is empty and there is no next state at this level.
Packit fb9d21
 * Otherwise, we assume that there is at least one element in the sequence
Packit fb9d21
 * (the one from which we derived the indentation in
Packit fb9d21
 * isl_stream_yaml_read_start_sequence.
Packit fb9d21
 *
Packit fb9d21
 * If we are in state isl_yaml_sequence, then there may or may
Packit fb9d21
 * not be a subsequent element in the same sequence.
Packit fb9d21
 * In flow format, the next element is preceded by a comma.
Packit fb9d21
 * In block format, the next element is introduced by a dash with
Packit fb9d21
 * the same indentation as that of the first element.
Packit fb9d21
 * If the first token is not a dash or if it has a smaller indentation,
Packit fb9d21
 * then we have reached the end of the current sequence.
Packit fb9d21
 */
Packit fb9d21
int isl_stream_yaml_next(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	enum isl_yaml_state state;
Packit fb9d21
	int indent;
Packit fb9d21
Packit fb9d21
	state = current_state(s);
Packit fb9d21
	if (state == isl_yaml_none)
Packit fb9d21
		isl_die(s->ctx, isl_error_invalid,
Packit fb9d21
			"not in YAML element", return -1);
Packit fb9d21
	switch (state) {
Packit fb9d21
	case isl_yaml_mapping_key_start:
Packit fb9d21
		if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW &&
Packit fb9d21
		    isl_stream_next_token_is(s, '}'))
Packit fb9d21
			return 0;
Packit fb9d21
		if (update_state(s, isl_yaml_mapping_key) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		return 1;
Packit fb9d21
	case isl_yaml_mapping_key:
Packit fb9d21
		tok = isl_stream_next_token(s);
Packit fb9d21
		if (!tok) {
Packit fb9d21
			if (s->eof)
Packit fb9d21
				isl_stream_error(s, NULL, "unexpected EOF");
Packit fb9d21
			return -1;
Packit fb9d21
		}
Packit fb9d21
		if (tok->type == ':') {
Packit fb9d21
			isl_token_free(tok);
Packit fb9d21
			if (update_state(s, isl_yaml_mapping_val) < 0)
Packit fb9d21
				return -1;
Packit fb9d21
			return 1;
Packit fb9d21
		}
Packit fb9d21
		isl_stream_error(s, tok, "expecting ':'");
Packit fb9d21
		isl_stream_push_token(s, tok);
Packit fb9d21
		return -1;
Packit fb9d21
	case isl_yaml_mapping_val:
Packit fb9d21
		if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
Packit fb9d21
			if (!isl_stream_eat_if_available(s, ','))
Packit fb9d21
				return 0;
Packit fb9d21
			if (update_state(s, isl_yaml_mapping_key) < 0)
Packit fb9d21
				return -1;
Packit fb9d21
			return 1;
Packit fb9d21
		}
Packit fb9d21
		tok = isl_stream_next_token(s);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return 0;
Packit fb9d21
		indent = tok->col - 1;
Packit fb9d21
		isl_stream_push_token(s, tok);
Packit fb9d21
		if (indent < get_yaml_indent(s))
Packit fb9d21
			return 0;
Packit fb9d21
		if (update_state(s, isl_yaml_mapping_key) < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		return 1;
Packit fb9d21
	case isl_yaml_sequence_start:
Packit fb9d21
		if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
Packit fb9d21
			if (isl_stream_next_token_is(s, ']'))
Packit fb9d21
				return 0;
Packit fb9d21
			if (update_state(s, isl_yaml_sequence) < 0)
Packit fb9d21
				return -1;
Packit fb9d21
			return 1;
Packit fb9d21
		}
Packit fb9d21
		tok = isl_stream_next_token(s);
Packit fb9d21
		if (!tok) {
Packit fb9d21
			if (s->eof)
Packit fb9d21
				isl_stream_error(s, NULL, "unexpected EOF");
Packit fb9d21
			return -1;
Packit fb9d21
		}
Packit fb9d21
		if (tok->type == '-') {
Packit fb9d21
			isl_token_free(tok);
Packit fb9d21
			if (update_state(s, isl_yaml_sequence) < 0)
Packit fb9d21
				return -1;
Packit fb9d21
			return 1;
Packit fb9d21
		}
Packit fb9d21
		isl_stream_error(s, tok, "expecting '-'");
Packit fb9d21
		isl_stream_push_token(s, tok);
Packit fb9d21
		return 0;
Packit fb9d21
	case isl_yaml_sequence:
Packit fb9d21
		if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW)
Packit fb9d21
			return isl_stream_eat_if_available(s, ',');
Packit fb9d21
		tok = isl_stream_next_token(s);
Packit fb9d21
		if (!tok)
Packit fb9d21
			return 0;
Packit fb9d21
		indent = tok->col - 1;
Packit fb9d21
		if (indent < get_yaml_indent(s) || tok->type != '-') {
Packit fb9d21
			isl_stream_push_token(s, tok);
Packit fb9d21
			return 0;
Packit fb9d21
		}
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return 1;
Packit fb9d21
	default:
Packit fb9d21
		isl_die(s->ctx, isl_error_internal,
Packit fb9d21
			"unexpected state", return 0);
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Start reading a YAML mapping.
Packit fb9d21
 * Return 0 on success and -1 on error.
Packit fb9d21
 *
Packit fb9d21
 * If the first token on the stream is a '{' then we remove this token
Packit fb9d21
 * from the stream and keep track of the fact that the mapping
Packit fb9d21
 * is given in flow format.
Packit fb9d21
 * Otherwise, we assume the first token is the first key of the mapping and
Packit fb9d21
 * keep track of its indentation, but keep the token on the stream.
Packit fb9d21
 * In both cases, the next token we expect is the first key of the mapping.
Packit fb9d21
 */
Packit fb9d21
int isl_stream_yaml_read_start_mapping(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	int indent;
Packit fb9d21
Packit fb9d21
	if (push_state(s, isl_yaml_mapping_key_start) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok) {
Packit fb9d21
		if (s->eof)
Packit fb9d21
			isl_stream_error(s, NULL, "unexpected EOF");
Packit fb9d21
		return -1;
Packit fb9d21
	}
Packit fb9d21
	if (isl_token_get_type(tok) == '{') {
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
Packit fb9d21
	}
Packit fb9d21
	indent = tok->col - 1;
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
Packit fb9d21
	return set_yaml_indent(s, indent);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Finish reading a YAML mapping.
Packit fb9d21
 * Return 0 on success and -1 on error.
Packit fb9d21
 *
Packit fb9d21
 * If the mapping started with a '{', then we expect a '}' to close
Packit fb9d21
 * the mapping.
Packit fb9d21
 * Otherwise, we double-check that the next token (if any)
Packit fb9d21
 * has a smaller indentation than that of the current mapping.
Packit fb9d21
 */
Packit fb9d21
int isl_stream_yaml_read_end_mapping(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	int indent;
Packit fb9d21
Packit fb9d21
	if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
Packit fb9d21
		if (isl_stream_eat(s, '}') < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		return pop_state(s);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return pop_state(s);
Packit fb9d21
Packit fb9d21
	indent = tok->col - 1;
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
Packit fb9d21
	if (indent >= get_yaml_indent(s))
Packit fb9d21
		isl_die(isl_stream_get_ctx(s), isl_error_invalid,
Packit fb9d21
			"mapping not finished", return -1);
Packit fb9d21
Packit fb9d21
	return pop_state(s);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Start reading a YAML sequence.
Packit fb9d21
 * Return 0 on success and -1 on error.
Packit fb9d21
 *
Packit fb9d21
 * If the first token on the stream is a '[' then we remove this token
Packit fb9d21
 * from the stream and keep track of the fact that the sequence
Packit fb9d21
 * is given in flow format.
Packit fb9d21
 * Otherwise, we assume the first token is the dash that introduces
Packit fb9d21
 * the first element of the sequence and keep track of its indentation,
Packit fb9d21
 * but keep the token on the stream.
Packit fb9d21
 * In both cases, the next token we expect is the first element
Packit fb9d21
 * of the sequence.
Packit fb9d21
 */
Packit fb9d21
int isl_stream_yaml_read_start_sequence(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	int indent;
Packit fb9d21
Packit fb9d21
	if (push_state(s, isl_yaml_sequence_start) < 0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok) {
Packit fb9d21
		if (s->eof)
Packit fb9d21
			isl_stream_error(s, NULL, "unexpected EOF");
Packit fb9d21
		return -1;
Packit fb9d21
	}
Packit fb9d21
	if (isl_token_get_type(tok) == '[') {
Packit fb9d21
		isl_token_free(tok);
Packit fb9d21
		return set_yaml_indent(s, ISL_YAML_INDENT_FLOW);
Packit fb9d21
	}
Packit fb9d21
	indent = tok->col - 1;
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
Packit fb9d21
	return set_yaml_indent(s, indent);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Finish reading a YAML sequence.
Packit fb9d21
 * Return 0 on success and -1 on error.
Packit fb9d21
 *
Packit fb9d21
 * If the sequence started with a '[', then we expect a ']' to close
Packit fb9d21
 * the sequence.
Packit fb9d21
 * Otherwise, we double-check that the next token (if any)
Packit fb9d21
 * is not a dash or that it has a smaller indentation than
Packit fb9d21
 * that of the current sequence.
Packit fb9d21
 */
Packit fb9d21
int isl_stream_yaml_read_end_sequence(__isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	struct isl_token *tok;
Packit fb9d21
	int indent;
Packit fb9d21
	int dash;
Packit fb9d21
Packit fb9d21
	if (get_yaml_indent(s) == ISL_YAML_INDENT_FLOW) {
Packit fb9d21
		if (isl_stream_eat(s, ']') < 0)
Packit fb9d21
			return -1;
Packit fb9d21
		return pop_state(s);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	tok = isl_stream_next_token(s);
Packit fb9d21
	if (!tok)
Packit fb9d21
		return pop_state(s);
Packit fb9d21
Packit fb9d21
	indent = tok->col - 1;
Packit fb9d21
	dash = tok->type == '-';
Packit fb9d21
	isl_stream_push_token(s, tok);
Packit fb9d21
Packit fb9d21
	if (indent >= get_yaml_indent(s) && dash)
Packit fb9d21
		isl_die(isl_stream_get_ctx(s), isl_error_invalid,
Packit fb9d21
			"sequence not finished", return -1);
Packit fb9d21
Packit fb9d21
	return pop_state(s);
Packit fb9d21
}