source gen and ir gen now use the same seq generator

This commit is contained in:
Youmu Liu
2026-07-10 18:49:57 +08:00
parent ef94bf1567
commit ae8a09efc2
2 changed files with 54 additions and 15 deletions
@@ -18,6 +18,28 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Abstraction namespace StgSharp.RegularAnalysis.Abstraction
{ {
public static class SequenceEmitterExtensions
{
public static SequenceEmitter<string> AppendLine(
this SequenceEmitter<string> emitter
)
{
return emitter is null ? null! : emitter.Append(string.Empty);
}
public static SequenceEmitter<string> AppendLine(
this SequenceEmitter<string> emitter,
string line
)
{
return emitter is null ?
null! :
emitter.Append(string.IsNullOrEmpty(line) ? string.Empty : line);
}
}
public sealed class SequenceEmitter<T> public sealed class SequenceEmitter<T>
{ {
@@ -35,6 +57,8 @@ namespace StgSharp.RegularAnalysis.Abstraction
public int Count { get; private set; } public int Count { get; private set; }
private static bool IsString { get; } = typeof(T) == typeof(string);
public SequenceEmitter<T> Append( public SequenceEmitter<T> Append(
T unit T unit
) )
@@ -6,6 +6,7 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using StgSharp.RegularAnalysis.Abstraction;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -19,43 +20,57 @@ namespace StgSharp.RegularAnalysis.Text
public partial class RegexAnalyzer public partial class RegexAnalyzer
{ {
internal static StringBuilder GenerateSource( internal static SequenceEmitter<string> GenerateSource(
List<RegexIR> ir TextRegexSource source
) )
{ {
StringBuilder sb = new(); SequenceEmitter<string> se = new();
List<StringBuilder> sub_match_list = []; List<SequenceEmitter<string>> sub_match_list = [];
ReadOnlySpan<RegexIR> source = CollectionsMarshal.AsSpan(ir); RegexInfo info = source.Info;
List<RegexIR> ir = source.IRs;
ReadOnlySpan<RegexIR> ir_sp = CollectionsMarshal.AsSpan(ir);
RegexIR[] window = new RegexIR[8]; RegexIR[] window = new RegexIR[8];
Span<RegexIR> scan_win = window; Span<RegexIR> scan_win = window;
_ = sb.AppendLine(@"public abstract MatchResult Match(ReadOnlySpan<char> text)")// _ = se.AppendLine(@"public override MatchResult Match(ReadOnlySpan<char> text)")//
.AppendLine(@"{"); .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; int index = 0;
do do
{ {
int count = int.Min(8, source.Length - index); int count = int.Min(8, ir_sp.Length - index);
source.Slice(index, count).CopyTo(scan_win); ir_sp.Slice(index, count).CopyTo(scan_win);
} while (index < ir.Count); } while (index < ir.Count);
foreach (StringBuilder _sub in sub_match_list) foreach (SequenceEmitter<string> _sub in sub_match_list)
{ {
_ = sb.AppendLine() // _ = se.AppendLine() //
.Append(_sub); .Append(_sub);
} }
return sb; _ = se.Append(@"}");
/*
return se;
/**/
int TryGenerateMatch( int TryGenerateMatch(
Span<RegexIR> scan_win, Span<RegexIR> scan_win,
StringBuilder sb SequenceEmitter<string> sb
) )
{ {
if (scan_win[0] is RegexMatchSeqIR seq) { if (scan_win[0] is not RegexMatchSeqIR seq) {
_ = sb.AppendLine(string.Empty); return -1;
} }
_ = sb.Append(string.Empty);
return 1;
} }
/**/ /**/
} }