reformulated the parsing of the parameters and
made it memory safe
This commit is contained in:
@@ -248,23 +248,42 @@ namespace parser {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void free_vec(std::vector<ast::identifier*> v) {
|
||||||
|
for (auto& e : v)
|
||||||
|
delete e;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<ast::identifier*> parser::parse_function_parameters() {
|
std::vector<ast::identifier*> parser::parse_function_parameters() {
|
||||||
|
if (next.type == token::type::RPAREN) {
|
||||||
|
next_token();
|
||||||
|
return {}; // no params
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<ast::identifier*> ret;
|
std::vector<ast::identifier*> ret;
|
||||||
while (next.type != token::type::RPAREN
|
if (!expect_next(token::type::IDENTIFIER))
|
||||||
&& expect_next(token::type::IDENTIFIER)) {
|
return {};
|
||||||
ret.push_back(parse_identifier());
|
ret.push_back(parse_identifier());
|
||||||
if (next.type == token::type::RPAREN)
|
|
||||||
break;
|
while (next.type == token::type::COMMA) {
|
||||||
if (!expect_next(token::type::COMMA))
|
next_token();
|
||||||
|
if (!expect_next(token::type::IDENTIFIER)) {
|
||||||
|
free_vec(ret);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
ret.push_back(parse_identifier());
|
||||||
|
}
|
||||||
|
|
||||||
if (current.type == token::type::COMMA
|
if (current.type == token::type::COMMA
|
||||||
&& next.type == token::type::RPAREN) {
|
&& next.type == token::type::RPAREN) {
|
||||||
next_error(token::type::IDENTIFIER);
|
next_error(token::type::IDENTIFIER);
|
||||||
|
free_vec(ret);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (!expect_next(token::type::RPAREN))
|
|
||||||
|
if (!expect_next(token::type::RPAREN)) {
|
||||||
|
free_vec(ret);
|
||||||
return {};
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@@ -53,13 +53,13 @@ TEST_SUITE("Parser: function") {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
SUBCASE("Missing comma with no closing paren one param") {
|
SUBCASE("Missing closing paren one param") {
|
||||||
test_failing_parsing("fn (x { return 2; }", {token::type::COMMA});
|
test_failing_parsing("fn (x { return 2; }", {token::type::RPAREN});
|
||||||
}
|
}
|
||||||
SUBCASE("Missing closing paren two params") {
|
SUBCASE("Missing closing paren two params") {
|
||||||
test_failing_parsing(
|
test_failing_parsing(
|
||||||
"fn (x, y { return 2; }",
|
"fn (x, y { return 2; }",
|
||||||
{token::type::COMMA}
|
{token::type::RPAREN}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user