1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include "programparser.h"
13 using namespace ProgramSyntax;
15 ProgramParser::Operator ProgramParser::operators[] =
17 { "[", 2, BINARY, LEFT_TO_RIGHT },
18 { "(", 2, BINARY, LEFT_TO_RIGHT },
19 { ".", 2, BINARY, LEFT_TO_RIGHT },
20 { "++", 2, POSTFIX, LEFT_TO_RIGHT },
21 { "--", 2, POSTFIX, LEFT_TO_RIGHT },
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 { "~", 3, PREFIX, RIGHT_TO_LEFT },
27 { "!", 3, PREFIX, RIGHT_TO_LEFT },
28 { "*", 4, BINARY, LEFT_TO_RIGHT },
29 { "/", 4, BINARY, LEFT_TO_RIGHT },
30 { "%", 4, BINARY, LEFT_TO_RIGHT },
31 { "+", 5, BINARY, LEFT_TO_RIGHT },
32 { "-", 5, BINARY, LEFT_TO_RIGHT },
33 { "<<", 6, BINARY, LEFT_TO_RIGHT },
34 { ">>", 6, BINARY, LEFT_TO_RIGHT },
35 { "<", 7, BINARY, LEFT_TO_RIGHT },
36 { ">", 7, BINARY, LEFT_TO_RIGHT },
37 { "<=", 7, BINARY, LEFT_TO_RIGHT },
38 { ">=", 7, BINARY, LEFT_TO_RIGHT },
39 { "==", 8, BINARY, LEFT_TO_RIGHT },
40 { "!=", 8, BINARY, LEFT_TO_RIGHT },
41 { "&", 9, BINARY, LEFT_TO_RIGHT },
42 { "^", 10, BINARY, LEFT_TO_RIGHT },
43 { "|", 11, BINARY, LEFT_TO_RIGHT },
44 { "&&", 12, BINARY, LEFT_TO_RIGHT },
45 { "^^", 13, BINARY, LEFT_TO_RIGHT },
46 { "||", 14, BINARY, LEFT_TO_RIGHT },
47 { "?", 15, BINARY, RIGHT_TO_LEFT },
48 { ":", 15, 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 { "^=", 16, BINARY, RIGHT_TO_LEFT },
59 { "|=", 16, BINARY, RIGHT_TO_LEFT },
60 { ",", 17, BINARY, LEFT_TO_RIGHT },
61 { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
64 ProgramParser::ProgramParser():
68 ProgramParser::~ProgramParser()
73 Module &ProgramParser::parse(const string &s, const string &n, unsigned i)
82 Module &ProgramParser::parse(IO::Base &io, const string &n, unsigned i)
90 unsigned len = io.read(buffer, sizeof(buffer));
91 source.append(buffer, len);
97 void ProgramParser::parse_source()
101 string::size_type slashes = source.find("//////");
102 if(slashes==string::npos)
105 string::size_type newline = source.find('\n', slashes);
106 string pragma = format("#pragma MSP stage(%s)", source.substr(slashes+6, newline-slashes-6));
107 source.replace(slashes, newline-slashes, pragma);
112 cur_stage = &module->shared;
113 iter = source.begin();
114 source_end = source.end();
116 allow_preprocess = true;
117 while(RefPtr<Statement> statement = parse_global_declaration())
118 cur_stage->content.body.push_back(statement);
121 string ProgramParser::format_error(const std::string &message)
123 string location = format("%s:%d: ", source_name, current_line);
124 return location+message;
127 string ProgramParser::format_syntax_error(const std::string &expected)
129 return format_error(format("Syntax error at '%s': expected %s", last_token, expected));
132 const string &ProgramParser::peek_token(unsigned index)
134 while(next_tokens.size()<=index)
135 next_tokens.push_back(parse_token_());
136 return (last_token = next_tokens[index]);
139 const string &ProgramParser::parse_token()
141 if(!next_tokens.empty())
143 last_token = next_tokens.front();
144 next_tokens.pop_front();
148 return (last_token = parse_token_());
151 string ProgramParser::parse_token_()
155 skip_comment_and_whitespace();
158 else if(allow_preprocess && *iter=='#')
160 allow_preprocess = false;
161 SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
164 else if(isalpha(*iter) || *iter=='_')
165 return parse_identifier();
166 else if(isdigit(*iter))
167 return parse_number();
169 return parse_other();
173 string ProgramParser::parse_identifier()
176 while(iter!=source_end)
178 if(isalnum(*iter) || *iter=='_')
187 string ProgramParser::parse_number()
189 bool accept_sign = false;
191 while(iter!=source_end)
193 if(isdigit(*iter) || *iter=='.')
195 else if(*iter=='e' || *iter=='E')
200 else if(accept_sign && (*iter=='+' || *iter=='-'))
209 string ProgramParser::parse_other()
214 string token(1, *iter++);
215 for(unsigned i=1; (i<3 && iter!=source_end); ++i)
217 bool matched = false;
218 for(const Operator *j=operators; (!matched && j->type); ++j)
220 matched = (j->token[i]==*iter);
221 for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
222 matched = (j->token[k]==token[k]);
234 void ProgramParser::skip_comment_and_whitespace()
236 unsigned comment = 0;
237 while(iter!=source_end)
243 else if(!isspace(*iter))
264 else if(comment==3 && *iter=='*')
277 allow_preprocess = (comment<3);
284 void ProgramParser::expect(const string &token)
286 string parsed = parse_token();
288 throw runtime_error(format_syntax_error(format("'%s'", token)));
291 string ProgramParser::expect_type()
293 string token = parse_token();
295 throw runtime_error(format_syntax_error("a type"));
299 string ProgramParser::expect_identifier()
301 string token = parse_token();
302 if(!is_identifier(token))
303 throw runtime_error(format_syntax_error("an identifier"));
307 bool ProgramParser::check(const string &token)
309 bool result = (peek_token()==token);
315 bool ProgramParser::is_interface_qualifier(const string &token)
317 return (token=="uniform" || token=="in" || token=="out");
320 bool ProgramParser::is_sampling_qualifier(const string &token)
322 return (token=="centroid" || token=="sample");
325 bool ProgramParser::is_interpolation_qualifier(const string &token)
327 return (token=="smooth" || token=="flat" || token=="noperspective");
330 bool ProgramParser::is_precision_qualifier(const string &token)
332 return (token=="highp" || token=="mediump" || token=="lowp");
335 bool ProgramParser::is_qualifier(const string &token)
337 return (token=="const" ||
338 is_interface_qualifier(token) ||
339 is_sampling_qualifier(token) ||
340 is_interpolation_qualifier(token) ||
341 is_precision_qualifier(token));
344 bool ProgramParser::is_builtin_type(const string &token)
346 static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
347 return re.match(token);
350 bool ProgramParser::is_type(const string &token)
352 return is_builtin_type(token) || declared_types.count(token);
355 bool ProgramParser::is_identifier(const string &token)
357 static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
358 return re.match(token);
361 void ProgramParser::preprocess()
365 string::const_iterator line_end = iter;
366 for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
367 SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
369 string token = peek_token();
372 else if(token=="version")
373 preprocess_version();
374 else if(token=="define" || token=="undef" || token=="if" || token=="ifdef" || token=="ifndef" || token=="else" ||
375 token=="elif" || token=="endif" || token=="error" || token=="extension" || token=="line")
376 throw runtime_error(format_error(format("Unsupported preprocessor directive '%s'", token)));
377 else if(!token.empty())
378 throw runtime_error(format_syntax_error("a preprocessor directive"));
383 void ProgramParser::preprocess_version()
386 string token = parse_token();
387 unsigned version = lexical_cast<unsigned>(token);
388 cur_stage->required_version = Version(version/100, version%100);
390 token = parse_token();
392 throw runtime_error(format_syntax_error("end of line"));
395 void ProgramParser::preprocess_pragma()
398 string token = parse_token();
400 preprocess_pragma_msp();
403 void ProgramParser::preprocess_pragma_msp()
405 string token = peek_token();
409 throw runtime_error(format_error(format("Unrecognized MSP pragma '%s'", token)));
411 token = parse_token();
413 throw runtime_error(format_syntax_error("end of line"));
416 void ProgramParser::preprocess_stage()
418 if(!allow_stage_change)
419 throw runtime_error(format_error("Changing stage not allowed here"));
423 string token = expect_identifier();
424 StageType stage = SHARED;
427 else if(token=="geometry")
429 else if(token=="fragment")
432 throw runtime_error(format_syntax_error("stage identifier"));
435 if(stage<=cur_stage->type)
436 throw runtime_error(format_error(format("Stage '%s' not allowed here", token)));
438 module->stages.push_back(stage);
440 if(cur_stage->type!=SHARED)
441 module->stages.back().previous = cur_stage;
442 cur_stage = &module->stages.back();
445 RefPtr<Statement> ProgramParser::parse_global_declaration()
447 allow_stage_change = true;
448 string token = peek_token();
449 allow_stage_change = false;
452 return parse_import();
453 else if(token=="precision")
454 return parse_precision();
455 else if(token=="layout")
457 RefPtr<Layout> layout = parse_layout();
458 token = peek_token();
459 if(is_interface_qualifier(token) && peek_token(1)==";")
461 RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
462 iface_lo->source = source_index;
463 iface_lo->line = current_line;
464 iface_lo->layout.qualifiers = layout->qualifiers;
465 iface_lo->interface = parse_token();
471 RefPtr<VariableDeclaration> var = parse_variable_declaration();
472 var->layout = layout;
476 else if(token=="struct")
477 return parse_struct_declaration();
478 else if(is_interface_qualifier(token))
480 string next = peek_token(1);
481 if(is_type(next) || is_qualifier(next))
482 return parse_variable_declaration();
484 return parse_interface_block();
486 else if(is_qualifier(token))
487 return parse_variable_declaration();
488 else if(is_type(token))
490 if(peek_token(2)=="(")
491 return parse_function_declaration();
493 return parse_variable_declaration();
495 else if(token.empty())
498 throw runtime_error(format_syntax_error("a global declaration"));
501 RefPtr<Statement> ProgramParser::parse_statement()
503 string token = peek_token();
505 return parse_conditional();
506 else if(token=="for")
508 else if(token=="while")
509 return parse_while();
510 else if(token=="passthrough")
511 return parse_passthrough();
512 else if(token=="return")
513 return parse_return();
514 else if(token=="break" || token=="continue" || token=="discard")
516 RefPtr<Jump> jump = new Jump;
517 jump->source = source_index;
518 jump->line = current_line;
519 jump->keyword = parse_token();
524 else if(is_qualifier(token) || is_type(token))
525 return parse_variable_declaration();
526 else if(!token.empty())
528 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
529 expr->source = source_index;
530 expr->line = current_line;
531 expr->expression = parse_expression();
537 throw runtime_error(format_syntax_error("a statement"));
540 RefPtr<Import> ProgramParser::parse_import()
542 if(cur_stage->type!=SHARED)
543 throw runtime_error(format_error("Imports are only allowed in the shared section"));
546 RefPtr<Import> import = new Import;
547 import->source = source_index;
548 import->line = current_line;
549 import->module = expect_identifier();
554 RefPtr<Precision> ProgramParser::parse_precision()
557 RefPtr<Precision> precision = new Precision;
558 precision->source = source_index;
559 precision->line = current_line;
561 precision->precision = parse_token();
562 if(!is_precision_qualifier(precision->precision))
563 throw runtime_error(format_syntax_error("a precision qualifier"));
565 precision->type = parse_token();
566 // Not entirely accurate; only float, int and sampler types are allowed
567 if(!is_builtin_type(precision->type))
568 throw runtime_error(format_syntax_error("a builtin type"));
575 RefPtr<Layout> ProgramParser::parse_layout()
579 RefPtr<Layout> layout = new Layout;
582 string token = parse_token();
584 throw runtime_error(format_syntax_error("a layout qualifier name"));
586 layout->qualifiers.push_back(Layout::Qualifier());
587 Layout::Qualifier &qual = layout->qualifiers.back();
588 qual.identifier = token;
591 qual.value = parse_token();
593 if(peek_token()==")")
603 void ProgramParser::parse_block(Block &block, bool require_braces)
605 bool have_braces = (require_braces || peek_token()=="{");
611 while(peek_token()!="}")
612 block.body.push_back(parse_statement());
615 block.body.push_back(parse_statement());
617 block.use_braces = (require_braces || block.body.size()!=1);
623 RefPtr<Expression> ProgramParser::parse_expression(unsigned precedence)
625 RefPtr<Expression> left;
626 VariableReference *left_var = 0;
629 string token = peek_token();
631 const Operator *oper = 0;
632 for(Operator *i=operators; (!oper && i->type); ++i)
633 if(token==i->token && (!left || i->type!=PREFIX) && (left || i->type!=POSTFIX))
636 if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
641 throw runtime_error(format_syntax_error("an expression"));
648 throw runtime_error(format_error("Syntax error before '(': function name must be an identifier"));
649 left = parse_function_call(*left_var);
653 RefPtr<MemberAccess> memacc = new MemberAccess;
656 memacc->member = expect_identifier();
659 else if(oper && oper->type==POSTFIX)
661 RefPtr<UnaryExpression> unary = new UnaryExpression;
662 unary->oper = parse_token();
663 unary->prefix = false;
664 unary->expression = left;
667 else if(oper && oper->type==BINARY)
668 left = parse_binary(left, oper);
670 throw runtime_error(format_syntax_error("an operator"));
678 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
679 parexpr->expression = parse_expression();
683 else if(isdigit(token[0]) || token=="true" || token=="false")
685 RefPtr<Literal> literal = new Literal;
686 literal->token = parse_token();
689 else if(is_identifier(token))
691 RefPtr<VariableReference> var = new VariableReference;
692 var->name = expect_identifier();
694 left_var = var.get();
696 else if(oper && oper->type==PREFIX)
698 RefPtr<UnaryExpression> unary = new UnaryExpression;
699 unary->oper = parse_token();
700 unary->prefix = true;
701 unary->expression = parse_expression(oper->precedence);
705 throw runtime_error(format_syntax_error("an expression"));
710 RefPtr<BinaryExpression> ProgramParser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
712 RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
714 binary->oper = parse_token();
715 if(binary->oper=="[")
717 binary->right = parse_expression();
722 binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT));
726 RefPtr<FunctionCall> ProgramParser::parse_function_call(const VariableReference &var)
728 RefPtr<FunctionCall> call = new FunctionCall;
729 call->name = var.name;
730 call->constructor = is_type(call->name);
732 while(peek_token()!=")")
734 if(!call->arguments.empty())
736 call->arguments.push_back(parse_expression());
742 RefPtr<StructDeclaration> ProgramParser::parse_struct_declaration()
745 RefPtr<StructDeclaration> strct = new StructDeclaration;
746 strct->source = source_index;
747 strct->line = current_line;
749 strct->name = expect_identifier();
750 parse_block(strct->members, true);
753 declared_types.insert(strct->name);
757 RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
759 RefPtr<VariableDeclaration> var = new VariableDeclaration;
760 var->source = source_index;
761 var->line = current_line;
763 string token = peek_token();
764 while(is_qualifier(token))
767 if(is_interface_qualifier(token))
768 var->interface = token;
769 else if(is_sampling_qualifier(token))
770 var->sampling = token;
771 else if(is_interpolation_qualifier(token))
772 var->interpolation = token;
773 else if(is_precision_qualifier(token))
774 var->precision = token;
775 else if(token=="const")
776 var->constant = true;
777 token = peek_token();
780 var->type = expect_type();
781 var->name = expect_identifier();
788 var->array_size = parse_expression();
794 var->init_expression = parse_expression();
800 RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
802 RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
803 func->source = source_index;
804 func->line = current_line;
806 func->return_type = expect_type();
807 func->name = expect_identifier();
809 while(peek_token()!=")")
811 if(!func->parameters.empty())
814 RefPtr<VariableDeclaration> var = new VariableDeclaration;
815 string token = peek_token();
816 if(token=="in" || token=="out" || token=="inout")
817 var->interface = parse_token();
818 var->type = expect_type();
819 var->name = expect_identifier();
820 func->parameters.push_back(var);
824 string token = peek_token();
827 func->definition = func.get();
828 parse_block(func->body, true);
833 throw runtime_error(format_syntax_error("'{' or ';'"));
838 RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
840 RefPtr<InterfaceBlock> iface = new InterfaceBlock;
841 iface->source = source_index;
842 iface->line = current_line;
844 iface->interface = parse_token();
845 if(!is_interface_qualifier(iface->interface))
846 throw runtime_error(format_syntax_error("an interface qualifier"));
848 iface->name = expect_identifier();
849 parse_block(iface->members, true);
852 iface->instance_name = expect_identifier();
864 RefPtr<Conditional> ProgramParser::parse_conditional()
867 RefPtr<Conditional> cond = new Conditional;
868 cond->source = source_index;
869 cond->line = current_line;
871 cond->condition = parse_expression();
874 parse_block(cond->body, false);
876 string token = peek_token();
880 parse_block(cond->else_body, false);
886 RefPtr<Iteration> ProgramParser::parse_for()
889 RefPtr<Iteration> loop = new Iteration;
890 loop->source = source_index;
891 loop->line = current_line;
893 string token = peek_token();
895 loop->init_statement = parse_statement();
900 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
901 expr->expression = parse_expression();
902 loop->init_statement = expr;
906 if(peek_token()!=";")
907 loop->condition = parse_expression();
909 if(peek_token()!=")")
910 loop->loop_expression = parse_expression();
913 parse_block(loop->body, false);
918 RefPtr<Iteration> ProgramParser::parse_while()
921 RefPtr<Iteration> loop = new Iteration;
922 loop->source = source_index;
923 loop->line = current_line;
925 loop->condition = parse_expression();
928 parse_block(loop->body, false);
933 RefPtr<Passthrough> ProgramParser::parse_passthrough()
935 expect("passthrough");
936 RefPtr<Passthrough> pass = new Passthrough;
937 pass->source = source_index;
938 pass->line = current_line;
939 if(cur_stage->type==GEOMETRY)
942 pass->subscript = parse_expression();
949 RefPtr<Return> ProgramParser::parse_return()
952 RefPtr<Return> ret = new Return;
953 ret->source = source_index;
954 ret->line = current_line;
955 if(peek_token()!=";")
956 ret->expression = parse_expression();