Working on tree optimizer
This commit is contained in:
@@ -28,7 +28,9 @@
|
||||
using StgSharp.RegularAnalysis.Abstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -43,9 +45,118 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
||||
List<RegexAstNode> cases = [];
|
||||
RegexAstNode current;
|
||||
_ = enumerator.MoveNext(); // skip root
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
RegexAstNode current = enumerator.Current;
|
||||
current = enumerator.Current;
|
||||
current.Parent.RemoveChild(current);
|
||||
current.Parent = RegexAstNode.Empty;
|
||||
if (current.Parent.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
continue;
|
||||
} else
|
||||
{
|
||||
cases.Add(current);
|
||||
enumerator.SkipNode(current);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < cases.Count - 1; i++)
|
||||
{
|
||||
cases[i].AppendNode(cases[i + 1]);
|
||||
cases[i].Parent = root;
|
||||
}
|
||||
return cases[0];
|
||||
}
|
||||
|
||||
private static void MergeConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
||||
return;
|
||||
}
|
||||
List<string> buffer = [];
|
||||
int length = 0;
|
||||
RegexAstNode right = root.Right;
|
||||
RegexAstNode left = root.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT) {
|
||||
return;
|
||||
}
|
||||
string str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
while (RegexAstNode.IsNullOrEmpty(right) &&
|
||||
right.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
left = right.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT)
|
||||
{
|
||||
break;
|
||||
}
|
||||
str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
root.Right = right.Right;
|
||||
|
||||
// TODO: remove right from tree
|
||||
right = root.Right;
|
||||
}
|
||||
str = string.Create(length, buffer, static(
|
||||
span,
|
||||
buffer
|
||||
) => {
|
||||
int offset = 0;
|
||||
for (int i = 0; i < buffer.Count; i++)
|
||||
{
|
||||
buffer[i].AsSpan().CopyTo(span[offset..]);
|
||||
offset += buffer[i].Length;
|
||||
}
|
||||
});
|
||||
left = root.Left;
|
||||
root.Left = new RegexAstNode(new Token<RegexElementLabel>(str, left.Source.Line,
|
||||
left.Source.Column,
|
||||
str.Length == 1 ?
|
||||
RegexElementLabel.UNIT :
|
||||
RegexElementLabel.UNIT_SPAN));
|
||||
}
|
||||
|
||||
private static void RotateConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
RegexAstNode left = root.Left;
|
||||
while (RegexAstNode.IsNullOrEmpty(left) &&
|
||||
left.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
/*
|
||||
* root → root
|
||||
* ╱ ╲ ╱ ╲
|
||||
* left c a left
|
||||
* ╱ ╲ ╱ ╲
|
||||
* a b b c
|
||||
*/
|
||||
RegexAstNode a = left.Left;
|
||||
RegexAstNode b = left.Right;
|
||||
RegexAstNode c = root.Right;
|
||||
root.Left = a;
|
||||
if (RegexAstNode.IsNullOrEmpty(a)) {
|
||||
a.Parent = root;
|
||||
}
|
||||
|
||||
root.Right = left;
|
||||
left.Left = b;
|
||||
left.Right = c;
|
||||
if (RegexAstNode.IsNullOrEmpty(b)) {
|
||||
b.Parent = left;
|
||||
}
|
||||
|
||||
if (RegexAstNode.IsNullOrEmpty(c)) {
|
||||
c.Parent = left;
|
||||
}
|
||||
left = root.Left;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user