From 8f2aafdffd42003317b6c3d95bbe4a5040b40b10 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 19:40:19 +0900 Subject: [PATCH] =?UTF-8?q?is=20not=20=E6=A7=8B=E6=96=87=E3=81=AE=E6=9B=B8?= =?UTF-8?q?=E3=81=8D=E6=8F=9B=E3=81=88=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripting/LuaSyntaxExtensions.cs | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs diff --git a/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs b/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs new file mode 100644 index 0000000..a81b86f --- /dev/null +++ b/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs @@ -0,0 +1,188 @@ +using System; +using System.Text; + +namespace LuaScript.Compat +{ + internal static class LuaSyntaxExtensions + { + public static string Rewrite(string source) + { + if (string.IsNullOrEmpty(source) || source.IndexOf("is", StringComparison.Ordinal) < 0) + return source; + + var builder = new StringBuilder(source.Length); + int i = 0; + int length = source.Length; + + while (i < length) + { + char c = source[i]; + + if (c == '-' && i + 1 < length && source[i + 1] == '-') + { + int start = i; + i = SkipComment(source, i + 2); + builder.Append(source, start, i - start); + continue; + } + + if (c == '"' || c == '\'') + { + int start = i; + i = SkipShortString(source, i); + builder.Append(source, start, i - start); + continue; + } + + if (c == '[') + { + int level = LongBracketLevel(source, i); + if (level >= 0) + { + int start = i; + i = SkipLongBracket(source, i, level); + builder.Append(source, start, i - start); + continue; + } + } + + if (IsNameStart(c)) + { + int start = i; + i = SkipName(source, i); + if (i - start == 2 && source[start] == 'i' && source[start + 1] == 's' + && !IsMemberAccess(source, start) + && TryMatchNot(source, i, out int afterNot)) + { + builder.Append("~="); + i = afterNot; + } + else + { + builder.Append(source, start, i - start); + } + continue; + } + + builder.Append(c); + i++; + } + + return builder.ToString(); + } + + private static bool TryMatchNot(string source, int index, out int afterNot) + { + afterNot = index; + int i = index; + int length = source.Length; + int wsStart = i; + while (i < length && (source[i] == ' ' || source[i] == '\t')) + i++; + if (i == wsStart || i + 3 > length) + return false; + if (source[i] != 'n' || source[i + 1] != 'o' || source[i + 2] != 't') + return false; + int after = i + 3; + if (after < length && IsNamePart(source[after])) + return false; + afterNot = after; + return true; + } + + private static bool IsMemberAccess(string source, int nameStart) + { + for (int j = nameStart - 1; j >= 0; j--) + { + char c = source[j]; + if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') + continue; + return c == '.' || c == ':'; + } + return false; + } + + private static int SkipComment(string source, int i) + { + int level = LongBracketLevel(source, i); + if (level >= 0) + return SkipLongBracket(source, i, level); + + int length = source.Length; + while (i < length && source[i] != '\n') + i++; + return i; + } + + private static int LongBracketLevel(string source, int i) + { + if (i >= source.Length || source[i] != '[') + return -1; + int j = i + 1; + int level = 0; + while (j < source.Length && source[j] == '=') + { + level++; + j++; + } + return j < source.Length && source[j] == '[' ? level : -1; + } + + private static int SkipLongBracket(string source, int i, int level) + { + int length = source.Length; + i += level + 2; + while (i < length) + { + if (source[i] == ']') + { + int j = i + 1; + int count = 0; + while (j < length && source[j] == '=') + { + count++; + j++; + } + if (count == level && j < length && source[j] == ']') + return j + 1; + } + i++; + } + return length; + } + + private static int SkipShortString(string source, int start) + { + char quote = source[start]; + int i = start + 1; + int length = source.Length; + while (i < length) + { + char c = source[i]; + if (c == '\\') + { + i += 2; + continue; + } + if (c == quote) + return i + 1; + if (c == '\n') + return i; + i++; + } + return length; + } + + private static int SkipName(string source, int i) + { + int length = source.Length; + while (i < length && IsNamePart(source[i])) + i++; + return i; + } + + private static bool IsNameStart(char c) => c == '_' || char.IsLetter(c); + + private static bool IsNamePart(char c) => c == '_' || char.IsLetterOrDigit(c); + } +}