Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #64.
Converts Java 15 text blocks to C# raw string literals.
The bug
TextBlockLiteralExprderives fromLiteralStringValueExpr, notStringLiteralExpr. Because the visitor registry walks the base-type chain, text blocks matched no visitor and conversion threwNotImplementedException— a hard failure rather than a silent mis-conversion.The fix
A new
TextBlockLiteralExpressionVisitorplus one registry entry. Two details worth calling out, both verified against the parser rather than assumed:asString()already applies the text block rules — it strips incidental whitespace and resolves escapes, including\sand trailing-backslash line continuations. So the value must not be unescaped again, unlikeStringLiteralExpressionVisitor, which callsRegex.Unescape. Doing so here would double-unescape and corrupt\\.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
VisitLiteralExpressionTestscovering de-indentation, escapes, and embedded quotes.Resources/Java15TextBlocks.javaadded 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.TreatWarningsAsErrorsis 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