IR generate and optmize is done. But now it's not easy for source gen, so designing prefix rather than TRY and CONDITION.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2026-07-05 23:54:05 +08:00
parent 7edb94943d
commit 5bc83b23fc
+36 -82
View File
@@ -55,6 +55,19 @@ namespace StgSharp.RegularAnalysis.Text
} }
internal enum RegexIRPrefix
{
Prev_Success = 1 << 0,
Prev_Fail = 1 << 1,
Always = 1 << 2,
ExeCondition = Prev_Fail | Prev_Success | Always,
Pop_Fail = 1 << 3,
Pop_Success = 1 << 4,
Pop_Condition = Pop_Success | Pop_Fail,
}
internal interface IRegexComplex internal interface IRegexComplex
{ {
@@ -79,10 +92,7 @@ namespace StgSharp.RegularAnalysis.Text
} }
internal sealed class RegexFindComplexIR( internal sealed class RegexFindComplexIR(bool nearest, int count) : RegexIR, IRegexComplex
bool nearest,
int count
) : RegexIR, IRegexComplex
{ {
public bool IsFullyOptimized { get; set; } public bool IsFullyOptimized { get; set; }
@@ -93,9 +103,7 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Find_Complex; internal override RegexIRCommand Command => RegexIRCommand.Find_Complex;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t FIND_COMPLEX {(IsNearest ? "NEAREST" : "FURTHEST")} COUNT: {CommandCount}"; return $"[{lineNum}]\t FIND_COMPLEX {(IsNearest ? "NEAREST" : "FURTHEST")} COUNT: {CommandCount}";
} }
@@ -107,16 +115,11 @@ namespace StgSharp.RegularAnalysis.Text
internal abstract RegexIRCommand Command { get; } internal abstract RegexIRCommand Command { get; }
internal abstract string AsCommand( internal abstract string AsCommand(int lineNum);
int lineNum
);
} }
internal sealed class RegexFindSeqIR( internal sealed class RegexFindSeqIR(string pattern, bool isFirst) : RegexIR
string pattern,
bool isFirst
) : RegexIR
{ {
public bool IsFirst { get; } = isFirst; public bool IsFirst { get; } = isFirst;
@@ -125,9 +128,7 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Find_Seq; internal override RegexIRCommand Command => RegexIRCommand.Find_Seq;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t FIND_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\""; return $"[{lineNum}]\t FIND_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
} }
@@ -137,46 +138,35 @@ namespace StgSharp.RegularAnalysis.Text
/// <summary> /// <summary>
/// Searches for a certain string pattern or charset from current position. /// Searches for a certain string pattern or charset from current position.
/// </summary> /// </summary>
internal sealed class RegexMatchSeqIR( internal sealed class RegexMatchSeqIR(string pattern) : RegexIR
string pattern
) : RegexIR
{ {
public string Pattern { get; } = pattern; public string Pattern { get; } = pattern;
internal override RegexIRCommand Command => RegexIRCommand.Match_Seq; internal override RegexIRCommand Command => RegexIRCommand.Match_Seq;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t MATCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\""; return $"[{lineNum}]\t MATCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
} }
} }
internal sealed class RegexMatchSetIR( internal sealed class RegexMatchSetIR(string charset) : RegexIR
string charset
) : RegexIR
{ {
public string Charset { get; } = charset; public string Charset { get; } = charset;
internal override RegexIRCommand Command => RegexIRCommand.Match_Set; internal override RegexIRCommand Command => RegexIRCommand.Match_Set;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t MATCH_SET \"{Charset}\""; return $"[{lineNum}]\t MATCH_SET \"{Charset}\"";
} }
} }
internal sealed class RegexConditionIR( internal sealed class RegexConditionIR(int success, int fail) : RegexIR
int success,
int fail
) : RegexIR
{ {
public int SuccessCase { get; } = success; public int SuccessCase { get; } = success;
@@ -185,19 +175,14 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Condition; internal override RegexIRCommand Command => RegexIRCommand.Condition;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t CONDITION SUCCESS: {SuccessCase} FAIL: {FailCase}"; return $"[{lineNum}]\t CONDITION SUCCESS: {SuccessCase} FAIL: {FailCase}";
} }
} }
internal sealed class RegexPackIR( internal sealed class RegexPackIR(bool condition, string name = "") : RegexIR
bool condition,
string name = ""
) : RegexIR
{ {
public bool Condition { get; } = condition; public bool Condition { get; } = condition;
@@ -206,21 +191,14 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Pack; internal override RegexIRCommand Command => RegexIRCommand.Pack;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t PACK {(Condition ? "SUCCESS" : "FAIL")} GROUP: \"{GroupName}\""; return $"[{lineNum}]\t PACK {(Condition ? "SUCCESS" : "FAIL")} GROUP: \"{GroupName}\"";
} }
} }
internal sealed class RegexCountComplexIR( internal sealed class RegexCountComplexIR(int min, int max, bool isGreedy, int operation) : RegexIR, IRegexCount, IRegexComplex
int min,
int max,
bool isGreedy,
int operation
) : RegexIR, IRegexCount, IRegexComplex
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -239,21 +217,14 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command { get; } = RegexIRCommand.Count_Complex; internal override RegexIRCommand Command { get; } = RegexIRCommand.Count_Complex;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t COUNT {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} OPERATION: {CommandCount}"; return $"[{lineNum}]\t COUNT {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} OPERATION: {CommandCount}";
} }
} }
internal sealed class RegexCountSeqIR( internal sealed class RegexCountSeqIR(int min, int max, bool isGreedy, string seq) : RegexIR, IRegexCount
int min,
int max,
bool isGreedy,
string seq
) : RegexIR, IRegexCount
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -270,21 +241,14 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command { get; } = RegexIRCommand.Count_Seq; internal override RegexIRCommand Command { get; } = RegexIRCommand.Count_Seq;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t COUNT_SEQ {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SEQ: \"{Sequence}\""; return $"[{lineNum}]\t COUNT_SEQ {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SEQ: \"{Sequence}\"";
} }
} }
internal sealed class RegexCountSetIR( internal sealed class RegexCountSetIR(int min, int max, bool isGreedy, string set) : RegexIR, IRegexCount
int min,
int max,
bool isGreedy,
string set
) : RegexIR, IRegexCount
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -303,18 +267,14 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Count_Set; internal override RegexIRCommand Command => RegexIRCommand.Count_Set;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t COUNT_SET {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SET: \"{CharSet}\""; return $"[{lineNum}]\t COUNT_SET {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SET: \"{CharSet}\"";
} }
} }
internal sealed class RegexTryIR( internal sealed class RegexTryIR(int count) : RegexIR, IRegexComplex
int count
) : RegexIR, IRegexComplex
{ {
public bool IsFullyOptimized { get; set; } public bool IsFullyOptimized { get; set; }
@@ -323,27 +283,21 @@ namespace StgSharp.RegularAnalysis.Text
internal override RegexIRCommand Command => RegexIRCommand.Try; internal override RegexIRCommand Command => RegexIRCommand.Try;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t TRY {CommandCount}"; return $"[{lineNum}]\t TRY {CommandCount}";
} }
} }
internal sealed class RegexPopIR( internal sealed class RegexPopIR(bool isSuccess) : RegexIR
bool isSuccess
) : RegexIR
{ {
public bool IsSuccess { get; } = isSuccess; public bool IsSuccess { get; } = isSuccess;
internal override RegexIRCommand Command => RegexIRCommand.Pop; internal override RegexIRCommand Command => RegexIRCommand.Pop;
internal override string AsCommand( internal override string AsCommand(int lineNum)
int lineNum
)
{ {
return $"[{lineNum}]\t POP {(IsSuccess ? "SUCCESS" : "FAIL")}"; return $"[{lineNum}]\t POP {(IsSuccess ? "SUCCESS" : "FAIL")}";
} }