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
{
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>
{
@@ -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<T> Append(
T unit
)
@@ -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<RegexIR> ir
internal static SequenceEmitter<string> GenerateSource(
TextRegexSource source
)
{
StringBuilder sb = new();
List<StringBuilder> sub_match_list = [];
SequenceEmitter<string> se = new();
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];
Span<RegexIR> scan_win = window;
_ = sb.AppendLine(@"public abstract MatchResult Match(ReadOnlySpan<char> text)")//
_ = se.AppendLine(@"public override MatchResult Match(ReadOnlySpan<char> 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<string> _sub in sub_match_list)
{
_ = sb.AppendLine() //
_ = se.AppendLine() //
.Append(_sub);
}
return sb;
/*
_ = se.Append(@"}");
return se;
/**/
int TryGenerateMatch(
Span<RegexIR> scan_win,
StringBuilder sb
SequenceEmitter<string> 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;
}
/**/
}