]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
Finishing the preprocessing flag for the lexer, added preprocess.c to test it
authorWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 2 Nov 2012 17:28:54 +0000 (18:28 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 2 Nov 2012 17:34:14 +0000 (18:34 +0100)
Makefile
lexer.c
main.c
preprocess.c [new file with mode: 0644]

index a51aed38ac21cc404c5abba7965af8e8b25cf7ac..660ec843d17b7e6a458e9aec00bfa855ee67af94 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -25,6 +25,7 @@ OBJ     = \
           code.o      \
           ast.o       \
           ir.o        \
+          preprocess.o \
           error.o
 OBJ_A = test/ast-test.o
 OBJ_I = test/ir-test.o
diff --git a/lexer.c b/lexer.c
index eb215f49107283cbe434b7c906426fb532b2b842..4d184d083d67afdfa1527173d237ba3059590e7b 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -350,6 +350,7 @@ printf(   "line one\n"
 static int lex_skipwhite(lex_file *lex)
 {
     int ch = 0;
+    bool haswhite = false;
 
     do
     {
@@ -359,7 +360,7 @@ static int lex_skipwhite(lex_file *lex)
                 if (ch == '\n') {
                     /* end-of-line */
                     /* see if there was whitespace first */
-                    if (lex->tok.value_count) {
+                    if (haswhite) { /* (lex->tok.value_count) { */
                         lex_ungetch(lex, ch);
                         if (!lex_endtoken(lex))
                             return TOKEN_FATAL;
@@ -368,19 +369,19 @@ static int lex_skipwhite(lex_file *lex)
                     /* otherwise return EOL */
                     return TOKEN_EOL;
                 }
+                haswhite = true;
                 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);
             if (ch == '/')
             {
                 /* one line comment */
+                haswhite = true;
                 ch = lex_getch(lex);
 
                 if (lex->flags.preprocessing) {
@@ -407,6 +408,7 @@ static int lex_skipwhite(lex_file *lex)
             if (ch == '*')
             {
                 /* multiline comment */
+                haswhite = true;
                 if (lex->flags.preprocessing) {
                     if (!lex_tokench(lex, ' ') ||
                         !lex_tokench(lex, ' '))
@@ -448,6 +450,12 @@ static int lex_skipwhite(lex_file *lex)
         }
     } while (ch != EOF && isspace(ch));
 
+    if (haswhite) {
+        if (!lex_endtoken(lex))
+            return TOKEN_FATAL;
+        lex_ungetch(lex, ch);
+        return TOKEN_WHITE;
+    }
     return ch;
 }
 
@@ -675,7 +683,7 @@ 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)) {
+    if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) {
         return (lex->tok.ttype = ch);
     }
 
diff --git a/main.c b/main.c
index a65a735ebb6b351442bf6c8be743bc31bc654a76..3fb25ca0441f61eaa77b196db21acc4ee1928948 100644 (file)
--- a/main.c
+++ b/main.c
@@ -460,6 +460,14 @@ int main(int argc, char **argv) {
                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
                      ("unknown"))))));
 
+            if (opts_pp_only) {
+                if (!preprocess(items_data[itr].filename)) {
+                    retval = 1;
+                    goto cleanup;
+                }
+                continue;
+            }
+
             if (!parser_compile(items_data[itr].filename)) {
                 retval = 1;
                 goto cleanup;
diff --git a/preprocess.c b/preprocess.c
new file mode 100644 (file)
index 0000000..adf5e5e
--- /dev/null
@@ -0,0 +1,26 @@
+#include "gmqcc.h"
+#include "lexer.h"
+
+bool preprocess(const char *filename)
+{
+    int tok;
+    lex_file *lex = lex_open(filename);
+    lex->flags.preprocessing = true;
+
+    do {
+        tok = lex_do(lex);
+        if (tok == TOKEN_EOL)
+            printf("EOL");
+        else if (tok >= TOKEN_START && tok <= TOKEN_FATAL)
+            printf("%s: ", _tokennames[tok - TOKEN_START]);
+        else
+            printf("TOKEN: '%c'", tok);
+        if (tok == TOKEN_WHITE)
+            printf(">>%s<<\n", lex->tok.value);
+        else
+            printf("\n");
+    } while (tok < TOKEN_EOF);
+
+    lex_close(lex);
+    return true;
+}