From b6eac2094123e8d772f5de1c5d05ef9cc6cf1f5d Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 21:32:27 +0900 Subject: [PATCH] =?UTF-8?q?=E6=A7=8B=E6=96=87=E8=A6=8F=E5=89=87=E3=83=AC?= =?UTF-8?q?=E3=82=B8=E3=82=B9=E3=83=88=E3=83=AA=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Generator/LuaSyntaxRuleGenerator.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 LuaScript.Generator/LuaSyntaxRuleGenerator.cs diff --git a/LuaScript.Generator/LuaSyntaxRuleGenerator.cs b/LuaScript.Generator/LuaSyntaxRuleGenerator.cs new file mode 100644 index 0000000..9f57a96 --- /dev/null +++ b/LuaScript.Generator/LuaSyntaxRuleGenerator.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace LuaScript.Generator +{ + internal sealed record LuaSyntaxRuleModel(string TypeName, int Order); + + [Generator] + public sealed class LuaSyntaxRuleGenerator : IIncrementalGenerator + { + private const string RuleAttributeName = "LuaScript.Compat.Syntax.LuaSyntaxRuleAttribute"; + private const string RuleInterfaceName = "LuaScript.Compat.Syntax.ILuaSyntaxRule"; + + public void Initialize(IncrementalGeneratorInitializationContext context) + { + var rules = context.SyntaxProvider + .ForAttributeWithMetadataName( + RuleAttributeName, + static (node, _) => node is ClassDeclarationSyntax, + static (ctx, _) => CreateModel(ctx)) + .Where(static model => model is not null) + .Select(static (model, _) => model!); + + context.RegisterSourceOutput(rules.Collect(), static (ctx, models) => + { + if (models.Length == 0) + return; + ctx.AddSource("LuaSyntaxRuleRegistry.g.cs", SourceText.From(Emit(models), Encoding.UTF8)); + }); + } + + private static LuaSyntaxRuleModel? CreateModel(GeneratorAttributeSyntaxContext context) + { + if (context.TargetSymbol is not INamedTypeSymbol symbol) + return null; + + if (!symbol.AllInterfaces.Any(static contract => contract.ToDisplayString() == RuleInterfaceName)) + return null; + + int order = 0; + var arguments = context.Attributes[0].ConstructorArguments; + if (arguments.Length > 0 && arguments[0].Value is int value) + order = value; + + return new LuaSyntaxRuleModel(symbol.ToDisplayString(), order); + } + + private static string Emit(ImmutableArray models) + { + var ordered = models + .OrderBy(static m => m.Order) + .ThenBy(static m => m.TypeName, StringComparer.Ordinal) + .ToArray(); + + var builder = new StringBuilder(); + builder.AppendLine("#nullable enable"); + builder.AppendLine("namespace LuaScript.Compat.Syntax"); + builder.AppendLine("{"); + builder.AppendLine(" internal static class LuaSyntaxRuleRegistry"); + builder.AppendLine(" {"); + builder.AppendLine(" internal static readonly global::LuaScript.Compat.Syntax.ILuaSyntaxRule[] Rules = new global::LuaScript.Compat.Syntax.ILuaSyntaxRule[]"); + builder.AppendLine(" {"); + foreach (var model in ordered) + builder.AppendLine($" new global::{model.TypeName}(),"); + builder.AppendLine(" };"); + builder.AppendLine(" }"); + builder.AppendLine("}"); + return builder.ToString(); + } + } +}