Blender V5.3
grammar_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7namespace blender::gpu::tests {
8
9using namespace shader;
10using namespace std;
11
12struct TestParser {
15
16 string source;
17 string result;
20
21 TestParser(const string &src) : source(src), parser(err_handler)
22 {
23 parser.language = Language::BSL;
24 try {
26 }
27 catch (...) {
28 EXPECT_TRUE(err_handler.err.has_value());
29 }
30 }
31
32 string error() const
33 {
34 return err_handler.err.has_value() ? err_handler.err.value().message : "";
35 }
36
38 {
39 return parser.root();
40 }
41};
42
43TEST(bsl_grammar, EmptyTranslationUnit)
44{
45 TestParser parser(" \n\n ");
46 EXPECT_EQ(parser.error(), "");
47
48 EXPECT_TRUE(parser.root().is_valid());
49}
50
51TEST(bsl_grammar, PreprocessorDirectives)
52{
54 "#define MY_MACRO 1\n"
55 "#ifndef TEST\n"
56 "#define TEST_VAL 2.0\n"
57 "#endif\n");
58 EXPECT_EQ(parser.error(), "");
59}
60
61TEST(bsl_grammar, NamespaceDeclarations)
62{
64 "namespace blender::gpu::shader {\n"
65 " struct InnerStruct {};\n"
66 "}");
67 EXPECT_EQ(parser.error(), "");
68}
69
70TEST(bsl_grammar, UsingStatements)
71{
73 "using namespace blender::gpu;\n"
74 "using MyType = other::Namespace::Type;");
75 EXPECT_EQ(parser.error(), "");
76}
77
78TEST(bsl_grammar, StandardStructDeclaration)
79{
81 "struct MyStruct {\n"
82 " int field_a;\n"
83 " float field_b;\n"
84 "};");
85 EXPECT_EQ(parser.error(), "");
86}
87
88TEST(bsl_grammar, StructWithAccessSpecifiers)
89{
91 "class MyClass {\n"
92 " public:\n"
93 " int public_var;\n"
94 " private:\n"
95 " float private_var;\n"
96 "};");
97 EXPECT_EQ(parser.error(), "");
98}
99
100TEST(bsl_grammar, MemberKeywordsAndAttributes)
101{
102 // Verifies C++ / explicit host-shared patterns allowed by the grammar
104 "struct HostShared {\n"
105 " static const int constant_val = 42;\n"
106 " constexpr float pi = 3.14159;\n"
107 "};");
108 EXPECT_EQ(parser.error(), "");
109}
110
111TEST(bsl_grammar, PipelineDeclarations)
112{
114 "PipelineGraphic MyGraphicsPipeline(int sub_pass, float scale);\n"
115 "PipelineCompute MyComputePipeline(int work_groups);");
116 EXPECT_EQ(parser.error(), "");
117}
118
119TEST(bsl_grammar, TemplateDeclarationsAndInstantiations)
120{
122 "template <typename T, int N>\n"
123 "struct BufferArray {\n"
124 " T data[N];\n"
125 "};\n"
126 "template struct BufferArray<float, 4>;"); // Explicit instantiation
127 EXPECT_EQ(parser.error(), "");
128}
129
130TEST(bsl_grammar, FunctionDeclarationsAndDefinitions)
131{
133 "void compute_data(const int input, float &output) {\n"
134 " output = float(input) * 2.0;\n"
135 "}");
136 EXPECT_EQ(parser.error(), "");
137}
138
139TEST(bsl_grammar, ConditionalStatements)
140{
142 "void check_value(int x) {\n"
143 " if constexpr (true) {\n"
144 " x += 1;\n"
145 " } else if (x == 0) {\n"
146 " x = -1;\n"
147 " } else {\n"
148 " x = 0;\n"
149 " }\n"
150 "}");
151 EXPECT_EQ(parser.error(), "");
152}
153
154TEST(bsl_grammar, LoopConstructs)
155{
157 "void loops() {\n"
158 " for (int i = 0; i < 10; i++) { break; }\n"
159 " while (false) { continue; }\n"
160 " do { int x = 0; } while (false);\n"
161 "}");
162 EXPECT_EQ(parser.error(), "");
163}
164
165TEST(bsl_grammar, SwitchCaseParsing)
166{
168 "void handle_switch(int val) {\n"
169 " switch(val) {\n"
170 " case 1:\n"
171 " return;\n"
172 " case -2:\n"
173 " break;\n"
174 " default:\n"
175 " break;\n"
176 " }\n"
177 "}");
178 EXPECT_EQ(parser.error(), "");
179}
180
181TEST(bsl_grammar, Attributes)
182{
183 TestParser parser("[[nodiscard]] int structural_func();");
184 EXPECT_EQ(parser.error(), "");
185
186 TestParser unsupported("[[vendor::attribute(1, 2)]] int structural_func();");
187 EXPECT_EQ(unsupported.error(), "Unexpected token \":\", expected attribute");
188}
189
190TEST(bsl_grammar, Enum)
191{
192 {
194 "enum class Color : int {\n"
195 " Red = 0,\n"
196 " Green = 1,\n"
197 " Blue\n"
198 "};");
199 EXPECT_EQ(parser.error(), "");
200 }
201 {
203 "enum InvalidEnum {\n"
204 " ValueA\n"
205 "};");
206 EXPECT_EQ(parser.error(), "enum declaration must explicitly use an underlying type");
207 }
208}
209
210TEST(bsl_grammar, ForwardClassDeclarations)
211{
212 TestParser parser("struct ForwardDeclared;");
213 EXPECT_EQ(parser.error(), "Forward declaration of classes is not supported");
214}
215
216TEST(bsl_grammar, UnexpectedTokens)
217{
218 TestParser parser("namespace A { @invalid_token; }");
219 EXPECT_EQ(parser.error(), "Unexpected token \"@\": Expecting declaration");
220}
221
222} // namespace blender::gpu::tests
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
Definition processor.hh:45
parser::ErrorHandler ErrorHandler
Definition processor.hh:44
#define EXPECT_TRUE(result)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
@ Enum
Definition types.hh:106
void set_str(const std::string_view input)
SourceProcessor::Parser Parser
parser::ast::LocalScope root() const
SourceProcessor::ErrorHandler ErrorHandler