]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/parser.cpp
Avoid generating invalid interfaces in geometry shader
[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.qualifiers = layout->qualifiers;
272                         iface_lo->interface = tokenizer.parse_token();
273                         tokenizer.expect(";");
274                         return iface_lo;
275                 }
276                 else
277                 {
278                         RefPtr<VariableDeclaration> var = parse_variable_declaration();
279                         var->layout = layout;
280                         return var;
281                 }
282         }
283         else if(token=="typedef")
284                 return parse_type_declaration();
285         else if(token=="struct")
286                 return parse_struct_declaration();
287         else if(is_interface_qualifier(token))
288         {
289                 string next = tokenizer.peek_token(1);
290                 if(is_type(next) || is_qualifier(next))
291                         return parse_variable_declaration();
292                 else
293                         return parse_interface_block();
294         }
295         else if(is_qualifier(token))
296                 return parse_variable_declaration();
297         else if(is_type(token))
298         {
299                 if(tokenizer.peek_token(2)=="(")
300                         return parse_function_declaration();
301                 else
302                         return parse_variable_declaration();
303         }
304         else if(token.empty())
305                 return 0;
306         else
307                 throw parse_error(tokenizer.get_location(), token, "a global declaration");
308 }
309
310 RefPtr<Statement> Parser::parse_statement()
311 {
312         string token = tokenizer.peek_token();
313         if(token=="if")
314                 return parse_conditional();
315         else if(token=="for")
316                 return parse_for();
317         else if(token=="while")
318                 return parse_while();
319         else if(token=="passthrough")
320                 return parse_passthrough();
321         else if(token=="return")
322                 return parse_return();
323         else if(token=="break" || token=="continue" || token=="discard")
324         {
325                 RefPtr<Jump> jump = create_node<Jump>();
326                 jump->keyword = tokenizer.parse_token();
327                 tokenizer.expect(";");
328
329                 return jump;
330         }
331         else if(is_qualifier(token) || is_type(token))
332                 return parse_variable_declaration();
333         else if(token==";")
334         {
335                 tokenizer.parse_token();
336                 throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
337         }
338         else if(!token.empty())
339         {
340                 RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
341                 expr->expression = parse_expression();
342                 tokenizer.expect(";");
343
344                 return expr;
345         }
346         else
347                 throw parse_error(tokenizer.get_location(), token, "a statement");
348 }
349
350 RefPtr<Import> Parser::parse_import()
351 {
352         if(cur_stage->type!=Stage::SHARED)
353                 throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
354
355         tokenizer.expect("import");
356         RefPtr<Import> import = create_node<Import>();
357         import->module = expect_identifier();
358         tokenizer.expect(";");
359         return import;
360 }
361
362 RefPtr<Precision> Parser::parse_precision()
363 {
364         tokenizer.expect("precision");
365         RefPtr<Precision> precision = create_node<Precision>();
366
367         precision->precision = tokenizer.parse_token();
368         if(!is_precision_qualifier(precision->precision))
369                 throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
370
371         // TODO Add validation for this
372         precision->type = expect_type();
373
374         tokenizer.expect(";");
375
376         return precision;
377 }
378
379 RefPtr<Layout> Parser::parse_layout()
380 {
381         tokenizer.expect("layout");
382         tokenizer.expect("(");
383         RefPtr<Layout> layout = create_node<Layout>();
384         while(1)
385         {
386                 string token = tokenizer.parse_token();
387                 if(token==")")
388                         throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
389
390                 layout->qualifiers.push_back(Layout::Qualifier());
391                 Layout::Qualifier &qual = layout->qualifiers.back();
392                 qual.name = token;
393
394                 if((qual.has_value = check("=")))
395                 {
396                         if(qual.name=="constant_id" && tokenizer.peek_token()=="auto")
397                         {
398                                 qual.value = -1;
399                                 tokenizer.parse_token();
400                         }
401                         else
402                                 qual.value = expect_integer();
403                 }
404
405                 if(tokenizer.peek_token()==")")
406                         break;
407
408                 tokenizer.expect(",");
409         }
410         tokenizer.expect(")");
411
412         return layout;
413 }
414
415 template<typename T>
416 void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
417 {
418         bool have_braces = (require_braces || tokenizer.peek_token()=="{");
419         if(have_braces)
420                 tokenizer.expect("{");
421
422         if(have_braces)
423         {
424                 while(tokenizer.peek_token()!="}")
425                         if(RefPtr<Statement> node = parse_with_recovery(parse_content))
426                                 block.body.push_back(node);
427         }
428         else
429                 block.body.push_back((this->*parse_content)());
430
431         block.use_braces = (require_braces || block.body.size()!=1);
432
433         if(have_braces)
434                 tokenizer.expect("}");
435 }
436
437 RefPtr<Expression> Parser::parse_expression(unsigned precedence)
438 {
439         RefPtr<Expression> left;
440         VariableReference *left_var = 0;
441         while(1)
442         {
443                 string token = tokenizer.peek_token();
444
445                 const Operator *oper = 0;
446                 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
447                         if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
448                                 oper = i;
449
450                 bool lower_precedence = (oper && oper->type!=Operator::PREFIX && precedence && oper->precedence>=precedence);
451                 if(token==";" || token==")" || token=="]" || token=="," || lower_precedence)
452                 {
453                         if(left)
454                                 return left;
455                         else
456                                 throw parse_error(tokenizer.get_location(), token, "an expression");
457                 }
458                 else if(left)
459                 {
460                         if(token=="(")
461                         {
462                                 if(!left_var)
463                                         throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
464                                 left = parse_function_call(*left_var);
465                         }
466                         else if(token==".")
467                         {
468                                 RefPtr<MemberAccess> memacc = create_node<MemberAccess>();
469                                 memacc->left = left;
470                                 memacc->oper = oper;
471                                 tokenizer.parse_token();
472                                 memacc->member = expect_identifier();
473                                 left = memacc;
474                         }
475                         else if(oper && oper->type==Operator::POSTFIX)
476                         {
477                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
478                                 unary->oper = oper;
479                                 tokenizer.parse_token();
480                                 unary->expression = left;
481                                 left = unary;
482                         }
483                         else if(oper && oper->type==Operator::BINARY)
484                                 left = parse_binary(left, *oper);
485                         else
486                                 throw parse_error(tokenizer.get_location(), token, "an operator");
487                         left_var = 0;
488                 }
489                 else
490                 {
491                         if(token=="(")
492                         {
493                                 tokenizer.parse_token();
494                                 RefPtr<ParenthesizedExpression> parexpr = create_node<ParenthesizedExpression>();
495                                 parexpr->expression = parse_expression();
496                                 tokenizer.expect(")");
497                                 left = parexpr;
498                         }
499                         else if(isdigit(token[0]) || token=="true" || token=="false")
500                                 left = parse_literal();
501                         else if(is_identifier(token))
502                         {
503                                 RefPtr<VariableReference> var = create_node<VariableReference>();
504                                 var->name = expect_identifier();
505                                 left = var;
506                                 left_var = var.get();
507                         }
508                         else if(oper && oper->type==Operator::PREFIX)
509                         {
510                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
511                                 unary->oper = oper;
512                                 tokenizer.parse_token();
513                                 unary->expression = parse_expression(oper->precedence);
514                                 left = unary;
515                         }
516                         else
517                                 throw parse_error(tokenizer.get_location(), token, "an expression");
518                 }
519         }
520 }
521
522 RefPtr<Literal> Parser::parse_literal()
523 {
524         RefPtr<Literal> literal = create_node<Literal>();
525         literal->token = tokenizer.parse_token();
526         if(isdigit(literal->token[0]))
527         {
528                 // TODO have the tokenizer return the type of the token
529                 if(isnumrc(literal->token))
530                         literal->value = lexical_cast<int>(literal->token);
531                 else
532                         literal->value = lexical_cast<float>(literal->token);
533         }
534         else if(literal->token=="true" || literal->token=="false")
535                 literal->value = (literal->token=="true");
536         else
537                 throw parse_error(tokenizer.get_location(), literal->token, "a literal");
538
539         return literal;
540 }
541
542 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
543 {
544         RefPtr<BinaryExpression> binary = (oper.precedence==16 ?
545                 static_cast<RefPtr<BinaryExpression> >(create_node<Assignment>()) : create_node<BinaryExpression>());
546         binary->left = left;
547         binary->oper = &oper;
548         tokenizer.expect(oper.token);
549         if(oper.token[0]=='[')
550         {
551                 binary->right = parse_expression();
552                 tokenizer.expect("]");
553         }
554         else
555                 binary->right = parse_expression(oper.precedence+(oper.assoc==Operator::RIGHT_TO_LEFT));
556         return binary;
557 }
558
559 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
560 {
561         RefPtr<FunctionCall> call = create_node<FunctionCall>();
562         call->name = var.name;
563         call->constructor = is_type(call->name);
564         call->oper = &Operator::get_operator("(", Operator::POSTFIX);
565         tokenizer.expect("(");
566         while(tokenizer.peek_token()!=")")
567         {
568                 if(!call->arguments.empty())
569                         tokenizer.expect(",");
570                 call->arguments.push_back(parse_expression());
571         }
572         tokenizer.expect(")");
573         return call;
574 }
575
576 RefPtr<TypeDeclaration> Parser::parse_type_declaration()
577 {
578         tokenizer.expect("typedef");
579
580         RefPtr<TypeDeclaration> type;
581         if(tokenizer.peek_token()=="image")
582                 type = parse_image_type_declaration();
583         else
584                 type = parse_basic_type_declaration();
585
586         tokenizer.expect(";");
587         declared_types.insert(type->name);
588         return type;
589 }
590
591 RefPtr<BasicTypeDeclaration> Parser::parse_basic_type_declaration()
592 {
593         RefPtr<BasicTypeDeclaration> type = create_node<BasicTypeDeclaration>();
594
595         if(tokenizer.peek_token()=="vector")
596         {
597                 type->kind = BasicTypeDeclaration::VECTOR;
598
599                 tokenizer.parse_token();
600                 tokenizer.expect("(");
601                 type->size = expect_integer();
602                 tokenizer.expect(")");
603         }
604
605         type->base = expect_type();
606         type->name = expect_identifier();
607
608         if(type->kind==BasicTypeDeclaration::ALIAS && check("["))
609         {
610                 type->kind = BasicTypeDeclaration::ARRAY;
611                 tokenizer.expect("]");
612         }
613
614         return type;
615 }
616
617 RefPtr<ImageTypeDeclaration> Parser::parse_image_type_declaration()
618 {
619         tokenizer.expect("image");
620         tokenizer.expect("(");
621
622         RefPtr<ImageTypeDeclaration> type = create_node<ImageTypeDeclaration>();
623         while(1)
624         {
625                 string token = tokenizer.parse_token();
626                 if(token=="dimensions")
627                 {
628                         tokenizer.expect("=");
629                         token = tokenizer.parse_token();
630                         if(token=="1")
631                                 type->dimensions = ImageTypeDeclaration::ONE;
632                         else if(token=="2")
633                                 type->dimensions = ImageTypeDeclaration::TWO;
634                         else if(token=="3")
635                                 type->dimensions = ImageTypeDeclaration::THREE;
636                         else if(token=="cube")
637                                 type->dimensions = ImageTypeDeclaration::CUBE;
638                         else
639                                 throw parse_error(tokenizer.get_location(), token, "dimensions");
640
641                         if(check("["))
642                         {
643                                 type->array = true;
644                                 tokenizer.expect("]");
645                         }
646                 }
647                 else if(token=="sampled")
648                         type->sampled = true;
649                 else if(token=="shadow")
650                         type->shadow = true;
651                 else
652                         throw parse_error(tokenizer.get_location(), token, "image type attribute");
653
654                 token = tokenizer.peek_token();
655                 if(token==")")
656                         break;
657
658                 tokenizer.expect(",");
659         }
660         tokenizer.expect(")");
661
662         type->base = expect_type();
663         type->name = expect_identifier();
664
665         return type;
666 }
667
668 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
669 {
670         tokenizer.expect("struct");
671         RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
672
673         strct->name = expect_identifier();
674         parse_block(strct->members, true, &Parser::parse_variable_declaration);
675         tokenizer.expect(";");
676
677         declared_types.insert(strct->name);
678         return strct;
679 }
680
681 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
682 {
683         RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
684
685         string token = tokenizer.peek_token();
686         while(is_qualifier(token))
687         {
688                 tokenizer.parse_token();
689                 if(is_interface_qualifier(token))
690                         var->interface = token;
691                 else if(is_sampling_qualifier(token))
692                         var->sampling = token;
693                 else if(is_interpolation_qualifier(token))
694                         var->interpolation = token;
695                 else if(is_precision_qualifier(token))
696                         var->precision = token;
697                 else if(token=="const")
698                         var->constant = true;
699                 token = tokenizer.peek_token();
700         }
701
702         var->type = expect_type();
703         var->name = expect_identifier();
704
705         if(check("["))
706         {
707                 var->array = true;
708                 if(!check("]"))
709                 {
710                         var->array_size = parse_expression();
711                         tokenizer.expect("]");
712                 }
713         }
714
715         if(check("="))
716                 var->init_expression = parse_expression();
717
718         tokenizer.expect(";");
719         return var;
720 }
721
722 RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
723 {
724         RefPtr<Layout> layout;
725         if(tokenizer.peek_token()=="layout")
726                 layout = parse_layout();
727
728         RefPtr<VariableDeclaration> var = parse_variable_declaration();
729         var->layout = layout;
730
731         return var;
732 }
733
734 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
735 {
736         RefPtr<FunctionDeclaration> func = create_node<FunctionDeclaration>();
737
738         func->return_type = expect_type();
739         func->name = expect_identifier();
740         tokenizer.expect("(");
741         while(tokenizer.peek_token()!=")")
742         {
743                 if(!func->parameters.empty())
744                         tokenizer.expect(",");
745
746                 RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
747                 string token = tokenizer.peek_token();
748                 if(token=="in" || token=="out" || token=="inout")
749                         var->interface = tokenizer.parse_token();
750                 var->type = expect_type();
751                 var->name = expect_identifier();
752                 func->parameters.push_back(var);
753         }
754         tokenizer.expect(")");
755
756         string token = tokenizer.peek_token();
757         if(token=="{")
758         {
759                 func->definition = func.get();
760                 parse_block(func->body, true, &Parser::parse_statement);
761         }
762         else if(token==";")
763                 tokenizer.parse_token();
764         else
765                 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
766
767         return func;
768 }
769
770 RefPtr<InterfaceBlock> Parser::parse_interface_block()
771 {
772         RefPtr<InterfaceBlock> iface = create_node<InterfaceBlock>();
773
774         iface->interface = tokenizer.parse_token();
775         if(!is_interface_qualifier(iface->interface))
776                 throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
777
778         iface->name = expect_identifier();
779         iface->members = new Block;
780         parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
781         if(!check(";"))
782         {
783                 iface->instance_name = expect_identifier();
784                 if(check("["))
785                 {
786                         iface->array = true;
787                         tokenizer.expect("]");
788                 }
789                 tokenizer.expect(";");
790         }
791
792         return iface;
793 }
794
795 RefPtr<Conditional> Parser::parse_conditional()
796 {
797         tokenizer.expect("if");
798         RefPtr<Conditional> cond = create_node<Conditional>();
799         tokenizer.expect("(");
800         cond->condition = parse_expression();
801         tokenizer.expect(")");
802
803         parse_block(cond->body, false, &Parser::parse_statement);
804
805         string token = tokenizer.peek_token();
806         if(token=="else")
807         {
808                 tokenizer.parse_token();
809                 parse_block(cond->else_body, false, &Parser::parse_statement);
810         }
811
812         return cond;
813 }
814
815 RefPtr<Iteration> Parser::parse_for()
816 {
817         tokenizer.expect("for");
818         RefPtr<Iteration> loop = create_node<Iteration>();
819         tokenizer.expect("(");
820         string token = tokenizer.peek_token();
821         if(is_type(token))
822                 loop->init_statement = parse_statement();
823         else
824         {
825                 if(token!=";")
826                 {
827                         RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
828                         expr->expression = parse_expression();
829                         loop->init_statement = expr;
830                 }
831                 tokenizer.expect(";");
832         }
833         if(tokenizer.peek_token()!=";")
834                 loop->condition = parse_expression();
835         tokenizer.expect(";");
836         if(tokenizer.peek_token()!=")")
837                 loop->loop_expression = parse_expression();
838         tokenizer.expect(")");
839
840         parse_block(loop->body, false, &Parser::parse_statement);
841
842         return loop;
843 }
844
845 RefPtr<Iteration> Parser::parse_while()
846 {
847         tokenizer.expect("while");
848         RefPtr<Iteration> loop = create_node<Iteration>();
849         tokenizer.expect("(");
850         loop->condition = parse_expression();
851         tokenizer.expect(")");
852
853         parse_block(loop->body, false, &Parser::parse_statement);
854
855         return loop;
856 }
857
858 RefPtr<Passthrough> Parser::parse_passthrough()
859 {
860         tokenizer.expect("passthrough");
861         RefPtr<Passthrough> pass = create_node<Passthrough>();
862         if(cur_stage->type==Stage::GEOMETRY)
863         {
864                 tokenizer.expect("[");
865                 pass->subscript = parse_expression();
866                 tokenizer.expect("]");
867         }
868         tokenizer.expect(";");
869         return pass;
870 }
871
872 RefPtr<Return> Parser::parse_return()
873 {
874         tokenizer.expect("return");
875         RefPtr<Return> ret = create_node<Return>();
876         if(tokenizer.peek_token()!=";")
877                 ret->expression = parse_expression();
878         tokenizer.expect(";");
879         return ret;
880 }
881
882 } // namespace SL
883 } // namespace GL
884 } // namespace Msp