discuss regex ir opt

This commit is contained in:
Youmu Liu
2026-06-22 18:18:16 +08:00
parent 062ffacf88
commit 7e5e4d1f7a
18 changed files with 1499 additions and 127 deletions
@@ -28,7 +28,7 @@
using StgSharp.RegularAnalysis.Abstraction; using StgSharp.RegularAnalysis.Abstraction;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel> internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
{ {
@@ -31,7 +31,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
[Flags] [Flags]
public enum RegexElementLabel public enum RegexElementLabel
@@ -32,25 +32,24 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal enum RegexIRCommand internal enum RegexIRCommand
{ {
Search_Seq = 0, Match_Seq = 0,
Search_Set = 1, Match_Set = 1,
SearchChar = 2, Condition = 3,
ConfigFail = 3, Try = 4,
Condition = 4, Count = 5,
Try = 5, Count_Seq = 6,
Count = 6, Count_Set = 7,
Count_Seq = 7, Pop = 8,
Count_Set = 8, Pack = 9,
Pop = 10
} }
internal abstract class RegexIR internal abstract class RegexHIR
{ {
internal abstract RegexIRCommand Command { get; } internal abstract RegexIRCommand Command { get; }
@@ -64,70 +63,46 @@ namespace StgSharp.RegularAnalysis.TextRegex
/// <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 RegexMatchSeqHIR(
string pattern string pattern
) : RegexIR ) : RegexHIR
{ {
public string Pattern { get; } = pattern; public string Pattern { get; } = pattern;
internal override RegexIRCommand Command => RegexIRCommand.Search_Seq; internal override RegexIRCommand Command => RegexIRCommand.Match_Seq;
internal override string AsCommand( internal override string AsCommand(
int lineNum int lineNum
) )
{ {
return $"[{lineNum}]\t SEARCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\""; return $"[{lineNum}]\t MATCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
} }
} }
internal sealed class RegexMatchSetIR( internal sealed class RegexMatchSetHIR(
string charset string charset
) : RegexIR ) : RegexHIR
{ {
public string Charset { get; } = charset; public string Charset { get; } = charset;
internal override RegexIRCommand Command => RegexIRCommand.Search_Set; internal override RegexIRCommand Command => RegexIRCommand.Match_Set;
internal override string AsCommand( internal override string AsCommand(
int lineNum int lineNum
) )
{ {
return $"[{lineNum}]\t SEARCH_SET \"{Charset}\""; return $"[{lineNum}]\t MATCH_SET \"{Charset}\"";
} }
} }
/// <summary> internal sealed class RegexConditionHIR(
/// Configures the failure jump of current position. The failMod ranges 0-2
/// </summary>
/// <param name="target">
///
/// </param>
internal sealed class RegexConfigFailIR(
int failMod
) : RegexIR
{
public int FailMod { get; } = failMod & 0x3;
internal override RegexIRCommand Command => RegexIRCommand.ConfigFail;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t CONFIG_FAIL {FailMod}";
}
}
internal sealed class RegexConditionIR(
int success, int success,
int fail int fail
) : RegexIR ) : RegexHIR
{ {
public int SuccessCase { get; } = success; public int SuccessCase { get; } = success;
@@ -145,12 +120,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
internal sealed class RegexCountIR( internal sealed class RegexPackHIR(
bool condition,
string name = ""
) : RegexHIR
{
public bool Condition { get; } = condition;
public string GroupName { get; } = name;
internal override RegexIRCommand Command => RegexIRCommand.Pack;
internal override string AsCommand(
int lineNum
)
{
return $"[{lineNum}]\t PACK {(Condition ? "SUCCESS" : "FAIL")} GROUP: \"{GroupName}\"";
}
}
internal sealed class RegexCountHIR(
int min, int min,
int max, int max,
bool isGreedy, bool isGreedy,
int operation int operation
) : RegexIR ) : RegexHIR
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -172,12 +168,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
internal sealed class RegexCountSeqIR( internal sealed class RegexCountSeqHIR(
int min, int min,
int max, int max,
bool isGreedy, bool isGreedy,
string seq string seq
) : RegexIR ) : RegexHIR
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -199,12 +195,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
internal sealed class RegexCountSetIR( internal sealed class RegexCountSetHIR(
int min, int min,
int max, int max,
bool isGreedy, bool isGreedy,
string set string set
) : RegexIR ) : RegexHIR
{ {
public bool IsGreedy { get; } = isGreedy; public bool IsGreedy { get; } = isGreedy;
@@ -226,9 +222,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
internal sealed class RegexTryIR( internal sealed class RegexTryHIR(
int count int count
) : RegexIR ) : RegexHIR
{ {
public int CountOfCommand { get; } = count; public int CountOfCommand { get; } = count;
@@ -244,14 +240,14 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
internal sealed class RegexPopIR( internal sealed class RegexPopHIR(
bool isSuccess bool isSuccess
) : RegexIR ) : RegexHIR
{ {
public bool IsSuccess { get; } = isSuccess; public bool IsSuccess { get; } = isSuccess;
internal override RegexIRCommand Command => RegexIRCommand.Try; internal override RegexIRCommand Command => RegexIRCommand.Pop;
internal override string AsCommand( internal override string AsCommand(
int lineNum int lineNum
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegexIRGenerator" // file="RegexHIRGenerator"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -32,15 +32,15 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal class RegexIRGenerator internal class RegexHIRGenerator
{ {
private Node _head = new(); private Node _head = new();
private Node _tail; private Node _tail;
public RegexIRGenerator() public RegexHIRGenerator()
{ {
_tail = _head; _tail = _head;
} }
@@ -52,15 +52,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int fail int fail
) )
{ {
_tail.Add(new RegexConditionIR(success, fail)); _tail.Add(new RegexConditionHIR(success, fail));
Count++;
}
public void EmitConfigFail(
int fail
)
{
_tail.Add(new RegexConfigFailIR(fail));
Count++; Count++;
} }
@@ -71,7 +63,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int operation int operation
) )
{ {
_tail.Add(new RegexCountIR(min, max, isGreedy, operation)); _tail.Add(new RegexCountHIR(min, max, isGreedy, operation));
Count++; Count++;
} }
@@ -82,7 +74,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
string seq string seq
) )
{ {
_tail.Add(new RegexCountSeqIR(min, max, isGreedy, seq)); _tail.Add(new RegexCountSeqHIR(min, max, isGreedy, seq));
Count++; Count++;
} }
@@ -93,12 +85,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
string set string set
) )
{ {
_tail.Add(new RegexCountSetIR(min, max, isGreedy, set)); _tail.Add(new RegexCountSetHIR(min, max, isGreedy, set));
Count++; Count++;
} }
public void EmitIRStream( public void EmitIRStream(
RegexIRGenerator stream RegexHIRGenerator stream
) )
{ {
if (stream is null || stream.Count == 0) { if (stream is null || stream.Count == 0) {
@@ -113,7 +105,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
string unit string unit
) )
{ {
_tail.Add(new RegexMatchSeqIR(unit)); _tail.Add(new RegexMatchSeqHIR(unit));
Count++; Count++;
} }
@@ -121,7 +113,16 @@ namespace StgSharp.RegularAnalysis.TextRegex
string unit string unit
) )
{ {
_tail.Add(new RegexMatchSetIR(unit)); _tail.Add(new RegexMatchSetHIR(unit));
Count++;
}
public void EmitPack(
bool condition,
string name = ""
)
{
_tail.Add(new RegexPackHIR(condition, name));
Count++; Count++;
} }
@@ -129,7 +130,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
bool isSuccess bool isSuccess
) )
{ {
_tail.Add(new RegexPopIR(isSuccess)); _tail.Add(new RegexPopHIR(isSuccess));
Count++; Count++;
} }
@@ -137,13 +138,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
int count int count
) )
{ {
_tail.Add(new RegexTryIR(count)); _tail.Add(new RegexTryHIR(count));
Count++; Count++;
} }
public List<RegexIR> ToList() public List<RegexHIR> ToList()
{ {
List<RegexIR> result = new(Count); List<RegexHIR> result = new(Count);
Node current = _head; Node current = _head;
while (current != null) while (current != null)
{ {
@@ -160,11 +161,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
public int Count => IRList.Count; public int Count => IRList.Count;
public List<RegexIR> IRList { get; } = []; public List<RegexHIR> IRList { get; } = [];
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add( public void Add(
RegexIR ir RegexHIR ir
) )
{ {
IRList.Add(ir); IRList.Add(ir);
@@ -37,7 +37,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
public partial class RegexInterpreter public partial class RegexInterpreter
{ {
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegexInterpreter.IRGenerate" // file="RegexInterpreter.HIRGenerate"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -38,13 +38,13 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
public partial class RegexInterpreter public partial class RegexInterpreter
{ {
internal static List<RegexIR> GenerateIR( internal static List<RegexHIR> GenerateIR(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
) )
{ {
return GenerateIRRecursion(tree.Root, 0).ToList(); return GenerateIRRecursion(tree.Root, 0).ToList();
@@ -53,7 +53,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EmitCount( private static void EmitCount(
int curDepth, int curDepth,
RegexIRGenerator ir, RegexHIRGenerator ir,
int min, int min,
int max, int max,
bool isGreedy, bool isGreedy,
@@ -66,7 +66,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
if (isRightOperator) // seq to be count is complex if (isRightOperator) // seq to be count is complex
{ {
ir.EmitCount(min, max, isGreedy, 1); ir.EmitCount(min, max, isGreedy, 1);
RegexIRGenerator rightIR = GenerateIRRecursion(right, curDepth + 1); RegexHIRGenerator rightIR = GenerateIRRecursion(right, curDepth + 1);
ir.EmitTry(rightIR.Count); ir.EmitTry(rightIR.Count);
ir.EmitIRStream(rightIR); ir.EmitIRStream(rightIR);
} else } else
@@ -89,9 +89,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
} }
private static RegexIRGenerator GenerateIRRecursion( private static RegexHIRGenerator GenerateIRRecursion(
RegexAstNode node, RegexAstNode node,
int depth int depth
) )
{ {
#pragma warning disable IDE0010 #pragma warning disable IDE0010
@@ -102,8 +102,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
int curDepth = depth; int curDepth = depth;
RegexAstNode left = node.Left; RegexAstNode left = node.Left;
RegexAstNode right = node.Right; RegexAstNode right = node.Right;
RegexIRGenerator leftIR; RegexHIRGenerator leftIR;
RegexIRGenerator rightIR; RegexHIRGenerator rightIR;
Token<RegexElementLabel> token = node.Source; Token<RegexElementLabel> token = node.Source;
@@ -116,15 +116,16 @@ namespace StgSharp.RegularAnalysis.TextRegex
bool is_group = group_source.Length == 1; // simple group: (pattern) bool is_group = group_source.Length == 1; // simple group: (pattern)
string group_name = string group_name =
group_source.StartsWith(@"(?<", StringComparison.Ordinal) ? group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
group_source[3..^2] : /* named group (?<name>) */ group_source[3..^1] : /* named group (?<name>) */
string.Empty; /* unnamed group (?:) */ string.Empty; /* unnamed group (?:) */
RegexIRGenerator groupIR = new(); RegexHIRGenerator groupIR = new();
rightIR = GenerateIRRecursion(right, curDepth + 1); rightIR = GenerateIRRecursion(right, curDepth + 1);
groupIR.EmitTry(rightIR.Count); groupIR.EmitTry(rightIR.Count);
groupIR.EmitIRStream(rightIR); groupIR.EmitIRStream(rightIR);
groupIR.EmitCondition(2, 1); groupIR.EmitCondition(2, 1);
/*generate pack ir here*/ groupIR.EmitPop(false);
groupIR.EmitPack(true, group_name);
return groupIR; return groupIR;
case RegexElementLabel.COUNT: case RegexElementLabel.COUNT:
string count_source = token.Value; string count_source = token.Value;
@@ -143,14 +144,14 @@ namespace StgSharp.RegularAnalysis.TextRegex
_ => throw new InvalidOperationException("Unexpected sequence mask for count operator"), _ => throw new InvalidOperationException("Unexpected sequence mask for count operator"),
}; };
ReadOnlySpan<char> count_source_span = count_source; ReadOnlySpan<char> count_source_span = count_source;
RegexIRGenerator countIR = new(); RegexHIRGenerator countIR = new();
if (count_source_span[0] == '*') // infinite count if (count_source_span[0] == '*') // infinite count
{ {
EmitCount(curDepth, countIR, 0, int.MaxValue, isGreedy, seq); EmitCount(curDepth, countIR, 0, -1, isGreedy, seq);
} else if (count_source_span[0] == '+') // at least one } else if (count_source_span[0] == '+') // at least one
{ {
EmitCount(curDepth, countIR, 1, int.MaxValue, isGreedy, seq); EmitCount(curDepth, countIR, 1, -1, isGreedy, seq);
} else if (count_source_span[0] == '?') // zero or one } else if (count_source_span[0] == '?') // zero or one
{ {
EmitCount(curDepth, countIR, 0, 1, isGreedy, seq); EmitCount(curDepth, countIR, 0, 1, isGreedy, seq);
@@ -159,7 +160,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int comma_pos = count_source_span.IndexOf(','); int comma_pos = count_source_span.IndexOf(',');
if (comma_pos == -1) if (comma_pos == -1)
{ {
if (int.TryParse(count_source[1..(isGreedy ? ^3 : ^2)], out int count)) if (int.TryParse(count_source[1..(isGreedy ? ^2 : ^1)], out int count))
{ {
EmitCount(curDepth, countIR, count, count, isGreedy, seq); EmitCount(curDepth, countIR, count, count, isGreedy, seq);
} else } else
@@ -169,7 +170,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
} else } else
{ {
if (int.TryParse(count_source[1..comma_pos], out int min) && if (int.TryParse(count_source[1..comma_pos], out int min) &&
int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^3 : ^2)], out int max)) int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^1 : ^2)], out int max))
{ {
EmitCount(curDepth, countIR, min, max, isGreedy, seq); EmitCount(curDepth, countIR, min, max, isGreedy, seq);
} else } else
@@ -184,7 +185,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
return countIR; return countIR;
case RegexElementLabel.CONCAT: case RegexElementLabel.CONCAT:
RegexIRGenerator concat = new(); RegexHIRGenerator concat = new();
// process left node // process left node
if (RegexAstNode.IsNullOrEmpty(left)) if (RegexAstNode.IsNullOrEmpty(left))
@@ -237,13 +238,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
return concat; return concat;
case RegexElementLabel.ALT: case RegexElementLabel.ALT:
RegexIRGenerator alt = new(); RegexHIRGenerator alt = new();
List<RegexIRGenerator> casesIRList = []; List<RegexHIRGenerator> casesIRList = [];
RegexAstNode cur_node = right; RegexAstNode cur_node = right;
int ir_count = 0; int ir_count = 0;
while (!RegexAstNode.IsNullOrEmpty(cur_node)) while (!RegexAstNode.IsNullOrEmpty(cur_node))
{ {
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1); RegexHIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1);
casesIRList.Add(_case); casesIRList.Add(_case);
ir_count += _case.Count; ir_count += _case.Count;
cur_node = cur_node.Next; cur_node = cur_node.Next;
@@ -251,7 +252,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
int case_count = casesIRList.Count; int case_count = casesIRList.Count;
while (case_count != 0) while (case_count != 0)
{ {
RegexIRGenerator cur_ir = casesIRList[^case_count]; RegexHIRGenerator cur_ir = casesIRList[^case_count];
ir_count -= cur_ir.Count; ir_count -= cur_ir.Count;
case_count--; case_count--;
alt.EmitTry(cur_ir.Count); alt.EmitTry(cur_ir.Count);
@@ -264,11 +265,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
case RegexElementLabel.UNIT_SPAN: case RegexElementLabel.UNIT_SPAN:
goto case RegexElementLabel.UNIT; goto case RegexElementLabel.UNIT;
case RegexElementLabel.UNIT: case RegexElementLabel.UNIT:
RegexIRGenerator unit_seq = new(); RegexHIRGenerator unit_seq = new();
unit_seq.EmitMatchSeq(node.Source.Value); unit_seq.EmitMatchSeq(node.Source.Value);
return unit_seq; return unit_seq;
case RegexElementLabel.UNIT_SET: case RegexElementLabel.UNIT_SET:
RegexIRGenerator unit_set = new(); RegexHIRGenerator unit_set = new();
unit_set.EmitMatchSet(node.Source.Value); unit_set.EmitMatchSet(node.Source.Value);
return unit_set; return unit_set;
default: default:
@@ -0,0 +1,69 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.IRScan"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
internal static void ScanAndMarkIROptimization(
RegexHIR ir
)
{
switch (ir.Command)
{
case RegexIRCommand.Match_Seq:
break;
case RegexIRCommand.Match_Set:
break;
case RegexIRCommand.Pack:
break;
case RegexIRCommand.Condition:
break;
case RegexIRCommand.Try:
break;
case RegexIRCommand.Count:
break;
case RegexIRCommand.Count_Seq:
break;
case RegexIRCommand.Count_Set:
break;
case RegexIRCommand.Pop:
break;
default:
break;
}
}
}
}
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.SourceGenerate"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public partial class RegexInterpreter
{
internal static string GetSourceCode(
IEnumerable<RegexHIR> irs
)
{
StringBuilder sb = new StringBuilder();
foreach (RegexHIR ir in irs) {
ScanAndMarkIROptimization(ir);
}
return sb.ToString();
}
}
}
@@ -35,7 +35,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
public partial class RegexInterpreter public partial class RegexInterpreter
{ {
+60
View File
@@ -0,0 +1,60 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexLIR"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
internal enum LIRCommand
{
Search = 0,
Count = 1,
Try = 2,
}
internal abstract class RegexLIR
{
public abstract LIRCommand Command { get; }
}
internal enum RegexSearchMethod
{
Match,
Find,
Assert,
}
}
@@ -34,7 +34,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal class RegexTokenParser : TokenParser<RegexElementLabel, RegexElementLabel> internal class RegexTokenParser : TokenParser<RegexElementLabel, RegexElementLabel>
{ {
@@ -34,7 +34,7 @@ using System.Security.Permissions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal class RegexTokenReader : ITokenReader<RegexElementLabel> internal class RegexTokenReader : ITokenReader<RegexElementLabel>
{ {
@@ -32,7 +32,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex namespace StgSharp.RegularAnalysis.Text
{ {
internal static class RegexTokenUtil internal static class RegexTokenUtil
{ {
@@ -0,0 +1,50 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TextHelper"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public static class TextHelper
{
public static bool TryLastIndexOf(
this ReadOnlySpan<char> span,
ReadOnlySpan<char> value,
out int index
)
{
index = span.LastIndexOf(value);
return index != -1;
}
}
}
@@ -0,0 +1,47 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TextRegex"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.Text
{
public abstract class TextRegex
{
public abstract bool IsMatch(
ReadOnlySpan<char> text
);
}
public ref struct MatchResult { }
}
+3 -3
View File
@@ -26,7 +26,7 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.RegularAnalysis.Abstraction; using StgSharp.RegularAnalysis.Abstraction;
using StgSharp.RegularAnalysis.TextRegex; using StgSharp.RegularAnalysis.Text;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -44,7 +44,7 @@ namespace StgSharp
@"abc[0-9]x|abc[0-9]y|abd[0-9]x|abd[0-9]y|xyz", @"abc[0-9]x|abc[0-9]y|abd[0-9]x|abd[0-9]y|xyz",
@"get_name|get_value|get_type|set_name|set_value|set_type", @"get_name|get_value|get_type|set_name|set_value|set_type",
@"helix|hello|help|hero|hers|her", @"helix|hello|help|hero|hers|her",
@"a*.[abc]*(abcd|a(bc)c)+(?<name>abc)" @"a*.[abc]*(abcd|a(bc)c)+(?<name>abc{11,13})"
]; ];
public static void Run() public static void Run()
@@ -61,7 +61,7 @@ namespace StgSharp
Console.Write(new string('|', enumerator.Depth)); Console.Write(new string('|', enumerator.Depth));
Console.WriteLine($"\t{node.Source.Value}\t{node.Source.Flag}"); Console.WriteLine($"\t{node.Source.Value}\t{node.Source.Flag}");
} }
List<RegexIR> ir = RegexInterpreter.GenerateIR(tree); List<RegexHIR> ir = RegexInterpreter.GenerateIR(tree);
Console.WriteLine("-----IR-----"); Console.WriteLine("-----IR-----");
for (int i = 0; i < ir.Count; i++) { for (int i = 0; i < ir.Count; i++) {
Console.WriteLine(ir[i].AsCommand(i)); Console.WriteLine(ir[i].AsCommand(i));
File diff suppressed because it is too large Load Diff
+56 -9
View File
@@ -14,15 +14,62 @@
使用DEFINE指令定义的符号定义在符号区中,符号的类型通过第一次赋值确定,源生成模式下,可以通过第一个赋值指令推导其类型 使用DEFINE指令定义的符号定义在符号区中,符号的类型通过第一次赋值确定,源生成模式下,可以通过第一个赋值指令推导其类型
## 算子 ## HIR算子
1. 搜索算子(SEARCH %str; SEARCH %unicode),向后搜索确定长度序列或者满足规则的字符集,更新活动指针,不更新锚点指针,搜索结果保存在全局上下文中 1. 搜索算子(SEARCH %str; SEARCH %unicode),向后搜索确定长度序列或者满足规则的字符集,更新活动指针,不更新锚点指针,搜索结果保存在全局上下文中
2. 搜索算子(SEARCH %set_expression),向后匹配一个满足字符集要求的字符,更新活动指针,不更新锚点指针,搜索结果保存在全局上下文中 2. 搜索算子(SEARCH %set_expression),向后匹配一个满足字符集要求的字符,更新活动指针,不更新锚点指针,搜索结果保存在全局上下文中
3. 失败配置指令(CINFIG_FAIL %fail),指定当前匹配栈后续如何处理一条指令的失败,可选为:FAST,立刻终止当前层级;CONTINUE,记录这个fail,但是继续操作;REVERSE,反过来,成功了才终止 3. 条件指令(CONDITION %cmd_true %cmd_fail),比较前一条SEARCH指令是否成功,并指定跳转到指定的位置,如果某种条件不需要跳转,可以写1。如果想要将fail向上级继续抛出,可以%cmd_fail写0.
4. 条件指令(CONDITION %cmd_true %cmd_fail),比较前一条SEARCH指令是否成功,并指定跳转到指定的位置,如果某种条件不需要跳转,可以写1。如果想要将fail向上级继续抛出,可以%cmd_fail写0. 4. 计数指令(COUNT %min %max %is_greedy %cmd),重复执行指令cmd,直到满足参数配置的数量为止
5. 跳转执行指令(TRY %begin %end),尝试idx指向的IR块,并记住执行结果数据 5. 快速序列计数指令(COUNT_SEQ %min %max %is_greedy %seq),快速搜索一个确定性序列,直到满足参数配置的数量为止
6. 计数指令(COUNT %min %max %is_greedy %cmd),重复执行指令cmd,直到满足参数配置的数量为止 6. 快速字符集计数指令(COUNT_SET %min %max %is_greedy %set),快速搜索一个字符集,直到满足参数配置的数量为止
7. 快速序列计数指令(COUNT_SEQ %min %max %is_greedy %seq),快速搜索一个确定性序列,直到满足参数配置的数量为止 7. 尝试(TRY %count),对自身向后的一段内容在新的匹配栈层级中进行运行,被分层的指令数量由COUNT指令确定
8. 快速字符集计数指令(COUNT_SET %min %max %is_greedy %set),快速搜索一个字符集,直到满足参数配置的数量为止 8. 弹出(POP %is_success),提前终止一个try块,并用is_success表示弹出行为视为匹配成功还是失败
9. 尝试(TRY %count),对自身向后的一段内容在新的匹配栈层级中进行运行,被分层的指令数量由COUNT指令确定 9. 打包指令(PACK %is_success %name),检查上一条语句的结果,将匹配结果打包并记录为指定的名字。如果名字为空,则使用默认的序号进行索引
10. 弹出(POP %is_success),提前终止一个try块,并用is_success表示弹出行为视为匹配成功还是失败
## 优化策略
应当注意的是,将HIT进行进一步优化或者展开的目的:
1. 尽可能降低源生成或者运行时执行中,对多指令上下文的依赖,尽可能保证每一个IR可以单独输出最佳的代码
2. 减少多余的结构,使得一行指令尽可能处理多的数据
### 语义改写优化
1. 计数器展开
建议在lower做
对于形如a+b或者a{2,4}b的正则表达式,可以优化为a*(ab|b)和a{2,3}(ab|b)的写法
2. 确定性计数展开
建议在lower做
对于形如a{m,n}的正则表达式,将其展开为a{m}a{0,n-m}的形式
3. charset合并
建议在ast-opt做
对于形如[ abc ]|[ def ]的表达式,将其合并为[ abcdef ]
4. charset展开
在lower做
对于形如abc[ de ]的表达式,如果字符集容量不超过4,可以站卡为(abcd|abce)形式
### 匹配策略改变
1. 长度预测
在source-gen阶段做
对于一个正则表达式,可以计算其所需的最小长度,可以在开始的时候提前进行比较
2. .*优化
在lower阶段做
对于COUNT_SET min=0 max=-1 "."指令,也就是.*操作,将后续指令从match转为find
3. [ set ]*优化
在source-gen阶段做
对于其他的COUNT_SET min=0 max=-1 "???"指令,使用IndexOf,IndexOfAnyExcept等进行优化
4. 首字母检查
**这个不好做,因为IR直接把ALT打碎成if-else串了**
对于(abc|defg|xyz)表达式,可以检查其首字母
### C# span opt trick
1. Span.StartWith
2. [ 0-9 ]展开为IndexOf("0123456789")