scanning min scan length
This commit is contained in:
@@ -24,10 +24,11 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
{
|
||||
|
||||
internal static List<RegexIR> GenerateIR(
|
||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
|
||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree,
|
||||
ref RegexInfo info
|
||||
)
|
||||
{
|
||||
return GenerateIRRecursion(tree.Root, 0).ToList();
|
||||
return GenerateIRRecursion(tree.Root, 0, ref info).ToList();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -37,7 +38,8 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
int min,
|
||||
int max,
|
||||
bool isGreedy,
|
||||
RegexAstNode right
|
||||
RegexAstNode right,
|
||||
ref RegexInfo info
|
||||
)
|
||||
{
|
||||
bool is_operator = !RegexAstNode.IsNullOrEmpty(right) &&
|
||||
@@ -45,7 +47,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
|
||||
if (is_operator) // seq to be count is complex
|
||||
{
|
||||
RegexIRGenerator right_ir = GenerateIRRecursion(right, curDepth + 1);
|
||||
RegexIRGenerator right_ir = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
ir.EmitCountComplex(min, max, isGreedy, right_ir.Count);
|
||||
ir.EmitIRStream(right_ir);
|
||||
} else
|
||||
@@ -70,7 +72,8 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
|
||||
private static RegexIRGenerator GenerateIRRecursion(
|
||||
RegexAstNode node,
|
||||
int depth
|
||||
int depth,
|
||||
ref RegexInfo info
|
||||
)
|
||||
{
|
||||
#pragma warning disable IDE0010
|
||||
@@ -99,7 +102,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
string.Empty; /* unnamed group (?:) */
|
||||
|
||||
RegexIRGenerator groupIR = new();
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1);
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
groupIR.EmitTry(rightIR.Count);
|
||||
groupIR.EmitIRStream(rightIR);
|
||||
groupIR.EmitCondition(2, 1);
|
||||
@@ -109,31 +112,19 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
case RegexElementLabel.COUNT:
|
||||
string count_source = token.Value;
|
||||
bool isGreedy = !(count_source.Length > 1 && count_source.EndsWith('?'));
|
||||
int seq_mask = (RegexAstNode.IsNullOrEmpty(left) ? 0 : 2) + (RegexAstNode.IsNullOrEmpty(right) ?
|
||||
0 :
|
||||
1);
|
||||
RegexAstNode seq = seq_mask switch
|
||||
{
|
||||
// no seq, invalid count operator
|
||||
0 => throw new InvalidOperationException("Count operator must have a sequence to operate on"),
|
||||
1 => right,// right seq
|
||||
2 => left,// left seq
|
||||
// both sides, invalid count operator
|
||||
3 => throw new InvalidOperationException("Count operator cannot have two sequences to operate on"),
|
||||
_ => throw new InvalidOperationException("Unexpected sequence mask for count operator"),
|
||||
};
|
||||
RegexAstNode seq = right;
|
||||
ReadOnlySpan<char> count_source_span = count_source;
|
||||
RegexIRGenerator count_ir = new();
|
||||
|
||||
if (count_source_span[0] == '*') // infinite count
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 0, -1, isGreedy, seq);
|
||||
EmitCount(curDepth, count_ir, 0, -1, isGreedy, seq, ref info);
|
||||
} else if (count_source_span[0] == '+') // at least one
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 1, -1, isGreedy, seq);
|
||||
EmitCount(curDepth, count_ir, 1, -1, isGreedy, seq, ref info);
|
||||
} else if (count_source_span[0] == '?') // zero or one
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 0, 1, isGreedy, seq);
|
||||
EmitCount(curDepth, count_ir, 0, 1, isGreedy, seq, ref info);
|
||||
} else if (count_source_span[0] == '{') // zero or one, non-greedy
|
||||
{
|
||||
int comma_pos = count_source_span.IndexOf(',');
|
||||
@@ -141,7 +132,8 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
{
|
||||
if (int.TryParse(count_source[1..(isGreedy ? ^2 : ^1)], out int count))
|
||||
{
|
||||
EmitCount(curDepth, count_ir, count, count, isGreedy, seq);
|
||||
EmitCount(curDepth, count_ir, count, count, isGreedy, seq,
|
||||
ref info);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
@@ -151,7 +143,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
if (int.TryParse(count_source[1..comma_pos], out int min) &&
|
||||
int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^1 : ^2)], out int max))
|
||||
{
|
||||
EmitCount(curDepth, count_ir, min, max, isGreedy, seq);
|
||||
EmitCount(curDepth, count_ir, min, max, isGreedy, seq, ref info);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
@@ -187,7 +179,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
}
|
||||
} else if ((left.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
leftIR = GenerateIRRecursion(left, curDepth + 1);
|
||||
leftIR = GenerateIRRecursion(left, curDepth + 1, ref info);
|
||||
concat.EmitIRStream(leftIR);
|
||||
}
|
||||
|
||||
@@ -212,7 +204,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
}
|
||||
} else if ((right.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1);
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
concat.EmitIRStream(rightIR);
|
||||
}
|
||||
return concat;
|
||||
@@ -223,7 +215,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
int ir_count = 0;
|
||||
while (!RegexAstNode.IsNullOrEmpty(cur_node))
|
||||
{
|
||||
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1);
|
||||
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1, ref info);
|
||||
casesIRList.Add(_case);
|
||||
ir_count += _case.Count;
|
||||
cur_node = cur_node.Next;
|
||||
@@ -255,6 +247,41 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
return null!;
|
||||
}
|
||||
#pragma warning restore IDE0010
|
||||
|
||||
|
||||
// info.MinPredictLength = ComputeMinLength(tree.Root);
|
||||
|
||||
static int ComputeMinLength(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
if (node.EqualityTypeConvert is RegexElementLabel.UNIT_SPAN or
|
||||
RegexElementLabel.UNIT)
|
||||
{
|
||||
return node.Value.Length;
|
||||
} else if (node.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
return ComputeMinLength(node.Left) + ComputeMinLength(node.Right);
|
||||
} else if (node.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
RegexAstNode case_0 = node.Right;
|
||||
int length = int.MaxValue;
|
||||
do
|
||||
{
|
||||
length = int.Min(ComputeMinLength(case_0), length);
|
||||
case_0 = case_0.Next;
|
||||
} while (!RegexAstNode.IsNullOrEmpty(case_0));
|
||||
return length;
|
||||
} else if (node.EqualityTypeConvert == RegexElementLabel.COUNT)
|
||||
{
|
||||
string source = node.Source.Value;
|
||||
int single_count = ComputeMinLength(node.Right);
|
||||
return 0;
|
||||
} else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user