19using namespace blender::gpu::shader::parser;
74#define NODE(type) NodeScope node(this, NodeType::type);
124 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected declaration");
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] !=
'\\') {
150 if (new_line != std::string::npos) {
155 curr = lex[tok_id + 1];
173 bool struct_decl(
bool expect_template_params =
false,
bool expect_body =
true)
180 if (peek() ==
'{' && !expect_template_params && expect_body) {
194 curr = at_semi_colon;
195 error(
"Forward declaration of classes is not supported");
199 if (peek() ==
Word) {
202 return next().is_valid();
211 return match(
';').is_valid();
247 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected member declaration");
256 auto state = save_state();
260 restore_state(
state);
267 std::string_view tok_str =
curr.
str();
268 if (!tok_str.starts_with(
"Pipeline")) {
273 if (tok_str !=
"PipelineGraphic" && tok_str !=
"PipelineCompute") {
291 auto state = save_state();
295 restore_state(
state);
297 if (peek() ==
Enum) {
307 auto state = save_state();
311 restore_state(
state);
316 bool var_decl(
bool is_member =
false,
bool expect_semicolon =
true)
335 error(
"Static keyword must be placed before constexpr");
347 }
while (match_if(
','));
350 bool valid = peek() ==
';' || peek() ==
')' || peek() ==
'}';
351 if (expect_semicolon) {
352 valid = match(
';').is_valid();
357 void declarator(
bool optional_id_name =
true,
bool optional_bitfield =
false)
360 bool par = match_if(
'(');
362 if (par || peek() ==
'&') {
368 if (optional_id_name) {
379 if (optional_bitfield) {
403 bool func_decl(
bool expect_template_params =
false,
bool expect_body =
true)
418 if (peek() ==
Const) {
424 return match(
';').is_valid();
450 error(
"enum declaration must explicitly use an underlying type");
481 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected enum value");
515 const bool designated = (peek() ==
'.');
543 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected initializer");
561 if (peek() ==
Const) {
568 }
while (match_if(
','));
581 }
while (match_if(
','));
627 func_decl(expect_template_params, expect_body);
657 error(
"Default arguments are not supported inside template declaration");
666 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected template argument");
690 }
while (match_if(
','));
701 void expression(
bool break_on_comma =
false,
bool break_on_equal =
false)
704 bool is_first_statement =
true;
723 if (peek_next(1) !=
':') {
779 if (break_on_equal) {
816 if (break_on_comma) {
834 if (is_first_statement) {
840 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected expression operand");
843 is_first_statement =
false;
868 bool has_brackets =
true;
869 if (optional_brackets) {
870 has_brackets = match_if(
'{');
928 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected local statement");
940 auto state = save_state();
941 if (
var_decl(
false, expect_semicolon)) {
945 restore_state(
state);
948 if (
curr.
str() ==
"auto" && peek_next(1) ==
'[') {
956 if (expect_semicolon) {
968 }
while (match_if(
','));
992 }
while (peek() ==
'[');
1000 if (peek() ==
'[') {
1001 error(
"Unexpected attribute specifier");
1004 if (!match_if(
']')) {
1012 if (peek() ==
'[') {
1029 if (peek() ==
'[') {
1037 if (peek() ==
'(') {
1045 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected attribute");
1070 while (peek() ==
'#') {
1074 while (peek() ==
Else) {
1075 if (peek_next(1) ==
If) {
1082 while (peek() ==
'#') {
1130 error(
"Unexpected token \"" + to_str(peek()) +
"\", expected switch case");
1167 for (
int i = 0; i < arg_needed &&
curr.
is_valid(); i++) {
1169 if (i != arg_needed - 1) {
1180 if (peek() ==
Word) {
1194 if (peek() ==
Word || (peek() ==
':' && peek_next(1) ==
':')) {
1206 if (peek() ==
':') {
1213 while (peek() ==
':' && peek_next(1) ==
':') {
1223 if (must_end_with_template_arg && !ends_with_template) {
1224 error(
"Expected template arguments");
1241 void error(
const std::string &
str)
1250 Token match(
char expected)
1253 error(
"Syntax Error: Expected token \"" + to_str(
TokenType(expected)) +
"\" but got \"" +
1261 Token match(
char expected,
char expected2)
1264 error(
"Syntax Error: Expected token \"" + to_str(
TokenType(expected)) +
"\" or \"" +
1272 Token match(
char expected,
char expected2,
char expected3)
1277 error(
"Syntax Error: Expected token \"" + to_str(
TokenType(expected)) +
"\" or \"" +
1279 "\" but got \"" + to_str(
curr.
type()) +
"\"");
1287 template<
typename... Args>
bool match_if(Args... expected)
1301 struct ParserState {
1310 parser->no_error =
false;
1316 bool no_error =
false;
1319 ParserState save_state()
1325 void restore_state(
const ParserState &
state)
std::vector< NodeData > Nodes
void report(Token tok, std::string_view message)
void identify_template_tokens()
void reset_template_tokens()
std::vector< ast::NodeData > ast_nodes
ast::LocalScope root() const
void parse_bsl(ErrorHandler &err_handler)
NodeScope(BSLParser *p, NodeType type)
void qualified_id(bool must_end_with_template_arg=false)
void attribute_optional()
void template_argument_list()
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 preprocessor_directive()
void function_argument_list()
void template_parameter_list()
void local_scope(bool optional_brackets=false)
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 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()
void numerical_constant()
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_
std::string_view str() const
Token(const TokenBuffer *buf, int32_t index)
const TokenType & type() const
Token next(int i=1) const