source gen and ir gen now use the same seq generator

This commit is contained in:
Youmu Liu
2026-07-09 18:24:38 +08:00
parent 11ccd2568b
commit ef94bf1567
7 changed files with 327 additions and 237 deletions
@@ -5,6 +5,7 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -14,143 +15,113 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
internal class RegexIRGenerator
internal sealed class RegexIRGenerator
{
private Node _head = new();
private Node _tail;
private readonly SequenceEmitter<RegexIR> _stream = new();
public RegexIRGenerator()
{
_tail = _head;
}
public int Count => _stream.Count;
public int Count { get; private set; }
public void EmitCondition(
int success,
int fail
public RegexIRGenerator EmitCondition(
int success,
int fail
)
{
_tail.Add(new RegexConditionIR(success, fail));
Count++;
return Emit(new RegexConditionIR(success, fail));
}
public void EmitCountComplex(
int min,
int max,
bool isGreedy,
int operation
public RegexIRGenerator EmitCountComplex(
int min,
int max,
bool isGreedy,
int operation
)
{
_tail.Add(new RegexCountComplexIR(min, max, isGreedy, operation));
Count++;
return Emit(new RegexCountComplexIR(min, max, isGreedy, operation));
}
public void EmitCountSeq(
int min,
int max,
bool isGreedy,
string seq
public RegexIRGenerator EmitCountSeq(
int min,
int max,
bool isGreedy,
string seq
)
{
_tail.Add(new RegexCountSeqIR(min, max, isGreedy, seq));
Count++;
return Emit(new RegexCountSeqIR(min, max, isGreedy, seq));
}
public void EmitCountSet(
int min,
int max,
bool isGreedy,
string set
public RegexIRGenerator EmitCountSet(
int min,
int max,
bool isGreedy,
string set
)
{
_tail.Add(new RegexCountSetIR(min, max, isGreedy, set));
Count++;
return Emit(new RegexCountSetIR(min, max, isGreedy, set));
}
public void EmitIRStream(
RegexIRGenerator stream
public RegexIRGenerator EmitIRStream(
RegexIRGenerator generator
)
{
if (stream is null || stream.Count == 0) {
return;
}
_tail.Next = stream._head;
_tail = stream._tail;
Count += stream.Count;
ArgumentNullException.ThrowIfNull(generator);
_ = _stream.Append(generator._stream);
return this;
}
public void EmitMatchSeq(
string unit
public RegexIRGenerator EmitMatchSeq(
string unit
)
{
_tail.Add(new RegexMatchSeqIR(unit));
Count++;
return Emit(new RegexMatchSeqIR(unit));
}
public void EmitMatchSet(
string unit
public RegexIRGenerator EmitMatchSet(
string unit
)
{
_tail.Add(new RegexMatchSetIR(unit));
Count++;
return Emit(new RegexMatchSetIR(unit));
}
public void EmitPack(
bool condition,
string name = ""
public RegexIRGenerator EmitPack(
bool condition,
string name = ""
)
{
_tail.Add(new RegexPackIR(condition, name));
Count++;
return Emit(new RegexPackIR(condition, name));
}
public void EmitPop(
bool isSuccess
public RegexIRGenerator EmitPop(
bool isSuccess
)
{
_tail.Add(new RegexPopIR(isSuccess));
Count++;
return Emit(new RegexPopIR(isSuccess));
}
public void EmitTry(
int count
public RegexIRGenerator EmitTry(
int count
)
{
_tail.Add(new RegexTryIR(count));
Count++;
return Emit(new RegexTryIR(count));
}
public SequenceEmitter<RegexIR>.Enumerator GetEnumerator()
{
return _stream.GetEnumerator();
}
public List<RegexIR> ToList()
{
List<RegexIR> result = new(Count);
Node current = _head;
while (current != null)
{
result.AddRange(current.IRList);
current = current.Next!;
}
return result;
return _stream.ToList();
}
private class Node
private RegexIRGenerator Emit(
RegexIR ir
)
{
public Node? Next { get; set; }
public int Count => IRList.Count;
public List<RegexIR> IRList { get; } = [];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(
RegexIR ir
)
{
IRList.Add(ir);
}
_ = _stream.Append(ir);
return this;
}
}