#include <isl_ctx.h>
#include "isl_stream.h"
+struct isl_keyword {
+ char *name;
+ enum isl_token_type type;
+};
+
+static int same_name(const void *entry, const void *val)
+{
+ const struct isl_keyword *keyword = (const struct isl_keyword *)entry;
+
+ return !strcmp(keyword->name, val);
+}
+
+enum isl_token_type isl_stream_register_keyword(struct isl_stream *s,
+ const char *name)
+{
+ struct isl_hash_table_entry *entry;
+ struct isl_keyword *keyword;
+ uint32_t name_hash;
+
+ if (!s->keywords) {
+ s->keywords = isl_hash_table_alloc(s->ctx, 10);
+ if (!s->keywords)
+ return ISL_TOKEN_ERROR;
+ s->next_type = ISL_TOKEN_LAST;
+ }
+
+ name_hash = isl_hash_string(isl_hash_init(), name);
+
+ entry = isl_hash_table_find(s->ctx, s->keywords, name_hash,
+ same_name, name, 1);
+ if (!entry)
+ return ISL_TOKEN_ERROR;
+ if (entry->data) {
+ keyword = entry->data;
+ return keyword->type;
+ }
+
+ keyword = isl_calloc_type(s->ctx, struct isl_keyword);
+ if (!keyword)
+ return ISL_TOKEN_ERROR;
+ keyword->type = s->next_type++;
+ keyword->name = strdup(name);
+ if (!keyword->name) {
+ free(keyword);
+ return ISL_TOKEN_ERROR;
+ }
+ entry->data = keyword;
+
+ return keyword->type;
+}
+
static struct isl_token *isl_token_new(struct isl_ctx *ctx,
int line, int col, unsigned on_new_line)
{
for (i = 0; i < 5; ++i)
s->tokens[i] = NULL;
s->n_token = 0;
+ s->keywords = NULL;
return s;
error:
isl_stream_free(s);
s->tokens[s->n_token++] = tok;
}
-struct isl_token *isl_stream_next_token(struct isl_stream *s)
+static enum isl_token_type check_keywords(struct isl_stream *s)
+{
+ struct isl_hash_table_entry *entry;
+ struct isl_keyword *keyword;
+ uint32_t name_hash;
+
+ if (!strcasecmp(s->buffer, "exists"))
+ return ISL_TOKEN_EXISTS;
+ if (!strcasecmp(s->buffer, "and"))
+ return ISL_TOKEN_AND;
+ if (!strcasecmp(s->buffer, "or"))
+ return ISL_TOKEN_OR;
+ if (!strcasecmp(s->buffer, "infty"))
+ return ISL_TOKEN_INFTY;
+ if (!strcasecmp(s->buffer, "infinity"))
+ return ISL_TOKEN_INFTY;
+ if (!strcasecmp(s->buffer, "NaN"))
+ return ISL_TOKEN_NAN;
+
+ if (!s->keywords)
+ return ISL_TOKEN_IDENT;
+
+ name_hash = isl_hash_string(isl_hash_init(), s->buffer);
+ entry = isl_hash_table_find(s->ctx, s->keywords, name_hash, same_name,
+ s->buffer, 0);
+ if (entry) {
+ keyword = entry->data;
+ return keyword->type;
+ }
+
+ return ISL_TOKEN_IDENT;
+}
+
+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];
+ }
+
+ if (same_line && s->c == '\n')
+ return NULL;
s->len = 0;
while ((c = isl_stream_getc(s)) != -1 && c != '\n')
/* nothing */
;
- if (c == -1)
+ if (c == -1 || (same_line && c == '\n'))
break;
- } else if (!isspace(c))
+ } 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 == ')' ||
c == '*' ||
c == '^' ||
c == '=' ||
+ c == '@' ||
c == ',' ||
- c == ':' ||
+ c == ';' ||
c == '[' ||
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)) {
if (!tok)
return NULL;
isl_stream_push_char(s, c);
- while ((c = isl_stream_getc(s)) != -1 && isalnum(c))
+ while ((c = isl_stream_getc(s)) != -1 &&
+ (isalnum(c) || 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 {
- tok->type = ISL_TOKEN_IDENT;
+ tok->type = check_keywords(s);
+ if (tok->type == ISL_TOKEN_IDENT)
tok->u.s = strdup(s->buffer);
+ 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->type = ISL_TOKEN_DEF;
+ return tok;
}
+ if (c != -1)
+ isl_stream_ungetc(s, c);
+ tok->type = ':';
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_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_if_available(struct isl_stream *s, int type)
+{
+ struct isl_token *tok;
+
+ tok = isl_stream_next_token(s);
+ if (!tok)
+ return 0;
+ if (tok->type == type) {
+ isl_token_free(tok);
+ return 1;
+ }
+ isl_stream_push_token(s, tok);
+ return 0;
+}
+
+int isl_stream_next_token_is(struct isl_stream *s, int type)
+{
+ struct isl_token *tok;
+ int r;
+
+ tok = isl_stream_next_token(s);
+ if (!tok)
+ return 0;
+ r = tok->type == type;
+ isl_stream_push_token(s, tok);
+ return r;
+}
+
+char *isl_stream_read_ident_if_available(struct isl_stream *s)
+{
+ struct isl_token *tok;
+
+ tok = isl_stream_next_token(s);
+ if (!tok)
+ return NULL;
+ if (tok->type == ISL_TOKEN_IDENT) {
+ char *ident = strdup(tok->u.s);
+ isl_token_free(tok);
+ return ident;
+ }
+ isl_stream_push_token(s, tok);
+ return NULL;
+}
+
int isl_stream_eat(struct isl_stream *s, int type)
{
struct isl_token *tok;
return -1;
}
+int isl_stream_is_empty(struct isl_stream *s)
+{
+ struct isl_token *tok;
+
+ tok = isl_stream_next_token(s);
+
+ if (!tok)
+ return 1;
+
+ isl_stream_push_token(s, tok);
+ return 0;
+}
+
+static int free_keyword(void *p)
+{
+ struct isl_keyword *keyword = p;
+
+ free(keyword->name);
+ free(keyword);
+
+ return 0;
+}
+
void isl_stream_free(struct isl_stream *s)
{
if (!s)
isl_stream_error(s, tok, "unexpected token");
isl_token_free(tok);
}
+ if (s->keywords) {
+ isl_hash_table_foreach(s->ctx, s->keywords, free_keyword);
+ isl_hash_table_free(s->ctx, s->keywords);
+ }
isl_ctx_deref(s->ctx);
free(s);
}