--- /dev/null
+#include <msp/core/raii.h>
+#include <msp/strings/format.h>
+#include <msp/strings/regex.h>
+#include "programparser.h"
+
+#undef interface
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+using namespace ProgramSyntax;
+
+ProgramParser::Operator ProgramParser::operators[] =
+{
+ { "[", 2, BINARY, LEFT_TO_RIGHT },
+ { "(", 2, BINARY, LEFT_TO_RIGHT },
+ { ".", 2, BINARY, LEFT_TO_RIGHT },
+ { "++", 2, POSTFIX, LEFT_TO_RIGHT },
+ { "--", 2, POSTFIX, LEFT_TO_RIGHT },
+ { "++", 3, PREFIX, RIGHT_TO_LEFT },
+ { "--", 3, PREFIX, RIGHT_TO_LEFT },
+ { "+", 3, PREFIX, RIGHT_TO_LEFT },
+ { "-", 3, PREFIX, RIGHT_TO_LEFT },
+ { "~", 3, PREFIX, RIGHT_TO_LEFT },
+ { "!", 3, PREFIX, RIGHT_TO_LEFT },
+ { "*", 4, BINARY, LEFT_TO_RIGHT },
+ { "/", 4, BINARY, LEFT_TO_RIGHT },
+ { "%", 4, BINARY, LEFT_TO_RIGHT },
+ { "+", 5, BINARY, LEFT_TO_RIGHT },
+ { "-", 5, BINARY, LEFT_TO_RIGHT },
+ { "<<", 6, BINARY, LEFT_TO_RIGHT },
+ { ">>", 6, BINARY, LEFT_TO_RIGHT },
+ { "<", 7, BINARY, LEFT_TO_RIGHT },
+ { ">", 7, BINARY, LEFT_TO_RIGHT },
+ { "<=", 7, BINARY, LEFT_TO_RIGHT },
+ { ">=", 7, BINARY, LEFT_TO_RIGHT },
+ { "==", 8, BINARY, LEFT_TO_RIGHT },
+ { "!=", 8, BINARY, LEFT_TO_RIGHT },
+ { "&", 9, BINARY, LEFT_TO_RIGHT },
+ { "^", 10, BINARY, LEFT_TO_RIGHT },
+ { "|", 11, BINARY, LEFT_TO_RIGHT },
+ { "&&", 12, BINARY, LEFT_TO_RIGHT },
+ { "^^", 13, BINARY, LEFT_TO_RIGHT },
+ { "||", 14, BINARY, LEFT_TO_RIGHT },
+ { "?", 15, BINARY, RIGHT_TO_LEFT },
+ { ":", 15, BINARY, RIGHT_TO_LEFT },
+ { "=", 16, BINARY, RIGHT_TO_LEFT },
+ { "+=", 16, BINARY, RIGHT_TO_LEFT },
+ { "-=", 16, BINARY, RIGHT_TO_LEFT },
+ { "*=", 16, BINARY, RIGHT_TO_LEFT },
+ { "/=", 16, BINARY, RIGHT_TO_LEFT },
+ { "%=", 16, BINARY, RIGHT_TO_LEFT },
+ { "<<=", 16, BINARY, RIGHT_TO_LEFT },
+ { ">>=", 16, BINARY, RIGHT_TO_LEFT },
+ { "&=", 16, BINARY, RIGHT_TO_LEFT },
+ { "^=", 16, BINARY, RIGHT_TO_LEFT },
+ { "|=", 16, BINARY, RIGHT_TO_LEFT },
+ { ",", 17, BINARY, LEFT_TO_RIGHT },
+ { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
+};
+
+ProgramParser::ProgramParser():
+ module(0)
+{ }
+
+ProgramParser::~ProgramParser()
+{
+ delete module;
+}
+
+Module &ProgramParser::parse(const string &s, const string &n, unsigned i)
+{
+ source = s;
+ source_name = n;
+ source_index = i;
+ parse_source();
+ return *module;
+}
+
+Module &ProgramParser::parse(IO::Base &io, const string &n, unsigned i)
+{
+ source = string();
+ source_name = n;
+ source_index = i;
+ while(!io.eof())
+ {
+ char buffer[4096];
+ unsigned len = io.read(buffer, sizeof(buffer));
+ source.append(buffer, len);
+ }
+ parse_source();
+ return *module;
+}
+
+void ProgramParser::parse_source()
+{
+ while(1)
+ {
+ string::size_type slashes = source.find("//////");
+ if(slashes==string::npos)
+ break;
+
+ 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<Statement> statement = parse_global_declaration())
+ cur_stage->content.body.push_back(statement);
+}
+
+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 (last_token = next_tokens[index]);
+}
+
+const string &ProgramParser::parse_token()
+{
+ if(!next_tokens.empty())
+ {
+ last_token = next_tokens.front();
+ next_tokens.pop_front();
+ return last_token;
+ }
+
+ return (last_token = parse_token_());
+}
+
+string ProgramParser::parse_token_()
+{
+ 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)
+ {
+ if(isalnum(*iter) || *iter=='_')
+ ident += *iter++;
+ else
+ break;
+ }
+
+ return ident;
+}
+
+string ProgramParser::parse_number()
+{
+ bool accept_sign = false;
+ string number;
+ while(iter!=source_end)
+ {
+ if(isdigit(*iter) || *iter=='.')
+ number += *iter++;
+ else if(*iter=='e' || *iter=='E')
+ {
+ number += *iter++;
+ accept_sign = true;
+ }
+ else if(accept_sign && (*iter=='+' || *iter=='-'))
+ number += *iter++;
+ else
+ break;
+ }
+
+ return number;
+}
+
+string ProgramParser::parse_other()
+{
+ if(iter==source_end)
+ return string();
+
+ string token(1, *iter++);
+ for(unsigned i=1; (i<3 && iter!=source_end); ++i)
+ {
+ bool matched = false;
+ for(const Operator *j=operators; (!matched && j->type); ++j)
+ {
+ matched = (j->token[i]==*iter);
+ for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
+ matched = (j->token[k]==token[k]);
+ }
+
+ if(!matched)
+ break;
+
+ token += *iter++;
+ }
+
+ return token;
+}
+
+void ProgramParser::skip_comment_and_whitespace()
+{
+ unsigned comment = 0;
+ while(iter!=source_end)
+ {
+ if(comment==0)
+ {
+ if(*iter=='/')
+ comment = 1;
+ else if(!isspace(*iter))
+ break;
+ }
+ else if(comment==1)
+ {
+ if(*iter=='/')
+ comment = 2;
+ else if(*iter=='*')
+ comment = 3;
+ else
+ {
+ comment = 0;
+ --iter;
+ break;
+ }
+ }
+ else if(comment==2)
+ {
+ if(*iter=='\n')
+ comment = 0;
+ }
+ else if(comment==3 && *iter=='*')
+ comment = 4;
+ else if(comment==4)
+ {
+ if(*iter=='/')
+ comment = 0;
+ else if(*iter!='*')
+ comment = 3;
+ }
+
+ if(*iter=='\n')
+ {
+ ++current_line;
+ allow_preprocess = (comment<3);
+ }
+
+ ++iter;
+ }
+}
+
+void ProgramParser::expect(const string &token)
+{
+ string parsed = parse_token();
+ if(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_syntax_error("a type"));
+ return token;
+}
+
+string ProgramParser::expect_identifier()
+{
+ string token = parse_token();
+ if(!is_identifier(token))
+ throw runtime_error(format_syntax_error("an identifier"));
+ return token;
+}
+
+bool ProgramParser::check(const string &token)
+{
+ bool result = (peek_token()==token);
+ if(result)
+ parse_token();
+ return result;
+}
+
+bool ProgramParser::is_interface_qualifier(const string &token)
+{
+ return (token=="uniform" || token=="in" || token=="out");
+}
+
+bool ProgramParser::is_sampling_qualifier(const string &token)
+{
+ return (token=="centroid" || token=="sample");
+}
+
+bool ProgramParser::is_interpolation_qualifier(const string &token)
+{
+ return (token=="smooth" || token=="flat" || token=="noperspective");
+}
+
+bool ProgramParser::is_precision_qualifier(const string &token)
+{
+ return (token=="highp" || token=="mediump" || token=="lowp");
+}
+
+bool ProgramParser::is_qualifier(const string &token)
+{
+ return (token=="const" ||
+ is_interface_qualifier(token) ||
+ is_sampling_qualifier(token) ||
+ is_interpolation_qualifier(token) ||
+ is_precision_qualifier(token));
+}
+
+bool ProgramParser::is_builtin_type(const string &token)
+{
+ static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
+ return re.match(token);
+}
+
+bool ProgramParser::is_type(const string &token)
+{
+ return is_builtin_type(token) || declared_types.count(token);
+}
+
+bool ProgramParser::is_identifier(const string &token)
+{
+ static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
+ 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=="version")
+ preprocess_version();
+ else if(token=="define" || token=="undef" || token=="if" || token=="ifdef" || token=="ifndef" || token=="else" ||
+ token=="elif" || token=="endif" || token=="error" || token=="extension" || 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_version()
+{
+ expect("version");
+ string token = parse_token();
+ unsigned version = lexical_cast<unsigned>(token);
+ cur_stage->required_version = Version(version/100, version%100);
+
+ token = parse_token();
+ if(!token.empty())
+ throw runtime_error(format_syntax_error("end of line"));
+}
+
+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<Statement> 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")
+ return parse_precision();
+ else if(token=="layout")
+ {
+ RefPtr<Layout> layout = parse_layout();
+ token = peek_token();
+ if(is_interface_qualifier(token) && peek_token(1)==";")
+ {
+ RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
+ iface_lo->source = source_index;
+ iface_lo->line = current_line;
+ iface_lo->layout.qualifiers = layout->qualifiers;
+ iface_lo->interface = parse_token();
+ expect(";");
+ return iface_lo;
+ }
+ else
+ {
+ RefPtr<VariableDeclaration> var = parse_variable_declaration();
+ var->layout = layout;
+ return var;
+ }
+ }
+ else if(token=="struct")
+ return parse_struct_declaration();
+ else if(is_interface_qualifier(token))
+ {
+ string next = peek_token(1);
+ if(is_type(next) || is_qualifier(next))
+ return parse_variable_declaration();
+ else
+ return parse_interface_block();
+ }
+ else if(is_qualifier(token))
+ return parse_variable_declaration();
+ else if(is_type(token))
+ {
+ if(peek_token(2)=="(")
+ return parse_function_declaration();
+ else
+ return parse_variable_declaration();
+ }
+ else if(token.empty())
+ return 0;
+ else
+ throw runtime_error(format_syntax_error("a global declaration"));
+}
+
+RefPtr<Statement> ProgramParser::parse_statement()
+{
+ string token = peek_token();
+ if(token=="if")
+ return parse_conditional();
+ else if(token=="for")
+ return parse_for();
+ else if(token=="while")
+ return parse_while();
+ else if(token=="passthrough")
+ return parse_passthrough();
+ else if(token=="return")
+ return parse_return();
+ else if(token=="break" || token=="continue" || token=="discard")
+ {
+ RefPtr<Jump> jump = new Jump;
+ jump->source = source_index;
+ jump->line = current_line;
+ jump->keyword = parse_token();
+ expect(";");
+
+ return jump;
+ }
+ else if(is_qualifier(token) || is_type(token))
+ return parse_variable_declaration();
+ else if(!token.empty())
+ {
+ RefPtr<ExpressionStatement> expr = new ExpressionStatement;
+ expr->source = source_index;
+ expr->line = current_line;
+ expr->expression = parse_expression();
+ expect(";");
+
+ return expr;
+ }
+ else
+ throw runtime_error(format_syntax_error("a statement"));
+}
+
+RefPtr<Import> ProgramParser::parse_import()
+{
+ if(cur_stage->type!=SHARED)
+ throw runtime_error(format_error("Imports are only allowed in the shared section"));
+
+ expect("import");
+ RefPtr<Import> import = new Import;
+ import->source = source_index;
+ import->line = current_line;
+ import->module = expect_identifier();
+ expect(";");
+ return import;
+}
+
+RefPtr<Precision> ProgramParser::parse_precision()
+{
+ expect("precision");
+ RefPtr<Precision> precision = new Precision;
+ precision->source = source_index;
+ precision->line = current_line;
+
+ precision->precision = parse_token();
+ if(!is_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_syntax_error("a builtin type"));
+
+ expect(";");
+
+ return precision;
+}
+
+RefPtr<Layout> ProgramParser::parse_layout()
+{
+ expect("layout");
+ expect("(");
+ RefPtr<Layout> layout = new Layout;
+ while(1)
+ {
+ string token = parse_token();
+ if(token==")")
+ throw runtime_error(format_syntax_error("a layout qualifier name"));
+
+ layout->qualifiers.push_back(Layout::Qualifier());
+ Layout::Qualifier &qual = layout->qualifiers.back();
+ qual.identifier = token;
+
+ if(check("="))
+ qual.value = parse_token();
+
+ if(peek_token()==")")
+ break;
+
+ expect(",");
+ }
+ expect(")");
+
+ return layout;
+}
+
+void ProgramParser::parse_block(Block &block, bool require_braces)
+{
+ bool have_braces = (require_braces || peek_token()=="{");
+ if(have_braces)
+ expect("{");
+
+ if(have_braces)
+ {
+ while(peek_token()!="}")
+ block.body.push_back(parse_statement());
+ }
+ else
+ block.body.push_back(parse_statement());
+
+ block.use_braces = (require_braces || block.body.size()!=1);
+
+ if(have_braces)
+ expect("}");
+}
+
+RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
+{
+ RefPtr<Expression> left;
+ VariableReference *left_var = 0;
+ while(1)
+ {
+ string token = peek_token();
+
+ const Operator *oper = 0;
+ for(Operator *i=operators; (!oper && i->type); ++i)
+ if(token==i->token && (!left || i->type!=PREFIX) && (left || i->type!=POSTFIX))
+ oper = i;
+
+ if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
+ {
+ if(left)
+ return left;
+ else
+ throw runtime_error(format_syntax_error("an expression"));
+ }
+ else if(left)
+ {
+ if(token=="(")
+ {
+ if(!left_var)
+ throw runtime_error(format_error("Syntax error before '(': function name must be an identifier"));
+ left = parse_function_call(*left_var);
+ }
+ else if(token==".")
+ {
+ RefPtr<MemberAccess> memacc = new MemberAccess;
+ memacc->left = left;
+ parse_token();
+ memacc->member = expect_identifier();
+ left = memacc;
+ }
+ else if(oper && oper->type==POSTFIX)
+ {
+ RefPtr<UnaryExpression> unary = new UnaryExpression;
+ unary->oper = parse_token();
+ unary->prefix = false;
+ unary->expression = left;
+ left = unary;
+ }
+ else if(oper && oper->type==BINARY)
+ left = parse_binary(left, oper);
+ else
+ throw runtime_error(format_syntax_error("an operator"));
+ left_var = 0;
+ }
+ else
+ {
+ if(token=="(")
+ {
+ parse_token();
+ RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
+ parexpr->expression = parse_expression();
+ expect(")");
+ left = parexpr;
+ }
+ else if(isdigit(token[0]) || token=="true" || token=="false")
+ {
+ RefPtr<Literal> literal = new Literal;
+ literal->token = parse_token();
+ left = literal;
+ }
+ else if(is_identifier(token))
+ {
+ RefPtr<VariableReference> var = new VariableReference;
+ var->name = expect_identifier();
+ left = var;
+ left_var = var.get();
+ }
+ else if(oper && oper->type==PREFIX)
+ {
+ RefPtr<UnaryExpression> unary = new UnaryExpression;
+ unary->oper = parse_token();
+ unary->prefix = true;
+ unary->expression = parse_expression(oper->precedence);
+ left = unary;
+ }
+ else
+ throw runtime_error(format_syntax_error("an expression"));
+ }
+ }
+}
+
+RefPtr<BinaryExpression> ProgramParser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
+{
+ RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
+ binary->left = left;
+ binary->oper = parse_token();
+ if(binary->oper=="[")
+ {
+ binary->right = parse_expression();
+ expect("]");
+ binary->after = "]";
+ }
+ else
+ binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT));
+ return binary;
+}
+
+RefPtr<FunctionCall> ProgramParser::parse_function_call(const VariableReference &var)
+{
+ RefPtr<FunctionCall> call = new FunctionCall;
+ call->name = var.name;
+ call->constructor = is_type(call->name);
+ expect("(");
+ while(peek_token()!=")")
+ {
+ if(!call->arguments.empty())
+ expect(",");
+ call->arguments.push_back(parse_expression());
+ }
+ expect(")");
+ return call;
+}
+
+RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
+{
+ expect("struct");
+ RefPtr<StructDeclaration> strct = new StructDeclaration;
+ strct->source = source_index;
+ strct->line = current_line;
+
+ strct->name = expect_identifier();
+ parse_block(strct->members, true);
+ expect(";");
+
+ declared_types.insert(strct->name);
+ return strct;
+}
+
+RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
+{
+ RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ var->source = source_index;
+ var->line = current_line;
+
+ string token = peek_token();
+ while(is_qualifier(token))
+ {
+ parse_token();
+ if(is_interface_qualifier(token))
+ var->interface = token;
+ else if(is_sampling_qualifier(token))
+ var->sampling = token;
+ else if(is_interpolation_qualifier(token))
+ var->interpolation = token;
+ else if(is_precision_qualifier(token))
+ var->precision = token;
+ else if(token=="const")
+ var->constant = true;
+ token = peek_token();
+ }
+
+ var->type = expect_type();
+ var->name = expect_identifier();
+
+ if(check("["))
+ {
+ var->array = true;
+ if(!check("]"))
+ {
+ var->array_size = parse_expression();
+ expect("]");
+ }
+ }
+
+ if(check("="))
+ var->init_expression = parse_expression();
+
+ expect(";");
+ return var;
+}
+
+RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
+{
+ RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
+ func->source = source_index;
+ func->line = current_line;
+
+ func->return_type = expect_type();
+ func->name = expect_identifier();
+ expect("(");
+ while(peek_token()!=")")
+ {
+ if(!func->parameters.empty())
+ expect(",");
+
+ RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ string token = peek_token();
+ if(token=="in" || token=="out" || token=="inout")
+ var->interface = parse_token();
+ var->type = expect_type();
+ var->name = expect_identifier();
+ func->parameters.push_back(var);
+ }
+ expect(")");
+
+ string token = peek_token();
+ if(token=="{")
+ {
+ func->definition = func.get();
+ parse_block(func->body, true);
+ }
+ else if(token==";")
+ parse_token();
+ else
+ throw runtime_error(format_syntax_error("'{' or ';'"));
+
+ return func;
+}
+
+RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
+{
+ RefPtr<InterfaceBlock> iface = new InterfaceBlock;
+ iface->source = source_index;
+ iface->line = current_line;
+
+ iface->interface = parse_token();
+ if(!is_interface_qualifier(iface->interface))
+ throw runtime_error(format_syntax_error("an interface qualifier"));
+
+ iface->name = expect_identifier();
+ parse_block(iface->members, true);
+ if(!check(";"))
+ {
+ iface->instance_name = expect_identifier();
+ if(check("["))
+ {
+ iface->array = true;
+ expect("]");
+ }
+ expect(";");
+ }
+
+ return iface;
+}
+
+RefPtr<Conditional> ProgramParser::parse_conditional()
+{
+ expect("if");
+ RefPtr<Conditional> cond = new Conditional;
+ cond->source = source_index;
+ cond->line = current_line;
+ expect("(");
+ cond->condition = parse_expression();
+ expect(")");
+
+ parse_block(cond->body, false);
+
+ string token = peek_token();
+ if(token=="else")
+ {
+ parse_token();
+ parse_block(cond->else_body, false);
+ }
+
+ return cond;
+}
+
+RefPtr<Iteration> ProgramParser::parse_for()
+{
+ expect("for");
+ RefPtr<Iteration> loop = new Iteration;
+ loop->source = source_index;
+ loop->line = current_line;
+ expect("(");
+ string token = peek_token();
+ if(is_type(token))
+ loop->init_statement = parse_statement();
+ else
+ {
+ if(token!=";")
+ {
+ RefPtr<ExpressionStatement> expr = new ExpressionStatement;
+ expr->expression = parse_expression();
+ loop->init_statement = expr;
+ }
+ expect(";");
+ }
+ if(peek_token()!=";")
+ loop->condition = parse_expression();
+ expect(";");
+ if(peek_token()!=")")
+ loop->loop_expression = parse_expression();
+ expect(")");
+
+ parse_block(loop->body, false);
+
+ return loop;
+}
+
+RefPtr<Iteration> ProgramParser::parse_while()
+{
+ expect("while");
+ RefPtr<Iteration> loop = new Iteration;
+ loop->source = source_index;
+ loop->line = current_line;
+ expect("(");
+ loop->condition = parse_expression();
+ expect(")");
+
+ parse_block(loop->body, false);
+
+ return loop;
+}
+
+RefPtr<Passthrough> ProgramParser::parse_passthrough()
+{
+ expect("passthrough");
+ RefPtr<Passthrough> pass = new Passthrough;
+ pass->source = source_index;
+ pass->line = current_line;
+ if(cur_stage->type==GEOMETRY)
+ {
+ expect("[");
+ pass->subscript = parse_expression();
+ expect("]");
+ }
+ expect(";");
+ return pass;
+}
+
+RefPtr<Return> ProgramParser::parse_return()
+{
+ expect("return");
+ RefPtr<Return> ret = new Return;
+ ret->source = source_index;
+ ret->line = current_line;
+ if(peek_token()!=";")
+ ret->expression = parse_expression();
+ expect(";");
+ return ret;
+}
+
+} // namespace GL
+} // namespace Msp