Skip to content

Commit

Permalink
feat: raw strings
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jan 6, 2025
1 parent 8574016 commit c6bcd41
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions RobloxCS/LuauGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -980,16 +981,38 @@ public override Luau.Literal VisitInterpolatedStringText(InterpolatedStringTextS
public override Luau.Interpolation VisitInterpolation(InterpolationSyntax node) =>
new(Visit<Luau.Expression>(node.Expression));

public override Luau.Literal VisitLiteralExpression(LiteralExpressionSyntax node)
public override Luau.Expression VisitLiteralExpression(LiteralExpressionSyntax node)
{
var valueText = "";
switch (node.Kind())
{
case SyntaxKind.StringLiteralExpression:
case SyntaxKind.Utf8StringLiteralExpression:
case SyntaxKind.CharacterLiteralExpression:
valueText = $"\"{(node.Token.Text.StartsWith('@') ? Regex.Escape(node.Token.ValueText) : node.Token.ValueText)}\"";
{
var fullText = node.Token.Text;
var stringContents = node.Token.ValueText;
if (fullText.StartsWith('@')) // verbatim strings
stringContents = Regex.Escape(stringContents);
else if (fullText.StartsWith("\"\"\"")) // raw strings
{
var lines = stringContents.Split("\r\n").ToList();
var newStringContents = new StringBuilder();
var index = 0;
foreach (var line in lines)
{
newStringContents.Append(Regex.Escape(line));
if (index++ != lines.Count - 1)
newStringContents.Append("\\n");
}

valueText = $"\"{newStringContents}\"";
break;
}

valueText = $"\"{stringContents}\"";
break;
}

case SyntaxKind.NullLiteralExpression:
valueText = "nil";
Expand Down

0 comments on commit c6bcd41

Please sign in to comment.