]> git.tdb.fi Git - libs/gl.git/commitdiff
Add file and line information to ProgramParser errors
authorMikko Rasa <tdb@tdb.fi>
Tue, 10 Apr 2018 13:42:38 +0000 (16:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 10 Apr 2018 15:28:39 +0000 (18:28 +0300)
source/batch.cpp
source/program.cpp
source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programparser.h

index cba63567497499260222b4f7cbe6dd1194d2abbe..741d5a49413f5f2f0538e710ffe0148aa497de25 100644 (file)
@@ -60,12 +60,7 @@ Batch::Batch(PrimitiveType t):
        min_index(0),
        max_index(0),
        restart(false)
-{
-       /* Make sure we have glEnable/DisableClientState to go with
-       NV_primitive_restart */
-       if(!MSP_primitive_restart && NV_primitive_restart)
-               (void)(bool)MSP_legacy_features;
-}
+{ }
 
 Batch::~Batch()
 {
@@ -138,7 +133,11 @@ void Batch::append(const Batch &other)
        if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
        {
                if(!MSP_primitive_restart)
+               {
                        static Require _req(NV_primitive_restart);
+                       // Make sure we have glEnable/DisableClientState as well
+                       static Require _req2(MSP_legacy_features);
+               }
        }
 
        if(other.data.empty())
index 11ab151be4fcfef43586737969c43e638383c96b..270dbdc3e5675163017349d0cef901fcffaccdf8 100644 (file)
@@ -45,7 +45,7 @@ Program::Program(const std::string &source)
        if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl"))
        {
                if(RefPtr<IO::Seekable> io = Resources::get_builtins().open(source))
-                       compiler.compile(*io);
+                       compiler.compile(*io, source);
                else
                        throw IO::file_not_found(source);
        }
index b004b08bee6f533e437f09051ca28d9bfdb22fbb..2dabd7cd8203ff0910eb97c2528e354e0b3943ff 100644 (file)
@@ -40,20 +40,25 @@ ProgramCompiler::ProgramCompiler():
        module(0)
 { }
 
-void ProgramCompiler::compile(const string &source)
+void ProgramCompiler::compile(const string &source, const string &src_name)
 {
        resources = 0;
-       module = &parser.parse(source);
+       module = &parser.parse(source, src_name);
        process();
 }
 
-void ProgramCompiler::compile(IO::Base &io, Resources *res)
+void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
 {
        resources = res;
-       module = &parser.parse(io);
+       module = &parser.parse(io, src_name);
        process();
 }
 
+void ProgramCompiler::compile(IO::Base &io, const string &src_name)
+{
+       compile(io, 0, src_name);
+}
+
 void ProgramCompiler::add_shaders(Program &program)
 {
        if(!module)
@@ -84,7 +89,7 @@ void ProgramCompiler::add_shaders(Program &program)
 Module *ProgramCompiler::create_builtins_module()
 {
        ProgramParser parser;
-       Module *module = new Module(parser.parse(builtins_src));
+       Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
        {
                VariableResolver resolver;
@@ -137,7 +142,7 @@ void ProgramCompiler::import(const string &name)
        if(!io)
                throw runtime_error(format("module %s not found", name));
        ProgramParser import_parser;
-       Module &imported_module = import_parser.parse(*io);
+       Module &imported_module = import_parser.parse(*io, fn);
 
        inject_block(module->shared.content, imported_module.shared.content);
        apply<DeclarationCombiner>(module->shared);
index 6b1e1cdafe79f9be2ad0df6858cd89b95942e859..fea7a73fb78c3e8a0b68d12c7a9aa25f73018840 100644 (file)
@@ -376,8 +376,9 @@ private:
 public:
        ProgramCompiler();
 
-       void compile(const std::string &);
-       void compile(IO::Base &, Resources * = 0);
+       void compile(const std::string &, const std::string & = "<string>");
+       void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
+       void compile(IO::Base &, const std::string &);
        void add_shaders(Program &);
 
 private:
index 453eb18d31db554f0dd1fc47241a5c770db858dd..c3049dd3649bc5b2d3360b5afa7a4c571787d45c 100644 (file)
@@ -67,16 +67,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];
@@ -93,6 +95,7 @@ void ProgramParser::parse_source()
        module = new Module;
        cur_stage = &module->shared;
        iter = source.begin();
+       current_line = 1;
        while(1)
        {
                while(RefPtr<Node> statement = parse_global_declaration())
@@ -119,23 +122,34 @@ void ProgramParser::parse_source()
        }
 }
 
+string ProgramParser::format_error(const std::string &message)
+{
+       string location = format("%s:%d: ", source_name, current_line);
+       return location+message;
+}
+
+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_()
@@ -260,6 +274,9 @@ bool ProgramParser::skip_comment_and_whitespace()
                                comment = 3;
                }
 
+               if(*iter=='\n')
+                       ++current_line;
+
                ++iter;
        }
 
@@ -270,14 +287,14 @@ 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 +302,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;
 }
 
@@ -382,7 +399,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()
@@ -415,13 +432,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;
@@ -437,12 +454,12 @@ RefPtr<Precision> ProgramParser::parse_precision()
 
        precision->precision = parse_token();
        if(!is_precision_qualifier(precision->precision))
-               throw runtime_error(format("Parse error at '%s': expected a 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("Parse error at '%s': expected a builtin type", precision->type));
+               throw runtime_error(format_syntax_error("a builtin type"));
 
        expect(";");
 
@@ -458,7 +475,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();
@@ -515,14 +532,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==".")
@@ -544,7 +561,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
@@ -579,7 +596,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"));
                }
        }
 }
@@ -639,7 +656,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))
@@ -701,7 +718,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;
 }
@@ -712,7 +729,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);
index 927c02046fb822d17e67dcc0b8ecdfbe03bafc9f..be6da197481aa3d264c7516427b55859e40e04d9 100644 (file)
@@ -37,7 +37,10 @@ private:
        };
 
        std::string source;
+       std::string source_name;
+       unsigned current_line;
        std::string::const_iterator iter;
+       std::string last_token;
        std::deque<std::string> next_tokens;
        ProgramSyntax::Module *module;
        ProgramSyntax::Stage *cur_stage;
@@ -49,14 +52,17 @@ public:
        ProgramParser();
        ~ProgramParser();
 
-       ProgramSyntax::Module &parse(const std::string &);
-       ProgramSyntax::Module &parse(IO::Base &);
+       ProgramSyntax::Module &parse(const std::string &, const std::string &);
+       ProgramSyntax::Module &parse(IO::Base &, const std::string &);
 
 private:
        void parse_source();
 
+       std::string format_error(const std::string &);
+       std::string format_syntax_error(const std::string &);
+
        const std::string &peek_token(unsigned = 0);
-       std::string parse_token();
+       const std::string &parse_token();
        std::string parse_token_();
        std::string parse_identifier();
        std::string parse_number();