Blender V5.3
parser_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::parser;
10using namespace shader;
11using namespace std;
12
13TEST(shader_tool, Parser)
14{
17
18 ErrorHandler err_handler;
19
20 {
21 string input = R"(
221;
231.0;
242e10;
252e10f;
262.e10f;
272.0e-1f;
282.0e-1;
292.0e-1f;
300xFF;
310xFFu;
320+8;
33)";
34 string expect = R"(
351;1;1;1;1;1;1;1;1;1;1+1;)";
36 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
37 }
38 {
39 string input = R"(
40[[a(0,1,b), c, d(t)]]
41)";
42 string expect = R"(
43[[A(1,1,A),A,A(A)]])";
44 string scopes = R"(GABbcmmmbbcm)";
45 EXPECT_EQ(Parser(input, err_handler).token_types_str(), expect);
46 EXPECT_EQ(Parser(input, err_handler).scope_types_str, scopes);
47 }
48 {
49 string input = R"(
50template<typename F>;
51template<test<>>;
52template<>;
53template test<1, 2>;
54a << b;
55a >> b;
56a >= b;
57a == b;
58a <= b;
59a <<= b;
60a >>= b;
61)";
62 string expect = R"(
63t<BA>;t<A<>>;t<>;tA<1,1>;AwA;AWA;AGA;AEA;ALA;A5A;A6A;)";
64 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
65 }
66 {
67 string input = R"(
68struct T {
69 int t = 1;
70};
71class B {
72 T t;
73};
74)";
75 string expect = R"(
76sA{AA=1;};SA{AA;};)";
77 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
78 }
79 {
80 string input = R"(
81a /* Comment */
82//
83a
84/* //
85*/
86a
87// a
88a
89)";
90 string expect = R"(
91AZZAZAZA)";
92 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
93 }
94 {
95 string input = R"(
96namespace T {}
97namespace T::U::V {}
98)";
99 string expect = R"(
100nA{}nA::A::A{})";
101 string expect_scopes = R"(GNN)";
102 EXPECT_EQ(Parser(input, err_handler).token_types_str(), expect);
103 EXPECT_EQ(Parser(input, err_handler).scope_types_str, expect_scopes);
104 }
105 {
106 string input = R"(
107void f(int t = 0) {
108 int i = 0, u = 2, v = {1.0f};
109 {
110 v = i = u, v++;
111 if (v == i) {
112 return;
113 }
114 }
115}
116)";
117 string expect = R"(
118AA(AA=1){AA=1,A=1,A={1};{A=A=A,AP;i(AEA){r;}}})";
119 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
120 }
121 {
122 Parser parser("float i;", err_handler);
123 parser.insert_after(Token(parser, 0), "A ");
124 parser.insert_after(Token(parser, 0), "B ");
125 EXPECT_EQ(parser.result_get(), "float A B i;");
126 }
127 {
128 string input = R"(
129A
130#line 100
131B
132)";
133 Parser parser(input, err_handler);
134 string expect = R"(
135A#A1A)";
136 EXPECT_EQ(parser.token_types_str(), expect);
137
138 Token A = Token(parser, 1);
139 Token B = Token(parser, 5);
140
141 EXPECT_EQ(A.str(), "A");
142 EXPECT_EQ(B.str(), "B");
143 EXPECT_EQ(A.line_number(), 2);
144 EXPECT_EQ(B.line_number(), 100);
145 }
146 {
147 string input = R"(
148const bool foo;
149[[a]] int bar[0];
150)";
151
152 string expect = R"(
153match(, const, bool, , foo, , ;)
154match([a], , int, , bar, [0], ;)
155)";
156
157 Parser parser(input, err_handler);
158
159 string result = "\n";
160 parser().foreach_declaration([&](Scope attributes,
161 Token const_tok,
162 Token type,
163 Scope template_scope,
164 Token name,
165 Scope array,
166 Token decl_end) {
167 result += "match(";
168 result += string(attributes.str()) + ", ";
169 result += string(const_tok.str()) + ", ";
170 result += string(type.str()) + ", ";
171 result += string(template_scope.str()) + ", ";
172 result += string(name.str()) + ", ";
173 result += string(array.str()) + ", ";
174 result += string(decl_end.str()) + ")\n";
175 });
176
177 EXPECT_EQ(expect, result);
178 }
179}
180
181TEST(shader_tool, Cleanup)
182{
183 {
184 string input = R"(
185#line 2
186int b = 0;
187
188#if 0
189
190int a = 1;
191#elif 1
192#line 321
193#line 321
194int a = 0;
195#endif
196)";
197 string expect = R"(
198int b = 0;
199
200#if 0
201#elif 1
202#line 321
203int a = 0;
204#endif
205)";
206 auto [output, _, error] = process_test_string(input);
207 EXPECT_EQ(output, expect);
208 EXPECT_EQ(error, "");
209 }
210}
211
212} // namespace blender::gpu::tests
#define A
#define output
#define EXPECT_EQ(result, expect)
#define B
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337