add COUNT and CONCAT ir emitter
This commit is contained in:
@@ -28,8 +28,12 @@
|
||||
using StgSharp.RegularAnalysis.Abstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Data;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
@@ -45,27 +49,68 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
|
||||
)
|
||||
{
|
||||
return GenerateIRRecursion(tree.Root, 0);
|
||||
return GenerateIRRecursion(tree.Root, 0).ToList();
|
||||
}
|
||||
|
||||
private List<RegexIR> GenerateIRRecursion(
|
||||
RegexAstNode node,
|
||||
int depth
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void EmitCount(
|
||||
int curDepth,
|
||||
RegexIRGenerator ir,
|
||||
int min,
|
||||
int max,
|
||||
bool isGreedy,
|
||||
RegexAstNode right
|
||||
)
|
||||
{
|
||||
if (depth > 1024) {
|
||||
bool isRightOperator = !RegexAstNode.IsNullOrEmpty(right) &&
|
||||
(right.Source.Flag & RegexElementLabel.OPERATOR) != 0;
|
||||
|
||||
if (isRightOperator) // seq to be count is complex
|
||||
{
|
||||
ir.EmitCount(min, max, isGreedy, 1);
|
||||
RegexIRGenerator rightIR = GenerateIRRecursion(right, curDepth + 1);
|
||||
ir.EmitTry(rightIR.Count);
|
||||
ir.EmitIRStream(rightIR);
|
||||
} else
|
||||
{
|
||||
RegexElementLabel count_source_mask = right.Source.Flag;
|
||||
switch (count_source_mask)
|
||||
{
|
||||
case RegexElementLabel.UNIT:
|
||||
ir.EmitCountSeq(min, max, isGreedy, right.Source.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
ir.EmitCountSet(min, max, isGreedy, right.Source.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
ir.EmitCountSet(min, max, isGreedy, right.Source.Value);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException($"Unexpected regex element type for count operator: {count_source_mask}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RegexIRGenerator GenerateIRRecursion(
|
||||
RegexAstNode node,
|
||||
int depth
|
||||
)
|
||||
{
|
||||
#pragma warning disable IDE0010
|
||||
if (depth > 1024)
|
||||
{
|
||||
throw new OverflowException("Regex parse exceeds maximum recursion depth");
|
||||
}
|
||||
int curDepth = depth;
|
||||
RegexAstNode left = node.Left;
|
||||
RegexAstNode right = node.Right;
|
||||
List<RegexIR> leftIR = EmptyIR;
|
||||
List<RegexIR> rightIR = EmptyIR;
|
||||
RegexIRGenerator leftIR;
|
||||
RegexIRGenerator rightIR;
|
||||
|
||||
bool isLeftOperator = !RegexAstNode.IsNullOrEmpty(left) &&
|
||||
(left.Source.Flag & RegexElementLabel.OPERATOR) != 0;
|
||||
(left.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0;
|
||||
bool isRightOperator = !RegexAstNode.IsNullOrEmpty(right) &&
|
||||
(right.Source.Flag & RegexElementLabel.OPERATOR) != 0;
|
||||
(right.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0;
|
||||
|
||||
Token<RegexElementLabel> token = node.Source;
|
||||
|
||||
@@ -73,30 +118,127 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
case RegexElementLabel.GROUP_BEGIN:
|
||||
string group_source = token.Value;
|
||||
bool is_group = group_source.Length == 1;
|
||||
string group_name = group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
|
||||
group_source[3..^2] :
|
||||
string.Empty;
|
||||
|
||||
List<RegexIR> groupIR = [];
|
||||
// process group type
|
||||
bool is_group = group_source.Length == 1; // simple group: (pattern)
|
||||
string group_name =
|
||||
group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
|
||||
group_source[3..^2] : /* named group (?<name>) */
|
||||
string.Empty; /* unnamed group (?:) */
|
||||
|
||||
RegexIRGenerator groupIR = new();
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1);
|
||||
groupIR.Add(RegexIR.Try(rightIR.Count));
|
||||
groupIR.AddRange(rightIR);
|
||||
groupIR.Add(RegexIR.Condition(2, 1));
|
||||
groupIR.Add(RegexIR.PopLevel(false));
|
||||
groupIR.Add(RegexIR.PopLevel(true));
|
||||
break;
|
||||
case RegexElementLabel.COUNT:
|
||||
break;
|
||||
case RegexElementLabel.CONCAT:
|
||||
break;
|
||||
case RegexElementLabel.ALT:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
groupIR.EmitTry(rightIR.Count);
|
||||
groupIR.EmitIRStream(rightIR);
|
||||
groupIR.EmitCondition(2, 1);
|
||||
groupIR.EmitPopLevel(false);
|
||||
|
||||
return [];
|
||||
// a grouping op needed here, before popping
|
||||
groupIR.EmitPopLevel(true);
|
||||
return groupIR;
|
||||
case RegexElementLabel.COUNT:
|
||||
string count_source = token.Value;
|
||||
bool isGreedy = !(count_source.Length > 1 && !count_source.EndsWith('?'));
|
||||
ReadOnlySpan<char> count_source_span = count_source;
|
||||
RegexIRGenerator countIR = new();
|
||||
|
||||
if (count_source_span[0] == '*') // infinite count
|
||||
{
|
||||
EmitCount(curDepth, countIR, 0, int.MaxValue, isGreedy, right);
|
||||
} else if (count_source_span[0] == '+') // at least one
|
||||
{
|
||||
EmitCount(curDepth, countIR, 1, int.MaxValue, isGreedy, right);
|
||||
} else if (count_source_span[0] == '?') // zero or one
|
||||
{
|
||||
EmitCount(curDepth, countIR, 0, 1, isGreedy, right);
|
||||
} else if (count_source_span[0] == '{') // zero or one, non-greedy
|
||||
{
|
||||
int comma_pos = count_source_span.IndexOf(',');
|
||||
if (comma_pos == -1)
|
||||
{
|
||||
if (int.TryParse(count_source[1..(isGreedy ? ^3 : ^2)], out int count))
|
||||
{
|
||||
EmitCount(curDepth, countIR, count, count, isGreedy, right);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (int.TryParse(count_source[1..comma_pos], out int min) &&
|
||||
int.TryParse(count_source[(comma_pos + 1)..(isGreedy ? ^3 : ^2)], out int max))
|
||||
{
|
||||
EmitCount(curDepth, countIR, min, max, isGreedy, right);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid count format: {count_source}");
|
||||
}
|
||||
}
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
} // unknown count format
|
||||
|
||||
return countIR;
|
||||
case RegexElementLabel.CONCAT:
|
||||
RegexIRGenerator concat = new();
|
||||
|
||||
// process left node
|
||||
if (RegexAstNode.IsNullOrEmpty(left))
|
||||
{
|
||||
// do nothing
|
||||
} else if ((left.Source.Flag & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
switch (left.Source.Flag)
|
||||
{
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
goto case RegexElementLabel.UNIT_SPAN;
|
||||
case RegexElementLabel.UNIT:
|
||||
concat.EmitMatchSeq(left.Source.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
concat.EmitMatchSet(left.Source.Value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if ((left.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
leftIR = GenerateIRRecursion(left, curDepth + 1);
|
||||
concat.EmitIRStream(leftIR);
|
||||
}
|
||||
|
||||
// process right node
|
||||
if (RegexAstNode.IsNullOrEmpty(right))
|
||||
{
|
||||
// do nothing
|
||||
} else if ((right.Source.Flag & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
switch (right.Source.Flag)
|
||||
{
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
goto case RegexElementLabel.UNIT;
|
||||
case RegexElementLabel.UNIT:
|
||||
concat.EmitMatchSeq(right.Source.Value);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
concat.EmitMatchSet(right.Source.Value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if ((right.Source.Flag & RegexElementLabel.VAST_OPERATOR) != 0)
|
||||
{
|
||||
rightIR = GenerateIRRecursion(right, curDepth + 1);
|
||||
concat.EmitIRStream(rightIR);
|
||||
}
|
||||
return concat;
|
||||
case RegexElementLabel.ALT:
|
||||
return null!;
|
||||
default:
|
||||
return null!;
|
||||
}
|
||||
#pragma warning restore IDE0010
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user