diff --git a/src/parsing.cpp b/src/parsing.cpp index f71e896..2d3326e 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -49,7 +49,7 @@ namespace parsing { } } - Result, std::string> parse_array_postfix(token::TokenStream& stream) { + Result, std::string> parse_array_postfix(token::TokenStream& stream, bool allow_empty) { token::TokenStream inner{ stream }; try { std::optional returned{}; @@ -58,6 +58,9 @@ namespace parsing { if (inner.peek().type == token::Type::LiteralInt) { returned = std::stoi(inner.next().content); } + if (!allow_empty && !returned.has_value()) { + throw std::runtime_error("Expected array size"); + } inner.expect(token::Type::Symbol, "]"); stream.m_position = inner.m_position; @@ -263,7 +266,7 @@ namespace parsing { auto ty = parse_type(inner).unwrap(); 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()) { auto postfix = array_postfix.unwrap(); if (postfix) { @@ -276,7 +279,7 @@ namespace parsing { new types::PointerType(ty) }; } - array_postfix = parse_array_postfix(inner); + array_postfix = parse_array_postfix(inner, false); } std::optional> expr = {};