From: Mikko Rasa Date: Tue, 10 Apr 2018 13:42:38 +0000 (+0300) Subject: Add file and line information to ProgramParser errors X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=eb6a546dcffc1b67f0a23d8b075fbdc86a2872a0;p=libs%2Fgl.git Add file and line information to ProgramParser errors --- diff --git a/source/batch.cpp b/source/batch.cpp index cba63567..741d5a49 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -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()) diff --git a/source/program.cpp b/source/program.cpp index 11ab151b..270dbdc3 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -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 = Resources::get_builtins().open(source)) - compiler.compile(*io); + compiler.compile(*io, source); else throw IO::file_not_found(source); } diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index b004b08b..2dabd7cd 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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, "")); for(list::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(module->shared); diff --git a/source/programcompiler.h b/source/programcompiler.h index 6b1e1cda..fea7a73f 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -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 & = ""); + void compile(IO::Base &, Resources * = 0, const std::string & = ""); + void compile(IO::Base &, const std::string &); void add_shaders(Program &); private: diff --git a/source/programparser.cpp b/source/programparser.cpp index 453eb18d..c3049dd3 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -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 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 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 ProgramParser::parse_statement() @@ -415,13 +432,13 @@ RefPtr 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 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 = new Import; @@ -437,12 +454,12 @@ RefPtr 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 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 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 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 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 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 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 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); diff --git a/source/programparser.h b/source/programparser.h index 927c0204..be6da197 100644 --- a/source/programparser.h +++ b/source/programparser.h @@ -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 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();