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(")");
343 void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
345 bool have_braces = (require_braces || tokenizer.peek_token()=="{");
347 tokenizer.expect("{");
351 while(tokenizer.peek_token()!="}")
352 block.body.push_back((this->*parse_content)());
355 block.body.push_back((this->*parse_content)());
357 block.use_braces = (require_braces || block.body.size()!=1);
360 tokenizer.expect("}");
363 RefPtr<Expression> Parser::parse_expression(unsigned precedence)
365 RefPtr<Expression> left;
366 VariableReference *left_var = 0;
369 string token = tokenizer.peek_token();
371 const Operator *oper = 0;
372 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
373 if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
376 if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
381 throw parse_error(tokenizer.get_location(), token, "an expression");
388 throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
389 left = parse_function_call(*left_var);
393 RefPtr<MemberAccess> memacc = new MemberAccess;
395 tokenizer.parse_token();
396 memacc->member = expect_identifier();
399 else if(oper && oper->type==Operator::POSTFIX)
401 RefPtr<UnaryExpression> unary = new UnaryExpression;
402 unary->oper = tokenizer.parse_token();
403 unary->prefix = false;
404 unary->expression = left;
407 else if(oper && oper->type==Operator::BINARY)
408 left = parse_binary(left, oper);
410 throw parse_error(tokenizer.get_location(), token, "an operator");
417 tokenizer.parse_token();
418 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
419 parexpr->expression = parse_expression();
420 tokenizer.expect(")");
423 else if(isdigit(token[0]) || token=="true" || token=="false")
425 RefPtr<Literal> literal = new Literal;
426 literal->token = tokenizer.parse_token();
429 else if(is_identifier(token))
431 RefPtr<VariableReference> var = new VariableReference;
432 var->name = expect_identifier();
434 left_var = var.get();
436 else if(oper && oper->type==Operator::PREFIX)
438 RefPtr<UnaryExpression> unary = new UnaryExpression;
439 unary->oper = tokenizer.parse_token();
440 unary->prefix = true;
441 unary->expression = parse_expression(oper->precedence);
445 throw parse_error(tokenizer.get_location(), token, "an expression");
450 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator *oper)
452 RefPtr<BinaryExpression> binary = (oper->precedence==16 ? new Assignment : new BinaryExpression);
454 binary->oper = tokenizer.parse_token();
455 if(binary->oper=="[")
457 binary->right = parse_expression();
458 tokenizer.expect("]");
462 binary->right = parse_expression(oper->precedence+(oper->assoc==Operator::RIGHT_TO_LEFT));
466 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
468 RefPtr<FunctionCall> call = new FunctionCall;
469 call->name = var.name;
470 call->constructor = is_type(call->name);
471 tokenizer.expect("(");
472 while(tokenizer.peek_token()!=")")
474 if(!call->arguments.empty())
475 tokenizer.expect(",");
476 call->arguments.push_back(parse_expression());
478 tokenizer.expect(")");
482 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
484 tokenizer.expect("struct");
485 RefPtr<StructDeclaration> strct = new StructDeclaration;
486 strct->source = source_index;
487 strct->line = tokenizer.get_location().line;
489 strct->name = expect_identifier();
490 parse_block(strct->members, true, &Parser::parse_variable_declaration);
491 tokenizer.expect(";");
493 declared_types.insert(strct->name);
497 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
499 RefPtr<VariableDeclaration> var = new VariableDeclaration;
500 var->source = source_index;
501 var->line = tokenizer.get_location().line;
503 string token = tokenizer.peek_token();
504 while(is_qualifier(token))
506 tokenizer.parse_token();
507 if(is_interface_qualifier(token))
508 var->interface = token;
509 else if(is_sampling_qualifier(token))
510 var->sampling = token;
511 else if(is_interpolation_qualifier(token))
512 var->interpolation = token;
513 else if(is_precision_qualifier(token))
514 var->precision = token;
515 else if(token=="const")
516 var->constant = true;
517 token = tokenizer.peek_token();
520 var->type = expect_type();
521 var->name = expect_identifier();
528 var->array_size = parse_expression();
529 tokenizer.expect("]");
534 var->init_expression = parse_expression();
536 tokenizer.expect(";");
540 RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
542 RefPtr<Layout> layout;
543 if(tokenizer.peek_token()=="layout")
544 layout = parse_layout();
546 RefPtr<VariableDeclaration> var = parse_variable_declaration();
547 var->layout = layout;
552 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
554 RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
555 func->source = source_index;
556 func->line = tokenizer.get_location().line;
558 func->return_type = expect_type();
559 func->name = expect_identifier();
560 tokenizer.expect("(");
561 while(tokenizer.peek_token()!=")")
563 if(!func->parameters.empty())
564 tokenizer.expect(",");
566 RefPtr<VariableDeclaration> var = new VariableDeclaration;
567 string token = tokenizer.peek_token();
568 if(token=="in" || token=="out" || token=="inout")
569 var->interface = tokenizer.parse_token();
570 var->type = expect_type();
571 var->name = expect_identifier();
572 func->parameters.push_back(var);
574 tokenizer.expect(")");
576 string token = tokenizer.peek_token();
579 func->definition = func.get();
580 parse_block(func->body, true, &Parser::parse_statement);
583 tokenizer.parse_token();
585 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
590 RefPtr<InterfaceBlock> Parser::parse_interface_block()
592 RefPtr<InterfaceBlock> iface = new InterfaceBlock;
593 iface->source = source_index;
594 iface->line = tokenizer.get_location().line;
596 iface->interface = tokenizer.parse_token();
597 if(!is_interface_qualifier(iface->interface))
598 throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
600 iface->name = expect_identifier();
601 parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
604 iface->instance_name = expect_identifier();
608 tokenizer.expect("]");
610 tokenizer.expect(";");
616 RefPtr<Conditional> Parser::parse_conditional()
618 tokenizer.expect("if");
619 RefPtr<Conditional> cond = new Conditional;
620 cond->source = source_index;
621 cond->line = tokenizer.get_location().line;
622 tokenizer.expect("(");
623 cond->condition = parse_expression();
624 tokenizer.expect(")");
626 parse_block(cond->body, false, &Parser::parse_statement);
628 string token = tokenizer.peek_token();
631 tokenizer.parse_token();
632 parse_block(cond->else_body, false, &Parser::parse_statement);
638 RefPtr<Iteration> Parser::parse_for()
640 tokenizer.expect("for");
641 RefPtr<Iteration> loop = new Iteration;
642 loop->source = source_index;
643 loop->line = tokenizer.get_location().line;
644 tokenizer.expect("(");
645 string token = tokenizer.peek_token();
647 loop->init_statement = parse_statement();
652 RefPtr<ExpressionStatement> expr = new ExpressionStatement;
653 expr->expression = parse_expression();
654 loop->init_statement = expr;
656 tokenizer.expect(";");
658 if(tokenizer.peek_token()!=";")
659 loop->condition = parse_expression();
660 tokenizer.expect(";");
661 if(tokenizer.peek_token()!=")")
662 loop->loop_expression = parse_expression();
663 tokenizer.expect(")");
665 parse_block(loop->body, false, &Parser::parse_statement);
670 RefPtr<Iteration> Parser::parse_while()
672 tokenizer.expect("while");
673 RefPtr<Iteration> loop = new Iteration;
674 loop->source = source_index;
675 loop->line = tokenizer.get_location().line;
676 tokenizer.expect("(");
677 loop->condition = parse_expression();
678 tokenizer.expect(")");
680 parse_block(loop->body, false, &Parser::parse_statement);
685 RefPtr<Passthrough> Parser::parse_passthrough()
687 tokenizer.expect("passthrough");
688 RefPtr<Passthrough> pass = new Passthrough;
689 pass->source = source_index;
690 pass->line = tokenizer.get_location().line;
691 if(cur_stage->type==Stage::GEOMETRY)
693 tokenizer.expect("[");
694 pass->subscript = parse_expression();
695 tokenizer.expect("]");
697 tokenizer.expect(";");
701 RefPtr<Return> Parser::parse_return()
703 tokenizer.expect("return");
704 RefPtr<Return> ret = new Return;
705 ret->source = source_index;
706 ret->line = tokenizer.get_location().line;
707 if(tokenizer.peek_token()!=";")
708 ret->expression = parse_expression();
709 tokenizer.expect(";");