From ae8a09efc2418b49aeff48523a046a8ad5ada955 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Fri, 10 Jul 2026 18:49:57 +0800 Subject: [PATCH] source gen and ir gen now use the same seq generator --- .../Abstraction/SequenceEmitter.cs | 24 ++++++++++ .../Text/RegexAnalyzer.SourceGen.cs | 45 ++++++++++++------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/StgSharp.RegularAnalysis/Abstraction/SequenceEmitter.cs b/StgSharp.RegularAnalysis/Abstraction/SequenceEmitter.cs index e93ba087..e23e3bf7 100644 --- a/StgSharp.RegularAnalysis/Abstraction/SequenceEmitter.cs +++ b/StgSharp.RegularAnalysis/Abstraction/SequenceEmitter.cs @@ -18,6 +18,28 @@ using System.Threading.Tasks; namespace StgSharp.RegularAnalysis.Abstraction { + public static class SequenceEmitterExtensions + { + + public static SequenceEmitter AppendLine( + this SequenceEmitter emitter + ) + { + return emitter is null ? null! : emitter.Append(string.Empty); + } + + public static SequenceEmitter AppendLine( + this SequenceEmitter emitter, + string line + ) + { + return emitter is null ? + null! : + emitter.Append(string.IsNullOrEmpty(line) ? string.Empty : line); + } + + } + public sealed class SequenceEmitter { @@ -35,6 +57,8 @@ namespace StgSharp.RegularAnalysis.Abstraction public int Count { get; private set; } + private static bool IsString { get; } = typeof(T) == typeof(string); + public SequenceEmitter Append( T unit ) diff --git a/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs b/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs index fbaebf3a..b12b640e 100644 --- a/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs +++ b/StgSharp.RegularAnalysis/Text/RegexAnalyzer.SourceGen.cs @@ -6,6 +6,7 @@ // ----------------------------------------------------------------------------- using Microsoft.CodeAnalysis; +using StgSharp.RegularAnalysis.Abstraction; using System; using System.Collections.Generic; using System.Linq; @@ -19,43 +20,57 @@ namespace StgSharp.RegularAnalysis.Text public partial class RegexAnalyzer { - internal static StringBuilder GenerateSource( - List ir + internal static SequenceEmitter GenerateSource( + TextRegexSource source ) { - StringBuilder sb = new(); - List sub_match_list = []; + SequenceEmitter se = new(); + List> sub_match_list = []; - ReadOnlySpan source = CollectionsMarshal.AsSpan(ir); + RegexInfo info = source.Info; + List ir = source.IRs; + ReadOnlySpan ir_sp = CollectionsMarshal.AsSpan(ir); RegexIR[] window = new RegexIR[8]; Span scan_win = window; - _ = sb.AppendLine(@"public abstract MatchResult Match(ReadOnlySpan text)")// + _ = se.AppendLine(@"public override MatchResult Match(ReadOnlySpan text)")// .AppendLine(@"{"); + int region_count = info.RegionCount; + for (int i = 0; i < region_count; i++) + { + _ = se.AppendLine($@"int region_{i}_start = -1;")// + .AppendLine($@"int region_{i}_end = -1;") + .AppendLine($@"bool region_{i}_success;"); + } + int index = 0; do { - int count = int.Min(8, source.Length - index); - source.Slice(index, count).CopyTo(scan_win); + int count = int.Min(8, ir_sp.Length - index); + ir_sp.Slice(index, count).CopyTo(scan_win); } while (index < ir.Count); - foreach (StringBuilder _sub in sub_match_list) + foreach (SequenceEmitter _sub in sub_match_list) { - _ = sb.AppendLine() // + _ = se.AppendLine() // .Append(_sub); } - return sb; - /* + _ = se.Append(@"}"); + + return se; + /**/ int TryGenerateMatch( Span scan_win, - StringBuilder sb + SequenceEmitter sb ) { - if (scan_win[0] is RegexMatchSeqIR seq) { - _ = sb.AppendLine(string.Empty); + if (scan_win[0] is not RegexMatchSeqIR seq) { + return -1; } + _ = sb.Append(string.Empty); + return 1; } /**/ }