]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
preprocessing flag for the lexer
authorWolfgang (Blub) Bumiller <blub@speed.at>
Thu, 1 Nov 2012 22:22:58 +0000 (23:22 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 2 Nov 2012 17:33:51 +0000 (18:33 +0100)
lexer.c
lexer.h

diff --git a/lexer.c b/lexer.c
index 02a7ff4f77ff6a33011b24a2c426efb835b80442..eb215f49107283cbe434b7c906426fb532b2b842 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,
@@ -333,7 +354,27 @@ static int lex_skipwhite(lex_file *lex)
     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 (lex->tok.value_count) {
+                        lex_ungetch(lex, ch);
+                        if (!lex_endtoken(lex))
+                            return TOKEN_FATAL;
+                        return TOKEN_WHITE;
+                    }
+                    /* otherwise return EOL */
+                    return TOKEN_EOL;
+                }
+                if (!lex_tokench(lex, ch))
+                    return TOKEN_FATAL;
+            }
+            ch = lex_getch(lex);
+        }
+        if (lex->flags.preprocessing && !lex_tokench(lex, ch))
+            return TOKEN_FATAL;
 
         if (ch == '/') {
             ch = lex_getch(lex);
@@ -342,30 +383,59 @@ 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) {
+                    if (!lex_tokench(lex, ' ') ||
+                        !lex_tokench(lex, ' '))
+                    {
+                        return TOKEN_FATAL;
                     }
                 }
 
                 while (ch != EOF && ch != '\n') {
                     ch = lex_getch(lex);
+                    if (lex->flags.preprocessing && !lex_tokench(lex, ' '))
+                        return TOKEN_FATAL;
+                }
+                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) {
+                    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 == '/')
+                        if (ch == '/') {
+                            if (lex->flags.preprocessing) {
+                                if (!lex_tokench(lex, ' ') ||
+                                    !lex_tokench(lex, ' '))
+                                {
+                                    return TOKEN_FATAL;
+                                }
+                            }
                             break;
+                        }
+                    }
+                    if (lex->flags.preprocessing) {
+                        if (ch != '\n')
+                            ch = ' ';
+                        if (!lex_tokench(lex, ch))
+                            return TOKEN_FATAL;
                     }
                 }
                 ch = ' '; /* cause TRUE in the isspace check */
@@ -381,27 +451,6 @@ static int lex_skipwhite(lex_file *lex)
     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;
-    }
-    lex->tok.value_count--;
-    return true;
-}
-
 /* Get a token */
 static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
 {
@@ -626,6 +675,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 || TOKEN_FATAL)) {
+        return (lex->tok.ttype = ch);
+    }
+
     if (lex->eof)
         return (lex->tok.ttype = TOKEN_FATAL);
 
diff --git a/lexer.h b/lexer.h
index 8d525a5db5e54eb507ba35a6def53ff162f365ad..2142da64ad97193b0230849a336f0fe993df077c 100644 (file)
--- a/lexer.h
+++ b/lexer.h
@@ -57,6 +57,9 @@ enum {
     TOKEN_INTCONST,
     TOKEN_FLOATCONST,
 
+    TOKEN_WHITE,
+    TOKEN_EOL,
+
     TOKEN_EOF,
 
     /* We use '< TOKEN_ERROR', so TOKEN_FATAL must come after it and any
@@ -78,6 +81,8 @@ static const char *_tokennames[] = {
     "TOKEN_VECTORCONST",
     "TOKEN_INTCONST",
     "TOKEN_FLOATCONST",
+    "TOKEN_WHITE",
+    "TOKEN_EOL",
     "TOKEN_EOF",
     "TOKEN_ERROR",
     "TOKEN_FATAL",
@@ -109,6 +114,7 @@ typedef struct {
        struct {
            bool noops;
            bool nodigraphs; /* used when lexing string constants */
+           bool preprocessing; /* whitespace and EOLs become actual tokens */
        } flags;
 
     int framevalue;