source gen and ir gen now use the same seq generator
This commit is contained in:
@@ -28,7 +28,9 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
ref RegexInfo info
|
||||
)
|
||||
{
|
||||
return GenerateIRRecursion(tree.Root, 0, ref info).ToList();
|
||||
List<RegexIR> list = GenerateIRRecursion(tree.Root, 0, out int length).ToList();
|
||||
info.MinPredictLength = length;
|
||||
return list;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -39,7 +41,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
int max,
|
||||
bool isGreedy,
|
||||
RegexAstNode right,
|
||||
ref RegexInfo info
|
||||
out int minLength
|
||||
)
|
||||
{
|
||||
bool is_operator = !RegexAstNode.IsNullOrEmpty(right) &&
|
||||
@@ -47,22 +49,25 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
|
||||
if (is_operator) // seq to be count is complex
|
||||
{
|
||||
RegexIRGenerator right_ir = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
ir.EmitCountComplex(min, max, isGreedy, right_ir.Count);
|
||||
ir.EmitIRStream(right_ir);
|
||||
RegexIRGenerator right_ir = GenerateIRRecursion(right, curDepth + 1, out minLength);
|
||||
_ = ir.EmitCountComplex(min, max, isGreedy, right_ir.Count);
|
||||
_ = ir.EmitIRStream(right_ir);
|
||||
} else
|
||||
{
|
||||
RegexElementLabel count_source_mask = right.Source.Flag;
|
||||
switch (count_source_mask)
|
||||
{
|
||||
case RegexElementLabel.UNIT:
|
||||
ir.EmitCountSeq(min, max, isGreedy, right.Source.Value);
|
||||
minLength = 1;
|
||||
_ = ir.EmitCountSeq(min, max, isGreedy, right.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
ir.EmitCountSet(min, max, isGreedy, right.Source.Value);
|
||||
minLength = 1;
|
||||
_ = ir.EmitCountSet(min, max, isGreedy, right.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
ir.EmitCountSeq(min, max, isGreedy, right.Source.Value);
|
||||
minLength = 1;
|
||||
_ = ir.EmitCountSeq(min, max, isGreedy, right.Value);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException($"Unexpected regex element type for count operator: {count_source_mask}");
|
||||
@@ -73,7 +78,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
private static RegexIRGenerator GenerateIRRecursion(
|
||||
RegexAstNode node,
|
||||
int depth,
|
||||
ref RegexInfo info
|
||||
out int minLength
|
||||
)
|
||||
{
|
||||
#pragma warning disable IDE0010
|
||||
@@ -102,12 +107,12 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
string.Empty; /* unnamed group (?:) */
|
||||
|
||||
RegexIRGenerator groupIR = new();
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
groupIR.EmitTry(rightIR.Count);
|
||||
groupIR.EmitIRStream(rightIR);
|
||||
groupIR.EmitCondition(2, 1);
|
||||
groupIR.EmitPop(false);
|
||||
groupIR.EmitPack(true, group_name);
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, out minLength);
|
||||
_ = groupIR.EmitTry(rightIR.Count)
|
||||
.EmitIRStream(rightIR)
|
||||
.EmitCondition(2, 1)
|
||||
.EmitPop(false)
|
||||
.EmitPack(true, group_name);
|
||||
return groupIR;
|
||||
case RegexElementLabel.COUNT:
|
||||
string count_source = token.Value;
|
||||
@@ -115,16 +120,19 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
RegexAstNode seq = right;
|
||||
ReadOnlySpan<char> count_source_span = count_source;
|
||||
RegexIRGenerator count_ir = new();
|
||||
|
||||
int length_once;
|
||||
if (count_source_span[0] == '*') // infinite count
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 0, -1, isGreedy, seq, ref info);
|
||||
EmitCount(curDepth, count_ir, 0, -1, isGreedy, seq, out _);
|
||||
minLength = 0;
|
||||
} else if (count_source_span[0] == '+') // at least one
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 1, -1, isGreedy, seq, ref info);
|
||||
EmitCount(curDepth, count_ir, 1, -1, isGreedy, seq, out length_once);
|
||||
minLength = length_once;
|
||||
} else if (count_source_span[0] == '?') // zero or one
|
||||
{
|
||||
EmitCount(curDepth, count_ir, 0, 1, isGreedy, seq, ref info);
|
||||
EmitCount(curDepth, count_ir, 0, 1, isGreedy, seq, out _);
|
||||
minLength = 0;
|
||||
} else if (count_source_span[0] == '{') // zero or one, non-greedy
|
||||
{
|
||||
int comma_pos = count_source_span.IndexOf(',');
|
||||
@@ -133,7 +141,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,
|
||||
ref info);
|
||||
out length_once);
|
||||
minLength = length_once * count;
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
@@ -143,7 +152,9 @@ 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, ref info);
|
||||
EmitCount(curDepth, count_ir, min, max, isGreedy, seq,
|
||||
out length_once);
|
||||
minLength = length_once * min;
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
@@ -159,6 +170,7 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
RegexIRGenerator concat = new();
|
||||
|
||||
// process left node
|
||||
int left_length = 0;
|
||||
if (RegexAstNode.IsNullOrEmpty(left))
|
||||
{
|
||||
// do nothing
|
||||
@@ -167,23 +179,28 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
switch (left.Source.Flag)
|
||||
{
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
goto case RegexElementLabel.UNIT;
|
||||
left_length = left.Value.Length;
|
||||
_ = concat.EmitMatchSeq(left.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT:
|
||||
concat.EmitMatchSeq(left.Source.Value);
|
||||
left_length = 1;
|
||||
_ = concat.EmitMatchSeq(left.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
concat.EmitMatchSet(left.Source.Value);
|
||||
left_length = 1;
|
||||
_ = concat.EmitMatchSet(left.Value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if ((left.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
leftIR = GenerateIRRecursion(left, curDepth + 1, ref info);
|
||||
concat.EmitIRStream(leftIR);
|
||||
leftIR = GenerateIRRecursion(left, curDepth + 1, out left_length);
|
||||
_ = concat.EmitIRStream(leftIR);
|
||||
}
|
||||
|
||||
// process right node
|
||||
int right_length = 0;
|
||||
if (RegexAstNode.IsNullOrEmpty(right))
|
||||
{
|
||||
// do nothing
|
||||
@@ -192,30 +209,37 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
switch (right.Source.Flag)
|
||||
{
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
goto case RegexElementLabel.UNIT;
|
||||
right_length = right.Value.Length;
|
||||
_ = concat.EmitMatchSeq(right.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT:
|
||||
concat.EmitMatchSeq(right.Source.Value);
|
||||
right_length = 1;
|
||||
_ = concat.EmitMatchSeq(right.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
concat.EmitMatchSet(right.Source.Value);
|
||||
right_length = 1;
|
||||
_ = concat.EmitMatchSet(right.Value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if ((right.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, ref info);
|
||||
concat.EmitIRStream(rightIR);
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1, out right_length);
|
||||
_ = concat.EmitIRStream(rightIR);
|
||||
}
|
||||
minLength = left_length + right_length;
|
||||
return concat;
|
||||
case RegexElementLabel.ALT:
|
||||
RegexIRGenerator alt = new();
|
||||
List<RegexIRGenerator> casesIRList = [];
|
||||
RegexAstNode cur_node = right;
|
||||
int ir_count = 0;
|
||||
int min_case_length = int.MaxValue;
|
||||
while (!RegexAstNode.IsNullOrEmpty(cur_node))
|
||||
{
|
||||
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1, ref info);
|
||||
RegexIRGenerator _case = GenerateIRRecursion(cur_node, curDepth + 1, out int case_length);
|
||||
min_case_length = int.Min(case_length, min_case_length);
|
||||
casesIRList.Add(_case);
|
||||
ir_count += _case.Count;
|
||||
cur_node = cur_node.Next;
|
||||
@@ -226,24 +250,31 @@ namespace StgSharp.RegularAnalysis.Text
|
||||
RegexIRGenerator cur_ir = casesIRList[^case_count];
|
||||
ir_count -= cur_ir.Count;
|
||||
case_count--;
|
||||
alt.EmitTry(cur_ir.Count);
|
||||
alt.EmitIRStream(cur_ir);
|
||||
alt.EmitCondition(1/* need to calc here */, 2);
|
||||
alt.EmitPop(true);
|
||||
_ = alt.EmitTry(cur_ir.Count)
|
||||
.EmitIRStream(cur_ir)
|
||||
.EmitCondition(1/* need to calc here */, 2)
|
||||
.EmitPop(true);
|
||||
}
|
||||
alt.EmitPop(false);
|
||||
_ = alt.EmitPop(false);
|
||||
minLength = min_case_length;
|
||||
return alt;
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
goto case RegexElementLabel.UNIT;
|
||||
RegexIRGenerator unit = new();
|
||||
_ = unit.EmitMatchSeq(node.Value);
|
||||
minLength = node.Value.Length;
|
||||
return unit;
|
||||
case RegexElementLabel.UNIT:
|
||||
RegexIRGenerator unit_seq = new();
|
||||
unit_seq.EmitMatchSeq(node.Source.Value);
|
||||
_ = unit_seq.EmitMatchSeq(node.Value);
|
||||
minLength = 1;
|
||||
return unit_seq;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
RegexIRGenerator unit_set = new();
|
||||
unit_set.EmitMatchSet(node.Source.Value);
|
||||
_ = unit_set.EmitMatchSet(node.Value);
|
||||
minLength = 1;
|
||||
return unit_set;
|
||||
default:
|
||||
minLength = 0;
|
||||
return null!;
|
||||
}
|
||||
#pragma warning restore IDE0010
|
||||
|
||||
Reference in New Issue
Block a user