1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include "programparser.h"
11 using namespace ProgramSyntax;
13 ProgramParser::Operator ProgramParser::operators[] =
15 { "[", 2, BINARY, LEFT_TO_RIGHT },
16 { "(", 2, BINARY, LEFT_TO_RIGHT },
17 { ".", 2, BINARY, LEFT_TO_RIGHT },
18 { "++", 2, POSTFIX, LEFT_TO_RIGHT },
19 { "--", 2, POSTFIX, LEFT_TO_RIGHT },
20 { "++", 3, PREFIX, RIGHT_TO_LEFT },
21 { "--", 3, PREFIX, RIGHT_TO_LEFT },
22 { "+", 3, PREFIX, RIGHT_TO_LEFT },
23 { "-", 3, PREFIX, RIGHT_TO_LEFT },
24 { "~", 3, PREFIX, RIGHT_TO_LEFT },
25 { "!", 3, PREFIX, RIGHT_TO_LEFT },
26 { "*", 4, BINARY, LEFT_TO_RIGHT },
27 { "/", 4, BINARY, LEFT_TO_RIGHT },
28 { "%", 4, BINARY, LEFT_TO_RIGHT },
29 { "+", 5, BINARY, LEFT_TO_RIGHT },
30 { "-", 5, BINARY, LEFT_TO_RIGHT },
31 { "<<", 6, BINARY, LEFT_TO_RIGHT },
32 { ">>", 6, BINARY, LEFT_TO_RIGHT },
33 { "<", 7, BINARY, LEFT_TO_RIGHT },
34 { ">", 7, BINARY, LEFT_TO_RIGHT },
35 { "<=", 7, BINARY, LEFT_TO_RIGHT },
36 { ">=", 7, BINARY, LEFT_TO_RIGHT },
37 { "==", 8, BINARY, LEFT_TO_RIGHT },
38 { "!=", 8, BINARY, LEFT_TO_RIGHT },
39 { "&", 9, BINARY, LEFT_TO_RIGHT },
40 { "^", 10, BINARY, LEFT_TO_RIGHT },
41 { "|", 11, BINARY, LEFT_TO_RIGHT },
42 { "&&", 12, BINARY, LEFT_TO_RIGHT },
43 { "^^", 13, BINARY, LEFT_TO_RIGHT },
44 { "||", 14, BINARY, LEFT_TO_RIGHT },
45 { "?", 15, BINARY, RIGHT_TO_LEFT },
46 { ":", 15, BINARY, RIGHT_TO_LEFT },
47 { "=", 16, BINARY, RIGHT_TO_LEFT },
48 { "+=", 16, BINARY, RIGHT_TO_LEFT },
49 { "-=", 16, BINARY, RIGHT_TO_LEFT },
50 { "*=", 16, BINARY, RIGHT_TO_LEFT },
51 { "/=", 16, BINARY, RIGHT_TO_LEFT },
52 { "%=", 16, BINARY, RIGHT_TO_LEFT },
53 { "<<=", 16, BINARY, RIGHT_TO_LEFT },
54 { ">>=", 16, BINARY, RIGHT_TO_LEFT },
55 { "&=", 16, BINARY, RIGHT_TO_LEFT },
56 { "^=", 16, BINARY, RIGHT_TO_LEFT },
57 { "|=", 16, BINARY, RIGHT_TO_LEFT },
58 { ",", 17, BINARY, LEFT_TO_RIGHT },
59 { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
62 ProgramParser::ProgramParser():
66 ProgramParser::~ProgramParser()
71 Module &ProgramParser::parse(const string &s, const string &n)
79 Module &ProgramParser::parse(IO::Base &io, const string &n)
86 unsigned len = io.read(buffer, sizeof(buffer));
87 source.append(buffer, len);
93 void ProgramParser::parse_source()
97 string::size_type slashes = source.find("//////");
98 if(slashes==string::npos)
101 string::size_type newline = source.find('\n', slashes);
102 string pragma = format("#pragma MSP stage(%s)", source.substr(slashes+6, newline-slashes-6));
103 source.replace(slashes, newline-slashes, pragma);
108 cur_stage = &module->shared;
109 iter = source.begin();
110 source_end = source.end();
112 allow_preprocess = true;
113 while(RefPtr<Node> statement = parse_global_declaration())
114 cur_stage->content.body.push_back(statement);
117 string ProgramParser::format_error(const std::string &message)
119 string location = format("%s:%d: ", source_name, current_line);
120 return location+message;
123 string ProgramParser::format_syntax_error(const std::string &expected)
125 return format_error(format("Syntax error at '%s': expected %s", last_token, expected));
128 const string &ProgramParser::peek_token(unsigned index)
130 while(next_tokens.size()<=index)
131 next_tokens.push_back(parse_token_());
132 return (last_token = next_tokens[index]);
135 const string &ProgramParser::parse_token()
137 if(!next_tokens.empty())
139 last_token = next_tokens.front();
140 next_tokens.pop_front();
144 return (last_token = parse_token_());
147 string ProgramParser::parse_token_()
151 skip_comment_and_whitespace();
154 else if(allow_preprocess && *iter=='#')
156 allow_preprocess = false;
157 SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
160 else if(isalpha(*iter) || *iter=='_')
161 return parse_identifier();
162 else if(isdigit(*iter))
163 return parse_number();
165 return parse_other();
169 string ProgramParser::parse_identifier()
172 while(iter!=source_end)
174 if(isalnum(*iter) || *iter=='_')
183 string ProgramParser::parse_number()
185 bool accept_sign = false;
187 while(iter!=source_end)
189 if(isdigit(*iter) || *iter=='.')
191 else if(*iter=='e' || *iter=='E')
196 else if(accept_sign && (*iter=='+' || *iter=='-'))
205 string ProgramParser::parse_other()
210 string token(1, *iter++);
211 for(unsigned i=1; (i<3 && iter!=source_end); ++i)
213 bool matched = false;
214 for(const Operator *j=operators; (!matched && j->type); ++j)
216 matched = (j->token[i]==*iter);
217 for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
218 matched = (j->token[k]==token[k]);
230 void ProgramParser::skip_comment_and_whitespace()
232 unsigned comment = 0;
233 while(iter!=source_end)
239 else if(!isspace(*iter))
260 else if(comment==3 && *iter=='*')
273 allow_preprocess = (comment<3);
280 void ProgramParser::expect(const string &token)
282 string parsed = parse_token();
284 throw runtime_error(format_syntax_error(format("'%s'", token)));
287 string ProgramParser::expect_type()
289 string token = parse_token();
291 throw runtime_error(format_syntax_error("a type"));
295 string ProgramParser::expect_identifier()
297 string token = parse_token();
298 if(!is_identifier(token))
299 throw runtime_error(format_syntax_error("an identifier"));
303 bool ProgramParser::check(const string &token)
305 bool result = (peek_token()==token);
311 bool ProgramParser::is_interface_qualifier(const string &token)
313 return (token=="uniform" || token=="in" || token=="out");
316 bool ProgramParser::is_sampling_qualifier(const string &token)
318 return token=="centroid";
321 bool ProgramParser::is_precision_qualifier(const string &token)
323 return (token=="highp" || token=="mediump" || token=="lowp");
326 bool ProgramParser::is_qualifier(const string &token)
328 return (token=="const" || is_interface_qualifier(token) || is_sampling_qualifier(token) || is_precision_qualifier(token));
331 bool ProgramParser::is_builtin_type(const string &token)
333 static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D)(Array)?(Shadow)?|Cube(Shadow)?|3D))$");
334 return re.match(token);
337 bool ProgramParser::is_type(const string &token)
339 return is_builtin_type(token) || declared_types.count(token);
342 bool ProgramParser::is_identifier(const string &token)
344 static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
345 return re.match(token);
348 void ProgramParser::preprocess()
352 string::const_iterator line_end = iter;
353 for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
354 SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
356 string token = peek_token();
359 else if(token=="define" || token=="undef" || token=="if" || token=="ifdef" || token=="ifndef" || token=="else" ||
360 token=="elif" || token=="endif" || token=="error" || token=="extension" || token=="version" || token=="line")
361 throw runtime_error(format_error(format("Unsupported preprocessor directive '%s'", token)));
362 else if(!token.empty())
363 throw runtime_error(format_syntax_error("a preprocessor directive"));
368 void ProgramParser::preprocess_pragma()
371 string token = parse_token();
373 preprocess_pragma_msp();
376 void ProgramParser::preprocess_pragma_msp()
378 string token = peek_token();
382 throw runtime_error(format_error(format("Unrecognized MSP pragma '%s'", token)));
384 token = parse_token();
386 throw runtime_error(format_syntax_error("end of line"));
389 void ProgramParser::preprocess_stage()
391 if(!allow_stage_change)
392 throw runtime_error(format_error("Changing stage not allowed here"));
396 string token = expect_identifier();
397 StageType stage = SHARED;
400 else if(token=="geometry")
402 else if(token=="fragment")
405 throw runtime_error(format_syntax_error("stage identifier"));
408 if(stage<=cur_stage->type)
409 throw runtime_error(format_error(format("Stage '%s' not allowed here", token)));
411 module->stages.push_back(stage);
413 if(cur_stage->type!=SHARED)
414 module->stages.back().previous = cur_stage;
415 cur_stage = &module->stages.back();
418 RefPtr<Node> ProgramParser::parse_global_declaration()
420 allow_stage_change = true;
421 string token = peek_token();
422 allow_stage_change = false;
425 return parse_import();
426 else if(token=="precision")
427 return parse_precision();
428 else if(token=="layout")
430 RefPtr<Layout> layout = parse_layout();
431 token = peek_token();
432 if(is_interface_qualifier(token) && peek_token(1)==";")
434 RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
435 iface_lo->layout.qualifiers = layout->qualifiers;
436 iface_lo->interface = parse_token();
442 RefPtr<VariableDeclaration> var = parse_variable_declaration();
443 var->layout = layout;
447 else if(token=="struct")
448 return parse_struct_declaration();
449 else if(is_interface_qualifier(token))
451 string next = peek_token(1);
452 if(is_type(next) || is_precision_qualifier(next))
453 return parse_variable_declaration();
455 return parse_interface_block();
457 else if(is_qualifier(token))
458 return parse_variable_declaration();
459 else if(is_type(token))
461 if(peek_token(2)=="(")
462 return parse_function_declaration();
464 return parse_variable_declaration();
466 else if(token.empty())
469 throw runtime_error(format_syntax_error("a global declaration"));
472 RefPtr<Node> ProgramParser::parse_statement()
474 string token = peek_token();
476 return parse_conditional();
477 else if(token=="for")
478 return parse_iteration();
479 else if(token=="passthrough")
480 return parse_passthrough();
481 else if(token=="return")
482 return parse_return();
483 else if(token=="break" || token=="continue" || token=="discard")
485 RefPtr<Jump> jump = new Jump;
486 jump->keyword = parse_token();
491 else if(is_qualifier(token) || is_type(token))
492 return parse_variable_declaration();
493 else if(!token.empty())
495 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
496 expr->expression = parse_expression();
502 throw runtime_error(format_syntax_error("a statement"));
505 RefPtr<Import> ProgramParser::parse_import()
507 if(cur_stage->type!=SHARED)
508 throw runtime_error(format_error("Imports are only allowed in the shared section"));
511 RefPtr<Import> import = new Import;
512 import->module = parse_token();
517 RefPtr<Precision> ProgramParser::parse_precision()
520 RefPtr<Precision> precision = new Precision;
522 precision->precision = parse_token();
523 if(!is_precision_qualifier(precision->precision))
524 throw runtime_error(format_syntax_error("a precision qualifier"));
526 precision->type = parse_token();
527 // Not entirely accurate; only float, int and sampler types are allowed
528 if(!is_builtin_type(precision->type))
529 throw runtime_error(format_syntax_error("a builtin type"));
536 RefPtr<Layout> ProgramParser::parse_layout()
540 RefPtr<Layout> layout = new Layout;
543 string token = parse_token();
545 throw runtime_error(format_syntax_error("a layout qualifier name"));
547 layout->qualifiers.push_back(Layout::Qualifier());
548 Layout::Qualifier &qual = layout->qualifiers.back();
549 qual.identifier = token;
552 qual.value = parse_token();
554 if(peek_token()==")")
564 void ProgramParser::parse_block(Block &block, bool require_braces)
566 bool have_braces = (require_braces || peek_token()=="{");
572 while(peek_token()!="}")
573 block.body.push_back(parse_statement());
576 block.body.push_back(parse_statement());
578 block.use_braces = (require_braces || block.body.size()!=1);
584 RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
586 RefPtr<Expression> left;
587 VariableReference *left_var = 0;
590 string token = peek_token();
592 const Operator *oper = 0;
593 for(Operator *i=operators; (!oper && i->type); ++i)
594 if(token==i->token && (!left || i->type!=PREFIX) && (left || i->type!=POSTFIX))
597 if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
602 throw runtime_error(format_syntax_error("an expression"));
609 throw runtime_error(format_error("Syntax error before '(': function name must be an identifier"));
610 left = parse_function_call(*left_var);
614 RefPtr<MemberAccess> memacc = new MemberAccess;
617 memacc->member = expect_identifier();
620 else if(oper && oper->type==POSTFIX)
622 RefPtr<UnaryExpression> unary = new UnaryExpression;
623 unary->oper = parse_token();
624 unary->prefix = false;
625 unary->expression = left;
628 else if(oper && oper->type==BINARY)
629 left = parse_binary(left, oper);
631 throw runtime_error(format_syntax_error("an operator"));
639 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
640 parexpr->expression = parse_expression();
644 else if(isdigit(token[0]) || token=="true" || token=="false")
646 RefPtr<Literal> literal = new Literal;
647 literal->token = parse_token();
650 else if(is_identifier(token))
652 RefPtr<VariableReference> var = new VariableReference;
653 var->name = expect_identifier();
655 left_var = var.get();
657 else if(oper && oper->type==PREFIX)
659 RefPtr<UnaryExpression> unary = new UnaryExpression;
660 unary->oper = parse_token();
661 unary->prefix = true;
662 unary->expression = parse_expression(oper->precedence);
666 throw runtime_error(format_syntax_error("an expression"));
671 RefPtr<BinaryExpression> ProgramParser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
673 RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
675 binary->oper = parse_token();
676 if(binary->oper=="[")
678 binary->right = parse_expression();
683 binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT));
687 RefPtr<FunctionCall> ProgramParser::parse_function_call(const VariableReference &var)
689 RefPtr<FunctionCall> call = new FunctionCall;
690 call->name = var.name;
691 call->constructor = is_type(call->name);
693 while(peek_token()!=")")
695 if(!call->arguments.empty())
697 call->arguments.push_back(parse_expression());
703 RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
706 RefPtr<StructDeclaration> strct = new StructDeclaration;
708 strct->name = expect_identifier();
709 parse_block(strct->members, true);
712 declared_types.insert(strct->name);
716 RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
718 RefPtr<VariableDeclaration> var = new VariableDeclaration;
720 string token = peek_token();
721 if(is_sampling_qualifier(token))
723 var->sampling = parse_token();
724 token = peek_token();
725 if(!is_interface_qualifier(token))
726 throw runtime_error(format_syntax_error("an interface qualifier"));
729 if(is_interface_qualifier(token))
730 var->interface = parse_token();
731 else if(token=="const")
733 var->constant = true;
737 if(is_precision_qualifier(token))
738 var->precision = parse_token();
740 var->type = expect_type();
741 var->name = expect_identifier();
748 var->array_size = parse_expression();
754 var->init_expression = parse_expression();
760 RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
762 RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
764 func->return_type = expect_type();
765 func->name = expect_identifier();
767 while(peek_token()!=")")
769 if(!func->parameters.empty())
772 RefPtr<VariableDeclaration> var = new VariableDeclaration;
773 var->type = expect_type();
774 var->name = expect_identifier();
775 func->parameters.push_back(var);
779 string token = peek_token();
782 func->definition = func.get();
783 parse_block(func->body, true);
788 throw runtime_error(format_syntax_error("'{' or ';'"));
793 RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
795 RefPtr<InterfaceBlock> iface = new InterfaceBlock;
797 iface->interface = parse_token();
798 if(!is_interface_qualifier(iface->interface))
799 throw runtime_error(format_syntax_error("an interface qualifier"));
801 iface->name = expect_identifier();
802 parse_block(iface->members, true);
805 iface->instance_name = expect_identifier();
817 RefPtr<Conditional> ProgramParser::parse_conditional()
821 RefPtr<Conditional> cond = new Conditional;
822 cond->condition = parse_expression();
825 parse_block(cond->body, false);
827 string token = peek_token();
831 parse_block(cond->else_body, false);
837 RefPtr<Iteration> ProgramParser::parse_iteration()
841 RefPtr<Iteration> loop = new Iteration;
842 string token = peek_token();
844 loop->init_statement = parse_statement();
847 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
848 expr->expression = parse_expression();
850 loop->init_statement = expr;
852 loop->condition = parse_expression();
854 loop->loop_expression = parse_expression();
857 parse_block(loop->body, false);
862 RefPtr<Passthrough> ProgramParser::parse_passthrough()
864 expect("passthrough");
865 RefPtr<Passthrough> pass = new Passthrough;
866 if(cur_stage->type==GEOMETRY)
869 pass->subscript = parse_expression();
876 RefPtr<Return> ProgramParser::parse_return()
879 RefPtr<Return> ret = new Return;
880 if(peek_token()!=";")
881 ret->expression = parse_expression();