#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)
{
return;
if (tok->type == ISL_TOKEN_VALUE)
isl_int_clear(tok->u.v);
- else if (tok->type == ISL_TOKEN_IDENT)
+ else if (tok->type == ISL_TOKEN_IDENT || tok->type == ISL_TOKEN_STRING)
free(tok->u.s);
free(tok);
}
if (tok) {
if (tok->type < 256)
fprintf(stderr, "got '%c'\n", tok->type);
+ else if (tok->type == ISL_TOKEN_IDENT)
+ fprintf(stderr, "got ident '%s'\n", tok->u.s);
else
fprintf(stderr, "got token type %d\n", tok->type);
}
return NULL;
s->ctx = ctx;
isl_ctx_ref(s->ctx);
- s->size = 256;
s->file = NULL;
s->str = NULL;
- s->buffer = isl_alloc_array(ctx, char, s->size);
- if (!s->buffer)
- goto error;
s->len = 0;
s->line = 1;
s->col = 0;
for (i = 0; i < 5; ++i)
s->tokens[i] = NULL;
s->n_token = 0;
+ s->keywords = NULL;
+ s->size = 256;
+ s->buffer = isl_alloc_array(ctx, char, s->size);
+ if (!s->buffer)
+ goto error;
return s;
error:
isl_stream_free(s);
struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
{
- struct isl_stream *s = isl_stream_new(ctx);
- s->str = str;
- return s;
+ struct isl_stream *s = isl_stream_new(ctx);
+ if (!s)
+ return NULL;
+ s->str = str;
+ return s;
}
static int isl_stream_getc(struct isl_stream *s)
{
if (s->len >= s->size) {
s->size = (3*s->size)/2;
- s->buffer = isl_realloc_array(ctx, s->buffer, char, s->size);
+ s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
if (!s->buffer)
return -1;
}
s->tokens[s->n_token++] = tok;
}
+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 (!strcasecmp(s->buffer, "max"))
+ return ISL_TOKEN_MAX;
+
+ 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;
+}
+
+int isl_stream_skip_line(struct isl_stream *s)
+{
+ int c;
+
+ while ((c = isl_stream_getc(s)) != -1 && c != '\n')
+ /* nothing */
+ ;
+
+ return c == -1 ? -1 : 0;
+}
+
static struct isl_token *next_token(struct isl_stream *s, int same_line)
{
int c;
/* 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'))
+ if (isl_stream_skip_line(s) < 0)
+ break;
+ c = '\n';
+ if (same_line)
break;
} else if (!isspace(c) || (same_line && c == '\n'))
break;
c == '+' ||
c == '/' ||
c == '*' ||
+ c == '%' ||
c == '^' ||
c == '=' ||
+ c == '@' ||
c == ',' ||
+ c == '.' ||
c == ';' ||
c == '[' ||
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);
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->type = check_keywords(s);
+ if (tok->type == ISL_TOKEN_IDENT)
tok->u.s = strdup(s->buffer);
+ return tok;
+ }
+ if (c == '"') {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ tok->type = ISL_TOKEN_STRING;
+ tok->u.s = NULL;
+ while ((c = isl_stream_getc(s)) != -1 && c != '"' && c != '\n')
+ isl_stream_push_char(s, c);
+ if (c != '"') {
+ isl_stream_error(s, NULL, "unterminated string");
+ goto error;
}
+ isl_stream_push_char(s, '\0');
+ tok->u.s = strdup(s->buffer);
return tok;
}
if (c == ':') {
}
if (c != -1)
isl_stream_ungetc(s, c);
- tok->type = ':';
+ tok->type = (enum isl_token_type) ':';
return tok;
}
if (c == '>') {
if ((c = isl_stream_getc(s)) == '=') {
tok->type = ISL_TOKEN_GE;
return tok;
- }
+ } else if (c == '>') {
+ if ((c = isl_stream_getc(s)) == '=') {
+ tok->type = ISL_TOKEN_LEX_GE;
+ return tok;
+ }
+ tok->type = ISL_TOKEN_LEX_GT;
+ } else
+ tok->type = ISL_TOKEN_GT;
if (c != -1)
isl_stream_ungetc(s, c);
- tok->type = ISL_TOKEN_GT;
return tok;
}
if (c == '<') {
if ((c = isl_stream_getc(s)) == '=') {
tok->type = ISL_TOKEN_LE;
return tok;
- }
+ } else if (c == '<') {
+ if ((c = isl_stream_getc(s)) == '=') {
+ tok->type = ISL_TOKEN_LEX_LE;
+ return tok;
+ }
+ tok->type = ISL_TOKEN_LEX_LT;
+ } else
+ tok->type = ISL_TOKEN_LT;
if (c != -1)
isl_stream_ungetc(s, c);
- tok->type = ISL_TOKEN_LT;
return tok;
}
if (c == '&') {
return 0;
}
+static int free_keyword(void **p, void *user)
+{
+ struct isl_keyword *keyword = *p;
+
+ free(keyword->name);
+ free(keyword);
+
+ return 0;
+}
+
+void isl_stream_flush_tokens(struct isl_stream *s)
+{
+ int i;
+
+ if (!s)
+ return;
+ for (i = 0; i < s->n_token; ++i)
+ isl_token_free(s->tokens[i]);
+ s->n_token = 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, NULL);
+ isl_hash_table_free(s->ctx, s->keywords);
+ }
isl_ctx_deref(s->ctx);
free(s);
}