変換をトークン規則方式へ刷新

このコミットが含まれているのは:
routersys committed 2026-07-03 21:32:46 +09:00
1 親 d728c8d056
コミット 49a2ce29b2
1ファイル変更
+19 -161
+19 -161
ファイルの表示
@@ -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);
}
}