Skip to content

fix(compiler/tokenizer): track lines across multiline comments (#668)#669

Open
wilmveel wants to merge 2 commits into
masterfrom
fix/multiline-comment-line-tracking
Open

fix(compiler/tokenizer): track lines across multiline comments (#668)#669
wilmveel wants to merge 2 commits into
masterfrom
fix/multiline-comment-line-tracking

Conversation

@wilmveel

Copy link
Copy Markdown
Contributor

Summary

Fixes #668 — compile errors pointed to the wrong (earlier) line whenever a multiline comment preceded the offending token.

Comment tokens carry their full text as the token value. Single-line comments (^//.*\n) include a trailing newline, and multiline comments (/* ... */) include every embedded newline. However, Coordinates.nextCoordinates only advanced the line counter for NewLine tokens — comments fell through to the else branch, which advances only position/idx. Every line spanned by a multiline comment was therefore dropped from the line count, so any parser error reported after such a comment pointed to a line higher up in the file.

Fix

In Tokenizer.nextCoordinates, for non-NewLine tokens, count the newlines embedded in the token value. If there are any, advance line by that count and reset position to the column after the final newline. Tokens without embedded newlines keep the existing fast path.

Testing

  • Added ParserReferenceTest.shouldReportCorrectLineAfterMultiLineComment, which places an unresolvable reference after a 5-line block comment and asserts the error is reported on the correct line.
  • ./gradlew :src:compiler:core:jvmTest passes (existing coordinate assertions unaffected — idxAndLength accumulation is unchanged).

🤖 Generated with Claude Code

Comment tokens (both single-line `//...\n` and multiline `/* ... */`)
embed newlines in their value, but `nextCoordinates` only advanced the
line counter for `NewLine` tokens. As a result every line spanned by a
multiline comment was lost from the line count, so parser errors after
such a comment pointed to an earlier, incorrect line.

Count embedded newlines for non-`NewLine` tokens and advance the line
counter accordingly, resetting the column to the position after the
final newline.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@ZzAve ZzAve left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@sonarqubecloud

Copy link
Copy Markdown

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.

Compile errors point to odd locations when using multiline comments

2 participants