Fix parsing of empty arrays

This commit is contained in:
Sofia 2026-04-14 15:24:49 +03:00
parent 0400aa1d99
commit c0a2c41c33

View File

@ -49,7 +49,7 @@ namespace parsing {
} }
} }
Result<std::optional<uint32_t>, std::string> parse_array_postfix(token::TokenStream& stream) { Result<std::optional<uint32_t>, std::string> parse_array_postfix(token::TokenStream& stream, bool allow_empty) {
token::TokenStream inner{ stream }; token::TokenStream inner{ stream };
try { try {
std::optional<uint32_t> returned{}; std::optional<uint32_t> returned{};
@ -58,6 +58,9 @@ namespace parsing {
if (inner.peek().type == token::Type::LiteralInt) { if (inner.peek().type == token::Type::LiteralInt) {
returned = std::stoi(inner.next().content); returned = std::stoi(inner.next().content);
} }
if (!allow_empty && !returned.has_value()) {
throw std::runtime_error("Expected array size");
}
inner.expect(token::Type::Symbol, "]"); inner.expect(token::Type::Symbol, "]");
stream.m_position = inner.m_position; stream.m_position = inner.m_position;
@ -263,7 +266,7 @@ namespace parsing {
auto ty = parse_type(inner).unwrap(); auto ty = parse_type(inner).unwrap();
auto name = inner.expect(token::Type::Ident); auto name = inner.expect(token::Type::Ident);
auto array_postfix = parse_array_postfix(inner); auto array_postfix = parse_array_postfix(inner, false);
while (array_postfix.ok()) { while (array_postfix.ok()) {
auto postfix = array_postfix.unwrap(); auto postfix = array_postfix.unwrap();
if (postfix) { if (postfix) {
@ -276,7 +279,7 @@ namespace parsing {
new types::PointerType(ty) new types::PointerType(ty)
}; };
} }
array_postfix = parse_array_postfix(inner); array_postfix = parse_array_postfix(inner, false);
} }
std::optional<std::unique_ptr<AST::Expression>> expr = {}; std::optional<std::unique_ptr<AST::Expression>> expr = {};