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