add isl_stream_flush_tokens
[platform/upstream/isl.git] / isl_stream.c
index f85d90f..1267d24 100644 (file)
@@ -82,7 +82,7 @@ void isl_token_free(struct isl_token *tok)
                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);
 }
@@ -230,6 +230,17 @@ static enum isl_token_type check_keywords(struct isl_stream *s)
        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;
@@ -251,10 +262,10 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line)
        /* 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;
@@ -343,6 +354,22 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line)
                        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 == ':') {
                int c;
                tok = isl_token_new(s->ctx, line, col, old_line != line);
@@ -354,7 +381,7 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line)
                }
                if (c != -1)
                        isl_stream_ungetc(s, c);
-               tok->type = ':';
+               tok->type = (enum isl_token_type) ':';
                return tok;
        }
        if (c == '>') {
@@ -365,10 +392,16 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line)
                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 == '<') {
@@ -379,10 +412,16 @@ static struct isl_token *next_token(struct isl_stream *s, int same_line)
                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 == '&') {
@@ -507,6 +546,17 @@ static int free_keyword(void *p)
        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)