]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programparser.cpp
Fix a stale pointer issue with Animation
[libs/gl.git] / source / programparser.cpp
index da3ee0b99f5c383b5f9475676dd2d7b3eeb8c3ff..e7343547da1f5f736da555d68a1f4edeeef76a42 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
 #include "programparser.h"
@@ -67,16 +68,18 @@ ProgramParser::~ProgramParser()
        delete module;
 }
 
-Module &ProgramParser::parse(const string &s)
+Module &ProgramParser::parse(const string &s, const string &n)
 {
        source = s;
+       source_name = n;
        parse_source();
        return *module;
 }
 
-Module &ProgramParser::parse(IO::Base &io)
+Module &ProgramParser::parse(IO::Base &io, const string &n)
 {
        source = string();
+       source_name = n;
        while(!io.eof())
        {
                char buffer[4096];
@@ -89,72 +92,84 @@ Module &ProgramParser::parse(IO::Base &io)
 
 void ProgramParser::parse_source()
 {
+       while(1)
+       {
+               string::size_type slashes = source.find("//////");
+               if(slashes==string::npos)
+                       break;
+
+               string::size_type newline = source.find('\n', slashes);
+               string pragma = format("#pragma MSP stage(%s)", source.substr(slashes+6, newline-slashes-6));
+               source.replace(slashes, newline-slashes, pragma);
+       }
+
        delete module;
        module = new Module;
        cur_stage = &module->shared;
        iter = source.begin();
-       while(1)
-       {
-               while(RefPtr<Node> statement = parse_global_declaration())
-                       cur_stage->content.body.push_back(statement);
-
-               parse_token();
-               string token = parse_token();
-               if(token.empty())
-                       break;
-               else if(token=="vertex")
-                       module->stages.push_back(VERTEX);
-               else if(token=="geometry")
-                       module->stages.push_back(GEOMETRY);
-               else if(token=="fragment")
-                       module->stages.push_back(FRAGMENT);
-               else
-                       throw runtime_error(format("Parse error at '%s': expected stage identifier", token));
+       source_end = source.end();
+       current_line = 1;
+       allow_preprocess = true;
+       while(RefPtr<Node> statement = parse_global_declaration())
+               cur_stage->content.body.push_back(statement);
+}
 
-               if(cur_stage->type!=SHARED)
-                       module->stages.back().previous = cur_stage;
-               cur_stage = &module->stages.back();
+string ProgramParser::format_error(const std::string &message)
+{
+       string location = format("%s:%d: ", source_name, current_line);
+       return location+message;
+}
 
-               for(; (iter!=source.end() && *iter!='\n'); ++iter) ;
-       }
+string ProgramParser::format_syntax_error(const std::string &expected)
+{
+       return format_error(format("Syntax error at '%s': expected %s", last_token, expected));
 }
 
 const string &ProgramParser::peek_token(unsigned index)
 {
        while(next_tokens.size()<=index)
                next_tokens.push_back(parse_token_());
-       return next_tokens[index];
+       return (last_token = next_tokens[index]);
 }
 
-string ProgramParser::parse_token()
+const string &ProgramParser::parse_token()
 {
        if(!next_tokens.empty())
        {
-               string token = next_tokens.front();
+               last_token = next_tokens.front();
                next_tokens.pop_front();
-               return token;
+               return last_token;
        }
 
-       return parse_token_();
+       return (last_token = parse_token_());
 }
 
 string ProgramParser::parse_token_()
 {
-       if(!skip_comment_and_whitespace())
-               return string();
-
-       if(isalpha(*iter) || *iter=='_')
-               return parse_identifier();
-       else if(isdigit(*iter))
-               return parse_number();
-       else
-               return parse_other();
+       while(1)
+       {
+               skip_comment_and_whitespace();
+               if(iter==source_end)
+                       return string();
+               else if(allow_preprocess && *iter=='#')
+               {
+                       allow_preprocess = false;
+                       SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
+                       preprocess();
+               }
+               else if(isalpha(*iter) || *iter=='_')
+                       return parse_identifier();
+               else if(isdigit(*iter))
+                       return parse_number();
+               else
+                       return parse_other();
+       }
 }
 
 string ProgramParser::parse_identifier()
 {
        string ident;
-       while(iter!=source.end())
+       while(iter!=source_end)
        {
                if(isalnum(*iter) || *iter=='_')
                        ident += *iter++;
@@ -169,7 +184,7 @@ string ProgramParser::parse_number()
 {
        bool accept_sign = false;
        string number;
-       while(iter!=source.end())
+       while(iter!=source_end)
        {
                if(isdigit(*iter) || *iter=='.')
                        number += *iter++;
@@ -189,11 +204,11 @@ string ProgramParser::parse_number()
 
 string ProgramParser::parse_other()
 {
-       if(iter==source.end())
+       if(iter==source_end)
                return string();
 
        string token(1, *iter++);
-       for(unsigned i=1; (i<3 && iter!=source.end()); ++i)
+       for(unsigned i=1; (i<3 && iter!=source_end); ++i)
        {
                bool matched = false;
                for(const Operator *j=operators; (!matched && j->type); ++j)
@@ -212,11 +227,10 @@ string ProgramParser::parse_other()
        return token;
 }
 
-bool ProgramParser::skip_comment_and_whitespace()
+void ProgramParser::skip_comment_and_whitespace()
 {
        unsigned comment = 0;
-       unsigned slashes = 0;
-       while(iter!=source.end())
+       while(iter!=source_end)
        {
                if(comment==0)
                {
@@ -228,10 +242,7 @@ bool ProgramParser::skip_comment_and_whitespace()
                else if(comment==1)
                {
                        if(*iter=='/')
-                       {
                                comment = 2;
-                               slashes = 2;
-                       }
                        else if(*iter=='*')
                                comment = 3;
                        else
@@ -245,10 +256,6 @@ bool ProgramParser::skip_comment_and_whitespace()
                {
                        if(*iter=='\n')
                                comment = 0;
-                       else if(*iter=='/')
-                               ++slashes;
-                       else if(!isspace(*iter) && slashes>=6)
-                               return false;
                }
                else if(comment==3 && *iter=='*')
                        comment = 4;
@@ -260,24 +267,28 @@ bool ProgramParser::skip_comment_and_whitespace()
                                comment = 3;
                }
 
+               if(*iter=='\n')
+               {
+                       ++current_line;
+                       allow_preprocess = (comment<3);
+               }
+
                ++iter;
        }
-
-       return iter!=source.end();
 }
 
 void ProgramParser::expect(const string &token)
 {
        string parsed = parse_token();
        if(parsed!=token)
-               throw runtime_error(format("Parse error at '%s': expected '%s'", parsed, token));
+               throw runtime_error(format_syntax_error(format("'%s'", token)));
 }
 
 string ProgramParser::expect_type()
 {
        string token = parse_token();
        if(!is_type(token))
-               throw runtime_error(format("Parse error at '%s': expected a type", token));
+               throw runtime_error(format_syntax_error("a type"));
        return token;
 }
 
@@ -285,7 +296,7 @@ string ProgramParser::expect_identifier()
 {
        string token = parse_token();
        if(!is_identifier(token))
-               throw runtime_error(format("Parse error at '%s': expected an identifier", token));
+               throw runtime_error(format_syntax_error("an identifier"));
        return token;
 }
 
@@ -307,9 +318,14 @@ bool ProgramParser::is_sampling_qualifier(const string &token)
        return token=="centroid";
 }
 
+bool ProgramParser::is_precision_qualifier(const string &token)
+{
+       return (token=="highp" || token=="mediump" || token=="lowp");
+}
+
 bool ProgramParser::is_qualifier(const string &token)
 {
-       return (token=="const" || is_interface_qualifier(token) || is_sampling_qualifier(token));
+       return (token=="const" || is_interface_qualifier(token) || is_sampling_qualifier(token) || is_precision_qualifier(token));
 }
 
 bool ProgramParser::is_builtin_type(const string &token)
@@ -329,11 +345,86 @@ bool ProgramParser::is_identifier(const string &token)
        return re.match(token);
 }
 
+void ProgramParser::preprocess()
+{
+       expect("#");
+
+       string::const_iterator line_end = iter;
+       for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
+       SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
+
+       string token = peek_token();
+       if(token=="pragma")
+               preprocess_pragma();
+       else if(token=="define" || token=="undef" || token=="if" || token=="ifdef" || token=="ifndef" || token=="else" ||
+               token=="elif" || token=="endif" || token=="error" || token=="extension" || token=="version" || token=="line")
+               throw runtime_error(format_error(format("Unsupported preprocessor directive '%s'", token)));
+       else if(!token.empty())
+               throw runtime_error(format_syntax_error("a preprocessor directive"));
+
+       iter = line_end;
+}
+
+void ProgramParser::preprocess_pragma()
+{
+       expect("pragma");
+       string token = parse_token();
+       if(token=="MSP")
+               preprocess_pragma_msp();
+}
+
+void ProgramParser::preprocess_pragma_msp()
+{
+       string token = peek_token();
+       if(token=="stage")
+               preprocess_stage();
+       else
+               throw runtime_error(format_error(format("Unrecognized MSP pragma '%s'", token)));
+
+       token = parse_token();
+       if(!token.empty())
+               throw runtime_error(format_syntax_error("end of line"));
+}
+
+void ProgramParser::preprocess_stage()
+{
+       if(!allow_stage_change)
+               throw runtime_error(format_error("Changing stage not allowed here"));
+
+       expect("stage");
+       expect("(");
+       string token = expect_identifier();
+       StageType stage = SHARED;
+       if(token=="vertex")
+               stage = VERTEX;
+       else if(token=="geometry")
+               stage = GEOMETRY;
+       else if(token=="fragment")
+               stage = FRAGMENT;
+       else
+               throw runtime_error(format_syntax_error("stage identifier"));
+       expect(")");
+
+       if(stage<=cur_stage->type)
+               throw runtime_error(format_error(format("Stage '%s' not allowed here", token)));
+
+       module->stages.push_back(stage);
+
+       if(cur_stage->type!=SHARED)
+               module->stages.back().previous = cur_stage;
+       cur_stage = &module->stages.back();
+}
+
 RefPtr<Node> ProgramParser::parse_global_declaration()
 {
+       allow_stage_change = true;
        string token = peek_token();
+       allow_stage_change = false;
+
        if(token=="import")
                return parse_import();
+       else if(token=="precision")
+               return parse_precision();
        else if(token=="layout")
        {
                RefPtr<Layout> layout = parse_layout();
@@ -355,15 +446,16 @@ RefPtr<Node> ProgramParser::parse_global_declaration()
        }
        else if(token=="struct")
                return parse_struct_declaration();
-       else if(is_sampling_qualifier(token) || token=="const")
-               return parse_variable_declaration();
        else if(is_interface_qualifier(token))
        {
-               if(is_type(peek_token(1)))
+               string next = peek_token(1);
+               if(is_type(next) || is_precision_qualifier(next))
                        return parse_variable_declaration();
                else
                        return parse_interface_block();
        }
+       else if(is_qualifier(token))
+               return parse_variable_declaration();
        else if(is_type(token))
        {
                if(peek_token(2)=="(")
@@ -374,7 +466,7 @@ RefPtr<Node> ProgramParser::parse_global_declaration()
        else if(token.empty())
                return 0;
        else
-               throw runtime_error(format("Syntax error at '%s': expected a global declaration", token));
+               throw runtime_error(format_syntax_error("a global declaration"));
 }
 
 RefPtr<Node> ProgramParser::parse_statement()
@@ -388,6 +480,14 @@ RefPtr<Node> ProgramParser::parse_statement()
                return parse_passthrough();
        else if(token=="return")
                return parse_return();
+       else if(token=="break" || token=="continue" || token=="discard")
+       {
+               RefPtr<Jump> jump = new Jump;
+               jump->keyword = parse_token();
+               expect(";");
+
+               return jump;
+       }
        else if(is_qualifier(token) || is_type(token))
                return parse_variable_declaration();
        else if(!token.empty())
@@ -399,13 +499,13 @@ RefPtr<Node> ProgramParser::parse_statement()
                return expr;
        }
        else
-               throw runtime_error(format("Syntax error at '%s': expected a statement", token));
+               throw runtime_error(format_syntax_error("a statement"));
 }
 
 RefPtr<Import> ProgramParser::parse_import()
 {
        if(cur_stage->type!=SHARED)
-               throw runtime_error("Imports are only allowed in the shared section");
+               throw runtime_error(format_error("Imports are only allowed in the shared section"));
 
        expect("import");
        RefPtr<Import> import = new Import;
@@ -414,6 +514,25 @@ RefPtr<Import> ProgramParser::parse_import()
        return import;
 }
 
+RefPtr<Precision> ProgramParser::parse_precision()
+{
+       expect("precision");
+       RefPtr<Precision> precision = new Precision;
+
+       precision->precision = parse_token();
+       if(!is_precision_qualifier(precision->precision))
+               throw runtime_error(format_syntax_error("a precision qualifier"));
+
+       precision->type = parse_token();
+       // Not entirely accurate; only float, int and sampler types are allowed
+       if(!is_builtin_type(precision->type))
+               throw runtime_error(format_syntax_error("a builtin type"));
+
+       expect(";");
+
+       return precision;
+}
+
 RefPtr<Layout> ProgramParser::parse_layout()
 {
        expect("layout");
@@ -423,7 +542,7 @@ RefPtr<Layout> ProgramParser::parse_layout()
        {
                string token = parse_token();
                if(token==")")
-                       throw runtime_error(format("Parse error at '%s': expected layout qualifier id", token));
+                       throw runtime_error(format_syntax_error("a layout qualifier name"));
 
                layout->qualifiers.push_back(Layout::Qualifier());
                Layout::Qualifier &qual = layout->qualifiers.back();
@@ -480,14 +599,14 @@ RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
                        if(left)
                                return left;
                        else
-                               throw runtime_error(format("Parse error at '%s': expected an expression", token));
+                               throw runtime_error(format_syntax_error("an expression"));
                }
                else if(left)
                {
                        if(token=="(")
                        {
                                if(!left_var)
-                                       throw runtime_error(format("Parse error at '%s': function name must be an identifier", token));
+                                       throw runtime_error(format_error("Syntax error before '(': function name must be an identifier"));
                                left = parse_function_call(*left_var);
                        }
                        else if(token==".")
@@ -509,7 +628,7 @@ RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
                        else if(oper && oper->type==BINARY)
                                left = parse_binary(left, oper);
                        else
-                               throw runtime_error(format("Parse error at '%s': expected an operator", token));
+                               throw runtime_error(format_syntax_error("an operator"));
                        left_var = 0;
                }
                else
@@ -544,7 +663,7 @@ RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
                                left = unary;
                        }
                        else
-                               throw runtime_error(format("Parse error at '%s': expected an expression", token));
+                               throw runtime_error(format_syntax_error("an expression"));
                }
        }
 }
@@ -604,7 +723,7 @@ RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
                var->sampling = parse_token();
                token = peek_token();
                if(!is_interface_qualifier(token))
-                       throw runtime_error(format("Parse error at '%s': expected an interface qualifier", token));
+                       throw runtime_error(format_syntax_error("an interface qualifier"));
        }
 
        if(is_interface_qualifier(token))
@@ -615,6 +734,9 @@ RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
                parse_token();
        }
 
+       if(is_precision_qualifier(token))
+               var->precision = parse_token();
+
        var->type = expect_type();
        var->name = expect_identifier();
 
@@ -663,7 +785,7 @@ RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
        else if(token==";")
                parse_token();
        else
-               throw runtime_error(format("Parse error at '%s': expected '{' or ';'", token));
+               throw runtime_error(format_syntax_error("'{' or ';'"));
 
        return func;
 }
@@ -674,7 +796,7 @@ RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
 
        iface->interface = parse_token();
        if(!is_interface_qualifier(iface->interface))
-               throw runtime_error(format("Parse error at '%s': expected an interface qualifier", iface->interface));
+               throw runtime_error(format_syntax_error("an interface qualifier"));
 
        iface->name = expect_identifier();
        parse_block(iface->members, true);