Removed preprocessor_error(). Returns a Token to specify an error state now.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 13 Feb 2009 00:35:35 -0500
changeset 598 decc32dc03a7
parent 597 832dbfa63509
child 599 cef9f9f0f4f4
Removed preprocessor_error(). Returns a Token to specify an error state now.
mojoshader_assembler.c
mojoshader_internal.h
mojoshader_preprocessor.c
--- a/mojoshader_assembler.c	Thu Feb 12 22:57:21 2009 -0500
+++ b/mojoshader_assembler.c	Fri Feb 13 00:35:35 2009 -0500
@@ -209,22 +209,28 @@
 
 static Token _nexttoken(Context *ctx)
 {
-    ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen,
-                                        &ctx->tokenval);
-
-    if (preprocessor_outofmemory(ctx->preprocessor))
+    while (1)
     {
-        out_of_memory(ctx);
-        ctx->tokenval = TOKEN_EOI;
-        ctx->token = NULL;
-        ctx->tokenlen = 0;
-    } // if
-    else
-    {
-        const char *err = preprocessor_error(ctx->preprocessor);
-        if (err)
-            fail(ctx, err);
-    } // else
+        ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen,
+                                            &ctx->tokenval);
+
+        if (preprocessor_outofmemory(ctx->preprocessor))
+        {
+            out_of_memory(ctx);
+            ctx->tokenval = TOKEN_EOI;
+            ctx->token = NULL;
+            ctx->tokenlen = 0;
+            break;
+        } // if
+
+        if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR)
+        {
+            fail(ctx, ctx->token);
+            continue;
+        } // else
+
+        break;
+    } // while
 
     return ctx->tokenval;
 } // _nexttoken
--- a/mojoshader_internal.h	Thu Feb 12 22:57:21 2009 -0500
+++ b/mojoshader_internal.h	Fri Feb 13 00:35:35 2009 -0500
@@ -327,7 +327,8 @@
     TOKEN_PP_ERROR,
     TOKEN_PP_INCOMPLETE_COMMENT,
     TOKEN_PP_BAD_CHARS,
-    TOKEN_EOI
+    TOKEN_EOI,
+    TOKEN_PREPROCESSING_ERROR
 } Token;
 
 
@@ -361,7 +362,6 @@
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
 
 void preprocessor_end(Preprocessor *pp);
-const char *preprocessor_error(Preprocessor *pp);
 int preprocessor_outofmemory(Preprocessor *pp);
 const char *preprocessor_nexttoken(Preprocessor *_ctx,
                                    unsigned int *_len, Token *_token);
--- a/mojoshader_preprocessor.c	Thu Feb 12 22:57:21 2009 -0500
+++ b/mojoshader_preprocessor.c	Fri Feb 13 00:35:35 2009 -0500
@@ -45,7 +45,7 @@
 
 static inline void out_of_memory(Context *ctx)
 {
-    ctx->isfail = ctx->out_of_memory = 1;
+    ctx->out_of_memory = 1;
 } // out_of_memory
 
 static inline void *Malloc(Context *ctx, const size_t len)
@@ -76,7 +76,7 @@
     ctx->isfail = 1;
     va_list ap;
     va_start(ap, fmt);
-    vsnprintf(ctx->failstr, sizeof (ctx->failstr), fmt, ap);  // rebuild it.
+    vsnprintf(ctx->failstr, sizeof (ctx->failstr), fmt, ap);
     va_end(ap);
 } // failf
 
@@ -142,6 +142,7 @@
         TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
         TOKENCASE(TOKEN_PP_BAD_CHARS);
         TOKENCASE(TOKEN_EOI);
+        TOKENCASE(TOKEN_PREPROCESSING_ERROR);
         #undef TOKENCASE
 
         case ((Token) '\n'):
@@ -384,19 +385,6 @@
 } // preprocessor_end
 
 
-const char *preprocessor_error(Preprocessor *_ctx)
-{
-    Context *ctx = (Context *) _ctx;
-    if (ctx->isfail)
-    {
-        ctx->isfail = 0;
-        return ctx->failstr;
-    } // if
-
-    return NULL;
-} // preprocessor_error
-
-
 int preprocessor_outofmemory(Preprocessor *_ctx)
 {
     Context *ctx = (Context *) _ctx;
@@ -411,6 +399,14 @@
 
     while (1)
     {
+        if (ctx->isfail)
+        {
+            ctx->isfail = 0;
+            *_token = TOKEN_PREPROCESSING_ERROR;
+            *_len = strlen(ctx->failstr);
+            return ctx->failstr;
+        } // if
+
         IncludeState *state = ctx->include_stack;
         if (state == NULL)
         {
@@ -430,7 +426,7 @@
         else if (token == TOKEN_PP_INCOMPLETE_COMMENT)
         {
             fail(ctx, "Incomplete multiline comment");
-            continue;  // !!! FIXME: we should probably return TOKEN_ERROR or something.
+            continue;  // will return at top of loop.
         } // else if
 
         *_token = token;
@@ -657,7 +653,6 @@
 
     Token token = TOKEN_UNKNOWN;
     const char *tokstr = NULL;
-    const char *err = NULL;
 
     Buffer buffer;
     buffer_init(&buffer);
@@ -712,6 +707,58 @@
             } // if
         } // else if
 
+        else if (token == TOKEN_PREPROCESSING_ERROR)
+        {
+            if (!out_of_memory)
+            {
+                ErrorList *error = (ErrorList *) m(sizeof (ErrorList), d);
+                unsigned int pos = 0;
+                char *fname = NULL;
+                const char *str = preprocessor_sourcepos(pp, &pos);
+                if (str != NULL)
+                {
+                    fname = (char *) m(strlen(str) + 1, d);
+                    if (fname != NULL)
+                        strcpy(fname, str);
+                } // if
+
+                // !!! FIXME: cut and paste with other error handlers.
+                char *errstr = (char *) m(len + 1, d);
+                if (errstr != NULL)
+                    strcpy(errstr, tokstr);
+
+                out_of_memory = ((!error) || ((!fname) && (str)) || (!errstr));
+                if (out_of_memory)
+                {
+                    if (errstr) f(errstr, d);
+                    if (fname) f(fname, d);
+                    if (error) f(error, d);
+                } // if
+                else
+                {
+                    error->error.error = errstr;
+                    error->error.filename = fname;
+                    error->error.error_position = pos;
+                    error->next = NULL;
+
+                    ErrorList *prev = NULL;
+                    ErrorList *item = errors;
+                    while (item != NULL)
+                    {
+                        prev = item;
+                        item = item->next;
+                    } // while
+
+                    if (prev == NULL)
+                        errors = error;
+                    else
+                        prev->next = error;
+
+                    error_count++;
+                } // else
+            } // if
+        } // else if
+
         else
         {
             if (!out_of_memory)
@@ -724,57 +771,6 @@
 
         nl = isnewline;
 
-        if ((!out_of_memory) && ((err = preprocessor_error(pp)) != NULL))
-        {
-            ErrorList *error = (ErrorList *) m(sizeof (ErrorList), d);
-            unsigned int pos = 0;
-            char *fname = NULL;
-            const char *str = preprocessor_sourcepos(pp, &pos);
-            if (str != NULL)
-            {
-                fname = (char *) m(strlen(str) + 1, d);
-                if (fname != NULL)
-                    strcpy(fname, str);
-            } // if
-
-            // !!! FIXME: cut and paste with other error handlers.
-            char *errstr = (char *) m(strlen(err) + 1, d);
-            if (errstr != NULL)
-                strcpy(errstr, err);
-
-            out_of_memory = ((!error) || ((!fname) && (str)) || (!errstr));
-            if (out_of_memory)
-            {
-                if (errstr) f(errstr, d);
-                if (fname) f(fname, d);
-                if (error) f(error, d);
-            } // if
-            else
-            {
-                error->error.error = errstr;
-                error->error.filename = fname;
-                error->error.error_position = pos;
-                error->next = NULL;
-
-                ErrorList *prev = NULL;
-                ErrorList *item = errors;
-                while (item != NULL)
-                {
-                    prev = item;
-                    item = item->next;
-                } // while
-
-                if (prev == NULL)
-                    errors = error;
-                else
-                    prev->next = error;
-
-                error_count++;
-            } // else
-
-            continue;
-        } // if
-
         if (out_of_memory)
         {
             preprocessor_end(pp);