arrange ngra proj struct

This commit is contained in:
Youmu Liu
2026-01-30 18:07:16 +08:00
parent 01984dee28
commit c056938e7c
7 changed files with 283 additions and 259 deletions
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegularExpression" // file="GroupIndex"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -25,19 +25,26 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System.Runtime.InteropServices;
namespace StgSharp.RegularAnalysis namespace StgSharp.RegularAnalysis
{ {
public partial class RegularExpression public record struct GroupIndex
{ {
private class GroupIndex public GroupIndex(
int index,
string name
)
{ {
Index = index;
public int Index { get; set; } Name = name;
public string Name { get; set; }
} }
public int Index { get; internal set; }
public string Name { get; internal set; }
} }
} }
@@ -1,170 +0,0 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.StateMachine"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.State;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
public partial class RegularExpression
{
private readonly RegularExpressionState _state;
private class RegularExpressionState : ISimpleStateMachine<RegularExpressionStateNode, int, RegularExpressionStateContext>
{
private List<RegularExpressionStateNode> _nodes;
private RegularExpressionStateNode _currentState;
public RegularExpressionStateNode this[
int label
] => _nodes[label];
public ref readonly RegularExpressionStateNode GetCurrentStateNode()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
public bool TryMoveNext(
RegularExpressionStateContext ctx
)
{
throw new NotImplementedException();
}
#region node factory
public RegularExpressionCharNode AddChar(
char c
)
{
RegularExpressionCharNode node = new RegularExpressionCharNode(c);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
public RegularExpressionCharsetNode AddCharset(
ReadOnlySpan<char> chars,
bool isNegate
)
{
RegularExpressionCharsetNode node = new RegularExpressionCharsetNode(chars, isNegate);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
public (RegularExpressionLeftBoarderNode left) AddBoarder(string groupName, int groupIndex)
{
RegularExpressionLeftBoarderNode leftNode = new RegularExpressionLeftBoarderNode(new GroupIndex(groupName, groupIndex));
int leftIndex = _nodes.Count;
_nodes.Add(leftNode);
leftNode.FailNodeIndex = leftIndex; // Point to self by default
return (left: leftNode);
}
public RegularExpressionCounterNode AddCounter(
int minCount,
bool isEager
)
{
RegularExpressionCounterNode node = new RegularExpressionCounterNode(minCount, isEager);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
#endregion
}
private abstract class RegularExpressionStateNode : SimpleStateMachineNode<RegularExpressionStateNode, int, RegularExpressionStateContext>
{
public int FailNodeIndex { get; set; }
public abstract NodeState Type { get; }
public override bool TryTransaction(
ReadOnlySpan<RegularExpressionStateNode> nodeSpan,
RegularExpressionStateContext context,
out int label
)
{
context.Position++;
int pos = context.Position;
if (pos < 0)
{
label = FailNodeIndex;
return true;
}
char c = context.CurrentCharSpan[pos];
foreach (int index in AvailableNodes)
{
if (nodeSpan[index].IsValidateFrom(this, context))
{
label = index;
return true;
}
}
label = FailNodeIndex;
return true;
}
}
private class RegularExpressionStateContext : StateContext
{
public int Begin { get; set; }
public int Position { get; set; } = -1;
public ReadOnlySpan<char> CurrentCharSpan => Source.AsSpan()[Begin..Position];
public string Source { get; init; }
}
}
}
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegularExpression.Node.Count" // file="Node.Boarder"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -33,45 +33,64 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis namespace StgSharp.RegularAnalysis
{ {
public partial class RegularExpression internal class RegularExpressionLeftBoarderNode : RegularStateNode
{ {
private class RegularExpressionCounterNode : RegularExpressionStateNode public RegularExpressionLeftBoarderNode(
{ int index,
string name
private readonly int _maxCount;
private readonly int _minCount;
public RegularExpressionCounterNode(
int minCount,
bool isEager
) )
{ {
_minCount = minCount; Boarder = new GroupIndex
IsEager = isEager; {
Index = index,
Name = name
};
} }
public bool IsEager { get; init; } public event EventHandler<GroupIndex> OnBoarderEntered;
public int Count { get; private set; } public GroupIndex Boarder { get; private set; }
public override NodeState Type => NodeState.COUNT; public override NodeType Type => NodeType.LEFT_BOARDER;
public RegularExpressionStateNode LoopBack { get; set; }
public override bool IsValidateFrom( public override bool IsValidateFrom(
RegularExpressionStateNode former, RegularStateNode former,
RegularExpressionStateContext context RegularContext context
) )
{ {
return true; return true;
} }
public void Reset() }
internal class RegularExpressionRightBoarderNode : RegularStateNode
{ {
Count = 0;
public RegularExpressionRightBoarderNode(
int index,
string name
)
{
Boarder = new GroupIndex
{
Index = index,
Name = name
};
} }
public event EventHandler<GroupIndex> OnBoarderEntered;
public GroupIndex Boarder { get; private set; }
public override NodeType Type => NodeType.RIGHT_BOARDER;
public override bool IsValidateFrom(
RegularStateNode former,
RegularContext context
)
{
return true;
} }
} }
@@ -35,10 +35,7 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis namespace StgSharp.RegularAnalysis
{ {
public partial class RegularExpression internal class RegularExpressionCharNode : RegularStateNode
{
private class RegularExpressionCharNode : RegularExpressionStateNode
{ {
private char _char; private char _char;
@@ -50,11 +47,11 @@ namespace StgSharp.RegularAnalysis
_char = c; _char = c;
} }
public override NodeState Type => NodeState.CHAR; public override NodeType Type => NodeType.CHAR;
public override bool IsValidateFrom( public override bool IsValidateFrom(
RegularExpressionStateNode former, RegularStateNode former,
RegularExpressionStateContext context RegularContext context
) )
{ {
int pos = context.Position; int pos = context.Position;
@@ -64,7 +61,7 @@ namespace StgSharp.RegularAnalysis
} }
private class RegularExpressionCharsetNode : RegularExpressionStateNode internal class RegularExpressionCharsetNode : RegularStateNode
{ {
private bool _isNegated; private bool _isNegated;
@@ -91,11 +88,11 @@ namespace StgSharp.RegularAnalysis
_isNegated = isNegated; _isNegated = isNegated;
} }
public override NodeState Type => NodeState.CHARSET; public override NodeType Type => NodeType.CHARSET;
public override bool IsValidateFrom( public override bool IsValidateFrom(
RegularExpressionStateNode former, RegularStateNode former,
RegularExpressionStateContext context RegularContext context
) )
{ {
int pos = context.Position; int pos = context.Position;
@@ -104,7 +101,7 @@ namespace StgSharp.RegularAnalysis
return _isNegated ? !contains : contains; return _isNegated ? !contains : contains;
} }
}
} }
} }
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegularExpression.Node.Boarder" // file="Node.Count"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -33,38 +33,40 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis namespace StgSharp.RegularAnalysis
{ {
public partial class RegularExpression internal class RegularExpressionCounterNode : RegularStateNode
{ {
private class RegularExpressionLeftBoarderNode : RegularExpressionStateNode private readonly int _maxCount;
{ private readonly int _minCount;
public RegularExpressionLeftBoarderNode( public RegularExpressionCounterNode(
int index, int minCount,
string name bool isEager
) )
{ {
Boarder = new GroupIndex _minCount = minCount;
{ IsEager = isEager;
Index = index,
Name = name
};
} }
public event EventHandler<GroupIndex> OnBoarderEntered; public bool IsEager { get; init; }
public GroupIndex Boarder { get; private set; } public int Count { get; private set; }
public override NodeState Type => throw new NotImplementedException(); public override NodeType Type => NodeType.COUNT;
public RegularStateNode LoopBack { get; set; }
public override bool IsValidateFrom( public override bool IsValidateFrom(
RegularExpressionStateNode former, RegularStateNode former,
RegularExpressionStateContext context RegularContext context
) )
{ {
return true; return true;
} }
public void Reset()
{
Count = 0;
} }
} }
@@ -33,13 +33,10 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis namespace StgSharp.RegularAnalysis
{ {
public partial class RegularExpression internal enum NodeType
{ {
private enum NodeState // single unicode character: a、B、1、@、汉字 etc.
{
// single unicode character: a、b、1、@、汉字 etc.
CHAR, CHAR,
// collections of finate characters: [abc]、[^abc]、\d、\w、\s、\D、\W、\S etc. // collections of finate characters: [abc]、[^abc]、\d、\w、\s、\D、\W、\S etc.
@@ -55,9 +52,11 @@ namespace StgSharp.RegularAnalysis
RIGHT_BOARDER, RIGHT_BOARDER,
// zero length char or structure // zero length char or structure
EPSILON EPSILON,
} // more than one cases to match, like alter|nation
BRANCH
} }
} }
@@ -0,0 +1,170 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="StateMachine"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.State;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
internal class RegularExpressionState : ISimpleStateMachine<RegularStateNode, int, RegularContext>
{
private List<RegularStateNode> _nodes;
private RegularStateNode _currentState;
public RegularStateNode this[
int label
] => _nodes[label];
public ref readonly RegularStateNode GetCurrentStateNode()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
public bool TryMoveNext(
RegularContext ctx
)
{
throw new NotImplementedException();
}
#region node factory
public RegularExpressionCharNode AddChar(
char c
)
{
RegularExpressionCharNode node = new RegularExpressionCharNode(c);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
public RegularExpressionCharsetNode AddCharset(
ReadOnlySpan<char> chars,
bool isNegate
)
{
RegularExpressionCharsetNode node = new RegularExpressionCharsetNode(chars, isNegate);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
public (RegularExpressionLeftBoarderNode left,RegularExpressionRightBoarderNode right) AddBoarder(
string groupName,
int groupIndex
)
{
RegularExpressionLeftBoarderNode leftNode = new RegularExpressionLeftBoarderNode(groupIndex, groupName);
int leftIndex = _nodes.Count;
_nodes.Add(leftNode);
leftNode.FailNodeIndex = leftIndex; // Point to self by default
RegularExpressionRightBoarderNode rightNode = new RegularExpressionRightBoarderNode(groupIndex, groupName);
int rightIndex = _nodes.Count;
_nodes.Add(rightNode);
rightNode.FailNodeIndex = rightIndex; // Point to self by default
return (left: leftNode,right:rightNode);
}
public RegularExpressionCounterNode AddCounter(
int minCount,
bool isEager
)
{
RegularExpressionCounterNode node = new RegularExpressionCounterNode(minCount, isEager);
int index = _nodes.Count;
_nodes.Add(node);
node.FailNodeIndex = index; // Point to self by default
return node;
}
#endregion
}
internal abstract class RegularStateNode : SimpleStateMachineNode<RegularStateNode, int, RegularContext>
{
public int FailNodeIndex { get; set; }
public abstract NodeType Type { get; }
public override bool TryTransaction(
ReadOnlySpan<RegularStateNode> nodeSpan,
RegularContext context,
out int label
)
{
context.Position++;
int pos = context.Position;
if (pos < 0)
{
label = FailNodeIndex;
return true;
}
char c = context.CurrentCharSpan[pos];
foreach (int index in AvailableNodes)
{
if (nodeSpan[index].IsValidateFrom(this, context))
{
label = index;
return true;
}
}
label = FailNodeIndex;
return true;
}
}
internal class RegularContext : StateContext
{
public int Begin { get; set; }
public int Position { get; set; } = -1;
public ReadOnlySpan<char> CurrentCharSpan => Source.AsSpan()[Begin..Position];
public string Source { get; init; }
}
}