Skip to content

fix(parser): allow line breaks in type annotations - #21

Merged
zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:fix/type-annotation-line-breaks
Oct 27, 2026
Merged

zombocoder merged 1 commit into
zombocoder:mainfrom
Craftegy-NextGen:fix/type-annotation-line-breaks

Conversation

@sashml

@sashml sashml commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A type annotation cannot continue on the next line:

xs:
    List<Int> = [1, 2, 3]      // SyntaxError: Expected type name

xs: List
    <Int> = [1, 2, 3]          // SyntaxError: Expected '=' after type

Line breaks inside the type arguments (after <, around ,) are already accepted, so long generic types can be wrapped there but not at the start of the type.

Fix

In Parser::parseTypeName:

  • Skip newlines before the type name.
  • Skip newlines between the type name and <, but only when a < follows them. The newline that ends a declaration such as property x: Int is left alone.

Every input that parsed before parses the same way. The change only accepts the two forms above, which used to be syntax errors.

Tests

In tests/test_parser.cpp:

  • ParserTest.TypeAnnotationLineBreakAfterColon and ParserTest.TypeAnnotationLineBreakBeforeTypeArguments check that the declared type is List<Int>. Both fail on current main.
  • ParserTest.TypeAtEndOfLineEndsDeclaration checks that a property type at the end of a line still ends that declaration.

There is also a CHANGELOG entry under [Unreleased], Fixed.

Verification

  • Windows 10, MSVC 2022, Release, Ninja, GoogleTest from vcpkg: build OK, o2l_tests 515/515 passing.
  • With the Parser.cpp change reverted, the two line-break tests fail with the errors shown above.
  • A program that uses both forms runs under o2l run and exits 0.
  • Linux and macOS: covered by CI.
  • clang-format (repository .clang-format) reports nothing on the changed lines.

A type annotation could not continue on the next line. `xs:` followed by
`List<Int> = ...` on the next line failed with "Expected type name", and
`xs: List` followed by `<Int> = ...` 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 <sashml@users.noreply.github.com>
@zombocoder
zombocoder merged commit 7d5f90c into zombocoder:main Oct 27, 2026
6 checks passed
@sashml
sashml deleted the fix/type-annotation-line-breaks branch October 27, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants