From 6287e74fe551ae964d2871e0d731605159762a87 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 4 Jul 2026 14:42:27 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A3=9C=E9=96=93=E6=96=87=E5=AD=97=E5=88=97?= =?UTF-8?q?=E3=81=AE=E6=A7=8B=E6=96=87=E7=B3=96=E8=A1=A3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Syntax/Rules/LuaInterpolatedStringRule.cs | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 LuaScript.Compat/Scripting/Syntax/Rules/LuaInterpolatedStringRule.cs diff --git a/LuaScript.Compat/Scripting/Syntax/Rules/LuaInterpolatedStringRule.cs b/LuaScript.Compat/Scripting/Syntax/Rules/LuaInterpolatedStringRule.cs new file mode 100644 index 0000000..cc33172 --- /dev/null +++ b/LuaScript.Compat/Scripting/Syntax/Rules/LuaInterpolatedStringRule.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.Text; + +namespace LuaScript.Compat.Syntax.Rules +{ + [LuaSyntaxRule(5)] + internal sealed class LuaInterpolatedStringRule : ILuaSyntaxRule + { + public bool TryApply(LuaRewriteContext context) + { + if (context.Current.Kind != LuaSyntaxTokenKind.InterpolatedString) + return false; + + string text = context.Current.Text; + int end = text.Length; + if (end >= 2 && text[end - 1] == '`') + end--; + string content = text.Substring(1, end - 1); + + var pieces = new List(); + var literal = new StringBuilder(); + int i = 0; + while (i < content.Length) + { + char c = content[i]; + if (c == '\\' && i + 1 < content.Length) + { + literal.Append(content[i]); + literal.Append(content[i + 1]); + i += 2; + continue; + } + if (c == '{') + { + int close = FindClosingBrace(content, i + 1); + if (close < 0) + { + literal.Append(c); + i++; + continue; + } + string expression = content.Substring(i + 1, close - i - 1).Trim(); + if (expression.Length > 0) + { + FlushLiteral(pieces, literal); + pieces.Add($"tostring({LuaSyntaxExtensions.Rewrite(expression)})"); + } + i = close + 1; + continue; + } + literal.Append(c); + i++; + } + FlushLiteral(pieces, literal); + + context.Advance(); + if (pieces.Count == 0) + { + context.EmitRaw("\"\""); + return true; + } + if (pieces.Count == 1) + { + context.EmitRaw(pieces[0]); + return true; + } + context.EmitRaw($"({string.Join(" .. ", pieces)})"); + return true; + } + + private static void FlushLiteral(List pieces, StringBuilder literal) + { + if (literal.Length == 0) + return; + pieces.Add($"\"{literal.ToString().Replace("\"", "\\\"")}\""); + literal.Clear(); + } + + private static int FindClosingBrace(string content, int from) + { + int depth = 0; + char quote = '\0'; + for (int i = from; i < content.Length; i++) + { + char c = content[i]; + if (quote != '\0') + { + if (c == '\\') + { + i++; + continue; + } + if (c == quote) + quote = '\0'; + continue; + } + if (c == '"' || c == '\'') + { + quote = c; + continue; + } + if (c == '{') + { + depth++; + continue; + } + if (c == '}') + { + if (depth == 0) + return i; + depth--; + } + } + return -1; + } + } +}