mainly theoratical work on semi, add regex sample

This commit is contained in:
Youmu Liu
2026-07-27 18:11:31 +08:00
parent a74ebcd0b7
commit ac1e7c68f8
5 changed files with 91 additions and 6 deletions
+5 -3
View File
@@ -73,12 +73,14 @@ namespace StgSharp.RegularAnalysis.Text
}
internal interface IRegexMatch { }
/// <summary>
/// Searches for a certain string pattern or charset from current position.
/// </summary>
internal sealed class RegexMatchSeqIR(
string pattern
) : RegexIR(RegexIRCommand.Match_Seq), IRegexSequence
) : RegexIR(RegexIRCommand.Match_Seq), IRegexSequence, IRegexMatch
{
public string Pattern { get; } = pattern;
@@ -92,7 +94,7 @@ namespace StgSharp.RegularAnalysis.Text
internal sealed class RegexMatchSetIR(
string charset
) : RegexIR(RegexIRCommand.Match_Set)
) : RegexIR(RegexIRCommand.Match_Set), IRegexMatch
{
public string Charset { get; } = charset;
@@ -225,7 +227,7 @@ namespace StgSharp.RegularAnalysis.Text
internal sealed class RegexTryIR(
int count
) : RegexIR(RegexIRCommand.Try), IRegexComplex
) : RegexIR(RegexIRCommand.Try), IRegexComplex, IRegexMatch
{
public bool IsFullyOptimized { get; set; }
@@ -19,6 +19,24 @@ namespace StgSharp.RegularAnalysis.Text
internal partial class TextRegexSourceGen
{
private static int GenerateNamedCountAny(
ReadOnlySpan<RegexIR> region_ir,
SourceGenContext sc,
in SequenceEmitter<string> main_emitter,
in List<SequenceEmitter<string>> sub_emitter_list
)
{
if (region_ir[0] is not RegexCountSetIR { IsDotStar:true} ||
region_ir[1] is not RegexPackIR pack) {
return -1;
}
if (region_ir[2] is RegexMatchSeqIR) { }
_ = main_emitter.AppendLine($@"do").AppendLine($@"{{");
_ = main_emitter.AppendLine($@"}}").AppendLine($@"while(true);");
string group_idx = sc.MakeNewNamedGroup(pack.GroupName);
return 0;
}
private static void GenerateRegion(
ReadOnlySpan<RegexIR> region_ir,
SourceGenContext sc,
@@ -35,7 +53,8 @@ namespace StgSharp.RegularAnalysis.Text
int result;
int count = 0;
scan_win = ir_sp[index..];
if ((result = TryGenerateFindSeq(scan_win, sc, in main_emitter,
if ((result =
TryGenerateFindSeq(scan_win, sc, in main_emitter,
in sub_emitter_list)) > 0)
{
count += result;
@@ -77,7 +96,7 @@ namespace StgSharp.RegularAnalysis.Text
if (scan_win[1] is RegexMatchSeqIR seq)
{
string seq_pat = FormatStringLiteral(find.Pattern + seq.Pattern);
string seq_pat = FormatStringLiteral($"{find.Pattern}{seq.Pattern}");
_ = main_emitter.AppendLine(@$"//Match sequence:{seq_pat} at current pos")
.AppendLine($@"if( )");
}
@@ -50,6 +50,26 @@ namespace StgSharp.RegularAnalysis.Text
}
}
#region namig group
private int _min_named_group;
private Dictionary<string, int> _name_idx_map = [];
public string MakeNewNamedGroup(
string name
)
{
if (_name_idx_map.TryGetValue(name, out int idx)) {
return $"__named_group_{idx}";
}
idx = _min_named_group;
_name_idx_map[name] = idx;
_min_named_group++;
return $"__named_group_{idx}";
}
#endregion
public string FindResult
{
get