Optimizer is almost ok. Will go on source generate
This commit is contained in:
@@ -35,14 +35,15 @@ using System.Runtime.CompilerServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
public partial class RegexInterpreter
|
||||
{
|
||||
|
||||
private static List<RegexAstNode> FlattenAlt(
|
||||
RegexAstNode root
|
||||
private static RegexAstNode FlattenAlt(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
||||
@@ -59,7 +60,13 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
}
|
||||
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(
|
||||
@@ -69,6 +76,25 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
||||
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
|
||||
* ╱ ╲ /
|
||||
@@ -184,7 +210,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
)
|
||||
{
|
||||
UnionAlt union = new(0, cases);
|
||||
return union.AsNormal();
|
||||
RegexAstNode root = union.AsNormal();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private class UnionAlt : RegexAstNode
|
||||
@@ -196,11 +224,21 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
int level,
|
||||
RegexAstNode node
|
||||
)
|
||||
: base(new Token<RegexElementLabel>(string.Empty, 0, 0, 0))
|
||||
: base(node.Source)
|
||||
{
|
||||
Level = level;
|
||||
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;
|
||||
UnionEnd = node;
|
||||
}
|
||||
@@ -209,7 +247,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
int level,
|
||||
List<RegexAstNode> c
|
||||
)
|
||||
: base(new Token<RegexElementLabel>(string.Empty, 0, 0, 0))
|
||||
: base(c[0].Source)
|
||||
{
|
||||
Cases = c;
|
||||
Level = level;
|
||||
@@ -233,6 +271,17 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
List<RegexAstNode> buffer = [];
|
||||
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];
|
||||
int j = 0;
|
||||
for (; j < buffer.Count; j++)
|
||||
@@ -258,11 +307,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
buffer.Add(current);
|
||||
}
|
||||
}
|
||||
Cases = buffer;
|
||||
if (Cases.Count != buffer.Count) {
|
||||
Cases = buffer;
|
||||
}
|
||||
|
||||
// TODO problem1 没处理case链表
|
||||
// TODO problem2 没有处理立刻返回策略
|
||||
if (Cases.Count == 1)
|
||||
if (buffer.Count == 1)
|
||||
{
|
||||
// if there is only one case,
|
||||
// then we suppose that its contained cases has a same beginning,
|
||||
@@ -276,6 +325,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
alt.UnionBegin.Parent = UnionEnd;
|
||||
UnionEnd.Right = alt.UnionBegin;
|
||||
UnionEnd = alt.UnionBegin;
|
||||
}
|
||||
|
||||
Cases = alt.Cases;
|
||||
@@ -291,18 +341,34 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
Cases[i] = union.AsNormal();
|
||||
}
|
||||
}
|
||||
RegexAstNode branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
UnionEnd.Source.Column,
|
||||
RegexElementLabel.ALT));
|
||||
UnionEnd.Right = branch;
|
||||
branch.Parent = UnionEnd;
|
||||
UnionEnd = branch;
|
||||
|
||||
RegexAstNode branch;
|
||||
if (IsNullOrEmpty(UnionEnd))
|
||||
{
|
||||
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
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++)
|
||||
{
|
||||
Cases[i] ??= NewEmptyNode();
|
||||
Cases[i].AppendNode(Cases[i + 1]);
|
||||
Cases[i].Parent = UnionEnd;
|
||||
}
|
||||
UnionEnd.Right = Cases[0];
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(UnionBegin);
|
||||
|
||||
return UnionBegin;
|
||||
}
|
||||
|
||||
@@ -310,8 +376,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
RegexAstNode _case
|
||||
)
|
||||
{
|
||||
RegexAstNode right = _case.Right;
|
||||
right ??= Empty;
|
||||
RegexAstNode right = IsNullOrEmpty(_case.Right) ?
|
||||
new RegexAstNode(
|
||||
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column,
|
||||
RegexElementLabel.UNIT)
|
||||
) :
|
||||
_case.Right;
|
||||
Cases.Add(right);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user