]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lexer.c
Handle byte order mark for unicode. (0xEFBBBF)
[xonotic/gmqcc.git] / lexer.c
diff --git a/lexer.c b/lexer.c
index 5882bdbfc2e76f2100e12006194e001f55da90c1..03d1308597d0fa0ec94b8e1e67b9afa20f2d95d3 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -25,6 +25,7 @@
 
 #include "gmqcc.h"
 #include "lexer.h"
+
 /*
  * List of Keywords
  */
@@ -177,10 +178,14 @@ static void lex_token_new(lex_file *lex)
 }
 #endif
 
+static void lex_ungetch(lex_file *lex, int ch);
+static int lex_getch(lex_file *lex);
+
 lex_file* lex_open(const char *file)
 {
-    lex_file *lex;
-    FILE *in = fs_file_open(file, "rb");
+    lex_file  *lex;
+    fs_file_t *in = fs_file_open(file, "rb");
+    uint32_t   read;
 
     if (!in) {
         lexerror(NULL, "open failed: '%s'\n", file);
@@ -203,6 +208,19 @@ lex_file* lex_open(const char *file)
     lex->peekpos = 0;
     lex->eof     = false;
 
+    /* handle BOM */
+    if ((read = (lex_getch(lex) << 16) | (lex_getch(lex) << 8) | lex_getch(lex)) != 0xEFBBBF) {
+        lex_ungetch(lex, (read & 0x0000FF));
+        lex_ungetch(lex, (read & 0x00FF00) >> 8);
+        lex_ungetch(lex, (read & 0xFF0000) >> 16);
+    } else {
+        /*
+         * otherwise the lexer has advanced 3 bytes for the BOM, we need
+         * to set the column back to 0
+         */
+        lex->column = 0;
+    }
+
     vec_push(lex_filenames, lex->name);
     return lex;
 }
@@ -265,6 +283,8 @@ void lex_close(lex_file *lex)
     mem_d(lex);
 }
 
+
+
 static int lex_fgetc(lex_file *lex)
 {
     if (lex->file) {
@@ -273,11 +293,11 @@ static int lex_fgetc(lex_file *lex)
     }
     if (lex->open_string) {
         if (lex->open_string_pos >= lex->open_string_length)
-            return EOF;
+            return FS_FILE_EOF;
         lex->column++;
         return lex->open_string[lex->open_string_pos++];
     }
-    return EOF;
+    return FS_FILE_EOF;
 }
 
 /* Get or put-back data
@@ -285,7 +305,6 @@ static int lex_fgetc(lex_file *lex)
  * are working on.
  * The are merely wrapping get/put in order to count line numbers.
  */
-static void lex_ungetch(lex_file *lex, int ch);
 static int lex_try_trigraph(lex_file *lex, int old)
 {
     int c2, c3;
@@ -493,7 +512,7 @@ static bool lex_try_pragma(lex_file *lex)
         goto unroll;
 
     lex->line = line;
-    while (ch != '\n' && ch != EOF)
+    while (ch != '\n' && ch != FS_FILE_EOF)
         ch = lex_getch(lex);
     vec_free(command);
     vec_free(param);
@@ -573,7 +592,7 @@ static int lex_skipwhite(lex_file *lex, bool hadwhite)
     do
     {
         ch = lex_getch(lex);
-        while (ch != EOF && util_isspace(ch)) {
+        while (ch != FS_FILE_EOF && util_isspace(ch)) {
             if (ch == '\n') {
                 if (lex_try_pragma(lex))
                     continue;
@@ -613,7 +632,7 @@ static int lex_skipwhite(lex_file *lex, bool hadwhite)
                     lex_tokench(lex, ' ');
                 }
 
-                while (ch != EOF && ch != '\n') {
+                while (ch != FS_FILE_EOF && ch != '\n') {
                     if (lex->flags.preprocessing)
                         lex_tokench(lex, ' '); /* ch); */
                     ch = lex_getch(lex);
@@ -638,7 +657,7 @@ static int lex_skipwhite(lex_file *lex, bool hadwhite)
                     lex_tokench(lex, ' ');
                 }
 
-                while (ch != EOF)
+                while (ch != FS_FILE_EOF)
                 {
                     ch = lex_getch(lex);
                     if (ch == '*') {
@@ -671,7 +690,7 @@ static int lex_skipwhite(lex_file *lex, bool hadwhite)
             ch = '/';
             break;
         }
-    } while (ch != EOF && util_isspace(ch));
+    } while (ch != FS_FILE_EOF && util_isspace(ch));
 
     if (haswhite) {
         lex_endtoken(lex);
@@ -687,7 +706,7 @@ static bool GMQCC_WARN lex_finish_ident(lex_file *lex)
     int ch;
 
     ch = lex_getch(lex);
-    while (ch != EOF && isident(ch))
+    while (ch != FS_FILE_EOF && isident(ch))
     {
         lex_tokench(lex, ch);
         ch = lex_getch(lex);
@@ -707,7 +726,7 @@ static int lex_parse_frame(lex_file *lex)
     lex_token_new(lex);
 
     ch = lex_getch(lex);
-    while (ch != EOF && ch != '\n' && util_isspace(ch))
+    while (ch != FS_FILE_EOF && ch != '\n' && util_isspace(ch))
         ch = lex_getch(lex);
 
     if (ch == '\n')
@@ -765,10 +784,11 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
     int ch = 0;
     int nextch;
     bool hex;
+    bool oct;
     char u8buf[8]; /* way more than enough */
     int  u8len, uc;
 
-    while (ch != EOF)
+    while (ch != FS_FILE_EOF)
     {
         ch = lex_getch(lex);
         if (ch == quote)
@@ -777,18 +797,18 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
         if (lex->flags.preprocessing && ch == '\\') {
             lex_tokench(lex, ch);
             ch = lex_getch(lex);
-            if (ch == EOF) {
+            if (ch == FS_FILE_EOF) {
                 lexerror(lex, "unexpected end of file");
-                lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
+                lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
                 return (lex->tok.ttype = TOKEN_ERROR);
             }
             lex_tokench(lex, ch);
         }
         else if (ch == '\\') {
             ch = lex_getch(lex);
-            if (ch == EOF) {
+            if (ch == FS_FILE_EOF) {
                 lexerror(lex, "unexpected end of file");
-                lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
+                lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
                 return (lex->tok.ttype = TOKEN_ERROR);
             }
 
@@ -850,17 +870,18 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
                 chr = 0;
                 nextch = lex_getch(lex);
                 hex = (nextch == 'x');
-                if (!hex)
+                oct = (nextch == '0');
+                if (!hex && !oct)
                     lex_ungetch(lex, nextch);
                 for (nextch = lex_getch(lex); nextch != '}'; nextch = lex_getch(lex)) {
-                    if (!hex) {
+                    if (!hex && !oct) {
                         if (nextch >= '0' && nextch <= '9')
                             chr = chr * 10 + nextch - '0';
                         else {
                             lexerror(lex, "bad character code");
                             return (lex->tok.ttype = TOKEN_ERROR);
                         }
-                    } else {
+                    } else if (!oct) {
                         if (nextch >= '0' && nextch <= '9')
                             chr = chr * 0x10 + nextch - '0';
                         else if (nextch >= 'a' && nextch <= 'f')
@@ -871,6 +892,13 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
                             lexerror(lex, "bad character code");
                             return (lex->tok.ttype = TOKEN_ERROR);
                         }
+                    } else {
+                        if (nextch >= '0' && nextch <= '9')
+                            chr = chr * 8 + chr - '0';
+                        else {
+                            lexerror(lex, "bad character code");
+                            return (lex->tok.ttype = TOKEN_ERROR);
+                        }
                     }
                     if (chr > 0x10FFFF || (!OPTS_FLAG(UTF8) && chr > 255))
                     {
@@ -911,7 +939,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
             lex_tokench(lex, ch);
     }
     lexerror(lex, "unexpected end of file within string constant");
-    lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
+    lex_ungetch(lex, FS_FILE_EOF); /* next token to be TOKEN_EOF */
     return (lex->tok.ttype = TOKEN_ERROR);
 }
 
@@ -1032,7 +1060,7 @@ int lex_do(lex_file *lex)
     if (lex->eof)
         return (lex->tok.ttype = TOKEN_FATAL);
 
-    if (ch == EOF) {
+    if (ch == FS_FILE_EOF) {
         lex->eof = true;
         return (lex->tok.ttype = TOKEN_EOF);
     }
@@ -1069,7 +1097,7 @@ int lex_do(lex_file *lex)
         if (!strcmp(v, "framevalue"))
         {
             ch = lex_getch(lex);
-            while (ch != EOF && util_isspace(ch) && ch != '\n')
+            while (ch != FS_FILE_EOF && util_isspace(ch) && ch != '\n')
                 ch = lex_getch(lex);
 
             if (!util_isdigit(ch)) {
@@ -1149,7 +1177,7 @@ int lex_do(lex_file *lex)
             vec_free(lex->frames);
             /* skip line (fteqcc does it too) */
             ch = lex_getch(lex);
-            while (ch != EOF && ch != '\n')
+            while (ch != FS_FILE_EOF && ch != '\n')
                 ch = lex_getch(lex);
             return lex_do(lex);
         }
@@ -1163,7 +1191,7 @@ int lex_do(lex_file *lex)
         {
             /* skip line */
             ch = lex_getch(lex);
-            while (ch != EOF && ch != '\n')
+            while (ch != FS_FILE_EOF && ch != '\n')
                 ch = lex_getch(lex);
             return lex_do(lex);
         }
@@ -1473,14 +1501,10 @@ int lex_do(lex_file *lex)
         lex_endtoken(lex);
 
         lex->tok.ttype = TOKEN_CHARCONST;
-         /* It's a vector if we can successfully scan 3 floats */
-#ifdef _MSC_VER
-        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 ",
+
+        /* It's a vector if we can successfully scan 3 floats */
+        if (util_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;