Optimizer is almost ok. Will go on source generate
This commit is contained in:
@@ -106,6 +106,8 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
)
|
)
|
||||||
: this(tree.Root) { }
|
: this(tree.Root) { }
|
||||||
|
|
||||||
|
public int Depth => _stack.Count;
|
||||||
|
|
||||||
public TNode Current
|
public TNode Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|||||||
@@ -56,11 +56,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
public static RegexAstNode Empty { get; }
|
public static RegexAstNode Empty { get; }
|
||||||
= new EmptyRegexAstNode();
|
= new EmptyRegexAstNode();
|
||||||
|
|
||||||
public virtual RegexAstNode Previous { get; set; }
|
public RegexAstNode Previous { get; set; }
|
||||||
|
|
||||||
public virtual RegexAstNode Next { get; set; }
|
public RegexAstNode Next { get; set; }
|
||||||
|
|
||||||
public virtual RegexAstNode Parent { get; set; }
|
public RegexAstNode Parent { get; set; }
|
||||||
|
|
||||||
public RegexAstNode Left
|
public RegexAstNode Left
|
||||||
{
|
{
|
||||||
@@ -69,7 +69,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
{
|
{
|
||||||
field?.Parent = Empty;
|
field?.Parent = Empty;
|
||||||
field = value;
|
field = value;
|
||||||
value.Parent = this;
|
if (!IsNullOrEmpty(value)) {
|
||||||
|
value.Parent = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +82,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
{
|
{
|
||||||
field?.Parent = Empty;
|
field?.Parent = Empty;
|
||||||
field = value;
|
field = value;
|
||||||
value.Parent = this;
|
if (!IsNullOrEmpty(value)) {
|
||||||
|
value.Parent = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +97,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
|
|
||||||
public string CodeConvertTemplate { get; }
|
public string CodeConvertTemplate { get; }
|
||||||
|
|
||||||
public Token<RegexElementLabel> Source { get; private set; }
|
public Token<RegexElementLabel> Source { get; private protected set; }
|
||||||
|
|
||||||
public void AppendNode(
|
public void AppendNode(
|
||||||
RegexAstNode nextToken
|
RegexAstNode nextToken
|
||||||
@@ -123,7 +127,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
RegexAstNode node
|
RegexAstNode node
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return node is null || node is EmptyRegexAstNode;
|
return node is null || ReferenceEquals(node, Empty);
|
||||||
}
|
}
|
||||||
/**/
|
/**/
|
||||||
|
|
||||||
@@ -154,6 +158,23 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
{
|
{
|
||||||
return new EmptyRegexAstNode();
|
return new EmptyRegexAstNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void RemoveAndAsChild(
|
||||||
|
bool useLeft
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (useLeft)
|
||||||
|
{
|
||||||
|
Source = Left.Source;
|
||||||
|
Left.Parent = Empty;
|
||||||
|
Left = Empty;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Source = Right.Source;
|
||||||
|
Right.Parent = Empty;
|
||||||
|
Right = Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
/**/
|
/**/
|
||||||
|
|
||||||
internal void ReplaceBy(
|
internal void ReplaceBy(
|
||||||
@@ -163,10 +184,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
Source = node.Source;
|
Source = node.Source;
|
||||||
Left = node.Left;
|
Left = node.Left;
|
||||||
Right = node.Right;
|
Right = node.Right;
|
||||||
if (IsNullOrEmpty(Left)) {
|
if (!IsNullOrEmpty(Left)) {
|
||||||
Left.Parent = this;
|
Left.Parent = this;
|
||||||
}
|
}
|
||||||
if (IsNullOrEmpty(Right)) {
|
if (!IsNullOrEmpty(Right)) {
|
||||||
Right.Parent = this;
|
Right.Parent = this;
|
||||||
}
|
}
|
||||||
Previous = node.Previous;
|
Previous = node.Previous;
|
||||||
@@ -188,18 +209,6 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
|
|
||||||
public EmptyRegexAstNode() : base(Token<RegexElementLabel>.Empty) { }
|
public EmptyRegexAstNode() : base(Token<RegexElementLabel>.Empty) { }
|
||||||
|
|
||||||
public override RegexAstNode Next
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set { field = Empty; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override RegexAstNode Previous
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set { field = Empty; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,14 +35,15 @@ using System.Runtime.CompilerServices;
|
|||||||
using System.Security.Permissions;
|
using System.Security.Permissions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace StgSharp.RegularAnalysis.TextRegex
|
namespace StgSharp.RegularAnalysis.TextRegex
|
||||||
{
|
{
|
||||||
public partial class RegexInterpreter
|
public partial class RegexInterpreter
|
||||||
{
|
{
|
||||||
|
|
||||||
private static List<RegexAstNode> FlattenAlt(
|
private static RegexAstNode FlattenAlt(
|
||||||
RegexAstNode root
|
RegexAstNode root
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
||||||
@@ -59,7 +60,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
}
|
}
|
||||||
current.Parent.RemoveChild(current);
|
current.Parent.RemoveChild(current);
|
||||||
}
|
}
|
||||||
return cases;
|
for (int i = 0; i < cases.Count - 1; i++)
|
||||||
|
{
|
||||||
|
cases[i].AppendNode(cases[i + 1]);
|
||||||
|
cases[i].Parent = root;
|
||||||
|
}
|
||||||
|
cases[^1].Parent = root;
|
||||||
|
return cases[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool MergeConcat(
|
private static bool MergeConcat(
|
||||||
@@ -69,6 +76,25 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* concat → 'a'
|
||||||
|
* ╱
|
||||||
|
* 'a'
|
||||||
|
*/
|
||||||
|
if ((root.Left.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||||
|
RegexAstNode.IsNullOrEmpty(root.Right))
|
||||||
|
{
|
||||||
|
root.RemoveAndAsChild(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ((root.Right.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||||
|
RegexAstNode.IsNullOrEmpty(root.Left))
|
||||||
|
{
|
||||||
|
root.RemoveAndAsChild(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* concat → concat
|
* concat → concat
|
||||||
* ╱ ╲ /
|
* ╱ ╲ /
|
||||||
@@ -184,7 +210,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
UnionAlt union = new(0, cases);
|
UnionAlt union = new(0, cases);
|
||||||
return union.AsNormal();
|
RegexAstNode root = union.AsNormal();
|
||||||
|
|
||||||
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UnionAlt : RegexAstNode
|
private class UnionAlt : RegexAstNode
|
||||||
@@ -196,11 +224,21 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
int level,
|
int level,
|
||||||
RegexAstNode node
|
RegexAstNode node
|
||||||
)
|
)
|
||||||
: base(new Token<RegexElementLabel>(string.Empty, 0, 0, 0))
|
: base(node.Source)
|
||||||
{
|
{
|
||||||
Level = level;
|
Level = level;
|
||||||
Cases = new List<RegexAstNode>();
|
Cases = new List<RegexAstNode>();
|
||||||
Cases.Add(node.Right ?? Empty);
|
|
||||||
|
RegexAstNode right = IsNullOrEmpty(node.Right) ?
|
||||||
|
new RegexAstNode(
|
||||||
|
new Token<RegexElementLabel>(node.Value, 0, node.Source.Column,
|
||||||
|
RegexElementLabel.UNIT)
|
||||||
|
) :
|
||||||
|
node.Right;
|
||||||
|
Cases.Add(right);
|
||||||
|
|
||||||
|
node.PrependNode(Empty);
|
||||||
|
node.AppendNode(Empty);
|
||||||
UnionBegin = node;
|
UnionBegin = node;
|
||||||
UnionEnd = node;
|
UnionEnd = node;
|
||||||
}
|
}
|
||||||
@@ -209,7 +247,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
int level,
|
int level,
|
||||||
List<RegexAstNode> c
|
List<RegexAstNode> c
|
||||||
)
|
)
|
||||||
: base(new Token<RegexElementLabel>(string.Empty, 0, 0, 0))
|
: base(c[0].Source)
|
||||||
{
|
{
|
||||||
Cases = c;
|
Cases = c;
|
||||||
Level = level;
|
Level = level;
|
||||||
@@ -233,6 +271,17 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
List<RegexAstNode> buffer = [];
|
List<RegexAstNode> buffer = [];
|
||||||
for (int i = 0; i < Cases.Count; i++)
|
for (int i = 0; i < Cases.Count; i++)
|
||||||
{
|
{
|
||||||
|
if ((Cases[i].EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0)
|
||||||
|
{
|
||||||
|
RegexAstNode concat = new RegexAstNode(new Token<RegexElementLabel>(string.Empty,
|
||||||
|
0,
|
||||||
|
Cases[i].Source.Column,
|
||||||
|
RegexElementLabel.CONCAT));
|
||||||
|
concat.Left = Cases[i];
|
||||||
|
Cases[i].Parent = concat;
|
||||||
|
concat.Right = Empty;
|
||||||
|
Cases[i] = concat;
|
||||||
|
}
|
||||||
RegexAstNode current = Cases[i];
|
RegexAstNode current = Cases[i];
|
||||||
int j = 0;
|
int j = 0;
|
||||||
for (; j < buffer.Count; j++)
|
for (; j < buffer.Count; j++)
|
||||||
@@ -258,11 +307,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
buffer.Add(current);
|
buffer.Add(current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Cases = buffer;
|
if (Cases.Count != buffer.Count) {
|
||||||
|
Cases = buffer;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO problem1 没处理case链表
|
if (buffer.Count == 1)
|
||||||
// TODO problem2 没有处理立刻返回策略
|
|
||||||
if (Cases.Count == 1)
|
|
||||||
{
|
{
|
||||||
// if there is only one case,
|
// if there is only one case,
|
||||||
// then we suppose that its contained cases has a same beginning,
|
// then we suppose that its contained cases has a same beginning,
|
||||||
@@ -276,6 +325,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
{
|
{
|
||||||
alt.UnionBegin.Parent = UnionEnd;
|
alt.UnionBegin.Parent = UnionEnd;
|
||||||
UnionEnd.Right = alt.UnionBegin;
|
UnionEnd.Right = alt.UnionBegin;
|
||||||
|
UnionEnd = alt.UnionBegin;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cases = alt.Cases;
|
Cases = alt.Cases;
|
||||||
@@ -291,18 +341,34 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
Cases[i] = union.AsNormal();
|
Cases[i] = union.AsNormal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RegexAstNode branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
|
||||||
UnionEnd.Source.Column,
|
RegexAstNode branch;
|
||||||
RegexElementLabel.ALT));
|
if (IsNullOrEmpty(UnionEnd))
|
||||||
UnionEnd.Right = branch;
|
{
|
||||||
branch.Parent = UnionEnd;
|
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||||
UnionEnd = branch;
|
Source.Column,
|
||||||
|
RegexElementLabel.ALT));
|
||||||
|
UnionBegin = branch;
|
||||||
|
UnionEnd = branch;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||||
|
UnionEnd.Source.Column,
|
||||||
|
RegexElementLabel.ALT));
|
||||||
|
UnionEnd.Right = branch;
|
||||||
|
branch.Parent = UnionEnd;
|
||||||
|
UnionEnd = branch;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < Cases.Count - 1; i++)
|
for (int i = 0; i < Cases.Count - 1; i++)
|
||||||
{
|
{
|
||||||
|
Cases[i] ??= NewEmptyNode();
|
||||||
Cases[i].AppendNode(Cases[i + 1]);
|
Cases[i].AppendNode(Cases[i + 1]);
|
||||||
Cases[i].Parent = UnionEnd;
|
Cases[i].Parent = UnionEnd;
|
||||||
}
|
}
|
||||||
UnionEnd.Right = Cases[0];
|
UnionEnd.Right = Cases[0];
|
||||||
|
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(UnionBegin);
|
||||||
|
|
||||||
return UnionBegin;
|
return UnionBegin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,8 +376,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
RegexAstNode _case
|
RegexAstNode _case
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
RegexAstNode right = _case.Right;
|
RegexAstNode right = IsNullOrEmpty(_case.Right) ?
|
||||||
right ??= Empty;
|
new RegexAstNode(
|
||||||
|
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column,
|
||||||
|
RegexElementLabel.UNIT)
|
||||||
|
) :
|
||||||
|
_case.Right;
|
||||||
Cases.Add(right);
|
Cases.Add(right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -170,23 +170,40 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
RegexAstNode current;
|
RegexAstNode current;
|
||||||
|
|
||||||
// phase1: ALT flatten
|
// phase1: ALT flatten
|
||||||
// phase2: then ALT union
|
|
||||||
// phase3: CONCAT normalize
|
// phase3: CONCAT normalize
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
List<RegexAstNode> altNodes = [];
|
||||||
while (enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
current = enumerator.Current;
|
current = enumerator.Current;
|
||||||
RegexAstNode cur = current.Next;
|
|
||||||
if (current.EqualityTypeConvert == RegexElementLabel.ALT)
|
if (current.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||||
{
|
{
|
||||||
List<RegexAstNode> list = FlattenAlt(current);
|
RegexAstNode union = FlattenAlt(current);
|
||||||
RegexAstNode union = MergeAlt(list);
|
altNodes.Add(current);
|
||||||
current.ReplaceBy(union);
|
current.Right = union;
|
||||||
|
current.Left = RegexAstNode.Empty;
|
||||||
} else if (current.EqualityTypeConvert == RegexElementLabel.CONCAT) {
|
} else if (current.EqualityTypeConvert == RegexElementLabel.CONCAT) {
|
||||||
RotateConcat(current);
|
RotateConcat(current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<RegexAstNode> cases = [];
|
||||||
|
|
||||||
|
// phase2: then ALT union
|
||||||
|
foreach (RegexAstNode item in altNodes)
|
||||||
|
{
|
||||||
|
RegexAstNode _case = item.Right;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
cases.Add(_case);
|
||||||
|
_case = _case.Next;
|
||||||
|
} while (!RegexAstNode.IsNullOrEmpty(_case));
|
||||||
|
RegexAstNode newAlt = MergeAlt(cases);
|
||||||
|
item.ReplaceBy(newAlt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return;
|
||||||
|
|
||||||
// phase4: CONCAT merge
|
// phase4: CONCAT merge
|
||||||
enumerator.Reset();
|
enumerator.Reset();
|
||||||
while (enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
@@ -207,7 +224,6 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
RegexAstNode op
|
RegexAstNode op
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Processing a {op.Source.Flag} operator");
|
|
||||||
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||||
{
|
{
|
||||||
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||||
|
|||||||
@@ -38,16 +38,29 @@ namespace StgSharp
|
|||||||
public class TestRegexAnalyse
|
public class TestRegexAnalyse
|
||||||
{
|
{
|
||||||
|
|
||||||
private const string regex = @"abc|abd|abe|abf";
|
private static string[] regexSource = [
|
||||||
|
@"http|https",
|
||||||
|
@"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"
|
||||||
|
];
|
||||||
|
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
|
foreach (string regex in regexSource)
|
||||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree = interpreter.Tree;
|
{
|
||||||
Console.WriteLine();
|
RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
|
||||||
Console.WriteLine();
|
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree = interpreter.Tree;
|
||||||
foreach (RegexAstNode node in tree) {
|
Console.WriteLine($"result of \"{regex}\"");
|
||||||
Console.WriteLine($"{node.Source.Value}\t{node.Source.Flag}");
|
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(tree);
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
RegexAstNode node = enumerator.Current;
|
||||||
|
Console.Write(new string('|', enumerator.Depth));
|
||||||
|
Console.WriteLine($"\t{node.Source.Value}\t{node.Source.Flag}");
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user