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
+44
View File
@@ -122,3 +122,47 @@ static void Match_AB_2_3(ReadOnlySpan<char> input)
} while(false);
}
// (ab|[cd]|.)x
static void MatchDiffCase(ReadOnlySpan<char> seq)
{
/*
* [000] 0001, DEF , Sccs, NONE, 0000, MATCH_SEQ "ab"
* [001] 0001, Fail, Sccs, LINE, 0000, MATCH_SET "[cd]"
* [002] 0001, Fail, Sccs, LINE, 0001, MATCH_SET "."
* [003] 0000, Fail, DEF , LINE, 0002, POP FAIL
* [004] 0000, DEF , DEF , NONE, 0000, MATCH_SEQ "x"
*/
int ptr = 0;
if(seq.StartsWith("ab"))
{
ptr+=2;
}
else if((seq[0] == 'c' || seq[0] == 'd'))
{
ptr+=1;
}
else if(true)
{
ptr+=1;
}
else
{
return;
}
}
// (?<name>.*)x
static void MatchCountAnyGroup(ReadOnlySpan<char> seq)
{
/*
* [000] 0001, DEF , DEF , NONE, 0000, FIND_COMPLEX FURTHEST COUNT: -1
* [001] 0001, Sccs, DEF , LINE, 0000, PACK SUCCESS GROUP: "name"
* [002] 0000, DEF , DEF , NONE, 0000, MATCH_SEQ "234"
*/
int ptr = 0;
}
+1 -1
View File
@@ -21,7 +21,7 @@ namespace StgSharp
{
private static string[] regexSource = [
@"(?<name>123)(?<name>123)",
@"(?<name>.*)234",
/*
@"http|https|ftp|ftps",
@"abc[0-9]x|abc[0-9]y|abd[0-9]x|abd[0-9]y|xyz",