From ca0ffc9d7f5ae56968c7029449b29b4dda1e1fbf Mon Sep 17 00:00:00 2001 From: sashml Date: Mon, 26 Oct 2026 14:19:39 +0100 Subject: [PATCH] fix(parser): allow line breaks in type annotations A type annotation could not continue on the next line. `xs:` followed by `List = ...` on the next line failed with "Expected type name", and `xs: List` followed by ` = ...` failed with "Expected '=' after type". Line breaks inside the type arguments were already accepted. parseTypeName now skips newlines before the type name, and skips newlines between the type name and '<' only when a '<' follows, so the newline that ends a declaration such as `property x: Int` is kept. Tests: ParserTest.TypeAnnotationLineBreakAfterColon and TypeAnnotationLineBreakBeforeTypeArguments (both fail without the change), and TypeAtEndOfLineEndsDeclaration. Signed-off-by: sashml --- CHANGELOG.md | 1 + src/Parser.cpp | 15 ++++++++- tests/test_parser.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4a2e34..01da2f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - **Integer division overflow crash**: `INT_MIN / -1` (and the 128-bit `LONG_MIN / -1`) previously aborted the interpreter with `SIGFPE`, because the signed-overflow case was unguarded next to the existing divide-by-zero check. It now raises a catchable evaluation error. `x % -1`, mathematically `0` for every `x` but undefined behavior at the type minimum, now returns `0` instead of triggering the same faulting division. +- **Line breaks in type annotations**: a type annotation may now continue on the next line after the `:` (`xs:` followed by `List = ...` on the next line) and between the type name and its type arguments (`List` followed by ``). Both previously failed with a syntax error (`Expected type name`, `Expected '=' after type`). Line breaks inside the type arguments already worked. ### 📦 Collection Extensions - **List/Map/Set Enhancements**: Added sorting, slicing, and bulk operations (`addAll`, `removeAll`) to all major collection types. diff --git a/src/Parser.cpp b/src/Parser.cpp index 90902ef..109315e 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -1844,6 +1844,9 @@ ASTNodePtr Parser::parseContinueStatement() { } std::string Parser::parseTypeName() { + // Allow the type name to start on the next line, e.g. after ':' in `xs:\n List = ...` + while (match(TokenType::NEWLINE)) {} + std::string type_name; // Accept both identifiers and specific type keywords @@ -1859,7 +1862,17 @@ std::string Parser::parseTypeName() { } else { throw SyntaxError("Expected type name at line " + std::to_string(currentToken().line)); } - + + // Allow line breaks between the type name and '<', e.g. `List\n `. Skip them only + // when a '<' follows, so the newline that ends a declaration like `property x: Int` stays. + size_t newlines = 0; + while (peekToken(newlines).type == TokenType::NEWLINE) { + newlines++; + } + if (newlines > 0 && peekToken(newlines).type == TokenType::LESS_THAN) { + while (match(TokenType::NEWLINE)) {} + } + // Handle generic types like List and Map if (currentToken().type == TokenType::LESS_THAN) { std::string base_type = type_name; diff --git a/tests/test_parser.cpp b/tests/test_parser.cpp index 9bc32da..3d7442c 100644 --- a/tests/test_parser.cpp +++ b/tests/test_parser.cpp @@ -16,6 +16,7 @@ #include +#include "AST/BlockNode.hpp" #include "AST/EnumDeclarationNode.hpp" #include "AST/ImportNode.hpp" #include "AST/LiteralNode.hpp" @@ -23,6 +24,7 @@ #include "AST/ObjectNode.hpp" #include "AST/ProtocolDeclarationNode.hpp" #include "AST/RecordDeclarationNode.hpp" +#include "AST/VariableDeclarationNode.hpp" #include "Lexer.hpp" #include "Parser.hpp" @@ -529,4 +531,72 @@ TEST_F(ParserTest, VariableAssignmentEdgeCases) { ASSERT_EQ(nodes.size(), 1); auto object_node = dynamic_cast(nodes[0].get()); ASSERT_NE(object_node, nullptr); -} \ No newline at end of file +} + +// The first statement of the first method of the only object, as a variable declaration +static const VariableDeclarationNode* firstVariableDeclaration( + const std::vector& nodes) { + if (nodes.size() != 1) return nullptr; + auto object_node = dynamic_cast(nodes[0].get()); + if (object_node == nullptr || object_node->getMethods().empty()) return nullptr; + auto method = dynamic_cast(object_node->getMethods()[0].get()); + if (method == nullptr) return nullptr; + auto body = dynamic_cast(method->getBody().get()); + if (body == nullptr || body->getStatements().empty()) return nullptr; + return dynamic_cast(body->getStatements()[0].get()); +} + +// Test a line break between ':' and the type name +TEST_F(ParserTest, TypeAnnotationLineBreakAfterColon) { + auto nodes = parse(R"( + Object TestObject { + method test(): Int { + xs: + List = [1, 2, 3] + return xs.size() + } + } + )"); + + auto declaration = firstVariableDeclaration(nodes); + ASSERT_NE(declaration, nullptr); + EXPECT_EQ(declaration->getVariableName(), "xs"); + EXPECT_EQ(declaration->getTypeName(), "List"); +} + +// Test a line break between the type name and its type arguments +TEST_F(ParserTest, TypeAnnotationLineBreakBeforeTypeArguments) { + auto nodes = parse(R"( + Object TestObject { + method test(): Int { + xs: List + = [1, 2, 3] + return xs.size() + } + } + )"); + + auto declaration = firstVariableDeclaration(nodes); + ASSERT_NE(declaration, nullptr); + EXPECT_EQ(declaration->getVariableName(), "xs"); + EXPECT_EQ(declaration->getTypeName(), "List"); +} + +// Test that a type at the end of a line still ends its declaration +TEST_F(ParserTest, TypeAtEndOfLineEndsDeclaration) { + auto nodes = parse(R"( + Object TestObject { + property count: Int + property name: Text + method test(): Int { + return 1 + } + } + )"); + + ASSERT_EQ(nodes.size(), 1); + auto object_node = dynamic_cast(nodes[0].get()); + ASSERT_NE(object_node, nullptr); + EXPECT_EQ(object_node->getProperties().size(), 2); + EXPECT_EQ(object_node->getMethods().size(), 1); +}