1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
4 #include "glsl_error.h"
16 preprocessor(tokenizer),
19 tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
20 preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
21 preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
22 preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
23 preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
31 Module &Parser::parse(const string &s, const string &n, unsigned i)
38 Module &Parser::parse(IO::Base &io, const string &n, unsigned i)
44 unsigned len = io.read(buffer, sizeof(buffer));
45 source.append(buffer, len);
51 void Parser::parse_source(const string &name, unsigned index)
55 cur_stage = &module->shared;
58 source_reference(1, name);
59 tokenizer.begin(name, source);
60 while(RefPtr<Statement> statement = parse_global_declaration())
61 cur_stage->content.body.push_back(statement);
64 void Parser::set_required_version(const Version &ver)
66 cur_stage->required_features.glsl_version = ver;
69 void Parser::source_reference(unsigned index, const string &name)
72 throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
74 module->source_map.set_name(base_index+index-1, name);
77 void Parser::stage_change(Stage::Type stage)
79 if(!allow_stage_change)
80 throw invalid_shader_source(tokenizer.get_location(), "Changing stage not allowed here");
81 else if(stage<=cur_stage->type)
82 throw invalid_shader_source(tokenizer.get_location(), "Stage '%s' not allowed here", Stage::get_stage_name(stage));
84 module->stages.push_back(stage);
86 if(cur_stage->type!=Stage::SHARED)
87 module->stages.back().previous = cur_stage;
88 cur_stage = &module->stages.back();
91 void Parser::line_change(int index, unsigned line)
94 source_index = base_index+index-1;
100 string name = module->source_map.get_name(index);
102 name = format("<%d>", index);
103 tokenizer.set_location(Location(name, line));
106 string Parser::expect_type()
108 string token = tokenizer.parse_token();
110 throw parse_error(tokenizer.get_location(), token, "a type");
114 string Parser::expect_identifier()
116 string token = tokenizer.parse_token();
117 if(!is_identifier(token))
118 throw parse_error(tokenizer.get_location(), token, "an identifier");
122 int Parser::expect_integer()
124 string token = tokenizer.parse_token();
126 throw parse_error(tokenizer.get_location(), token, "an integer literal");
127 return lexical_cast<int>(token);
130 bool Parser::check(const string &token)
132 bool result = (tokenizer.peek_token()==token);
134 tokenizer.parse_token();
138 bool Parser::is_interface_qualifier(const string &token)
140 return (token=="uniform" || token=="in" || token=="out");
143 bool Parser::is_sampling_qualifier(const string &token)
145 return (token=="centroid" || token=="sample");
148 bool Parser::is_interpolation_qualifier(const string &token)
150 return (token=="smooth" || token=="flat" || token=="noperspective");
153 bool Parser::is_precision_qualifier(const string &token)
155 return (token=="highp" || token=="mediump" || token=="lowp");
158 bool Parser::is_qualifier(const string &token)
160 return (token=="const" ||
161 is_interface_qualifier(token) ||
162 is_sampling_qualifier(token) ||
163 is_interpolation_qualifier(token) ||
164 is_precision_qualifier(token));
167 bool Parser::is_builtin_type(const string &token)
169 static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
170 return re.match(token);
173 bool Parser::is_type(const string &token)
175 return is_builtin_type(token) || declared_types.count(token);
178 bool Parser::is_identifier(const string &token)
180 static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
181 return re.match(token);
184 RefPtr<Statement> Parser::parse_global_declaration()
186 allow_stage_change = true;
187 string token = tokenizer.peek_token();
188 allow_stage_change = false;
191 return parse_import();
192 else if(token=="precision")
193 return parse_precision();
194 else if(token=="layout")
196 RefPtr<Layout> layout = parse_layout();
197 token = tokenizer.peek_token();
198 if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
200 RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
201 iface_lo->source = source_index;
202 iface_lo->line = tokenizer.get_location().line;
203 iface_lo->layout.qualifiers = layout->qualifiers;
204 iface_lo->interface = tokenizer.parse_token();
205 tokenizer.expect(";");
210 RefPtr<VariableDeclaration> var = parse_variable_declaration();
211 var->layout = layout;
215 else if(token=="struct")
216 return parse_struct_declaration();
217 else if(is_interface_qualifier(token))
219 string next = tokenizer.peek_token(1);
220 if(is_type(next) || is_qualifier(next))
221 return parse_variable_declaration();
223 return parse_interface_block();
225 else if(is_qualifier(token))
226 return parse_variable_declaration();
227 else if(is_type(token))
229 if(tokenizer.peek_token(2)=="(")
230 return parse_function_declaration();
232 return parse_variable_declaration();
234 else if(token.empty())
237 throw parse_error(tokenizer.get_location(), token, "a global declaration");
240 RefPtr<Statement> Parser::parse_statement()
242 string token = tokenizer.peek_token();
244 return parse_conditional();
245 else if(token=="for")
247 else if(token=="while")
248 return parse_while();
249 else if(token=="passthrough")
250 return parse_passthrough();
251 else if(token=="return")
252 return parse_return();
253 else if(token=="break" || token=="continue" || token=="discard")
255 RefPtr<Jump> jump = new Jump;
256 jump->source = source_index;
257 jump->line = tokenizer.get_location().line;
258 jump->keyword = tokenizer.parse_token();
259 tokenizer.expect(";");
263 else if(is_qualifier(token) || is_type(token))
264 return parse_variable_declaration();
265 else if(!token.empty())
267 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
268 expr->source = source_index;
269 expr->line = tokenizer.get_location().line;
270 expr->expression = parse_expression();
271 tokenizer.expect(";");
276 throw parse_error(tokenizer.get_location(), token, "a statement");
279 RefPtr<Import> Parser::parse_import()
281 if(cur_stage->type!=Stage::SHARED)
282 throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
284 tokenizer.expect("import");
285 RefPtr<Import> import = new Import;
286 import->source = source_index;
287 import->line = tokenizer.get_location().line;
288 import->module = expect_identifier();
289 tokenizer.expect(";");
293 RefPtr<Precision> Parser::parse_precision()
295 tokenizer.expect("precision");
296 RefPtr<Precision> precision = new Precision;
297 precision->source = source_index;
298 precision->line = tokenizer.get_location().line;
300 precision->precision = tokenizer.parse_token();
301 if(!is_precision_qualifier(precision->precision))
302 throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
304 precision->type = tokenizer.parse_token();
305 // Not entirely accurate; only float, int and sampler types are allowed
306 if(!is_builtin_type(precision->type))
307 throw parse_error(tokenizer.get_location(), precision->type, "a builtin type");
309 tokenizer.expect(";");
314 RefPtr<Layout> Parser::parse_layout()
316 tokenizer.expect("layout");
317 tokenizer.expect("(");
318 RefPtr<Layout> layout = new Layout;
321 string token = tokenizer.parse_token();
323 throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
325 layout->qualifiers.push_back(Layout::Qualifier());
326 Layout::Qualifier &qual = layout->qualifiers.back();
329 if((qual.has_value = check("=")))
330 qual.value = expect_integer();
332 if(tokenizer.peek_token()==")")
335 tokenizer.expect(",");
337 tokenizer.expect(")");
342 void Parser::parse_block(Block &block, bool require_braces)
344 bool have_braces = (require_braces || tokenizer.peek_token()=="{");
346 tokenizer.expect("{");
350 while(tokenizer.peek_token()!="}")
351 block.body.push_back(parse_statement());
354 block.body.push_back(parse_statement());
356 block.use_braces = (require_braces || block.body.size()!=1);
359 tokenizer.expect("}");
362 RefPtr<Expression> Parser::parse_expression(unsigned precedence)
364 RefPtr<Expression> left;
365 VariableReference *left_var = 0;
368 string token = tokenizer.peek_token();
370 const Operator *oper = 0;
371 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
372 if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
375 if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
380 throw parse_error(tokenizer.get_location(), token, "an expression");
387 throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
388 left = parse_function_call(*left_var);
392 RefPtr<MemberAccess> memacc = new MemberAccess;
394 tokenizer.parse_token();
395 memacc->member = expect_identifier();
398 else if(oper && oper->type==Operator::POSTFIX)
400 RefPtr<UnaryExpression> unary = new UnaryExpression;
401 unary->oper = tokenizer.parse_token();
402 unary->prefix = false;
403 unary->expression = left;
406 else if(oper && oper->type==Operator::BINARY)
407 left = parse_binary(left, oper);
409 throw parse_error(tokenizer.get_location(), token, "an operator");
416 tokenizer.parse_token();
417 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
418 parexpr->expression = parse_expression();
419 tokenizer.expect(")");
422 else if(isdigit(token[0]) || token=="true" || token=="false")
424 RefPtr<Literal> literal = new Literal;
425 literal->token = tokenizer.parse_token();
428 else if(is_identifier(token))
430 RefPtr<VariableReference> var = new VariableReference;
431 var->name = expect_identifier();
433 left_var = var.get();
435 else if(oper && oper->type==Operator::PREFIX)
437 RefPtr<UnaryExpression> unary = new UnaryExpression;
438 unary->oper = tokenizer.parse_token();
439 unary->prefix = true;
440 unary->expression = parse_expression(oper->precedence);
444 throw parse_error(tokenizer.get_location(), token, "an expression");
449 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
451 RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
453 binary->oper = tokenizer.parse_token();
454 if(binary->oper=="[")
456 binary->right = parse_expression();
457 tokenizer.expect("]");
461 binary->right = parse_expression(oper->precedence+(oper->assoc==Operator::RIGHT_TO_LEFT));
465 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
467 RefPtr<FunctionCall> call = new FunctionCall;
468 call->name = var.name;
469 call->constructor = is_type(call->name);
470 tokenizer.expect("(");
471 while(tokenizer.peek_token()!=")")
473 if(!call->arguments.empty())
474 tokenizer.expect(",");
475 call->arguments.push_back(parse_expression());
477 tokenizer.expect(")");
481 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
483 tokenizer.expect("struct");
484 RefPtr<StructDeclaration> strct = new StructDeclaration;
485 strct->source = source_index;
486 strct->line = tokenizer.get_location().line;
488 strct->name = expect_identifier();
489 parse_block(strct->members, true);
490 tokenizer.expect(";");
492 declared_types.insert(strct->name);
496 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
498 RefPtr<VariableDeclaration> var = new VariableDeclaration;
499 var->source = source_index;
500 var->line = tokenizer.get_location().line;
502 string token = tokenizer.peek_token();
503 while(is_qualifier(token))
505 tokenizer.parse_token();
506 if(is_interface_qualifier(token))
507 var->interface = token;
508 else if(is_sampling_qualifier(token))
509 var->sampling = token;
510 else if(is_interpolation_qualifier(token))
511 var->interpolation = token;
512 else if(is_precision_qualifier(token))
513 var->precision = token;
514 else if(token=="const")
515 var->constant = true;
516 token = tokenizer.peek_token();
519 var->type = expect_type();
520 var->name = expect_identifier();
527 var->array_size = parse_expression();
528 tokenizer.expect("]");
533 var->init_expression = parse_expression();
535 tokenizer.expect(";");
539 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
541 RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
542 func->source = source_index;
543 func->line = tokenizer.get_location().line;
545 func->return_type = expect_type();
546 func->name = expect_identifier();
547 tokenizer.expect("(");
548 while(tokenizer.peek_token()!=")")
550 if(!func->parameters.empty())
551 tokenizer.expect(",");
553 RefPtr<VariableDeclaration> var = new VariableDeclaration;
554 string token = tokenizer.peek_token();
555 if(token=="in" || token=="out" || token=="inout")
556 var->interface = tokenizer.parse_token();
557 var->type = expect_type();
558 var->name = expect_identifier();
559 func->parameters.push_back(var);
561 tokenizer.expect(")");
563 string token = tokenizer.peek_token();
566 func->definition = func.get();
567 parse_block(func->body, true);
570 tokenizer.parse_token();
572 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
577 RefPtr<InterfaceBlock> Parser::parse_interface_block()
579 RefPtr<InterfaceBlock> iface = new InterfaceBlock;
580 iface->source = source_index;
581 iface->line = tokenizer.get_location().line;
583 iface->interface = tokenizer.parse_token();
584 if(!is_interface_qualifier(iface->interface))
585 throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
587 iface->name = expect_identifier();
588 parse_block(iface->members, true);
591 iface->instance_name = expect_identifier();
595 tokenizer.expect("]");
597 tokenizer.expect(";");
603 RefPtr<Conditional> Parser::parse_conditional()
605 tokenizer.expect("if");
606 RefPtr<Conditional> cond = new Conditional;
607 cond->source = source_index;
608 cond->line = tokenizer.get_location().line;
609 tokenizer.expect("(");
610 cond->condition = parse_expression();
611 tokenizer.expect(")");
613 parse_block(cond->body, false);
615 string token = tokenizer.peek_token();
618 tokenizer.parse_token();
619 parse_block(cond->else_body, false);
625 RefPtr<Iteration> Parser::parse_for()
627 tokenizer.expect("for");
628 RefPtr<Iteration> loop = new Iteration;
629 loop->source = source_index;
630 loop->line = tokenizer.get_location().line;
631 tokenizer.expect("(");
632 string token = tokenizer.peek_token();
634 loop->init_statement = parse_statement();
639 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
640 expr->expression = parse_expression();
641 loop->init_statement = expr;
643 tokenizer.expect(";");
645 if(tokenizer.peek_token()!=";")
646 loop->condition = parse_expression();
647 tokenizer.expect(";");
648 if(tokenizer.peek_token()!=")")
649 loop->loop_expression = parse_expression();
650 tokenizer.expect(")");
652 parse_block(loop->body, false);
657 RefPtr<Iteration> Parser::parse_while()
659 tokenizer.expect("while");
660 RefPtr<Iteration> loop = new Iteration;
661 loop->source = source_index;
662 loop->line = tokenizer.get_location().line;
663 tokenizer.expect("(");
664 loop->condition = parse_expression();
665 tokenizer.expect(")");
667 parse_block(loop->body, false);
672 RefPtr<Passthrough> Parser::parse_passthrough()
674 tokenizer.expect("passthrough");
675 RefPtr<Passthrough> pass = new Passthrough;
676 pass->source = source_index;
677 pass->line = tokenizer.get_location().line;
678 if(cur_stage->type==Stage::GEOMETRY)
680 tokenizer.expect("[");
681 pass->subscript = parse_expression();
682 tokenizer.expect("]");
684 tokenizer.expect(";");
688 RefPtr<Return> Parser::parse_return()
690 tokenizer.expect("return");
691 RefPtr<Return> ret = new Return;
692 ret->source = source_index;
693 ret->line = tokenizer.get_location().line;
694 if(tokenizer.peek_token()!=";")
695 ret->expression = parse_expression();
696 tokenizer.expect(";");