Blender V5.3
bsl/grammar.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include "ast.hh"
14#include "token.hh"
15#include "token_stream.hh"
16
17namespace bsl {
18using namespace std;
19using namespace blender::gpu::shader::parser;
20
21/*
22 * Simple Recursive Descent Parser that creates AST nodes.
23 *
24 * Follows the Blender Shading Language Specification.
25 */
26struct BSLParser {
33
35
37
40
41 struct NodeScope {
44 {
45 parser->push_node(type);
46 }
48 {
50 }
51 };
52
54 {
55 NodeID id = nodes.size();
56 nodes.emplace_back(nodes, id, curr_node, curr, type);
57 curr_node = id;
58 }
59
60 void pop_node()
61 {
62 assert(curr_node != -1);
63 nodes[curr_node].back = curr.index_ - 1;
64 curr_node = nodes[curr_node].parent;
65 }
66
68
73
74#define NODE(type) NodeScope node(this, NodeType::type);
75
77 {
79
80 /* Skip first whitespace token if it exists. */
81 if (peek() == NewLine || peek() == Space) {
82 next();
83 }
85 match(EndOfFile);
86 }
87
89 {
90 while (true) {
91 switch (peek()) {
92 case Hash:
94 break;
95 case Namespace:
97 break;
98 case Using:
100 break;
101 case Union:
102 case Class:
103 case Struct:
105 break;
106 case Enum:
107 enum_decl();
108 break;
109 case Template:
111 break;
112 case SquareOpen: /* Attr. */
113 case Const:
114 case Constexpr:
115 case Static:
116 case Inline:
117 case Word:
119 break;
120 case EndOfFile:
121 case BracketClose: /* For namespaces. */
122 return;
123 default:
124 error("Unexpected token \"" + to_str(peek()) + "\", expected declaration");
125 return;
126 }
127 }
128 }
129
130 /* Example: `#define A\n`. */
132 {
134 const LexerBase &lex = parser;
135
136 int tok_id = curr.index_;
137 while (true) {
138 const TokenType type = lex.types_[tok_id];
139 if (type == EndOfFile) {
140 tok_id--;
141 break;
142 }
143 std::string_view tok_str = lex[tok_id].str_with_whitespace();
144 size_t new_line = -1;
145 while ((new_line = tok_str.find("\n", new_line + 1)) != std::string::npos) {
146 if (new_line == 0 || tok_str[new_line - 1] != '\\') {
147 break;
148 }
149 }
150 if (new_line != std::string::npos) {
151 break;
152 }
153 tok_id++;
154 }
155 curr = lex[tok_id + 1];
156 }
157
158 /* Example: `namespace A::B {}`. */
160 {
162 match(Namespace);
163 qualified_id();
164 {
166 match('{');
168 match('}');
169 }
170 }
171
172 /* Example : `struct [[a]] A {}`. */
173 bool struct_decl(bool expect_template_params = false, bool expect_body = true)
174 {
176 match(Union, Struct, Class);
177 /* Optional attributes. */
179
180 if (peek() == '{' && !expect_template_params && expect_body) {
181 /* Nameless struct */
182 }
183 else {
184 qualified_id(expect_template_params);
185 if (!expect_body) {
186 match(';');
187 return curr.is_valid();
188 }
189 Token at_semi_colon = curr;
190 if (match_if(';')) {
191 if (!expect_body) {
192 return true; /* Template explicit instantiation. */
193 }
194 curr = at_semi_colon; /* For correct error token. */
195 error("Forward declaration of classes is not supported");
196 curr = at_semi_colon.next();
197 return false;
198 }
199 if (peek() == Word) {
200 /* Struct keyword usage in variable declaration. */
201 /* Supported because of explicit host shared struct members and C++ shared code. */
202 return next().is_valid();
203 }
204 }
205 {
207 match('{');
208 members_decl();
209 match('}');
210 }
211 return match(';').is_valid();
212 }
213
215 {
216 while (true) {
217 switch (peek()) {
218 case Hash:
220 break;
221 case Private:
222 case Public: {
224 next();
225 match(':');
226 break;
227 }
228 case Template:
230 break;
231 case Union:
232 case Class:
233 case Struct:
234 case Enum:
236 break;
237 case SquareOpen:
238 case Const:
239 case Static:
240 case Constexpr:
241 case Word:
242 func_or_var_decl(true);
243 break;
244 case BracketClose:
245 return;
246 default:
247 error("Unexpected token \"" + to_str(peek()) + "\", expected member declaration");
248 return;
249 }
250 }
251 }
252
254 {
255 {
256 auto state = save_state();
257 if (pipeline_decl()) {
258 return;
259 }
260 restore_state(state);
261 }
263 }
264
266 {
267 std::string_view tok_str = curr.str();
268 if (!tok_str.starts_with("Pipeline")) {
269 return false;
270 }
271
272 /* TODO(fclem): Maybe introduce a pipeline keyword. */
273 if (tok_str != "PipelineGraphic" && tok_str != "PipelineCompute") {
274 return false;
275 }
276
278 /* PipelineGraphic / PipelineCompute. */
279 qualified_id();
280 /* Name. */
281 qualified_id();
283 match(';');
284 return true;
285 }
286
287 void struct_decl_or_var_decl(bool is_member = false)
288 {
289 {
290 /* C++ explicit member type. Needed for legacy code-style. */
291 auto state = save_state();
292 if (var_decl(is_member)) {
293 return;
294 }
295 restore_state(state);
296 }
297 if (peek() == Enum) {
298 enum_decl();
299 return;
300 }
301 struct_decl();
302 }
303
304 void func_or_var_decl(bool is_member = false)
305 {
306 {
307 auto state = save_state();
308 if (var_decl(is_member)) {
309 return;
310 }
311 restore_state(state);
312 }
313 func_decl();
314 }
315
316 bool var_decl(bool is_member = false, bool expect_semicolon = true)
317 {
318 NODE(VarDecl);
320 {
321 NODE(IdType);
322 if (peek() == Word && curr.str() == "constant") {
323 /* MSL constant keyword. Used for enum values. */
324 next();
325 }
326 if (peek() == Static) {
328 match_if(Static);
329 }
330 if (peek() == Const || peek() == Constexpr) {
331 NODE(Const);
332 match_if(Const, Constexpr);
333
334 if (peek() == Static) {
335 error("Static keyword must be placed before constexpr");
336 return true;
337 }
338 }
339 if (is_member) {
340 /* Supported because of explicit host shared struct members. */
341 match_if(Struct, Class, Enum);
342 }
343 qualified_id();
344 }
345 do {
346 declarator(false, is_member);
347 } while (match_if(','));
348 /* Check if current token is valid. Otherwise we could be at end of file after the last
349 * semicolon. */
350 bool valid = peek() == ';' || peek() == ')' || peek() == '}';
351 if (expect_semicolon) {
352 valid = match(';').is_valid();
353 }
354 return valid;
355 }
356
357 void declarator(bool optional_id_name = true, bool optional_bitfield = false)
358 {
360 bool par = match_if('(');
361 /* NOTE: Require Ampersand after parenthesis to avoid mistaking the decl with a func call. */
362 if (par || peek() == '&') {
364 match('&');
365 }
366 /* Note that these should be unqualified. But for simplicity of the Symbol Table API we parse
367 * them as qualified. */
368 if (optional_id_name) {
370 }
371 else {
372 qualified_id();
373 }
374
375 if (par) {
376 match(')');
377 }
378
379 if (optional_bitfield) {
381 }
382
383 if (peek() == '{') {
385 }
386 else {
389 }
390 }
391
393 {
394 if (peek() != ':') {
395 return;
396 }
397 NODE(BitField);
398 match_if(Colon);
399 /* Stop at a default member initializer, it is not part of the bit width. */
400 expression(true, true);
401 }
402
403 bool func_decl(bool expect_template_params = false, bool expect_body = true)
404 {
405 NODE(FuncDecl);
407 if (peek() == Static) {
409 match_if(Static);
410 }
411 match_if(Inline);
412 {
413 NODE(IdType);
414 qualified_id();
415 }
416 qualified_id(expect_template_params);
418 if (peek() == Const) {
419 NODE(Const);
420 match(Const);
421 }
422 if (!expect_body) {
423 nodes[curr_node].type = NodeType::FuncForwardDecl;
424 return match(';').is_valid();
425 }
426 if (match_if(';')) {
427 /* Template instantiation or forward declaration. */
428 nodes[curr_node].type = NodeType::FuncForwardDecl;
429 return true;
430 }
431 if (peek() == '{') {
432 local_scope();
433 return true;
434 }
435 return false;
436 }
437
438 /* Example: `enum [[a]] A : a {}`. */
440 {
442 match(Enum);
443 /* Optional class qualifier. */
444 match_if(Class);
445 /* Optional attributes. */
447 /* Optional name. */
449 if (peek() != ':') {
450 error("enum declaration must explicitly use an underlying type");
451 return;
452 }
453 /* Underlying type. */
454 match(':');
455 qualified_id();
456
457 enum_values();
458 match(';');
459 }
460
462 {
464 match('{');
465 while (true) {
466 switch (peek()) {
467 case Hash:
469 break;
470 case Word: {
472 qualified_id();
474 match_if(',');
475 break;
476 }
477 case BracketClose:
478 match('}');
479 return;
480 default:
481 error("Unexpected token \"" + to_str(peek()) + "\", expected enum value");
482 return;
483 }
484 }
485 }
486
488 {
489 if (peek() == '=') {
490 assignment();
491 }
492 }
493
495 {
497 match('=');
499 }
500
502 {
503 if (peek() == '{') {
505 }
506 else {
507 expression(true);
508 }
509 }
510
512 {
514 match('{');
515 const bool designated = (peek() == '.');
516
517 while (true) {
518 switch (peek()) {
519 case BracketClose:
520 match('}');
521 return;
522 case Dot:
523 case This:
524 case Word:
525 case Number:
526 case Minus:
527 case Plus:
528 case ParOpen:
529 case BracketOpen:
530 if (designated) {
532 match('.');
534 assignment();
535 }
536 else {
539 }
540 match_if(',');
541 break;
542 default:
543 error("Unexpected token \"" + to_str(peek()) + "\", expected initializer");
544 return;
545 }
546 }
547 }
548
550 {
552 match('(');
553 if (match_if(')')) {
554 return;
555 }
556 do {
557 NODE(FuncArg);
559 {
560 NODE(IdType);
561 if (peek() == Const) {
562 NODE(Const);
563 match(Const);
564 }
565 qualified_id();
566 }
567 declarator();
568 } while (match_if(','));
569 match(')');
570 }
571
573 {
575 match('(');
576 if (match_if(')')) {
577 return;
578 }
579 do {
580 expression(true);
581 } while (match_if(','));
582 match(')');
583 }
584
586 {
587 if (peek_next(1) != TemplateOpen) {
589 }
590 else if (peek_next(2) == TemplateClose) {
592 }
593 else {
595 }
596 }
597
599 {
601 match(Template);
602 templated_declaration(true, false);
603 }
604
606 {
608 match(Template);
609 template_argument_list(); /* Should be empty. */
610 templated_declaration(true, true);
611 }
612
614 {
616 match(Template);
618 templated_declaration(false, true);
619 }
620
621 void templated_declaration(bool expect_template_params, bool expect_body)
622 {
623 if (peek() == Union || peek() == Struct || peek() == Class) {
624 struct_decl(expect_template_params, expect_body);
625 }
626 else {
627 func_decl(expect_template_params, expect_body);
628 }
629 }
630
632 {
634 match(TemplateOpen);
635 while (true) {
636 switch (peek()) {
637 case Typename: {
638 {
640 match(Typename);
641 qualified_id(); /* ID. */
642 }
643 match_if(',');
644 break;
645 }
646 case Struct:
647 case Class:
648 case Enum:
649 case Word: {
650 {
652 match_if(Struct, Class, Enum);
653 qualified_id(); /* Type. */
654 qualified_id(); /* ID. */
655 }
656 if (peek() == '=') {
657 error("Default arguments are not supported inside template declaration");
658 }
659 match_if(',');
660 break;
661 }
662 case TemplateClose:
663 match(TemplateClose);
664 return;
665 default:
666 error("Unexpected token \"" + to_str(peek()) + "\", expected template argument");
667 return;
668 }
669 }
670 }
671
673 {
674 if (peek() == TemplateOpen) {
676 return true;
677 }
678 return false;
679 }
680
682 {
684 match(TemplateOpen);
685 if (match_if(TemplateClose)) {
686 return;
687 }
688 do {
689 expression(true);
690 } while (match_if(','));
691 match(TemplateClose);
692 }
693
695 {
696 NODE(NumConst);
697 match_if(Minus);
698 match(Number);
699 }
700
701 void expression(bool break_on_comma = false, bool break_on_equal = false)
702 {
703 NODE(Expr);
704 bool is_first_statement = true;
705 while (true) {
706 switch (peek()) {
707 case SquareOpen:
708 subscript();
709 break;
710 case Template: {
711 {
713 match(Template);
714 {
715 NODE(FuncCall);
716 qualified_id(true);
718 }
719 }
720 break;
721 }
722 case Colon:
723 if (peek_next(1) != ':') {
724 /* This is the end of a scope. */
725 return;
726 }
727 /* This is the start of an identifier. */
728 [[fallthrough]];
729 case Word:
731 break;
732 case ParOpen: {
733 NODE(ExprSub);
734 match('(');
735 expression();
736 match(')');
737 break;
738 }
739 case String: {
741 match(String);
742 break;
743 }
744 case This: {
745 NODE(LocalVar);
746 {
748 match(This);
749 }
750 break;
751 }
752 case Number: {
753 NODE(NumConst);
754 match(Number);
755 break;
756 }
757 case Question: {
758 {
759 NODE(Op);
760 match('?');
761 }
762 expression();
763 {
764 NODE(Op);
765 match(':');
766 }
767 break;
768 }
769 case Minus: {
770 NODE(Op);
771 next();
772 if (peek() == '>') {
773 nodes[curr_node].type = NodeType::OpDeref;
774 next();
775 }
776 break;
777 }
778 case Assign:
779 if (break_on_equal) {
780 return;
781 }
782 [[fallthrough]];
783 case AssignAdd:
784 case AssignSub:
785 case AssignMul:
786 case AssignDiv:
787 case AssignLShift:
788 case AssignRShift:
789 case Ampersand:
790 case BitwiseNot:
791 case Decrement:
792 case Dot:
793 case Divide:
794 case Equal:
795 case GEqual:
796 case GThan:
797 case Increment:
798 case LEqual:
799 case LogicalAnd:
800 case LogicalOr:
801 case LThan:
802 case Modulo:
803 case Multiply:
804 case Not:
805 case NotEqual:
806 case Or:
807 case Plus:
808 case LShift:
809 case RShift:
810 case Xor: {
811 NODE(Op);
812 next();
813 break;
814 }
815 case Comma: {
816 if (break_on_comma) {
817 return;
818 }
819 /* Comma operator. */
820 NODE(Op);
821 match(',');
822 break;
823 }
824 case Struct: /* Legacy BSL `union_t<struct A>` syntax. Support for now. */
825 next();
826 break;
827 case BracketClose:
828 case SquareClose:
829 case TemplateClose:
830 case SemiColon:
831 case ParClose:
832 return;
833 case BracketOpen:
834 if (is_first_statement) {
836 break;
837 }
838 [[fallthrough]];
839 default:
840 error("Unexpected token \"" + to_str(peek()) + "\", expected expression operand");
841 return;
842 }
843 is_first_statement = false;
844 }
845 }
846
848 {
849 NODE(LocalVar);
850 qualified_id();
851 switch (peek()) {
852 case ParOpen:
853 nodes[curr_node].type = NodeType::FuncCall;
855 break;
856 case BracketOpen:
857 nodes[curr_node].type = NodeType::Constructor;
859 break;
860 default:
861 break;
862 }
863 }
864
865 void local_scope(bool optional_brackets = false)
866 {
868 bool has_brackets = true;
869 if (optional_brackets) {
870 has_brackets = match_if('{');
871 }
872 else {
873 match('{');
874 }
875 while (true) {
876 switch (peek()) {
877 case Hash:
879 break;
880 case BracketOpen:
881 local_scope();
882 break;
883 case For:
884 for_loop();
885 break;
886 case While:
887 while_loop();
888 break;
889 case Do:
891 break;
892 case Switch:
894 break;
895 case If:
896 if_statement();
897 break;
898 case Continue: {
900 match(Continue);
901 match(';');
902 break;
903 }
904 case Break: {
906 match(Break);
907 match(';');
908 break;
909 }
910 case Return: {
912 match(Return);
913 expression();
914 match(';');
915 break;
916 }
917 case Using:
919 break;
920 case Default:
921 case Case:
922 case BracketClose:
923 if (has_brackets) {
924 match('}');
925 }
926 return;
927 case EndOfFile:
928 error("Unexpected token \"" + to_str(peek()) + "\", expected local statement");
929 return;
930 default:
932 break;
933 }
934 }
935 }
936
937 void local_statement(bool expect_semicolon = true)
938 {
939 {
940 auto state = save_state();
941 if (var_decl(false, expect_semicolon)) {
942 return;
943 }
944 /* Restore state after failing to parse a declaration. */
945 restore_state(state);
946 }
947
948 if (curr.str() == "auto" && peek_next(1) == '[') {
950 return;
951 }
952
955 expression();
956 if (expect_semicolon) {
957 match(';');
958 }
959 }
960
962 {
964 match(Word); /* auto */
965 match('[');
966 do {
968 } while (match_if(','));
969 match(']');
970 assignment();
971 match(';');
972 }
973
975 {
977 match(Using);
978 match_if(Namespace);
979 qualified_id();
980 if (match_if(Assign)) {
981 qualified_id();
982 }
983 match(';');
984 }
985
987 {
988 if (peek() == '[') {
990 do {
991 subscript();
992 } while (peek() == '[');
993 }
994 }
995
997 {
999 match('[');
1000 if (peek() == '[') {
1001 error("Unexpected attribute specifier");
1002 return;
1003 }
1004 if (!match_if(']')) {
1005 expression(true);
1006 match(']');
1007 }
1008 }
1009
1011 {
1012 if (peek() == '[') {
1013 attribute();
1014 }
1015 }
1016
1018 {
1019 NODE(AttrList);
1020 match('[');
1021 match('[');
1022
1023 while (true) {
1024 switch (peek()) {
1025 case SquareClose:
1026 match(']');
1027 match(']');
1028 /* Attrs can be chained. Nest them so we can make AST traversal easier. */
1029 if (peek() == '[') {
1030 attribute();
1031 }
1032 return;
1033 case Word: {
1034 {
1035 NODE(Attr);
1037 if (peek() == '(') {
1039 }
1040 }
1041 match_if(',');
1042 break;
1043 }
1044 default:
1045 error("Unexpected token \"" + to_str(peek()) + "\", expected attribute");
1046 return;
1047 }
1048 }
1049 }
1050
1051 /* Example: `union {}`. */
1053 {
1054 NODE(ClassDecl);
1055 match(Union);
1056 match('{');
1057 members_decl();
1058 match('}');
1059 match(';');
1060 }
1061
1063 {
1064 {
1065 NODE(IfStmt);
1066 match(If);
1067 match_if(Constexpr);
1068 condition(1);
1069 local_scope();
1070 while (peek() == '#') {
1072 }
1073 }
1074 while (peek() == Else) {
1075 if (peek_next(1) == If) {
1077 match(Else);
1078 match(If);
1079 match_if(Constexpr);
1080 condition(1);
1081 local_scope();
1082 while (peek() == '#') {
1084 }
1085 }
1086 else {
1087 NODE(ElseStmt);
1088 match(Else);
1089 local_scope();
1090 break;
1091 }
1092 }
1093 }
1094
1096 {
1098 match(Switch);
1099 condition(1);
1100 match('{');
1101 while (true) {
1102 switch (peek()) {
1103 case Hash:
1105 break;
1106 case Case: {
1108 match(Case);
1109 if (peek() == Number || peek() == Minus) {
1111 }
1112 else {
1113 qualified_id();
1114 }
1115 match(Colon);
1116 local_scope(true);
1117 break;
1118 }
1119 case Default: {
1121 match(Default);
1122 match(Colon);
1123 local_scope(true);
1124 break;
1125 }
1126 case BracketClose:
1127 match('}');
1128 return;
1129 default:
1130 error("Unexpected token \"" + to_str(peek()) + "\", expected switch case");
1131 return;
1132 }
1133 }
1134 }
1135
1137 {
1138 NODE(ForLoop);
1139 match(For);
1140 condition(3);
1141 local_scope();
1142 }
1143
1145 {
1146 NODE(WhileLoop);
1147 match(While);
1148 condition(1);
1149 local_scope();
1150 }
1151
1153 {
1155 match(Do);
1156 local_scope();
1157 match(While);
1158 condition(1);
1159 match(';');
1160 }
1161
1162 void condition(int arg_needed)
1163 {
1164 {
1165 NODE(Condition);
1166 match('(');
1167 for (int i = 0; i < arg_needed && curr.is_valid(); i++) {
1168 local_statement(false);
1169 if (i != arg_needed - 1) {
1170 match(';');
1171 }
1172 }
1173 match(')');
1174 }
1176 }
1177
1179 {
1180 if (peek() == Word) {
1182 }
1183 }
1184
1185 /* Example: `a3234`. */
1187 {
1188 NODE(Id);
1189 match(Word);
1190 }
1191
1193 {
1194 if (peek() == Word || (peek() == ':' && peek_next(1) == ':')) {
1195 qualified_id();
1196 return true;
1197 }
1198 return false;
1199 }
1200
1201 /* Example: `A<1>::B<2>`. */
1202 void qualified_id(bool must_end_with_template_arg = false)
1203 {
1205 /* Global scope specifier. */
1206 if (peek() == ':') {
1208 match(':');
1209 match(':');
1210 }
1212 bool ends_with_template = template_parameter_list_optional();
1213 while (peek() == ':' && peek_next(1) == ':') {
1214 {
1216 match(':');
1217 match(':');
1218 }
1220 ends_with_template = template_parameter_list_optional();
1221 }
1222
1223 if (must_end_with_template_arg && !ends_with_template) {
1224 error("Expected template arguments");
1225 }
1226 }
1227
1228#undef NODE
1229
1230 private:
1231 TokenType peek() const
1232 {
1233 return curr.type();
1234 }
1235
1236 TokenType peek_next(int i) const
1237 {
1238 return curr.next(i).type();
1239 }
1240
1241 void error(const std::string &str)
1242 {
1243 if (!no_error) {
1245 }
1246 /* Set token to EndOfFile/Invalid. */
1247 curr = Token(parser);
1248 }
1249
1250 Token match(char expected)
1251 {
1252 if (curr != TokenType(expected)) {
1253 error("Syntax Error: Expected token \"" + to_str(TokenType(expected)) + "\" but got \"" +
1254 to_str(curr.type()) + "\"");
1255 }
1256 Token tok = curr;
1257 next();
1258 return tok;
1259 }
1260
1261 Token match(char expected, char expected2)
1262 {
1263 if (curr != TokenType(expected) && curr != TokenType(expected2)) {
1264 error("Syntax Error: Expected token \"" + to_str(TokenType(expected)) + "\" or \"" +
1265 to_str(TokenType(expected2)) + "\" but got \"" + to_str(curr.type()) + "\"");
1266 }
1267 Token tok = curr;
1268 next();
1269 return tok;
1270 }
1271
1272 Token match(char expected, char expected2, char expected3)
1273 {
1274 if (curr != TokenType(expected) && curr != TokenType(expected2) &&
1275 curr != TokenType(expected3))
1276 {
1277 error("Syntax Error: Expected token \"" + to_str(TokenType(expected)) + "\" or \"" +
1278 to_str(TokenType(expected2)) + "\" or \"" + to_str(TokenType(expected3)) +
1279 "\" but got \"" + to_str(curr.type()) + "\"");
1280 }
1281 Token tok = curr;
1282 next();
1283 return tok;
1284 }
1285
1286 /* Only go to next token if matching an optional token. */
1287 template<typename... Args> bool match_if(Args... expected)
1288 {
1289 if (((curr == TokenType(expected)) || ...)) {
1290 next();
1291 return true;
1292 }
1293 return false;
1294 }
1295
1296 Token next()
1297 {
1298 return curr = curr.next();
1299 }
1300
1301 struct ParserState {
1302 BSLParser *parser;
1303 Token curr;
1304 NodeID curr_node;
1305 size_t node_count;
1306 Node node;
1307
1308 ~ParserState()
1309 {
1310 parser->no_error = false;
1311 }
1312 };
1313
1314 /* Reporting error is super costly (has to find the source filename and line).
1315 * Disable it when doing backtracing. */
1316 bool no_error = false;
1317
1318 /* No error can be generated until the ParserState is destroyed. */
1319 ParserState save_state()
1320 {
1321 no_error = true;
1322 return {this, curr, curr_node, nodes.size(), nodes[curr_node]};
1323 }
1324
1325 void restore_state(const ParserState &state)
1326 {
1327 curr = state.curr;
1328 curr_node = state.curr_node;
1329 nodes.resize(state.node_count);
1330 nodes[curr_node] = state.node;
1332 }
1333};
1334
1335} // namespace bsl
1336
1338
1340{
1341 LexerBase &lex = *this;
1342
1344
1345 bsl::BSLParser p(*this, lex[0], err_handler);
1346 p.translation_unit();
1347
1348 this->ast_nodes = std::move(p.nodes);
1349 /* Add a trailing Node to ease iterator implementations. */
1350 this->ast_nodes.emplace_back();
1351
1353}
1354
1356{
1357 ast::Node(this, 0).print_ast();
1358}
1359
1361{
1362 return ast::LocalScope(ast::Node(this, 0));
1363}
1364
1365} // namespace blender::gpu::shader::parser
#define NODE(offset)
#define str(s)
const int state
std::vector< NodeData > Nodes
Definition ast.hh:94
Definition codegen.cc:18
TokenType
Definition types.hh:41
@ RShift
Definition types.hh:126
@ GThan
Definition types.hh:149
@ ParOpen
Definition types.hh:53
@ Default
Definition types.hh:129
@ While
Definition types.hh:92
@ EndOfFile
Definition types.hh:77
@ Private
Definition types.hh:123
@ AssignSub
Definition types.hh:135
@ LThan
Definition types.hh:150
@ For
Definition types.hh:91
@ BracketOpen
Definition types.hh:55
@ LogicalOr
Definition types.hh:93
@ Return
Definition types.hh:115
@ Do
Definition types.hh:87
@ Hash
Definition types.hh:49
@ Not
Definition types.hh:64
@ Static
Definition types.hh:105
@ Enum
Definition types.hh:106
@ Struct
Definition types.hh:117
@ Union
Definition types.hh:109
@ AssignAdd
Definition types.hh:133
@ SquareOpen
Definition types.hh:57
@ Question
Definition types.hh:63
@ Modulo
Definition types.hh:152
@ Space
Definition types.hh:47
@ LogicalAnd
Definition types.hh:81
@ Ampersand
Definition types.hh:50
@ BitwiseNot
Definition types.hh:151
@ Multiply
Definition types.hh:145
@ Dot
Definition types.hh:48
@ Switch
Definition types.hh:95
@ ParClose
Definition types.hh:54
@ AssignLShift
Definition types.hh:138
@ Case
Definition types.hh:96
@ Else
Definition types.hh:98
@ AssignRShift
Definition types.hh:139
@ LEqual
Definition types.hh:104
@ TemplateOpen
Definition types.hh:130
@ SquareClose
Definition types.hh:58
@ Plus
Definition types.hh:68
@ Break
Definition types.hh:83
@ Colon
Definition types.hh:65
@ Continue
Definition types.hh:110
@ TemplateClose
Definition types.hh:131
@ Using
Definition types.hh:121
@ Decrement
Definition types.hh:88
@ Increment
Definition types.hh:112
@ Constexpr
Definition types.hh:86
@ AssignDiv
Definition types.hh:137
@ NotEqual
Definition types.hh:89
@ String
Definition types.hh:154
@ If
Definition types.hh:97
@ NewLine
Definition types.hh:46
@ Number
Definition types.hh:44
@ Equal
Definition types.hh:90
@ Divide
Definition types.hh:70
@ Xor
Definition types.hh:148
@ SemiColon
Definition types.hh:62
@ LShift
Definition types.hh:125
@ Inline
Definition types.hh:103
@ Assign
Definition types.hh:61
@ Minus
Definition types.hh:69
@ Word
Definition types.hh:43
@ This
Definition types.hh:120
@ Typename
Definition types.hh:84
@ AssignMul
Definition types.hh:136
@ Public
Definition types.hh:124
@ GEqual
Definition types.hh:94
@ BracketClose
Definition types.hh:56
@ Or
Definition types.hh:147
@ Template
Definition types.hh:119
@ Class
Definition types.hh:118
@ Comma
Definition types.hh:66
void report(Token tok, std::string_view message)
std::vector< ast::NodeData > ast_nodes
void parse_bsl(ErrorHandler &err_handler)
NodeScope(BSLParser *p, NodeType type)
ParserBase & parser
void external_decl()
void qualified_id(bool must_end_with_template_arg=false)
ast::NodeID NodeID
void attribute_optional()
void template_argument_list()
void switch_statement()
void pipeline_or_func_or_var_decl()
void push_node(NodeType type)
bool var_decl(bool is_member=false, bool expect_semicolon=true)
void function_parameter_list()
void initializer_list()
void preprocessor_directive()
void function_argument_list()
void template_parameter_list()
void namespace_decl()
void local_scope(bool optional_brackets=false)
ast::NodeType NodeType
void translation_unit()
blender::gpu::shader::parser::Token Token
void template_instantiation()
void func_or_var_decl(bool is_member=false)
bool template_parameter_list_optional()
void local_statement(bool expect_semicolon=true)
BSLParser(ParserBase &parser, Token tok, ErrorHandler &error_handler)
bool struct_decl(bool expect_template_params=false, bool expect_body=true)
void condition(int arg_needed)
void structured_bindings()
void expression(bool break_on_comma=false, bool break_on_equal=false)
void assignment_optional()
ErrorHandler & error_handler
bool func_decl(bool expect_template_params=false, bool expect_body=true)
void declarator(bool optional_id_name=true, bool optional_bitfield=false)
void bitfield_optional()
void using_statement()
void unqualified_id_optional()
void template_specialization()
void struct_decl_or_var_decl(bool is_member=false)
void init_expression_or_initializer_list()
void template_declaration()
bool qualified_id_optional()
ast::TokenID TokenID
ast::NodeData Node
void numerical_constant()
void array_optional()
ast::Nodes Nodes
void function_call_or_id_or_initializer()
void templated_declaration(bool expect_template_params, bool expect_body)
void template_decl_or_spec_or_inst()
AlignedArrayPtr< TokenType > types_
Definition lexit.hh:125
std::string_view str() const
Definition lexit.hh:381
int32_t index_
Definition lexit.hh:51
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337
const TokenType & type() const
Definition lexit.hh:354
bool is_valid() const
Definition lexit.hh:65
Token next(int i=1) const
Definition lexit.hh:372