]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lexer.c
Fix linecounting mistake in try_digraph
[xonotic/gmqcc.git] / lexer.c
diff --git a/lexer.c b/lexer.c
index b637af9d0280b61ff3ecb8b218ed929e309af2c0..3660190946f4b4bfd5b273a0e7a71395e78a17b2 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -1,3 +1,25 @@
+/*
+ * Copyright (C) 2012
+ *     Wolfgang Bumiller
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -22,10 +44,10 @@ static size_t num_keywords_qc = sizeof(keywords_qc) / sizeof(keywords_qc[0]);
 
 /* For fte/gmgqcc */
 static const char *keywords_fg[] = {
-    "var",
     "switch", "case", "default",
     "struct", "union",
-    "break", "continue"
+    "break", "continue",
+    "typedef"
 };
 static size_t num_keywords_fg = sizeof(keywords_fg) / sizeof(keywords_fg[0]);
 
@@ -296,6 +318,11 @@ static int lex_try_digraph(lex_file *lex, int ch)
 {
     int c2;
     c2 = lex_fgetc(lex);
+    /* we just used fgetc() so count lines
+     * need to offset a \n the ungetch would recognize
+     */
+    if (!lex->push_line && c2 == '\n')
+        lex->line++;
     if      (ch == '<' && c2 == ':')
         return '[';
     else if (ch == ':' && c2 == '>')
@@ -423,7 +450,8 @@ static bool lex_try_pragma(lex_file *lex)
     if (!strcmp(command, "push")) {
         if (!strcmp(param, "line")) {
             lex->push_line++;
-            --line;
+            if (lex->push_line == 1)
+                --line;
         }
         else
             goto unroll;
@@ -432,7 +460,8 @@ static bool lex_try_pragma(lex_file *lex)
         if (!strcmp(param, "line")) {
             if (lex->push_line)
                 lex->push_line--;
-            --line;
+            if (lex->push_line == 0)
+                --line;
         }
         else
             goto unroll;
@@ -604,9 +633,13 @@ static int lex_skipwhite(lex_file *lex)
                             }
                             break;
                         }
+                        lex_ungetch(lex, ch);
                     }
                     if (lex->flags.preprocessing) {
-                        lex_tokench(lex, ' '); /* ch); */
+                        if (ch == '\n')
+                            lex_tokench(lex, '\n');
+                        else
+                            lex_tokench(lex, ' '); /* ch); */
                     }
                 }
                 ch = ' '; /* cause TRUE in the isspace check */
@@ -742,6 +775,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
             case 't':  ch = '\t'; break;
             case 'f':  ch = '\f'; break;
             case 'v':  ch = '\v'; break;
+            case '\n':  ch = '\n'; break;
             default:
                 lexwarn(lex, WARN_UNKNOWN_CONTROL_SEQUENCE, "unrecognized control sequence: \\%c", ch);
                 /* so we just add the character plus backslash no matter what it actually is */
@@ -834,7 +868,7 @@ static int GMQCC_WARN lex_finish_digit(lex_file *lex, int lastch)
 
 int lex_do(lex_file *lex)
 {
-    int ch, nextch;
+    int ch, nextch, thirdch;
 
     lex_token_new(lex);
 #if 0
@@ -857,14 +891,14 @@ int lex_do(lex_file *lex)
         continue;
     }
 
-    lex->sline = lex->line;
-    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);
     }
 
+    lex->sline = lex->line;
+    lex->tok.ctx.line = lex->sline;
+    lex->tok.ctx.file = lex->name;
+
     if (lex->eof)
         return (lex->tok.ttype = TOKEN_FATAL);
 
@@ -980,9 +1014,9 @@ int lex_do(lex_file *lex)
 
         if (!strcmp(v, "flush"))
         {
-            size_t frame;
-            for (frame = 0; frame < vec_size(lex->frames); ++frame)
-                mem_d(lex->frames[frame].name);
+            size_t fi;
+            for (fi = 0; fi < vec_size(lex->frames); ++fi)
+                mem_d(lex->frames[fi].name);
             vec_free(lex->frames);
             /* skip line (fteqcc does it too) */
             ch = lex_getch(lex);
@@ -1050,8 +1084,10 @@ int lex_do(lex_file *lex)
          */
         switch (ch)
         {
+            /*
             case '+':
             case '-':
+            */
             case '*':
             case '/':
             case '<':
@@ -1084,7 +1120,7 @@ int lex_do(lex_file *lex)
             nextch = lex_getch(lex);
             if (nextch != '.') {
                 lex_ungetch(lex, nextch);
-                lex_ungetch(lex, nextch);
+                lex_ungetch(lex, '.');
                 lex_endtoken(lex);
                 return (lex->tok.ttype = ch);
             }
@@ -1114,6 +1150,16 @@ int lex_do(lex_file *lex)
             lex_tokench(lex, nextch);
         } else if (ch == '-' && nextch == '>') {
             lex_tokench(lex, nextch);
+        } else if (ch == '&' && nextch == '~') {
+            thirdch = lex_getch(lex);
+            if (thirdch != '=') {
+                lex_ungetch(lex, thirdch);
+                lex_ungetch(lex, nextch);
+            }
+            else {
+                lex_tokench(lex, nextch);
+                lex_tokench(lex, thirdch);
+            }
         } else
             lex_ungetch(lex, nextch);