]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/parser.cpp
Redesign loading of GLSL sources
[libs/gl.git] / source / glsl / parser.cpp
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 "builtin.h"
6 #include "glsl_error.h"
7 #include "modulecache.h"
8 #include "parser.h"
9
10 #undef interface
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16 namespace SL {
17
18 Parser::Parser(ModuleCache *s):
19         mod_cache(s),
20         preprocessor(tokenizer),
21         module(0)
22 {
23         tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
24         preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
25         preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
26         preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
27         preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
28 }
29
30 void Parser::parse(Module &m, const string &s, const string &n, int i)
31 {
32         SetForScope<Module *> set_module(module, &m);
33         source = s;
34         parse_source(n, i);
35 }
36
37 void Parser::parse(Module &m, IO::Base &io, const string &n, int i)
38 {
39         SetForScope<Module *> set_module(module, &m);
40         source = string();
41         while(!io.eof())
42         {
43                 char buffer[4096];
44                 unsigned len = io.read(buffer, sizeof(buffer));
45                 source.append(buffer, len);
46         }
47         parse_source(n, i);
48 }
49
50 void Parser::parse_source(const string &name, int index)
51 {
52         cur_stage = &module->shared;
53         base_index = index;
54         source_index = index;
55         if(index>=0)
56                 source_reference(1, name);
57
58         // TODO Need to somehow get type names from imports
59         if(const Stage *builtin = get_builtins(Stage::SHARED))
60         {
61                 for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
62                         declared_types.insert(i->first);
63         }
64         else
65         {
66                 declared_types.insert("void");
67                 declared_types.insert("bool");
68                 declared_types.insert("int");
69                 declared_types.insert("uint");
70                 declared_types.insert("float");
71         }
72
73         tokenizer.begin(source, name);
74         allow_stage_change = true;
75         while(!tokenizer.peek_token().empty())
76                 if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
77                         cur_stage->content.body.push_back(statement);
78
79         if(!errors.empty())
80                 throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
81 }
82
83 void Parser::set_required_version(const Version &ver)
84 {
85         cur_stage->required_features.glsl_version = ver;
86 }
87
88 void Parser::source_reference(unsigned index, const string &name)
89 {
90         if(index<1 || base_index<0)
91                 throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
92
93         module->source_map.set_name(base_index+index-1, name);
94 }
95
96 void Parser::stage_change(Stage::Type stage)
97 {
98         if(!allow_stage_change)
99                 throw invalid_shader_source(tokenizer.get_location(), "Changing stage not allowed here");
100         else if(stage<=cur_stage->type)
101                 throw invalid_shader_source(tokenizer.get_location(), "Stage '%s' not allowed here", Stage::get_stage_name(stage));
102
103         module->stages.push_back(stage);
104
105         if(cur_stage->type!=Stage::SHARED)
106                 module->stages.back().previous = cur_stage;
107         cur_stage = &module->stages.back();
108 }
109
110 void Parser::line_change(int index, unsigned line)
111 {
112         if(index>0)
113                 source_index = base_index+index-1;
114         else if(index==0)
115                 source_index = 0;
116         else
117                 index = source_index;
118
119         string name = module->source_map.get_name(index);
120         if(name.empty())
121                 name = format("<%d>", index);
122         tokenizer.set_location(Location(name, line));
123 }
124
125 string Parser::expect_type()
126 {
127         string token = tokenizer.parse_token();
128         if(!is_type(token))
129                 throw parse_error(tokenizer.get_location(), token, "a type");
130         return token;
131 }
132
133 string Parser::expect_identifier()
134 {
135         string token = tokenizer.parse_token();
136         if(!is_identifier(token))
137                 throw parse_error(tokenizer.get_location(), token, "an identifier");
138         return token;
139 }
140
141 int Parser::expect_integer()
142 {
143         string token = tokenizer.parse_token();
144         if(!isnumrc(token))
145                 throw parse_error(tokenizer.get_location(), token, "an integer literal");
146         return lexical_cast<int>(token);
147 }
148
149 bool Parser::check(const string &token)
150 {
151         bool result = (tokenizer.peek_token()==token);
152         if(result)
153                 tokenizer.parse_token();
154         return result;
155 }
156
157 bool Parser::is_interface_qualifier(const string &token)
158 {
159         return (token=="uniform" || token=="in" || token=="out");
160 }
161
162 bool Parser::is_sampling_qualifier(const string &token)
163 {
164         return (token=="centroid" || token=="sample");
165 }
166
167 bool Parser::is_interpolation_qualifier(const string &token)
168 {
169         return (token=="smooth" || token=="flat" || token=="noperspective");
170 }
171
172 bool Parser::is_precision_qualifier(const string &token)
173 {
174         return (token=="highp" || token=="mediump" || token=="lowp");
175 }
176
177 bool Parser::is_qualifier(const string &token)
178 {
179         return (token=="const" ||
180                 is_interface_qualifier(token) ||
181                 is_sampling_qualifier(token) ||
182                 is_interpolation_qualifier(token) ||
183                 is_precision_qualifier(token));
184 }
185
186 bool Parser::is_type(const string &token)
187 {
188         return declared_types.count(token);
189 }
190
191 bool Parser::is_identifier(const string &token)
192 {
193         static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
194         return re.match(token);
195 }
196
197 template<typename T>
198 RefPtr<T> Parser::create_node()
199 {
200         RefPtr<T> node = new T;
201         node->source = source_index;
202         node->line = tokenizer.get_location().line;
203         return node;
204 }
205
206 template<typename T>
207 RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
208 {
209         tokenizer.clear_progress_mark();
210         try
211         {
212                 return (this->*parse_func)();
213         }
214         catch(const invalid_shader_source &exc)
215         {
216                 errors.push_back(exc.what());
217         }
218
219         if(tokenizer.get_last_token()!=";" || !tokenizer.get_progress_mark())
220         {
221                 unsigned scope_level = 0;
222                 while(1)
223                 {
224                         if(tokenizer.peek_token()=="}" && scope_level==0)
225                         {
226                                 if(!tokenizer.get_progress_mark())
227                                         tokenizer.parse_token();
228                                 break;
229                         }
230
231                         string token = tokenizer.parse_token();
232                         if(token=="}")
233                         {
234                                 --scope_level;
235                                 if(scope_level==0)
236                                         break;
237                         }
238                         else if(token=="{")
239                                 ++scope_level;
240                         else if(token==";" && scope_level==0)
241                                 break;
242                         else if(token.empty())
243                                 break;
244                 }
245         }
246
247         return RefPtr<T>();
248 }
249
250 RefPtr<Statement> Parser::parse_global_declaration()
251 {
252         string token = tokenizer.peek_token();
253         SetFlag disallow(allow_stage_change, false);
254
255         if(token=="import")
256                 return parse_import();
257         else if(token=="precision")
258                 return parse_precision();
259         else if(token=="layout")
260         {
261                 RefPtr<Layout> layout = parse_layout();
262                 token = tokenizer.peek_token();
263                 if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
264                 {
265                         RefPtr<InterfaceLayout> iface_lo = create_node<InterfaceLayout>();
266                         iface_lo->layout.source = layout->source;
267                         iface_lo->layout.line = layout->line;
268                         iface_lo->layout.qualifiers = layout->qualifiers;
269                         iface_lo->interface = tokenizer.parse_token();
270                         tokenizer.expect(";");
271                         return iface_lo;
272                 }
273                 else if(is_interface_qualifier(token) && tokenizer.peek_token(2)=="{")
274                 {
275                         RefPtr<InterfaceBlock> iface = parse_interface_block();
276                         iface->layout = layout;
277                         return iface;
278                 }
279                 else
280                 {
281                         RefPtr<VariableDeclaration> var = parse_variable_declaration();
282                         var->layout = layout;
283                         return var;
284                 }
285         }
286         else if(token=="typedef")
287                 return parse_type_declaration();
288         else if(token=="struct")
289                 return parse_struct_declaration();
290         else if(is_interface_qualifier(token))
291         {
292                 string next = tokenizer.peek_token(1);
293                 if(is_type(next) || is_qualifier(next))
294                         return parse_variable_declaration();
295                 else
296                         return parse_interface_block();
297         }
298         else if(is_qualifier(token))
299                 return parse_variable_declaration();
300         else if(token=="virtual")
301                 return parse_function_declaration();
302         else if(is_type(token))
303         {
304                 if(tokenizer.peek_token(2)=="(")
305                         return parse_function_declaration();
306                 else
307                         return parse_variable_declaration();
308         }
309         else if(token.empty())
310                 return 0;
311         else
312                 throw parse_error(tokenizer.get_location(), token, "a global declaration");
313 }
314
315 RefPtr<Statement> Parser::parse_statement()
316 {
317         string token = tokenizer.peek_token();
318         if(token=="if")
319                 return parse_conditional();
320         else if(token=="for")
321                 return parse_for();
322         else if(token=="while")
323                 return parse_while();
324         else if(token=="passthrough")
325                 return parse_passthrough();
326         else if(token=="return")
327                 return parse_return();
328         else if(token=="break" || token=="continue" || token=="discard")
329         {
330                 RefPtr<Jump> jump = create_node<Jump>();
331                 jump->keyword = tokenizer.parse_token();
332                 tokenizer.expect(";");
333
334                 return jump;
335         }
336         else if(is_qualifier(token) || is_type(token))
337                 return parse_variable_declaration();
338         else if(token==";")
339         {
340                 tokenizer.parse_token();
341                 throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
342         }
343         else if(!token.empty())
344         {
345                 RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
346                 expr->expression = parse_expression();
347                 tokenizer.expect(";");
348
349                 return expr;
350         }
351         else
352                 throw parse_error(tokenizer.get_location(), token, "a statement");
353 }
354
355 RefPtr<Import> Parser::parse_import()
356 {
357         if(cur_stage->type!=Stage::SHARED)
358                 throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
359
360         tokenizer.expect("import");
361         RefPtr<Import> import = create_node<Import>();
362         import->module = expect_identifier();
363         tokenizer.expect(";");
364         return import;
365 }
366
367 RefPtr<Precision> Parser::parse_precision()
368 {
369         tokenizer.expect("precision");
370         RefPtr<Precision> precision = create_node<Precision>();
371
372         precision->precision = tokenizer.parse_token();
373         if(!is_precision_qualifier(precision->precision))
374                 throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
375
376         // TODO Add validation for this
377         precision->type = expect_type();
378
379         tokenizer.expect(";");
380
381         return precision;
382 }
383
384 RefPtr<Layout> Parser::parse_layout()
385 {
386         tokenizer.expect("layout");
387         tokenizer.expect("(");
388         RefPtr<Layout> layout = create_node<Layout>();
389         while(1)
390         {
391                 string token = tokenizer.parse_token();
392                 if(token==")")
393                         throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
394
395                 layout->qualifiers.push_back(token);
396                 Layout::Qualifier &qual = layout->qualifiers.back();
397
398                 if((qual.has_value = check("=")))
399                 {
400                         if(qual.name=="constant_id" && tokenizer.peek_token()=="auto")
401                         {
402                                 qual.value = -1;
403                                 tokenizer.parse_token();
404                         }
405                         else
406                                 qual.value = expect_integer();
407                 }
408
409                 if(tokenizer.peek_token()==")")
410                         break;
411
412                 tokenizer.expect(",");
413         }
414         tokenizer.expect(")");
415
416         return layout;
417 }
418
419 template<typename T>
420 void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
421 {
422         bool have_braces = (require_braces || tokenizer.peek_token()=="{");
423         if(have_braces)
424                 tokenizer.expect("{");
425
426         if(have_braces)
427         {
428                 while(tokenizer.peek_token()!="}")
429                         if(RefPtr<Statement> node = parse_with_recovery(parse_content))
430                                 block.body.push_back(node);
431         }
432         else
433                 block.body.push_back((this->*parse_content)());
434
435         block.use_braces = (require_braces || block.body.size()!=1);
436
437         if(have_braces)
438                 tokenizer.expect("}");
439 }
440
441 RefPtr<Expression> Parser::parse_expression(const Operator *outer_oper)
442 {
443         unsigned outer_precedence = (outer_oper ? outer_oper->precedence+(outer_oper->assoc==Operator::RIGHT_TO_LEFT) : 20);
444         RefPtr<Expression> left;
445         VariableReference *left_var = 0;
446         while(1)
447         {
448                 string token = tokenizer.peek_token();
449
450                 const Operator *oper = 0;
451                 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
452                         if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
453                                 oper = i;
454
455                 bool lower_precedence = (oper && oper->type!=Operator::PREFIX && oper->precedence>=outer_precedence);
456                 if(token==";" || token==")" || token=="]" || token=="," || token==":" || lower_precedence)
457                 {
458                         if(left)
459                                 return left;
460                         else
461                                 throw parse_error(tokenizer.get_location(), token, "an expression");
462                 }
463                 else if(left)
464                 {
465                         if(token=="(")
466                         {
467                                 if(!left_var)
468                                         throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
469                                 left = parse_function_call(*left_var);
470                         }
471                         else if(token==".")
472                         {
473                                 RefPtr<MemberAccess> memacc = create_node<MemberAccess>();
474                                 memacc->left = left;
475                                 memacc->oper = oper;
476                                 tokenizer.parse_token();
477                                 memacc->member = expect_identifier();
478                                 left = memacc;
479                         }
480                         else if(oper && oper->type==Operator::POSTFIX)
481                         {
482                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
483                                 unary->oper = oper;
484                                 tokenizer.parse_token();
485                                 unary->expression = left;
486                                 left = unary;
487                         }
488                         else if(oper && oper->type==Operator::BINARY)
489                                 left = parse_binary(left, *oper);
490                         else if(oper && oper->type==Operator::TERNARY)
491                                 left = parse_ternary(left, *oper);
492                         else
493                                 throw parse_error(tokenizer.get_location(), token, "an operator");
494                         left_var = 0;
495                 }
496                 else
497                 {
498                         if(token=="(")
499                         {
500                                 tokenizer.parse_token();
501                                 left = parse_expression();
502                                 tokenizer.expect(")");
503                         }
504                         else if(isdigit(token[0]) || token=="true" || token=="false")
505                                 left = parse_literal();
506                         else if(is_identifier(token))
507                         {
508                                 RefPtr<VariableReference> var = create_node<VariableReference>();
509                                 var->name = expect_identifier();
510                                 left = var;
511                                 left_var = var.get();
512                         }
513                         else if(oper && oper->type==Operator::PREFIX)
514                         {
515                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
516                                 unary->oper = oper;
517                                 tokenizer.parse_token();
518                                 unary->expression = parse_expression(oper);
519                                 left = unary;
520                         }
521                         else
522                                 throw parse_error(tokenizer.get_location(), token, "an expression");
523                 }
524         }
525 }
526
527 RefPtr<Literal> Parser::parse_literal()
528 {
529         RefPtr<Literal> literal = create_node<Literal>();
530         literal->token = tokenizer.parse_token();
531         if(isdigit(literal->token[0]))
532         {
533                 // TODO have the tokenizer return the type of the token
534                 if(literal->token.back()=='u')
535                         literal->value = lexical_cast<unsigned>(literal->token.substr(0, literal->token.size()-1));
536                 else if(literal->token.back()=='f')
537                         literal->value = lexical_cast<float>(literal->token.substr(0, literal->token.size()-1));
538                 else if(literal->token.find('.')!=string::npos)
539                         literal->value = lexical_cast<float>(literal->token);
540                 else
541                         literal->value = lexical_cast<int>(literal->token);
542         }
543         else if(literal->token=="true" || literal->token=="false")
544                 literal->value = (literal->token=="true");
545         else
546                 throw parse_error(tokenizer.get_location(), literal->token, "a literal");
547
548         return literal;
549 }
550
551 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
552 {
553         RefPtr<BinaryExpression> binary = (oper.precedence==16 ?
554                 static_cast<RefPtr<BinaryExpression> >(create_node<Assignment>()) : create_node<BinaryExpression>());
555         binary->left = left;
556         binary->oper = &oper;
557         tokenizer.expect(oper.token);
558         if(oper.token2[0])
559         {
560                 binary->right = parse_expression();
561                 tokenizer.expect(oper.token2);
562         }
563         else
564                 binary->right = parse_expression(&oper);
565         return binary;
566 }
567
568 RefPtr<TernaryExpression> Parser::parse_ternary(const RefPtr<Expression> &cond, const Operator &oper)
569 {
570         RefPtr<TernaryExpression> ternary = create_node<TernaryExpression>();
571         ternary->condition = cond;
572         ternary->oper = &oper;
573         tokenizer.expect("?");
574         ternary->true_expr = parse_expression(&oper);
575         tokenizer.expect(":");
576         ternary->false_expr = parse_expression(&oper);
577         return ternary;
578 }
579
580 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
581 {
582         RefPtr<FunctionCall> call = create_node<FunctionCall>();
583         call->name = var.name;
584         call->oper = &Operator::get_operator("(", Operator::POSTFIX);
585         tokenizer.expect("(");
586         while(tokenizer.peek_token()!=")")
587         {
588                 if(!call->arguments.empty())
589                         tokenizer.expect(",");
590                 call->arguments.push_back(parse_expression());
591         }
592         tokenizer.expect(")");
593         return call;
594 }
595
596 RefPtr<TypeDeclaration> Parser::parse_type_declaration()
597 {
598         tokenizer.expect("typedef");
599
600         RefPtr<TypeDeclaration> type;
601         if(tokenizer.peek_token()=="image")
602                 type = parse_image_type_declaration();
603         else
604                 type = parse_basic_type_declaration();
605
606         tokenizer.expect(";");
607         declared_types.insert(type->name);
608         return type;
609 }
610
611 RefPtr<BasicTypeDeclaration> Parser::parse_basic_type_declaration()
612 {
613         RefPtr<BasicTypeDeclaration> type = create_node<BasicTypeDeclaration>();
614
615         if(tokenizer.peek_token()=="vector")
616         {
617                 type->kind = BasicTypeDeclaration::VECTOR;
618
619                 tokenizer.parse_token();
620                 tokenizer.expect("(");
621                 type->size = expect_integer();
622                 tokenizer.expect(")");
623         }
624
625         type->base = expect_type();
626         type->name = expect_identifier();
627
628         if(type->kind==BasicTypeDeclaration::ALIAS && check("["))
629         {
630                 type->kind = BasicTypeDeclaration::ARRAY;
631                 tokenizer.expect("]");
632         }
633
634         return type;
635 }
636
637 RefPtr<ImageTypeDeclaration> Parser::parse_image_type_declaration()
638 {
639         tokenizer.expect("image");
640         tokenizer.expect("(");
641
642         RefPtr<ImageTypeDeclaration> type = create_node<ImageTypeDeclaration>();
643         while(1)
644         {
645                 string token = tokenizer.parse_token();
646                 if(token=="dimensions")
647                 {
648                         tokenizer.expect("=");
649                         token = tokenizer.parse_token();
650                         if(token=="1")
651                                 type->dimensions = ImageTypeDeclaration::ONE;
652                         else if(token=="2")
653                                 type->dimensions = ImageTypeDeclaration::TWO;
654                         else if(token=="3")
655                                 type->dimensions = ImageTypeDeclaration::THREE;
656                         else if(token=="cube")
657                                 type->dimensions = ImageTypeDeclaration::CUBE;
658                         else
659                                 throw parse_error(tokenizer.get_location(), token, "dimensions");
660
661                         if(check("["))
662                         {
663                                 type->array = true;
664                                 tokenizer.expect("]");
665                         }
666                 }
667                 else if(token=="sampled")
668                         type->sampled = true;
669                 else if(token=="shadow")
670                         type->shadow = true;
671                 else
672                         throw parse_error(tokenizer.get_location(), token, "image type attribute");
673
674                 token = tokenizer.peek_token();
675                 if(token==")")
676                         break;
677
678                 tokenizer.expect(",");
679         }
680         tokenizer.expect(")");
681
682         type->base = expect_type();
683         type->name = expect_identifier();
684
685         return type;
686 }
687
688 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
689 {
690         tokenizer.expect("struct");
691         RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
692
693         strct->name = expect_identifier();
694         parse_block(strct->members, true, &Parser::parse_variable_declaration);
695         tokenizer.expect(";");
696
697         declared_types.insert(strct->name);
698         return strct;
699 }
700
701 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
702 {
703         RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
704
705         string token = tokenizer.peek_token();
706         while(is_qualifier(token))
707         {
708                 tokenizer.parse_token();
709                 if(is_interface_qualifier(token))
710                         var->interface = token;
711                 else if(is_sampling_qualifier(token))
712                         var->sampling = token;
713                 else if(is_interpolation_qualifier(token))
714                         var->interpolation = token;
715                 else if(is_precision_qualifier(token))
716                         var->precision = token;
717                 else if(token=="const")
718                         var->constant = true;
719                 token = tokenizer.peek_token();
720         }
721
722         var->type = expect_type();
723         var->name = expect_identifier();
724
725         if(check("["))
726         {
727                 var->array = true;
728                 if(!check("]"))
729                 {
730                         var->array_size = parse_expression();
731                         tokenizer.expect("]");
732                 }
733         }
734
735         if(check("="))
736                 var->init_expression = parse_expression();
737
738         tokenizer.expect(";");
739         return var;
740 }
741
742 RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
743 {
744         RefPtr<Layout> layout;
745         if(tokenizer.peek_token()=="layout")
746                 layout = parse_layout();
747
748         RefPtr<VariableDeclaration> var = parse_variable_declaration();
749         var->layout = layout;
750
751         return var;
752 }
753
754 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
755 {
756         RefPtr<FunctionDeclaration> func = create_node<FunctionDeclaration>();
757
758         func->virtua = check("virtual");
759         func->return_type = expect_type();
760         func->name = expect_identifier();
761         tokenizer.expect("(");
762         while(tokenizer.peek_token()!=")")
763         {
764                 if(!func->parameters.empty())
765                         tokenizer.expect(",");
766
767                 RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
768                 string token = tokenizer.peek_token();
769                 if(token=="in" || token=="out" || token=="inout")
770                         var->interface = tokenizer.parse_token();
771                 var->type = expect_type();
772                 var->name = expect_identifier();
773                 func->parameters.push_back(var);
774         }
775         tokenizer.expect(")");
776
777         func->overrd = check("override");
778
779         string token = tokenizer.peek_token();
780         if(token=="{")
781         {
782                 func->definition = func.get();
783                 parse_block(func->body, true, &Parser::parse_statement);
784         }
785         else if(token==";")
786                 tokenizer.parse_token();
787         else
788                 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
789
790         return func;
791 }
792
793 RefPtr<InterfaceBlock> Parser::parse_interface_block()
794 {
795         RefPtr<InterfaceBlock> iface = create_node<InterfaceBlock>();
796
797         iface->interface = tokenizer.parse_token();
798         if(!is_interface_qualifier(iface->interface))
799                 throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
800
801         iface->block_name = expect_identifier();
802         iface->members = new Block;
803         parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
804         if(!check(";"))
805         {
806                 iface->instance_name = expect_identifier();
807                 if(check("["))
808                 {
809                         iface->array = true;
810                         tokenizer.expect("]");
811                 }
812                 tokenizer.expect(";");
813         }
814
815         return iface;
816 }
817
818 RefPtr<Conditional> Parser::parse_conditional()
819 {
820         tokenizer.expect("if");
821         RefPtr<Conditional> cond = create_node<Conditional>();
822         tokenizer.expect("(");
823         cond->condition = parse_expression();
824         tokenizer.expect(")");
825
826         parse_block(cond->body, false, &Parser::parse_statement);
827
828         string token = tokenizer.peek_token();
829         if(token=="else")
830         {
831                 tokenizer.parse_token();
832                 parse_block(cond->else_body, false, &Parser::parse_statement);
833         }
834
835         return cond;
836 }
837
838 RefPtr<Iteration> Parser::parse_for()
839 {
840         tokenizer.expect("for");
841         RefPtr<Iteration> loop = create_node<Iteration>();
842         tokenizer.expect("(");
843         string token = tokenizer.peek_token();
844         if(is_type(token))
845                 loop->init_statement = parse_statement();
846         else
847         {
848                 if(token!=";")
849                 {
850                         RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
851                         expr->expression = parse_expression();
852                         loop->init_statement = expr;
853                 }
854                 tokenizer.expect(";");
855         }
856         if(tokenizer.peek_token()!=";")
857                 loop->condition = parse_expression();
858         tokenizer.expect(";");
859         if(tokenizer.peek_token()!=")")
860                 loop->loop_expression = parse_expression();
861         tokenizer.expect(")");
862
863         parse_block(loop->body, false, &Parser::parse_statement);
864
865         return loop;
866 }
867
868 RefPtr<Iteration> Parser::parse_while()
869 {
870         tokenizer.expect("while");
871         RefPtr<Iteration> loop = create_node<Iteration>();
872         tokenizer.expect("(");
873         loop->condition = parse_expression();
874         tokenizer.expect(")");
875
876         parse_block(loop->body, false, &Parser::parse_statement);
877
878         return loop;
879 }
880
881 RefPtr<Passthrough> Parser::parse_passthrough()
882 {
883         tokenizer.expect("passthrough");
884         RefPtr<Passthrough> pass = create_node<Passthrough>();
885         if(cur_stage->type==Stage::GEOMETRY)
886         {
887                 tokenizer.expect("[");
888                 pass->subscript = parse_expression();
889                 tokenizer.expect("]");
890         }
891         tokenizer.expect(";");
892         return pass;
893 }
894
895 RefPtr<Return> Parser::parse_return()
896 {
897         tokenizer.expect("return");
898         RefPtr<Return> ret = create_node<Return>();
899         if(tokenizer.peek_token()!=";")
900                 ret->expression = parse_expression();
901         tokenizer.expect(";");
902         return ret;
903 }
904
905 } // namespace SL
906 } // namespace GL
907 } // namespace Msp