]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/parser.cpp
Make the GLSL parser resilient against common errors
[libs/gl.git] / source / glsl / parser.cpp
index 60cc3516b0750db9f131e8a665b135c89d7dab7b..d9fdbc4ea998e56d98d2209e6a5d79558511bf34 100644 (file)
@@ -1,5 +1,7 @@
+#include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
 #include "glsl_error.h"
 #include "parser.h"
 
@@ -17,7 +19,9 @@ Parser::Parser():
 {
        tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
        preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
+       preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
        preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
+       preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
 }
 
 Parser::~Parser()
@@ -28,38 +32,52 @@ Parser::~Parser()
 Module &Parser::parse(const string &s, const string &n, unsigned i)
 {
        source = s;
-       source_index = i;
-       parse_source(n);
+       parse_source(n, i);
        return *module;
 }
 
 Module &Parser::parse(IO::Base &io, const string &n, unsigned i)
 {
        source = string();
-       source_index = i;
        while(!io.eof())
        {
                char buffer[4096];
                unsigned len = io.read(buffer, sizeof(buffer));
                source.append(buffer, len);
        }
-       parse_source(n);
+       parse_source(n, i);
        return *module;
 }
 
-void Parser::parse_source(const string &name)
+void Parser::parse_source(const string &name, unsigned index)
 {
        delete module;
        module = new Module;
        cur_stage = &module->shared;
+       base_index = index;
+       source_index = index;
+       source_reference(1, name);
        tokenizer.begin(name, source);
-       while(RefPtr<Statement> statement = parse_global_declaration())
-               cur_stage->content.body.push_back(statement);
+       allow_stage_change = true;
+       while(!tokenizer.peek_token().empty())
+               if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
+                       cur_stage->content.body.push_back(statement);
+
+       if(!errors.empty())
+               throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
 }
 
 void Parser::set_required_version(const Version &ver)
 {
-       cur_stage->required_version = ver;
+       cur_stage->required_features.glsl_version = ver;
+}
+
+void Parser::source_reference(unsigned index, const string &name)
+{
+       if(index<1)
+               throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
+
+       module->source_map.set_name(base_index+index-1, name);
 }
 
 void Parser::stage_change(Stage::Type stage)
@@ -76,6 +94,21 @@ void Parser::stage_change(Stage::Type stage)
        cur_stage = &module->stages.back();
 }
 
+void Parser::line_change(int index, unsigned line)
+{
+       if(index>0)
+               source_index = base_index+index-1;
+       else if(index==0)
+               source_index = 0;
+       else
+               index = source_index;
+
+       string name = module->source_map.get_name(index);
+       if(name.empty())
+               name = format("<%d>", index);
+       tokenizer.set_location(Location(name, line));
+}
+
 string Parser::expect_type()
 {
        string token = tokenizer.parse_token();
@@ -92,6 +125,14 @@ string Parser::expect_identifier()
        return token;
 }
 
+int Parser::expect_integer()
+{
+       string token = tokenizer.parse_token();
+       if(!isnumrc(token))
+               throw parse_error(tokenizer.get_location(), token, "an integer literal");
+       return lexical_cast<int>(token);
+}
+
 bool Parser::check(const string &token)
 {
        bool result = (tokenizer.peek_token()==token);
@@ -146,11 +187,54 @@ bool Parser::is_identifier(const string &token)
        return re.match(token);
 }
 
+template<typename T>
+RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
+{
+       tokenizer.clear_progress_mark();
+       try
+       {
+               return (this->*parse_func)();
+       }
+       catch(const invalid_shader_source &exc)
+       {
+               errors.push_back(exc.what());
+       }
+
+       if(tokenizer.get_last_token()!=";" || !tokenizer.get_progress_mark())
+       {
+               unsigned scope_level = 0;
+               while(1)
+               {
+                       if(tokenizer.peek_token()=="}" && scope_level==0)
+                       {
+                               if(!tokenizer.get_progress_mark())
+                                       tokenizer.parse_token();
+                               break;
+                       }
+
+                       string token = tokenizer.parse_token();
+                       if(token=="}")
+                       {
+                               --scope_level;
+                               if(scope_level==0)
+                                       break;
+                       }
+                       else if(token=="{")
+                               ++scope_level;
+                       else if(token==";" && scope_level==0)
+                               break;
+                       else if(token.empty())
+                               break;
+               }
+       }
+
+       return RefPtr<T>();
+}
+
 RefPtr<Statement> Parser::parse_global_declaration()
 {
-       allow_stage_change = true;
        string token = tokenizer.peek_token();
-       allow_stage_change = false;
+       SetFlag disallow(allow_stage_change, false);
 
        if(token=="import")
                return parse_import();
@@ -227,6 +311,11 @@ RefPtr<Statement> Parser::parse_statement()
        }
        else if(is_qualifier(token) || is_type(token))
                return parse_variable_declaration();
+       else if(token==";")
+       {
+               tokenizer.parse_token();
+               throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
+       }
        else if(!token.empty())
        {
                RefPtr<ExpressionStatement> expr = new ExpressionStatement;
@@ -289,10 +378,18 @@ RefPtr<Layout> Parser::parse_layout()
 
                layout->qualifiers.push_back(Layout::Qualifier());
                Layout::Qualifier &qual = layout->qualifiers.back();
-               qual.identifier = token;
+               qual.name = token;
 
-               if(check("="))
-                       qual.value = tokenizer.parse_token();
+               if((qual.has_value = check("=")))
+               {
+                       if(qual.name=="constant_id" && tokenizer.peek_token()=="auto")
+                       {
+                               qual.value = -1;
+                               tokenizer.parse_token();
+                       }
+                       else
+                               qual.value = expect_integer();
+               }
 
                if(tokenizer.peek_token()==")")
                        break;
@@ -304,7 +401,8 @@ RefPtr<Layout> Parser::parse_layout()
        return layout;
 }
 
-void Parser::parse_block(Block &block, bool require_braces)
+template<typename T>
+void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
 {
        bool have_braces = (require_braces || tokenizer.peek_token()=="{");
        if(have_braces)
@@ -313,10 +411,11 @@ void Parser::parse_block(Block &block, bool require_braces)
        if(have_braces)
        {
                while(tokenizer.peek_token()!="}")
-                       block.body.push_back(parse_statement());
+                       if(RefPtr<Statement> node = parse_with_recovery(parse_content))
+                               block.body.push_back(node);
        }
        else
-               block.body.push_back(parse_statement());
+               block.body.push_back((this->*parse_content)());
 
        block.use_braces = (require_braces || block.body.size()!=1);
 
@@ -451,7 +550,7 @@ RefPtr<StructDeclaration> Parser::parse_struct_declaration()
        strct->line = tokenizer.get_location().line;
 
        strct->name = expect_identifier();
-       parse_block(strct->members, true);
+       parse_block(strct->members, true, &Parser::parse_variable_declaration);
        tokenizer.expect(";");
 
        declared_types.insert(strct->name);
@@ -501,6 +600,18 @@ RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
        return var;
 }
 
+RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
+{
+       RefPtr<Layout> layout;
+       if(tokenizer.peek_token()=="layout")
+               layout = parse_layout();
+
+       RefPtr<VariableDeclaration> var = parse_variable_declaration();
+       var->layout = layout;
+
+       return var;
+}
+
 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
 {
        RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
@@ -529,7 +640,7 @@ RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
        if(token=="{")
        {
                func->definition = func.get();
-               parse_block(func->body, true);
+               parse_block(func->body, true, &Parser::parse_statement);
        }
        else if(token==";")
                tokenizer.parse_token();
@@ -550,7 +661,7 @@ RefPtr<InterfaceBlock> Parser::parse_interface_block()
                throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
 
        iface->name = expect_identifier();
-       parse_block(iface->members, true);
+       parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
        if(!check(";"))
        {
                iface->instance_name = expect_identifier();
@@ -575,13 +686,13 @@ RefPtr<Conditional> Parser::parse_conditional()
        cond->condition = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(cond->body, false);
+       parse_block(cond->body, false, &Parser::parse_statement);
 
        string token = tokenizer.peek_token();
        if(token=="else")
        {
                tokenizer.parse_token();
-               parse_block(cond->else_body, false);
+               parse_block(cond->else_body, false, &Parser::parse_statement);
        }
 
        return cond;
@@ -614,7 +725,7 @@ RefPtr<Iteration> Parser::parse_for()
                loop->loop_expression = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(loop->body, false);
+       parse_block(loop->body, false, &Parser::parse_statement);
 
        return loop;
 }
@@ -629,7 +740,7 @@ RefPtr<Iteration> Parser::parse_while()
        loop->condition = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(loop->body, false);
+       parse_block(loop->body, false, &Parser::parse_statement);
 
        return loop;
 }