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