X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fprogramparser.cpp;h=c3049dd3649bc5b2d3360b5afa7a4c571787d45c;hb=eb6a546dcffc1b67f0a23d8b075fbdc86a2872a0;hp=453eb18d31db554f0dd1fc47241a5c770db858dd;hpb=ed2d1c8c5ac065751a7887b178b4499007c1ffe4;p=libs%2Fgl.git 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);