add state nodes for regex state machine

This commit is contained in:
Youmu Liu
2026-01-27 18:40:44 +08:00
parent a36e6cbab4
commit 3881078770
13 changed files with 510 additions and 820 deletions
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.Node.Boarder"
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
public partial class RegularExpression
{
private class RegularExpressionLeftBoarderNode : RegularExpressionStateNode
{
public RegularExpressionLeftBoarderNode(
int index,
string name
)
{
Boarder = new GroupIndex
{
Index = index,
Name = name
};
}
public event EventHandler<GroupIndex> OnBoarderEntered;
public GroupIndex Boarder { get; private set; }
public override NodeState Type => throw new NotImplementedException();
public override bool IsValidateFrom(
RegularExpressionStateNode former,
RegularExpressionStateContext context
)
{
return true;
}
}
}
}
@@ -0,0 +1,110 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.Node.Char"
// 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.State;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
public partial class RegularExpression
{
private class RegularExpressionCharNode : RegularExpressionStateNode
{
private char _char;
public RegularExpressionCharNode(
char c
)
{
_char = c;
}
public override NodeState Type => NodeState.CHAR;
public override bool IsValidateFrom(
RegularExpressionStateNode former,
RegularExpressionStateContext context
)
{
int pos = context.Position;
char c = context.CurrentCharSpan[pos + 1];
return c == _char;
}
}
private class RegularExpressionCharsetNode : RegularExpressionStateNode
{
private bool _isNegated;
private HashSet<char> _charset;
public RegularExpressionCharsetNode(
IEnumerable<char> charset,
bool isNegated = false
)
{
_charset = new(charset);
_isNegated = isNegated;
}
public RegularExpressionCharsetNode(
ReadOnlySpan<char> charset,
bool isNegated = false
)
{
_charset = new(charset.Length);
foreach (char item in _charset) {
_charset.Add(item);
}
_isNegated = isNegated;
}
public override NodeState Type => NodeState.CHARSET;
public override bool IsValidateFrom(
RegularExpressionStateNode former,
RegularExpressionStateContext context
)
{
int pos = context.Position;
char c = context.CurrentCharSpan[pos + 1];
bool contains = _charset.Contains(c);
return _isNegated ? !contains : contains;
}
}
}
}
@@ -0,0 +1,78 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.Node.Count"
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
public partial class RegularExpression
{
private class RegularExpressionCounterNode : RegularExpressionStateNode
{
private readonly int _maxCount;
private readonly int _minCount;
public RegularExpressionCounterNode(
int minCount,
bool isEager
)
{
_minCount = minCount;
IsEager = isEager;
}
public bool IsEager { get; init; }
public int Count { get; private set; }
public override NodeState Type => NodeState.COUNT;
public RegularExpressionStateNode LoopBack { get; set; }
public override bool IsValidateFrom(
RegularExpressionStateNode former,
RegularExpressionStateContext context
)
{
return true;
}
public void Reset()
{
Count = 0;
}
}
}
}
@@ -0,0 +1,63 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.NodeState"
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis
{
public partial class RegularExpression
{
private enum NodeState
{
// single unicode character: a、b、1、@、汉字 etc.
CHAR,
// collections of finate characters: [abc]、[^abc]、\d、\w、\s、\D、\W、\S etc.
CHARSET,
// increase count of a counter, half function of *+? quantifiers
COUNT,
// left boarder of a sub sequnce, usually begin of match group or lookaround
LEFT_BOARDER,
// right boarder of a sub sequnce, usually end of match group or lookaround
RIGHT_BOARDER,
// zero length char or structure
EPSILON
}
}
}
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.State;
using System;
@@ -44,9 +45,12 @@ namespace StgSharp.RegularAnalysis
private class RegularExpressionState : ISimpleStateMachine<RegularExpressionStateNode, int, RegularExpressionStateContext>
{
public ref readonly RegularExpressionStateNode this[
int label
] => throw new NotImplementedException();
private List<RegularExpressionStateNode> _nodes;
private RegularExpressionStateNode _currentState;
public RegularExpressionStateNode this[
int label
] => _nodes[label];
public ref readonly RegularExpressionStateNode GetCurrentStateNode()
{
@@ -65,22 +69,77 @@ namespace StgSharp.RegularAnalysis
throw new NotImplementedException();
}
}
#region node factory
private struct RegularExpressionStateNode : ISimpleStateMachineNode<int, RegularExpressionStateContext>
{
public ICollection<int> AvailableStateLabels => throw new NotImplementedException();
public SimpleStateMachineNodeTransition<int, RegularExpressionStateContext>
Transition => throw new NotImplementedException();
public bool TryTransaction(
RegularExpressionStateContext context,
out int label
public RegularExpressionCharNode AddChar(
char c
)
{
throw new NotImplementedException();
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 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;
}
}
@@ -90,7 +149,7 @@ namespace StgSharp.RegularAnalysis
public int Begin { get; set; }
public int Position { get; set; }
public int Position { get; set; } = -1;
public ReadOnlySpan<char> CurrentCharSpan => Source.AsSpan()[Begin..Position];
+34 -25
View File
@@ -1,34 +1,43 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegularExpression.cs"
// file="RegularExpression"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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.
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.RegularAnalysis
{
public class RegularExpression { }
public partial class RegularExpression
{
private class GroupIndex
{
public int Index { get; set; }
public string Name { get; set; }
}
}
}
@@ -0,0 +1,24 @@
### basic
char/charset->char/charset
### count
char/charset-match->count increment
count increment-loop back->char/charset
char/charset-miss match->count end
count end-exit->count increment-judge->next node
### match group/lookaround
left boarder-> match group start //push stack, add group index
-> match logic // recored index
-> match group end //pop stack, set group end, build sub sequence