Skip to content

Support Java 15 text blocks (#64) - #161

Merged
paulirwin merged 1 commit into
masterfrom
issue/64
Aug 16, 2026
Merged

Support Java 15 text blocks (#64)#161
paulirwin merged 1 commit into
masterfrom
issue/64

Conversation

@paulirwin

Copy link
Copy Markdown
Owner

Fixes #64.

Converts Java 15 text blocks to C# raw string literals.

The bug

TextBlockLiteralExpr derives from LiteralStringValueExpr, not StringLiteralExpr. Because the visitor registry walks the base-type chain, text blocks matched no visitor and conversion threw NotImplementedException — a hard failure rather than a silent mis-conversion.

The fix

A new TextBlockLiteralExpressionVisitor plus one registry entry. Two details worth calling out, both verified against the parser rather than assumed:

  1. asString() already applies the text block rules — it strips incidental whitespace and resolves escapes, including \s and trailing-backslash line continuations. So the value must not be unescaped again, unlike StringLiteralExpressionVisitor, which calls Regex.Unescape. Doing so here would double-unescape and corrupt \\.

  2. Trailing newline handling. A Java text block ending in a newline keeps it, while C# treats the newline before the closing delimiter as part of the delimiter. The value is therefore written verbatim with the closing fence on its own line.

The delimiter is sized one quote longer than the longest run of quotes in the content, so text containing "" still converts to valid C#.

Testing

  • 4 new unit tests in VisitLiteralExpressionTests covering de-indentation, escapes, and embedded quotes.
  • New Resources/Java15TextBlocks.java added to the integration suite, which compiles and executes the generated C# and compares stdout — so the converted output is confirmed to match Java's runtime semantics.
  • Full suite: 291/291 passing; solution builds with 0 warnings (TreatWarningsAsErrors is on).

Known cosmetic limitation

The emitted literal's content sits flush-left rather than indented to match surrounding code, and carries a required blank line before the closing fence. Both are semantically correct. NormalizeWhitespace() does not re-indent raw string literals (they are a single verbatim token) and the visitor cannot know its final indentation, so indenting would risk changing the string's value. Happy to follow up with a post-formatting pass if you'd like it prettier.

🤖 Generated with Claude Code

Convert Java text blocks to C# raw string literals.

TextBlockLiteralExpr derives from LiteralStringValueExpr rather than
StringLiteralExpr, so the base-type walk in the visitor registry found no
match and conversion threw NotImplementedException.

Use asString() for the value, which already applies the text block rules:
incidental whitespace is stripped and escapes (including \s and trailing
backslash line continuations) are resolved. That means the value must not
be unescaped again, unlike StringLiteralExpressionVisitor.

The generated delimiter is one quote longer than the longest run of quotes
in the content, so text containing "" still converts to valid C#.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@paulirwin
paulirwin merged commit 5d6bb3b into master Aug 16, 2026
5 checks passed
@paulirwin
paulirwin deleted the issue/64 branch August 16, 2026 02:54
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.

Java 15 Text Block Support

1 participant