]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lexer.c
Initial platform / compiler specific code refactoring.
[xonotic/gmqcc.git] / lexer.c
diff --git a/lexer.c b/lexer.c
index feea8a2eb49cf5ce14873df7067d25cb4526e274..2d36afbc8f639d83cb52202f16f9dd9320701841 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -352,14 +352,18 @@ static int lex_getch(lex_file *lex)
 
     if (lex->peekpos) {
         lex->peekpos--;
-        if (!lex->push_line && lex->peek[lex->peekpos] == '\n')
+        if (!lex->push_line && lex->peek[lex->peekpos] == '\n') {
             lex->line++;
+            lex->column = 0;
+        }
         return lex->peek[lex->peekpos];
     }
 
     ch = lex_fgetc(lex);
-    if (!lex->push_line && ch == '\n')
+    if (!lex->push_line && ch == '\n') {
         lex->line++;
+        lex->column = 0;
+    }
     else if (ch == '?')
         return lex_try_trigraph(lex, ch);
     else if (!lex->flags.nodigraphs && (ch == '<' || ch == ':' || ch == '%'))
@@ -757,7 +761,7 @@ static bool lex_finish_frames(lex_file *lex)
 
 static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
 {
-    uchar_t chr;
+    utf8ch_t chr = 0;
     int ch = 0;
     int nextch;
     bool hex;
@@ -875,7 +879,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
                     }
                 }
                 if (OPTS_FLAG(UTF8) && chr >= 128) {
-                    u8len = u8_fromchar(chr, u8buf, sizeof(u8buf));
+                    u8len = utf8_from(u8buf, chr);
                     if (!u8len)
                         ch = 0;
                     else {
@@ -883,7 +887,8 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
                         lex->column += u8len;
                         for (uc = 0; uc < u8len; ++uc)
                             lex_tokench(lex, u8buf[uc]);
-                        /* the last character will be inserted with the tokench() call
+                        /*
+                         * the last character will be inserted with the tokench() call
                          * below the switch
                          */
                         ch = u8buf[uc];
@@ -1302,7 +1307,7 @@ int lex_do(lex_file *lex)
     }
 
     if (ch == '+' || ch == '-' || /* ++, --, +=, -=  and -> as well! */
-        ch == '>' || ch == '<' || /* <<, >>, <=, >=                  */
+        ch == '>' || ch == '<' || /* <<, >>, <=, >=  and >< as well! */
         ch == '=' || ch == '!' || /* <=>, ==, !=                     */
         ch == '&' || ch == '|' || /* &&, ||, &=, |=                  */
         ch == '~' || ch == '^'    /* ~=, ~, ^                        */
@@ -1310,7 +1315,9 @@ int lex_do(lex_file *lex)
         lex_tokench(lex, ch);
 
         nextch = lex_getch(lex);
-        if ((nextch == '=' && ch != '<') || (nextch == ch && ch != '!')) {
+        if ((nextch == '=' && ch != '<') ||
+            (nextch == ch  && ch != '!') ||
+            (nextch == '<' && ch == '>')) {
             lex_tokench(lex, nextch);
         } else if (ch == '<' && nextch == '=') {
             lex_tokench(lex, nextch);
@@ -1466,14 +1473,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 (platform_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;
@@ -1481,9 +1484,9 @@ int lex_do(lex_file *lex)
         else
         {
             if (!lex->flags.preprocessing && strlen(lex->tok.value) > 1) {
-                uchar_t u8char;
+                utf8ch_t u8char;
                 /* check for a valid utf8 character */
-                if (!OPTS_FLAG(UTF8) || !u8_analyze(lex->tok.value, NULL, NULL, &u8char, 8)) {
+                if (!OPTS_FLAG(UTF8) || !utf8_to(&u8char, (const unsigned char *)lex->tok.value, 8)) {
                     if (lexwarn(lex, WARN_MULTIBYTE_CHARACTER,
                                 ( OPTS_FLAG(UTF8) ? "invalid multibyte character sequence `%s`"
                                                   : "multibyte character: `%s`" ),
@@ -1513,6 +1516,6 @@ int lex_do(lex_file *lex)
         return (lex->tok.ttype = ch);
     }
 
-    lexerror(lex, "unknown token: `%s`", lex->tok.value);
+    lexerror(lex, "unknown token: `%c`", ch);
     return (lex->tok.ttype = TOKEN_ERROR);
 }