+#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/regex.h>
#include <msp/strings/utils.h>
source_index = index;
source_reference(1, name);
tokenizer.begin(name, source);
- while(RefPtr<Statement> statement = parse_global_declaration())
- cur_stage->content.body.push_back(statement);
+ allow_stage_change = true;
+ while(!tokenizer.peek_token().empty())
+ if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
+ cur_stage->content.body.push_back(statement);
+
+ if(!errors.empty())
+ throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
}
void Parser::set_required_version(const Version &ver)
return re.match(token);
}
+template<typename T>
+RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
+{
+ tokenizer.clear_progress_mark();
+ try
+ {
+ return (this->*parse_func)();
+ }
+ catch(const invalid_shader_source &exc)
+ {
+ errors.push_back(exc.what());
+ }
+
+ if(tokenizer.get_last_token()!=";" || !tokenizer.get_progress_mark())
+ {
+ unsigned scope_level = 0;
+ while(1)
+ {
+ if(tokenizer.peek_token()=="}" && scope_level==0)
+ {
+ if(!tokenizer.get_progress_mark())
+ tokenizer.parse_token();
+ break;
+ }
+
+ string token = tokenizer.parse_token();
+ if(token=="}")
+ {
+ --scope_level;
+ if(scope_level==0)
+ break;
+ }
+ else if(token=="{")
+ ++scope_level;
+ else if(token==";" && scope_level==0)
+ break;
+ else if(token.empty())
+ break;
+ }
+ }
+
+ return RefPtr<T>();
+}
+
RefPtr<Statement> Parser::parse_global_declaration()
{
- allow_stage_change = true;
string token = tokenizer.peek_token();
- allow_stage_change = false;
+ SetFlag disallow(allow_stage_change, false);
if(token=="import")
return parse_import();
}
else if(is_qualifier(token) || is_type(token))
return parse_variable_declaration();
+ else if(token==";")
+ {
+ tokenizer.parse_token();
+ throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
+ }
else if(!token.empty())
{
RefPtr<ExpressionStatement> expr = new ExpressionStatement;
if(have_braces)
{
while(tokenizer.peek_token()!="}")
- block.body.push_back((this->*parse_content)());
+ if(RefPtr<Statement> node = parse_with_recovery(parse_content))
+ block.body.push_back(node);
}
else
block.body.push_back((this->*parse_content)());
std::string::const_iterator iter;
std::string::const_iterator source_end;
Location location;
+ bool progress_mark;
bool allow_preprocess;
bool suppress_line_advance;
std::string last_token;
void begin(const std::string &, const std::string &);
const std::string &peek_token(unsigned = 0);
const std::string &parse_token();
+ const std::string &get_last_token() const { return last_token; }
void expect(const std::string &);
void set_location(const Location &);
+ void clear_progress_mark() { progress_mark = false; }
+ bool get_progress_mark() const { return progress_mark; }
const Location &get_location() const { return location; }
private:
std::string parse_token_();