From 49a2ce29b279e026e8d7b304be0fa7649c80f163 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 21:32:46 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A4=89=E6=8F=9B=E3=82=92=E3=83=88=E3=83=BC?= =?UTF-8?q?=E3=82=AF=E3=83=B3=E8=A6=8F=E5=89=87=E6=96=B9=E5=BC=8F=E3=81=B8?= =?UTF-8?q?=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripting/LuaSyntaxExtensions.cs | 180 ++---------------- 1 file changed, 19 insertions(+), 161 deletions(-) diff --git a/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs b/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs index a81b86f..e8549fe 100644 --- a/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs +++ b/LuaScript.Compat/Scripting/LuaSyntaxExtensions.cs @@ -1,5 +1,4 @@ -using System; -using System.Text; +using LuaScript.Compat.Syntax; namespace LuaScript.Compat { @@ -7,182 +6,41 @@ namespace LuaScript.Compat { public static string Rewrite(string source) { - if (string.IsNullOrEmpty(source) || source.IndexOf("is", StringComparison.Ordinal) < 0) + if (string.IsNullOrEmpty(source)) return source; - var builder = new StringBuilder(source.Length); - int i = 0; - int length = source.Length; + var tokens = LuaSyntaxLexer.Tokenize(source); + var context = new LuaRewriteContext(tokens); + var rules = LuaSyntaxRuleRegistry.Rules; - while (i < length) + while (!context.AtEnd) { - char c = source[i]; - - if (c == '-' && i + 1 < length && source[i + 1] == '-') + var current = context.Current; + if (current.IsTrivia || current.Kind == LuaSyntaxTokenKind.String) { - int start = i; - i = SkipComment(source, i + 2); - builder.Append(source, start, i - start); + context.Emit(current); + context.Advance(); continue; } - if (c == '"' || c == '\'') + bool matched = false; + foreach (var rule in rules) { - int start = i; - i = SkipShortString(source, i); - builder.Append(source, start, i - start); - continue; - } - - if (c == '[') - { - int level = LongBracketLevel(source, i); - if (level >= 0) + if (rule.TryApply(context)) { - int start = i; - i = SkipLongBracket(source, i, level); - builder.Append(source, start, i - start); - continue; + matched = true; + break; } } - if (IsNameStart(c)) + if (!matched) { - 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; + context.Emit(current); + context.Advance(); } - - builder.Append(c); - i++; } - return builder.ToString(); + return context.Changed ? context.Serialize() : source; } - - 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); } }