From 5c1c1272db8540a5e95c910692dfa9242f1d4f88 Mon Sep 17 00:00:00 2001 From: routersys Date: Mon, 29 Jun 2026 18:44:43 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=83=BC=E3=83=8D=E3=83=AB=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=9B=86=E5=90=88=E3=81=AELua=E5=AD=97=E5=8F=A5?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=99=A8=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- .../Kernel/KernelUnsupportedException.cs | 4 + LuaScript/Engine/Kernel/LuaLexer.cs | 310 ++++++++++++++++++ LuaScript/Engine/Kernel/LuaToken.cs | 28 ++ 3 files changed, 342 insertions(+) create mode 100644 LuaScript/Engine/Kernel/KernelUnsupportedException.cs create mode 100644 LuaScript/Engine/Kernel/LuaLexer.cs create mode 100644 LuaScript/Engine/Kernel/LuaToken.cs diff --git a/LuaScript/Engine/Kernel/KernelUnsupportedException.cs b/LuaScript/Engine/Kernel/KernelUnsupportedException.cs new file mode 100644 index 0000000..b1cc84c --- /dev/null +++ b/LuaScript/Engine/Kernel/KernelUnsupportedException.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Engine.Kernel +{ + internal sealed class KernelUnsupportedException(string reason) : Exception(reason); +} diff --git a/LuaScript/Engine/Kernel/LuaLexer.cs b/LuaScript/Engine/Kernel/LuaLexer.cs new file mode 100644 index 0000000..4672f9e --- /dev/null +++ b/LuaScript/Engine/Kernel/LuaLexer.cs @@ -0,0 +1,310 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace LuaScript.Engine.Kernel +{ + internal static class LuaLexer + { + private static readonly HashSet Keywords = new(StringComparer.Ordinal) + { + "and", "break", "do", "else", "elseif", "end", "false", "for", "function", + "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", + "true", "until", "while", + }; + + public static List Tokenize(string source) + { + var tokens = new List(); + int i = 0; + int length = source.Length; + + while (i < length) + { + char c = source[i]; + + if (c is ' ' or '\t' or '\r' or '\n' or '\f' or '\v') + { + i++; + continue; + } + + if (c == '-' && i + 1 < length && source[i + 1] == '-') + { + i = SkipComment(source, i + 2); + continue; + } + + if (c == '"' || c == '\'') + { + i = ReadShortString(source, i, tokens); + continue; + } + + if (c == '[' && i + 1 < length && (source[i + 1] == '[' || source[i + 1] == '=')) + { + int level = LongBracketLevel(source, i); + if (level >= 0) + { + i = ReadLongString(source, i, level, tokens); + continue; + } + } + + if (char.IsDigit(c) || (c == '.' && i + 1 < length && char.IsDigit(source[i + 1]))) + { + i = ReadNumber(source, i, tokens); + continue; + } + + if (IsNameStart(c)) + { + i = ReadName(source, i, tokens); + continue; + } + + i = ReadSymbol(source, i, tokens); + } + + tokens.Add(new LuaToken(LuaTokenKind.Eof, string.Empty, 0d)); + return tokens; + } + + 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++; + } + throw new KernelUnsupportedException("Unterminated long bracket."); + } + + private static int ReadLongString(string source, int start, int level, List tokens) + { + int end = SkipLongBracket(source, start, level); + int contentStart = start + level + 2; + int contentEnd = end - level - 2; + string text = source.Substring(contentStart, Math.Max(0, contentEnd - contentStart)); + tokens.Add(new LuaToken(LuaTokenKind.String, text, 0d)); + return end; + } + + private static int ReadShortString(string source, int start, List tokens) + { + char quote = source[start]; + int i = start + 1; + int length = source.Length; + var value = new System.Text.StringBuilder(); + + while (i < length) + { + char c = source[i]; + if (c == quote) + { + tokens.Add(new LuaToken(LuaTokenKind.String, value.ToString(), 0d)); + return i + 1; + } + if (c == '\n') + throw new KernelUnsupportedException("Unterminated string."); + if (c == '\\') + { + i = ReadEscape(source, i + 1, value); + continue; + } + value.Append(c); + i++; + } + throw new KernelUnsupportedException("Unterminated string."); + } + + private static int ReadEscape(string source, int i, System.Text.StringBuilder value) + { + if (i >= source.Length) + throw new KernelUnsupportedException("Unterminated string escape."); + + char c = source[i]; + switch (c) + { + case 'n': value.Append('\n'); return i + 1; + case 't': value.Append('\t'); return i + 1; + case 'r': value.Append('\r'); return i + 1; + case 'a': value.Append('\a'); return i + 1; + case 'b': value.Append('\b'); return i + 1; + case 'f': value.Append('\f'); return i + 1; + case 'v': value.Append('\v'); return i + 1; + case '\\': value.Append('\\'); return i + 1; + case '"': value.Append('"'); return i + 1; + case '\'': value.Append('\''); return i + 1; + case '\n': value.Append('\n'); return i + 1; + default: + if (char.IsDigit(c)) + { + int n = 0; + int count = 0; + while (i < source.Length && count < 3 && char.IsDigit(source[i])) + { + n = n * 10 + (source[i] - '0'); + i++; + count++; + } + value.Append((char)n); + return i; + } + value.Append(c); + return i + 1; + } + } + + private static int ReadNumber(string source, int start, List tokens) + { + int length = source.Length; + int i = start; + + if (source[i] == '0' && i + 1 < length && (source[i + 1] == 'x' || source[i + 1] == 'X')) + { + i += 2; + int digitsStart = i; + while (i < length && Uri.IsHexDigit(source[i])) + i++; + if (i == digitsStart) + throw new KernelUnsupportedException("Malformed hexadecimal literal."); + string hex = source.Substring(digitsStart, i - digitsStart); + double hexValue = (double)ulong.Parse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture); + tokens.Add(new LuaToken(LuaTokenKind.Number, source.Substring(start, i - start), hexValue)); + return i; + } + + while (i < length && char.IsDigit(source[i])) + i++; + if (i < length && source[i] == '.') + { + i++; + while (i < length && char.IsDigit(source[i])) + i++; + } + if (i < length && (source[i] == 'e' || source[i] == 'E')) + { + i++; + if (i < length && (source[i] == '+' || source[i] == '-')) + i++; + while (i < length && char.IsDigit(source[i])) + i++; + } + + string text = source.Substring(start, i - start); + double value = double.Parse(text, NumberStyles.Float, CultureInfo.InvariantCulture); + tokens.Add(new LuaToken(LuaTokenKind.Number, text, value)); + return i; + } + + private static int ReadName(string source, int start, List tokens) + { + int i = start; + int length = source.Length; + while (i < length && IsNamePart(source[i])) + i++; + string text = source.Substring(start, i - start); + tokens.Add(new LuaToken(Keywords.Contains(text) ? LuaTokenKind.Keyword : LuaTokenKind.Name, text, 0d)); + return i; + } + + private static int ReadSymbol(string source, int start, List tokens) + { + int length = source.Length; + char c = source[start]; + + if (c == '.' && start + 2 < length && source[start + 1] == '.' && source[start + 2] == '.') + { + tokens.Add(new LuaToken(LuaTokenKind.Symbol, "...", 0d)); + return start + 3; + } + + if (start + 1 < length) + { + string two = source.Substring(start, 2); + switch (two) + { + case "==": + case "~=": + case "<=": + case ">=": + case "..": + tokens.Add(new LuaToken(LuaTokenKind.Symbol, two, 0d)); + return start + 2; + } + } + + switch (c) + { + case '+': + case '-': + case '*': + case '/': + case '%': + case '^': + case '#': + case '<': + case '>': + case '=': + case '(': + case ')': + case '{': + case '}': + case '[': + case ']': + case ';': + case ':': + case ',': + case '.': + tokens.Add(new LuaToken(LuaTokenKind.Symbol, c.ToString(), 0d)); + return start + 1; + default: + throw new KernelUnsupportedException($"Unexpected character '{c}'."); + } + } + + private static bool IsNameStart(char c) => c == '_' || char.IsLetter(c); + + private static bool IsNamePart(char c) => c == '_' || char.IsLetterOrDigit(c); + } +} diff --git a/LuaScript/Engine/Kernel/LuaToken.cs b/LuaScript/Engine/Kernel/LuaToken.cs new file mode 100644 index 0000000..375bc86 --- /dev/null +++ b/LuaScript/Engine/Kernel/LuaToken.cs @@ -0,0 +1,28 @@ +using System; + +namespace LuaScript.Engine.Kernel +{ + internal enum LuaTokenKind + { + Name, + Number, + String, + Keyword, + Symbol, + Eof, + } + + internal readonly struct LuaToken(LuaTokenKind kind, string text, double number) + { + public LuaTokenKind Kind { get; } = kind; + public string Text { get; } = text; + public double Number { get; } = number; + + public bool Is(LuaTokenKind kind, string text) => + Kind == kind && string.Equals(Text, text, StringComparison.Ordinal); + + public bool IsSymbol(string text) => Is(LuaTokenKind.Symbol, text); + + public bool IsKeyword(string text) => Is(LuaTokenKind.Keyword, text); + } +}