+#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/regex.h>
#include "programparser.h"
void ProgramParser::parse_source()
{
- delete module;
- module = new Module;
- cur_stage = &module->shared;
- iter = source.begin();
- current_line = 1;
while(1)
{
- while(RefPtr<Node> statement = parse_global_declaration())
- cur_stage->content.body.push_back(statement);
-
- parse_token();
- string token = parse_token();
- if(token.empty())
+ string::size_type slashes = source.find("//////");
+ if(slashes==string::npos)
break;
- else if(token=="vertex")
- module->stages.push_back(VERTEX);
- else if(token=="geometry")
- module->stages.push_back(GEOMETRY);
- else if(token=="fragment")
- module->stages.push_back(FRAGMENT);
- else
- throw runtime_error(format("Parse error at '%s': expected stage identifier", token));
-
- if(cur_stage->type!=SHARED)
- module->stages.back().previous = cur_stage;
- cur_stage = &module->stages.back();
- for(; (iter!=source.end() && *iter!='\n'); ++iter) ;
+ string::size_type newline = source.find('\n', slashes);
+ string pragma = format("#pragma MSP stage(%s)", source.substr(slashes+6, newline-slashes-6));
+ source.replace(slashes, newline-slashes, pragma);
}
+
+ delete module;
+ module = new Module;
+ cur_stage = &module->shared;
+ iter = source.begin();
+ source_end = source.end();
+ current_line = 1;
+ allow_preprocess = true;
+ while(RefPtr<Node> statement = parse_global_declaration())
+ cur_stage->content.body.push_back(statement);
}
string ProgramParser::format_error(const std::string &message)
string ProgramParser::parse_token_()
{
- if(!skip_comment_and_whitespace())
- return string();
-
- if(isalpha(*iter) || *iter=='_')
- return parse_identifier();
- else if(isdigit(*iter))
- return parse_number();
- else
- return parse_other();
+ while(1)
+ {
+ skip_comment_and_whitespace();
+ if(iter==source_end)
+ return string();
+ else if(allow_preprocess && *iter=='#')
+ {
+ allow_preprocess = false;
+ SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
+ preprocess();
+ }
+ else if(isalpha(*iter) || *iter=='_')
+ return parse_identifier();
+ else if(isdigit(*iter))
+ return parse_number();
+ else
+ return parse_other();
+ }
}
string ProgramParser::parse_identifier()
{
string ident;
- while(iter!=source.end())
+ while(iter!=source_end)
{
if(isalnum(*iter) || *iter=='_')
ident += *iter++;
{
bool accept_sign = false;
string number;
- while(iter!=source.end())
+ while(iter!=source_end)
{
if(isdigit(*iter) || *iter=='.')
number += *iter++;
string ProgramParser::parse_other()
{
- if(iter==source.end())
+ if(iter==source_end)
return string();
string token(1, *iter++);
- for(unsigned i=1; (i<3 && iter!=source.end()); ++i)
+ for(unsigned i=1; (i<3 && iter!=source_end); ++i)
{
bool matched = false;
for(const Operator *j=operators; (!matched && j->type); ++j)
return token;
}
-bool ProgramParser::skip_comment_and_whitespace()
+void ProgramParser::skip_comment_and_whitespace()
{
unsigned comment = 0;
- unsigned slashes = 0;
- while(iter!=source.end())
+ while(iter!=source_end)
{
if(comment==0)
{
else if(comment==1)
{
if(*iter=='/')
- {
comment = 2;
- slashes = 2;
- }
else if(*iter=='*')
comment = 3;
else
{
if(*iter=='\n')
comment = 0;
- else if(*iter=='/')
- ++slashes;
- else if(!isspace(*iter) && slashes>=6)
- return false;
}
else if(comment==3 && *iter=='*')
comment = 4;
}
if(*iter=='\n')
+ {
++current_line;
+ allow_preprocess = (comment<3);
+ }
++iter;
}
-
- return iter!=source.end();
}
void ProgramParser::expect(const string &token)
return re.match(token);
}
+void ProgramParser::preprocess()
+{
+ expect("#");
+
+ string::const_iterator line_end = iter;
+ for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
+ SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
+
+ string token = peek_token();
+ if(token=="pragma")
+ preprocess_pragma();
+ 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")
+ throw runtime_error(format_error(format("Unsupported preprocessor directive '%s'", token)));
+ else if(!token.empty())
+ throw runtime_error(format_syntax_error("a preprocessor directive"));
+
+ iter = line_end;
+}
+
+void ProgramParser::preprocess_pragma()
+{
+ expect("pragma");
+ string token = parse_token();
+ if(token=="MSP")
+ preprocess_pragma_msp();
+}
+
+void ProgramParser::preprocess_pragma_msp()
+{
+ string token = peek_token();
+ if(token=="stage")
+ preprocess_stage();
+ else
+ throw runtime_error(format_error(format("Unrecognized MSP pragma '%s'", token)));
+
+ token = parse_token();
+ if(!token.empty())
+ throw runtime_error(format_syntax_error("end of line"));
+}
+
+void ProgramParser::preprocess_stage()
+{
+ if(!allow_stage_change)
+ throw runtime_error(format_error("Changing stage not allowed here"));
+
+ expect("stage");
+ expect("(");
+ string token = expect_identifier();
+ StageType stage = SHARED;
+ if(token=="vertex")
+ stage = VERTEX;
+ else if(token=="geometry")
+ stage = GEOMETRY;
+ else if(token=="fragment")
+ stage = FRAGMENT;
+ else
+ throw runtime_error(format_syntax_error("stage identifier"));
+ expect(")");
+
+ if(stage<=cur_stage->type)
+ throw runtime_error(format_error(format("Stage '%s' not allowed here", token)));
+
+ module->stages.push_back(stage);
+
+ if(cur_stage->type!=SHARED)
+ module->stages.back().previous = cur_stage;
+ cur_stage = &module->stages.back();
+}
+
RefPtr<Node> ProgramParser::parse_global_declaration()
{
+ allow_stage_change = true;
string token = peek_token();
+ allow_stage_change = false;
+
if(token=="import")
return parse_import();
else if(token=="precision")