]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lexer.c
Trash misinofmred README
[xonotic/gmqcc.git] / lexer.c
diff --git a/lexer.c b/lexer.c
index d5f22a3d5b6f1e967d706f8ab03a31f9931dc60b..0526b52cc4a44a0afec49926a0000ea58928b655 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -294,6 +294,27 @@ static bool isxdigit_only(int ch)
     return (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
 }
 
+/* Append a character to the token buffer */
+static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
+{
+    if (!token_value_add(&lex->tok, ch)) {
+        lexerror(lex, "out of memory");
+        return false;
+    }
+    return true;
+}
+
+/* Append a trailing null-byte */
+static bool GMQCC_WARN lex_endtoken(lex_file *lex)
+{
+    if (!token_value_add(&lex->tok, 0)) {
+        lexerror(lex, "out of memory");
+        return false;
+    }
+    lex->tok.value_count--;
+    return true;
+}
+
 /* Skip whitespace and comments and return the first
  * non-white character.
  * As this makes use of the above getch() ungetch() functions,
@@ -329,11 +350,31 @@ printf(   "line one\n"
 static int lex_skipwhite(lex_file *lex)
 {
     int ch = 0;
+    bool haswhite = false;
 
     do
     {
         ch = lex_getch(lex);
-        while (ch != EOF && isspace(ch)) ch = lex_getch(lex);
+        while (ch != EOF && isspace(ch)) {
+            if (lex->flags.preprocessing) {
+                if (ch == '\n') {
+                    /* end-of-line */
+                    /* see if there was whitespace first */
+                    if (haswhite) { /* (lex->tok.value_count) { */
+                        lex_ungetch(lex, ch);
+                        if (!lex_endtoken(lex))
+                            return TOKEN_FATAL;
+                        return TOKEN_WHITE;
+                    }
+                    /* otherwise return EOL */
+                    return TOKEN_EOL;
+                }
+                haswhite = true;
+                if (!lex_tokench(lex, ch))
+                    return TOKEN_FATAL;
+            }
+            ch = lex_getch(lex);
+        }
 
         if (ch == '/') {
             ch = lex_getch(lex);
@@ -342,39 +383,62 @@ static int lex_skipwhite(lex_file *lex)
                 /* one line comment */
                 ch = lex_getch(lex);
 
-                /* check for special: '/', '/', '*', '/' */
-                if (ch == '*') {
-                    ch = lex_getch(lex);
-                    if (ch == '/') {
-                        ch = ' ';
-                        continue;
+                if (lex->flags.preprocessing) {
+                    haswhite = true;
+                    if (!lex_tokench(lex, '/') ||
+                        !lex_tokench(lex, '/'))
+                    {
+                        return TOKEN_FATAL;
                     }
                 }
 
                 while (ch != EOF && ch != '\n') {
+                    if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+                        return TOKEN_FATAL;
                     ch = lex_getch(lex);
                 }
+                if (lex->flags.preprocessing) {
+                    lex_ungetch(lex, '\n');
+                    if (!lex_endtoken(lex))
+                        return TOKEN_FATAL;
+                    return TOKEN_WHITE;
+                }
                 continue;
             }
             if (ch == '*')
             {
                 /* multiline comment */
+                if (lex->flags.preprocessing) {
+                    haswhite = true;
+                    if (!lex_tokench(lex, '/') ||
+                        !lex_tokench(lex, '*'))
+                    {
+                        return TOKEN_FATAL;
+                    }
+                }
+
                 while (ch != EOF)
                 {
                     ch = lex_getch(lex);
                     if (ch == '*') {
                         ch = lex_getch(lex);
                         if (ch == '/') {
-                            ch = lex_getch(lex);
+                            if (lex->flags.preprocessing) {
+                                if (!lex_tokench(lex, '*') ||
+                                    !lex_tokench(lex, '/'))
+                                {
+                                    return TOKEN_FATAL;
+                                }
+                            }
                             break;
                         }
                     }
+                    if (lex->flags.preprocessing) {
+                        if (!lex_tokench(lex, ch))
+                            return TOKEN_FATAL;
+                    }
                 }
-                if (ch == '/') /* allow *//* direct following comment */
-                {
-                    lex_ungetch(lex, ch);
-                    ch = ' '; /* cause TRUE in the isspace check */
-                }
+                ch = ' '; /* cause TRUE in the isspace check */
                 continue;
             }
             /* Otherwise roll back to the slash and break out of the loop */
@@ -384,28 +448,13 @@ static int lex_skipwhite(lex_file *lex)
         }
     } while (ch != EOF && isspace(ch));
 
-    return ch;
-}
-
-/* Append a character to the token buffer */
-static bool GMQCC_WARN lex_tokench(lex_file *lex, int ch)
-{
-    if (!token_value_add(&lex->tok, ch)) {
-        lexerror(lex, "out of memory");
-        return false;
-    }
-    return true;
-}
-
-/* Append a trailing null-byte */
-static bool GMQCC_WARN lex_endtoken(lex_file *lex)
-{
-    if (!token_value_add(&lex->tok, 0)) {
-        lexerror(lex, "out of memory");
-        return false;
+    if (haswhite) {
+        if (!lex_endtoken(lex))
+            return TOKEN_FATAL;
+        lex_ungetch(lex, ch);
+        return TOKEN_WHITE;
     }
-    lex->tok.value_count--;
-    return true;
+    return ch;
 }
 
 /* Get a token */
@@ -501,7 +550,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
         if (ch == quote)
             return TOKEN_STRINGCONST;
 
-        if (ch == '\\') {
+        if (!lex->flags.preprocessing && ch == '\\') {
             ch = lex_getch(lex);
             if (ch == EOF) {
                 lexerror(lex, "unexpected end of file");
@@ -632,6 +681,10 @@ int lex_do(lex_file *lex)
     lex->tok.ctx.line = lex->sline;
     lex->tok.ctx.file = lex->name;
 
+    if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
+        return (lex->tok.ttype = ch);
+    }
+
     if (lex->eof)
         return (lex->tok.ttype = TOKEN_FATAL);
 
@@ -1013,8 +1066,12 @@ int lex_do(lex_file *lex)
     if (ch == '"')
     {
         lex->flags.nodigraphs = true;
+        if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+            return TOKEN_FATAL;
         lex->tok.ttype = lex_finish_string(lex, '"');
-        while (lex->tok.ttype == TOKEN_STRINGCONST)
+        if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+            return TOKEN_FATAL;
+        while (!lex->flags.preprocessing && lex->tok.ttype == TOKEN_STRINGCONST)
         {
             /* Allow c style "string" "continuation" */
             ch = lex_skipwhite(lex);
@@ -1038,23 +1095,28 @@ int lex_do(lex_file *lex)
          * Likewise actual unescaping has to be done by the parser.
          * The difference is we don't allow 'char' 'continuation'.
          */
-         lex->tok.ttype = lex_finish_string(lex, '\'');
-         if (!lex_endtoken(lex))
-              return (lex->tok.ttype = TOKEN_FATAL);
+        if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+            return TOKEN_FATAL;
+        lex->tok.ttype = lex_finish_string(lex, '\'');
+        if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+            return TOKEN_FATAL;
+        if (!lex_endtoken(lex))
+            return (lex->tok.ttype = TOKEN_FATAL);
 
          /* It's a vector if we can successfully scan 3 floats */
 #ifdef WIN32
-         if (sscanf_s(lex->tok.value, " %f %f %f ",
-                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
+        if (sscanf_s(lex->tok.value, " %f %f %f ",
+                   &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
 #else
-         if (sscanf(lex->tok.value, " %f %f %f ",
-                    &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
+        if (sscanf(lex->tok.value, " %f %f %f ",
+                   &lex->tok.constval.v.x, &lex->tok.constval.v.y, &lex->tok.constval.v.z) == 3)
 #endif
-         {
-              lex->tok.ttype = TOKEN_VECTORCONST;
-         }
 
-         return lex->tok.ttype;
+        {
+             lex->tok.ttype = TOKEN_VECTORCONST;
+        }
+
+        return lex->tok.ttype;
     }
 
     if (isdigit(ch))