extracted the parsing of the function parameters

into it's own function (will be useful for the
parsing of the parameters in the function calls)
This commit is contained in:
Karma Riuk
2025-07-15 01:18:00 +02:00
parent e2dfca2679
commit 4a9deff8da
2 changed files with 23 additions and 21 deletions

View File

@@ -248,6 +248,27 @@ namespace parser {
);
};
std::vector<ast::identifier*> parser::parse_function_parameters() {
std::vector<ast::identifier*> ret;
while (next.type != token::type::RPAREN
&& expect_next(token::type::IDENTIFIER)) {
ret.push_back(parse_identifier());
if (next.type == token::type::RPAREN)
break;
if (!expect_next(token::type::COMMA))
return {};
}
if (current.type == token::type::COMMA
&& next.type == token::type::RPAREN) {
next_error(token::type::IDENTIFIER);
return {};
}
if (!expect_next(token::type::RPAREN))
return {};
return ret;
}
ast::function_literal* parser::parse_function() {
TRACE_FUNCTION;
ast::function_literal* ret = new ast::function_literal(current);
@@ -256,27 +277,7 @@ namespace parser {
return nullptr;
}
while (next.type != token::type::RPAREN
&& expect_next(token::type::IDENTIFIER)) {
ret->parameters.push_back(parse_identifier());
if (next.type == token::type::RPAREN)
break;
if (!expect_next(token::type::COMMA)) {
delete ret;
return nullptr;
}
}
if (current.type == token::type::COMMA
&& next.type == token::type::RPAREN) {
next_error(token::type::IDENTIFIER);
delete ret;
return nullptr;
}
if (!expect_next(token::type::RPAREN)) {
delete ret;
return nullptr;
}
ret->parameters = parse_function_parameters();
if (!expect_next(token::type::LBRACE)) {
delete ret;

View File

@@ -60,6 +60,7 @@ namespace parser {
ast::identifier* parse_identifier();
ast::integer_literal* parse_integer();
ast::boolean_literal* parse_boolean();
std::vector<ast::identifier*> parse_function_parameters();
ast::function_literal* parse_function();
ast::prefix_expr* parse_prefix_expr();
ast::expression* parse_grouped_expr();