]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programparser.cpp
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / programparser.cpp
index b607e1cfb8f063abfec2127b7c57247809677a81..83a2879f4feeaa92ba5e947149b45064b134ae44 100644 (file)
@@ -3,6 +3,8 @@
 #include <msp/strings/regex.h>
 #include "programparser.h"
 
+#undef interface
+
 using namespace std;
 
 namespace Msp {
@@ -68,18 +70,20 @@ ProgramParser::~ProgramParser()
        delete module;
 }
 
-Module &ProgramParser::parse(const string &s, const string &n)
+Module &ProgramParser::parse(const string &s, const string &n, unsigned i)
 {
        source = s;
        source_name = n;
+       source_index = i;
        parse_source();
        return *module;
 }
 
-Module &ProgramParser::parse(IO::Base &io, const string &n)
+Module &ProgramParser::parse(IO::Base &io, const string &n, unsigned i)
 {
        source = string();
        source_name = n;
+       source_index = i;
        while(!io.eof())
        {
                char buffer[4096];
@@ -110,7 +114,7 @@ void ProgramParser::parse_source()
        source_end = source.end();
        current_line = 1;
        allow_preprocess = true;
-       while(RefPtr<Node> statement = parse_global_declaration())
+       while(RefPtr<Statement> statement = parse_global_declaration())
                cur_stage->content.body.push_back(statement);
 }
 
@@ -339,7 +343,7 @@ bool ProgramParser::is_qualifier(const string &token)
 
 bool ProgramParser::is_builtin_type(const string &token)
 {
-       static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D)(Array)?(Shadow)?|Cube(Shadow)?|3D))$");
+       static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
        return re.match(token);
 }
 
@@ -365,8 +369,10 @@ void ProgramParser::preprocess()
        string token = peek_token();
        if(token=="pragma")
                preprocess_pragma();
+       else if(token=="version")
+               preprocess_version();
        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")
+               token=="elif" || token=="endif" || token=="error" || token=="extension" || 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"));
@@ -374,6 +380,18 @@ void ProgramParser::preprocess()
        iter = line_end;
 }
 
+void ProgramParser::preprocess_version()
+{
+       expect("version");
+       string token = parse_token();
+       unsigned version = lexical_cast<unsigned>(token);
+       cur_stage->required_version = Version(version/100, version%100);
+
+       token = parse_token();
+       if(!token.empty())
+               throw runtime_error(format_syntax_error("end of line"));
+}
+
 void ProgramParser::preprocess_pragma()
 {
        expect("pragma");
@@ -424,7 +442,7 @@ void ProgramParser::preprocess_stage()
        cur_stage = &module->stages.back();
 }
 
-RefPtr<Node> ProgramParser::parse_global_declaration()
+RefPtr<Statement> ProgramParser::parse_global_declaration()
 {
        allow_stage_change = true;
        string token = peek_token();
@@ -441,6 +459,8 @@ RefPtr<Node> ProgramParser::parse_global_declaration()
                if(is_interface_qualifier(token) && peek_token(1)==";")
                {
                        RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
+                       iface_lo->source = source_index;
+                       iface_lo->line = current_line;
                        iface_lo->layout.qualifiers = layout->qualifiers;
                        iface_lo->interface = parse_token();
                        expect(";");
@@ -478,13 +498,15 @@ RefPtr<Node> ProgramParser::parse_global_declaration()
                throw runtime_error(format_syntax_error("a global declaration"));
 }
 
-RefPtr<Node> ProgramParser::parse_statement()
+RefPtr<Statement> ProgramParser::parse_statement()
 {
        string token = peek_token();
        if(token=="if")
                return parse_conditional();
        else if(token=="for")
-               return parse_iteration();
+               return parse_for();
+       else if(token=="while")
+               return parse_while();
        else if(token=="passthrough")
                return parse_passthrough();
        else if(token=="return")
@@ -492,6 +514,8 @@ RefPtr<Node> ProgramParser::parse_statement()
        else if(token=="break" || token=="continue" || token=="discard")
        {
                RefPtr<Jump> jump = new Jump;
+               jump->source = source_index;
+               jump->line = current_line;
                jump->keyword = parse_token();
                expect(";");
 
@@ -502,6 +526,8 @@ RefPtr<Node> ProgramParser::parse_statement()
        else if(!token.empty())
        {
                RefPtr<ExpressionStatement> expr = new ExpressionStatement;
+               expr->source = source_index;
+               expr->line = current_line;
                expr->expression = parse_expression();
                expect(";");
 
@@ -518,7 +544,9 @@ RefPtr<Import> ProgramParser::parse_import()
 
        expect("import");
        RefPtr<Import> import = new Import;
-       import->module = parse_token();
+       import->source = source_index;
+       import->line = current_line;
+       import->module = expect_identifier();
        expect(";");
        return import;
 }
@@ -527,6 +555,8 @@ RefPtr<Precision> ProgramParser::parse_precision()
 {
        expect("precision");
        RefPtr<Precision> precision = new Precision;
+       precision->source = source_index;
+       precision->line = current_line;
 
        precision->precision = parse_token();
        if(!is_precision_qualifier(precision->precision))
@@ -713,6 +743,8 @@ RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
 {
        expect("struct");
        RefPtr<StructDeclaration> strct = new StructDeclaration;
+       strct->source = source_index;
+       strct->line = current_line;
 
        strct->name = expect_identifier();
        parse_block(strct->members, true);
@@ -725,6 +757,8 @@ RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
 RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
 {
        RefPtr<VariableDeclaration> var = new VariableDeclaration;
+       var->source = source_index;
+       var->line = current_line;
 
        string token = peek_token();
        while(is_qualifier(token))
@@ -766,6 +800,8 @@ RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
 RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
 {
        RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
+       func->source = source_index;
+       func->line = current_line;
 
        func->return_type = expect_type();
        func->name = expect_identifier();
@@ -802,6 +838,8 @@ RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
 RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
 {
        RefPtr<InterfaceBlock> iface = new InterfaceBlock;
+       iface->source = source_index;
+       iface->line = current_line;
 
        iface->interface = parse_token();
        if(!is_interface_qualifier(iface->interface))
@@ -826,8 +864,10 @@ RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
 RefPtr<Conditional> ProgramParser::parse_conditional()
 {
        expect("if");
-       expect("(");
        RefPtr<Conditional> cond = new Conditional;
+       cond->source = source_index;
+       cond->line = current_line;
+       expect("(");
        cond->condition = parse_expression();
        expect(")");
 
@@ -843,11 +883,13 @@ RefPtr<Conditional> ProgramParser::parse_conditional()
        return cond;
 }
 
-RefPtr<Iteration> ProgramParser::parse_iteration()
+RefPtr<Iteration> ProgramParser::parse_for()
 {
        expect("for");
-       expect("(");
        RefPtr<Iteration> loop = new Iteration;
+       loop->source = source_index;
+       loop->line = current_line;
+       expect("(");
        string token = peek_token();
        if(is_type(token))
                loop->init_statement = parse_statement();
@@ -873,10 +915,27 @@ RefPtr<Iteration> ProgramParser::parse_iteration()
        return loop;
 }
 
+RefPtr<Iteration> ProgramParser::parse_while()
+{
+       expect("while");
+       RefPtr<Iteration> loop = new Iteration;
+       loop->source = source_index;
+       loop->line = current_line;
+       expect("(");
+       loop->condition = parse_expression();
+       expect(")");
+
+       parse_block(loop->body, false);
+
+       return loop;
+}
+
 RefPtr<Passthrough> ProgramParser::parse_passthrough()
 {
        expect("passthrough");
        RefPtr<Passthrough> pass = new Passthrough;
+       pass->source = source_index;
+       pass->line = current_line;
        if(cur_stage->type==GEOMETRY)
        {
                expect("[");
@@ -891,6 +950,8 @@ RefPtr<Return> ProgramParser::parse_return()
 {
        expect("return");
        RefPtr<Return> ret = new Return;
+       ret->source = source_index;
+       ret->line = current_line;
        if(peek_token()!=";")
                ret->expression = parse_expression();
        expect(";");