+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
#include <ctype.h>
#include <string.h>
#include <strings.h>
if (!s)
return NULL;
s->ctx = ctx;
+ isl_ctx_ref(s->ctx);
s->size = 256;
s->file = NULL;
s->str = NULL;
s->tokens[s->n_token++] = tok;
}
-struct isl_token *isl_stream_next_token(struct isl_stream *s)
+static struct isl_token *next_token(struct isl_stream *s, int same_line)
{
int c;
struct isl_token *tok = NULL;
int line, col;
int old_line = s->line;
- if (s->n_token)
+ if (s->n_token) {
+ if (same_line && s->tokens[s->n_token - 1]->on_new_line)
+ return NULL;
return s->tokens[--s->n_token];
+ }
s->len = 0;
- /* skip spaces */
- while ((c = isl_stream_getc(s)) != -1 && isspace(c))
- /* nothing */
- ;
+ /* skip spaces and comment lines */
+ while ((c = isl_stream_getc(s)) != -1) {
+ if (c == '#') {
+ while ((c = isl_stream_getc(s)) != -1 && c != '\n')
+ /* nothing */
+ ;
+ if (c == -1 || (same_line && c == '\n'))
+ break;
+ } else if (!isspace(c) || (same_line && c == '\n'))
+ break;
+ }
line = s->line;
col = s->col;
- if (c == -1)
+ if (c == -1 || (same_line && c == '\n'))
return NULL;
if (c == '(' ||
c == ')' ||
}
if (c != -1)
isl_stream_ungetc(s, c);
+ if (!isdigit(c)) {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ tok->type = (enum isl_token_type) '-';
+ return tok;
+ }
}
if (c == '-' || isdigit(c)) {
tok = isl_token_new(s->ctx, line, col, old_line != line);
goto error;
if (c != -1)
isl_stream_ungetc(s, c);
- if (s->len == 1 && s->buffer[0] == '-')
- isl_int_set_si(tok->u.v, -1);
- else {
- isl_stream_push_char(s, '\0');
- isl_int_read(tok->u.v, s->buffer);
- }
+ isl_stream_push_char(s, '\0');
+ isl_int_read(tok->u.v, s->buffer);
return tok;
}
if (isalpha(c)) {
isl_stream_push_char(s, c);
if (c != -1)
isl_stream_ungetc(s, c);
+ while ((c = isl_stream_getc(s)) != -1 && c == '\'')
+ isl_stream_push_char(s, c);
+ if (c != -1)
+ isl_stream_ungetc(s, c);
isl_stream_push_char(s, '\0');
if (!strcasecmp(s->buffer, "exists"))
tok->type = ISL_TOKEN_EXISTS;
+ else if (!strcasecmp(s->buffer, "and"))
+ tok->type = ISL_TOKEN_AND;
+ else if (!strcasecmp(s->buffer, "or"))
+ tok->type = ISL_TOKEN_OR;
else {
tok->type = ISL_TOKEN_IDENT;
tok->u.s = strdup(s->buffer);
}
if (c == '>') {
int c;
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
if ((c = isl_stream_getc(s)) == '=') {
- tok = isl_token_new(s->ctx, line, col, old_line != line);
- if (!tok)
- return NULL;
tok->type = ISL_TOKEN_GE;
return tok;
}
if (c != -1)
isl_stream_ungetc(s, c);
+ tok->type = ISL_TOKEN_GT;
+ return tok;
}
if (c == '<') {
int c;
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
if ((c = isl_stream_getc(s)) == '=') {
- tok = isl_token_new(s->ctx, line, col, old_line != line);
- if (!tok)
- return NULL;
tok->type = ISL_TOKEN_LE;
return tok;
}
if (c != -1)
isl_stream_ungetc(s, c);
+ tok->type = ISL_TOKEN_LT;
+ return tok;
}
if (c == '&') {
tok = isl_token_new(s->ctx, line, col, old_line != line);
isl_stream_ungetc(s, c);
return tok;
}
+ if (c == '|') {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ tok->type = ISL_TOKEN_OR;
+ if ((c = isl_stream_getc(s)) != '|' && c != -1)
+ isl_stream_ungetc(s, c);
+ return tok;
+ }
tok = isl_token_new(s->ctx, line, col, old_line != line);
if (!tok)
return NULL;
}
+struct isl_token *isl_stream_next_token(struct isl_stream *s)
+{
+ return next_token(s, 0);
+}
+
+struct isl_token *isl_stream_next_token_on_same_line(struct isl_stream *s)
+{
+ return next_token(s, 1);
+}
+
int isl_stream_eat(struct isl_stream *s, int type)
{
struct isl_token *tok;
isl_stream_error(s, tok, "unexpected token");
isl_token_free(tok);
}
+ isl_ctx_deref(s->ctx);
free(s);
}