]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/tokenizer.cpp
Follow the GLSL syntax rules more closely
[libs/gl.git] / source / glsl / tokenizer.cpp
index be432143d34e2c6f21c570287bb2ac483c2515a3..e73afd5273add2dec2f907f223b2b1e5421b9f9d 100644 (file)
@@ -61,17 +61,21 @@ string Tokenizer::parse_token_()
        while(1)
        {
                skip_comment_and_whitespace();
+               bool allow_preproc = allow_preprocess;
+               allow_preprocess = false;
                if(iter==source_end)
                        return string();
-               else if(allow_preprocess && *iter=='#')
+               else if(allow_preproc && *iter=='#')
                {
-                       allow_preprocess = false;
+                       ++iter;
                        preprocess();
                }
                else if(isalpha(*iter) || *iter=='_')
                        return parse_identifier();
                else if(isdigit(*iter))
                        return parse_number();
+               else if(*iter=='"' || *iter=='#' || *iter=='$' || *iter=='\'' || *iter=='@' || *iter=='\\' || *iter=='`')
+                       throw syntax_error(location, string(1, *iter), "Invalid character in source");
                else
                        return parse_other();
        }
@@ -106,23 +110,45 @@ string Tokenizer::parse_identifier()
 
 string Tokenizer::parse_number()
 {
-       bool accept_sign = false;
+       bool got_fract = false;
        string number;
        while(iter!=source_end)
        {
-               if(isdigit(*iter) || *iter=='.')
+               if(isdigit(*iter))
                        number += *iter++;
-               else if(*iter=='e' || *iter=='E')
+               else if(!got_fract && *iter=='.')
                {
                        number += *iter++;
-                       accept_sign = true;
+                       got_fract = true;
                }
-               else if(accept_sign && (*iter=='+' || *iter=='-'))
-                       number += *iter++;
                else
                        break;
        }
 
+       bool require_digit = false;
+       if(iter!=source_end && (*iter=='e' || *iter=='E'))
+       {
+               number += *iter++;
+               if(iter!=source_end && (*iter=='-' || *iter=='+'))
+                       number += *iter++;
+               require_digit = true;
+               while(iter!=source_end)
+               {
+                       if(isdigit(*iter))
+                       {
+                               number += *iter++;
+                               require_digit = false;
+                       }
+                       else
+                               break;
+               }
+       }
+
+       if(require_digit)
+               throw syntax_error(location, number, "Incomplete numeric literal");
+       else if(isalnum(*iter) || *iter=='_')
+               throw syntax_error(location, number, "Garbage at end of numeric literal");
+
        return number;
 }