1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include <msp/strings/utils.h>
5 #include "glsl_error.h"
17 preprocessor(tokenizer),
20 tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
21 preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
22 preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
23 preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
24 preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
32 Module &Parser::parse(const string &s, const string &n, int i)
39 Module &Parser::parse(IO::Base &io, const string &n, int i)
45 unsigned len = io.read(buffer, sizeof(buffer));
46 source.append(buffer, len);
52 void Parser::parse_source(const string &name, int index)
56 cur_stage = &module->shared;
60 source_reference(1, name);
61 tokenizer.begin(name, source);
62 allow_stage_change = true;
63 while(!tokenizer.peek_token().empty())
64 if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
65 cur_stage->content.body.push_back(statement);
68 throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
71 void Parser::set_required_version(const Version &ver)
73 cur_stage->required_features.glsl_version = ver;
76 void Parser::source_reference(unsigned index, const string &name)
78 if(index<1 || base_index<0)
79 throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
81 module->source_map.set_name(base_index+index-1, name);
84 void Parser::stage_change(Stage::Type stage)
86 if(!allow_stage_change)
87 throw invalid_shader_source(tokenizer.get_location(), "Changing stage not allowed here");
88 else if(stage<=cur_stage->type)
89 throw invalid_shader_source(tokenizer.get_location(), "Stage '%s' not allowed here", Stage::get_stage_name(stage));
91 module->stages.push_back(stage);
93 if(cur_stage->type!=Stage::SHARED)
94 module->stages.back().previous = cur_stage;
95 cur_stage = &module->stages.back();
98 void Parser::line_change(int index, unsigned line)
101 source_index = base_index+index-1;
105 index = source_index;
107 string name = module->source_map.get_name(index);
109 name = format("<%d>", index);
110 tokenizer.set_location(Location(name, line));
113 string Parser::expect_type()
115 string token = tokenizer.parse_token();
117 throw parse_error(tokenizer.get_location(), token, "a type");
121 string Parser::expect_identifier()
123 string token = tokenizer.parse_token();
124 if(!is_identifier(token))
125 throw parse_error(tokenizer.get_location(), token, "an identifier");
129 int Parser::expect_integer()
131 string token = tokenizer.parse_token();
133 throw parse_error(tokenizer.get_location(), token, "an integer literal");
134 return lexical_cast<int>(token);
137 bool Parser::check(const string &token)
139 bool result = (tokenizer.peek_token()==token);
141 tokenizer.parse_token();
145 bool Parser::is_interface_qualifier(const string &token)
147 return (token=="uniform" || token=="in" || token=="out");
150 bool Parser::is_sampling_qualifier(const string &token)
152 return (token=="centroid" || token=="sample");
155 bool Parser::is_interpolation_qualifier(const string &token)
157 return (token=="smooth" || token=="flat" || token=="noperspective");
160 bool Parser::is_precision_qualifier(const string &token)
162 return (token=="highp" || token=="mediump" || token=="lowp");
165 bool Parser::is_qualifier(const string &token)
167 return (token=="const" ||
168 is_interface_qualifier(token) ||
169 is_sampling_qualifier(token) ||
170 is_interpolation_qualifier(token) ||
171 is_precision_qualifier(token));
174 bool Parser::is_builtin_type(const string &token)
176 static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
177 return re.match(token);
180 bool Parser::is_type(const string &token)
182 return is_builtin_type(token) || declared_types.count(token);
185 bool Parser::is_identifier(const string &token)
187 static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
188 return re.match(token);
192 RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
194 tokenizer.clear_progress_mark();
197 return (this->*parse_func)();
199 catch(const invalid_shader_source &exc)
201 errors.push_back(exc.what());
204 if(tokenizer.get_last_token()!=";" || !tokenizer.get_progress_mark())
206 unsigned scope_level = 0;
209 if(tokenizer.peek_token()=="}" && scope_level==0)
211 if(!tokenizer.get_progress_mark())
212 tokenizer.parse_token();
216 string token = tokenizer.parse_token();
225 else if(token==";" && scope_level==0)
227 else if(token.empty())
235 RefPtr<Statement> Parser::parse_global_declaration()
237 string token = tokenizer.peek_token();
238 SetFlag disallow(allow_stage_change, false);
241 return parse_import();
242 else if(token=="precision")
243 return parse_precision();
244 else if(token=="layout")
246 RefPtr<Layout> layout = parse_layout();
247 token = tokenizer.peek_token();
248 if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
250 RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
251 iface_lo->source = source_index;
252 iface_lo->line = tokenizer.get_location().line;
253 iface_lo->layout.qualifiers = layout->qualifiers;
254 iface_lo->interface = tokenizer.parse_token();
255 tokenizer.expect(";");
260 RefPtr<VariableDeclaration> var = parse_variable_declaration();
261 var->layout = layout;
265 else if(token=="struct")
266 return parse_struct_declaration();
267 else if(is_interface_qualifier(token))
269 string next = tokenizer.peek_token(1);
270 if(is_type(next) || is_qualifier(next))
271 return parse_variable_declaration();
273 return parse_interface_block();
275 else if(is_qualifier(token))
276 return parse_variable_declaration();
277 else if(is_type(token))
279 if(tokenizer.peek_token(2)=="(")
280 return parse_function_declaration();
282 return parse_variable_declaration();
284 else if(token.empty())
287 throw parse_error(tokenizer.get_location(), token, "a global declaration");
290 RefPtr<Statement> Parser::parse_statement()
292 string token = tokenizer.peek_token();
294 return parse_conditional();
295 else if(token=="for")
297 else if(token=="while")
298 return parse_while();
299 else if(token=="passthrough")
300 return parse_passthrough();
301 else if(token=="return")
302 return parse_return();
303 else if(token=="break" || token=="continue" || token=="discard")
305 RefPtr<Jump> jump = new Jump;
306 jump->source = source_index;
307 jump->line = tokenizer.get_location().line;
308 jump->keyword = tokenizer.parse_token();
309 tokenizer.expect(";");
313 else if(is_qualifier(token) || is_type(token))
314 return parse_variable_declaration();
317 tokenizer.parse_token();
318 throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
320 else if(!token.empty())
322 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
323 expr->source = source_index;
324 expr->line = tokenizer.get_location().line;
325 expr->expression = parse_expression();
326 tokenizer.expect(";");
331 throw parse_error(tokenizer.get_location(), token, "a statement");
334 RefPtr<Import> Parser::parse_import()
336 if(cur_stage->type!=Stage::SHARED)
337 throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
339 tokenizer.expect("import");
340 RefPtr<Import> import = new Import;
341 import->source = source_index;
342 import->line = tokenizer.get_location().line;
343 import->module = expect_identifier();
344 tokenizer.expect(";");
348 RefPtr<Precision> Parser::parse_precision()
350 tokenizer.expect("precision");
351 RefPtr<Precision> precision = new Precision;
352 precision->source = source_index;
353 precision->line = tokenizer.get_location().line;
355 precision->precision = tokenizer.parse_token();
356 if(!is_precision_qualifier(precision->precision))
357 throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
359 precision->type = tokenizer.parse_token();
360 // Not entirely accurate; only float, int and sampler types are allowed
361 if(!is_builtin_type(precision->type))
362 throw parse_error(tokenizer.get_location(), precision->type, "a builtin type");
364 tokenizer.expect(";");
369 RefPtr<Layout> Parser::parse_layout()
371 tokenizer.expect("layout");
372 tokenizer.expect("(");
373 RefPtr<Layout> layout = new Layout;
376 string token = tokenizer.parse_token();
378 throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
380 layout->qualifiers.push_back(Layout::Qualifier());
381 Layout::Qualifier &qual = layout->qualifiers.back();
384 if((qual.has_value = check("=")))
386 if(qual.name=="constant_id" && tokenizer.peek_token()=="auto")
389 tokenizer.parse_token();
392 qual.value = expect_integer();
395 if(tokenizer.peek_token()==")")
398 tokenizer.expect(",");
400 tokenizer.expect(")");
406 void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
408 bool have_braces = (require_braces || tokenizer.peek_token()=="{");
410 tokenizer.expect("{");
414 while(tokenizer.peek_token()!="}")
415 if(RefPtr<Statement> node = parse_with_recovery(parse_content))
416 block.body.push_back(node);
419 block.body.push_back((this->*parse_content)());
421 block.use_braces = (require_braces || block.body.size()!=1);
424 tokenizer.expect("}");
427 RefPtr<Expression> Parser::parse_expression(unsigned precedence)
429 RefPtr<Expression> left;
430 VariableReference *left_var = 0;
433 string token = tokenizer.peek_token();
435 const Operator *oper = 0;
436 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
437 if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
440 if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
445 throw parse_error(tokenizer.get_location(), token, "an expression");
452 throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
453 left = parse_function_call(*left_var);
457 RefPtr<MemberAccess> memacc = new MemberAccess;
460 tokenizer.parse_token();
461 memacc->member = expect_identifier();
464 else if(oper && oper->type==Operator::POSTFIX)
466 RefPtr<UnaryExpression> unary = new UnaryExpression;
468 tokenizer.parse_token();
469 unary->expression = left;
472 else if(oper && oper->type==Operator::BINARY)
473 left = parse_binary(left, *oper);
475 throw parse_error(tokenizer.get_location(), token, "an operator");
482 tokenizer.parse_token();
483 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
484 parexpr->expression = parse_expression();
485 tokenizer.expect(")");
488 else if(isdigit(token[0]) || token=="true" || token=="false")
490 RefPtr<Literal> literal = new Literal;
491 literal->token = tokenizer.parse_token();
494 else if(is_identifier(token))
496 RefPtr<VariableReference> var = new VariableReference;
497 var->name = expect_identifier();
499 left_var = var.get();
501 else if(oper && oper->type==Operator::PREFIX)
503 RefPtr<UnaryExpression> unary = new UnaryExpression;
505 tokenizer.parse_token();
506 unary->expression = parse_expression(oper->precedence);
510 throw parse_error(tokenizer.get_location(), token, "an expression");
515 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
517 RefPtr<BinaryExpression> binary = (oper.precedence==16 ? new Assignment : new BinaryExpression);
519 binary->oper = &oper;
520 tokenizer.expect(oper.token);
521 if(oper.token[0]=='[')
523 binary->right = parse_expression();
524 tokenizer.expect("]");
527 binary->right = parse_expression(oper.precedence+(oper.assoc==Operator::RIGHT_TO_LEFT));
531 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
533 RefPtr<FunctionCall> call = new FunctionCall;
534 call->name = var.name;
535 call->constructor = is_type(call->name);
536 call->oper = &Operator::get_operator("(", Operator::POSTFIX);
537 tokenizer.expect("(");
538 while(tokenizer.peek_token()!=")")
540 if(!call->arguments.empty())
541 tokenizer.expect(",");
542 call->arguments.push_back(parse_expression());
544 tokenizer.expect(")");
548 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
550 tokenizer.expect("struct");
551 RefPtr<StructDeclaration> strct = new StructDeclaration;
552 strct->source = source_index;
553 strct->line = tokenizer.get_location().line;
555 strct->name = expect_identifier();
556 parse_block(strct->members, true, &Parser::parse_variable_declaration);
557 tokenizer.expect(";");
559 declared_types.insert(strct->name);
563 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
565 RefPtr<VariableDeclaration> var = new VariableDeclaration;
566 var->source = source_index;
567 var->line = tokenizer.get_location().line;
569 string token = tokenizer.peek_token();
570 while(is_qualifier(token))
572 tokenizer.parse_token();
573 if(is_interface_qualifier(token))
574 var->interface = token;
575 else if(is_sampling_qualifier(token))
576 var->sampling = token;
577 else if(is_interpolation_qualifier(token))
578 var->interpolation = token;
579 else if(is_precision_qualifier(token))
580 var->precision = token;
581 else if(token=="const")
582 var->constant = true;
583 token = tokenizer.peek_token();
586 var->type = expect_type();
587 var->name = expect_identifier();
594 var->array_size = parse_expression();
595 tokenizer.expect("]");
600 var->init_expression = parse_expression();
602 tokenizer.expect(";");
606 RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
608 RefPtr<Layout> layout;
609 if(tokenizer.peek_token()=="layout")
610 layout = parse_layout();
612 RefPtr<VariableDeclaration> var = parse_variable_declaration();
613 var->layout = layout;
618 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
620 RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
621 func->source = source_index;
622 func->line = tokenizer.get_location().line;
624 func->return_type = expect_type();
625 func->name = expect_identifier();
626 tokenizer.expect("(");
627 while(tokenizer.peek_token()!=")")
629 if(!func->parameters.empty())
630 tokenizer.expect(",");
632 RefPtr<VariableDeclaration> var = new VariableDeclaration;
633 string token = tokenizer.peek_token();
634 if(token=="in" || token=="out" || token=="inout")
635 var->interface = tokenizer.parse_token();
636 var->type = expect_type();
637 var->name = expect_identifier();
638 func->parameters.push_back(var);
640 tokenizer.expect(")");
642 string token = tokenizer.peek_token();
645 func->definition = func.get();
646 parse_block(func->body, true, &Parser::parse_statement);
649 tokenizer.parse_token();
651 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
656 RefPtr<InterfaceBlock> Parser::parse_interface_block()
658 RefPtr<InterfaceBlock> iface = new InterfaceBlock;
659 iface->source = source_index;
660 iface->line = tokenizer.get_location().line;
662 iface->interface = tokenizer.parse_token();
663 if(!is_interface_qualifier(iface->interface))
664 throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
666 iface->name = expect_identifier();
667 parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
670 iface->instance_name = expect_identifier();
674 tokenizer.expect("]");
676 tokenizer.expect(";");
682 RefPtr<Conditional> Parser::parse_conditional()
684 tokenizer.expect("if");
685 RefPtr<Conditional> cond = new Conditional;
686 cond->source = source_index;
687 cond->line = tokenizer.get_location().line;
688 tokenizer.expect("(");
689 cond->condition = parse_expression();
690 tokenizer.expect(")");
692 parse_block(cond->body, false, &Parser::parse_statement);
694 string token = tokenizer.peek_token();
697 tokenizer.parse_token();
698 parse_block(cond->else_body, false, &Parser::parse_statement);
704 RefPtr<Iteration> Parser::parse_for()
706 tokenizer.expect("for");
707 RefPtr<Iteration> loop = new Iteration;
708 loop->source = source_index;
709 loop->line = tokenizer.get_location().line;
710 tokenizer.expect("(");
711 string token = tokenizer.peek_token();
713 loop->init_statement = parse_statement();
718 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
719 expr->expression = parse_expression();
720 loop->init_statement = expr;
722 tokenizer.expect(";");
724 if(tokenizer.peek_token()!=";")
725 loop->condition = parse_expression();
726 tokenizer.expect(";");
727 if(tokenizer.peek_token()!=")")
728 loop->loop_expression = parse_expression();
729 tokenizer.expect(")");
731 parse_block(loop->body, false, &Parser::parse_statement);
736 RefPtr<Iteration> Parser::parse_while()
738 tokenizer.expect("while");
739 RefPtr<Iteration> loop = new Iteration;
740 loop->source = source_index;
741 loop->line = tokenizer.get_location().line;
742 tokenizer.expect("(");
743 loop->condition = parse_expression();
744 tokenizer.expect(")");
746 parse_block(loop->body, false, &Parser::parse_statement);
751 RefPtr<Passthrough> Parser::parse_passthrough()
753 tokenizer.expect("passthrough");
754 RefPtr<Passthrough> pass = new Passthrough;
755 pass->source = source_index;
756 pass->line = tokenizer.get_location().line;
757 if(cur_stage->type==Stage::GEOMETRY)
759 tokenizer.expect("[");
760 pass->subscript = parse_expression();
761 tokenizer.expect("]");
763 tokenizer.expect(";");
767 RefPtr<Return> Parser::parse_return()
769 tokenizer.expect("return");
770 RefPtr<Return> ret = new Return;
771 ret->source = source_index;
772 ret->line = tokenizer.get_location().line;
773 if(tokenizer.peek_token()!=";")
774 ret->expression = parse_expression();
775 tokenizer.expect(";");