Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = ...` on the next line) and between the type name and its type arguments (`List` followed by `<Int>`). 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.
Expand Down
15 changes: 14 additions & 1 deletion src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = ...`
while (match(TokenType::NEWLINE)) {}

std::string type_name;

// Accept both identifiers and specific type keywords
Expand All @@ -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 <Int>`. 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<Int> and Map<Key, Value>
if (currentToken().type == TokenType::LESS_THAN) {
std::string base_type = type_name;
Expand Down
72 changes: 71 additions & 1 deletion tests/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

#include <gtest/gtest.h>

#include "AST/BlockNode.hpp"
#include "AST/EnumDeclarationNode.hpp"
#include "AST/ImportNode.hpp"
#include "AST/LiteralNode.hpp"
#include "AST/MethodDeclarationNode.hpp"
#include "AST/ObjectNode.hpp"
#include "AST/ProtocolDeclarationNode.hpp"
#include "AST/RecordDeclarationNode.hpp"
#include "AST/VariableDeclarationNode.hpp"
#include "Lexer.hpp"
#include "Parser.hpp"

Expand Down Expand Up @@ -529,4 +531,72 @@ TEST_F(ParserTest, VariableAssignmentEdgeCases) {
ASSERT_EQ(nodes.size(), 1);
auto object_node = dynamic_cast<ObjectNode*>(nodes[0].get());
ASSERT_NE(object_node, nullptr);
}
}

// The first statement of the first method of the only object, as a variable declaration
static const VariableDeclarationNode* firstVariableDeclaration(
const std::vector<ASTNodePtr>& nodes) {
if (nodes.size() != 1) return nullptr;
auto object_node = dynamic_cast<ObjectNode*>(nodes[0].get());
if (object_node == nullptr || object_node->getMethods().empty()) return nullptr;
auto method = dynamic_cast<MethodDeclarationNode*>(object_node->getMethods()[0].get());
if (method == nullptr) return nullptr;
auto body = dynamic_cast<BlockNode*>(method->getBody().get());
if (body == nullptr || body->getStatements().empty()) return nullptr;
return dynamic_cast<VariableDeclarationNode*>(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<Int> = [1, 2, 3]
return xs.size()
}
}
)");

auto declaration = firstVariableDeclaration(nodes);
ASSERT_NE(declaration, nullptr);
EXPECT_EQ(declaration->getVariableName(), "xs");
EXPECT_EQ(declaration->getTypeName(), "List<Int>");
}

// 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
<Int> = [1, 2, 3]
return xs.size()
}
}
)");

auto declaration = firstVariableDeclaration(nodes);
ASSERT_NE(declaration, nullptr);
EXPECT_EQ(declaration->getVariableName(), "xs");
EXPECT_EQ(declaration->getTypeName(), "List<Int>");
}

// 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<ObjectNode*>(nodes[0].get());
ASSERT_NE(object_node, nullptr);
EXPECT_EQ(object_node->getProperties().size(), 2);
EXPECT_EQ(object_node->getMethods().size(), 1);
}
Loading