diff --git a/StgSharp.Benchmark/StgSharp.Benchmark.csproj b/StgSharp.Benchmark/StgSharp.Benchmark.csproj
index f99ec316..741ea950 100644
--- a/StgSharp.Benchmark/StgSharp.Benchmark.csproj
+++ b/StgSharp.Benchmark/StgSharp.Benchmark.csproj
@@ -14,7 +14,7 @@
-
+
diff --git a/StgSharp.RegularAnalysis/Abstraction/AbstractSyntaxTree.cs b/StgSharp.RegularAnalysis/Abstraction/AbstractSyntaxTree.cs
index 455b57bf..e946e5dc 100644
--- a/StgSharp.RegularAnalysis/Abstraction/AbstractSyntaxTree.cs
+++ b/StgSharp.RegularAnalysis/Abstraction/AbstractSyntaxTree.cs
@@ -1,33 +1,37 @@
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="AbstractSyntaxTree"
+// file="AbstractSyntaxTree.cs"
// Project: StgSharp
-// AuthorGroup: Nitload
-// Copyright (c) Nitload. All rights reserved.
+// AuthorGroup: Nitload Space
+// Copyright (c) Nitload Space. 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
@@ -35,8 +39,7 @@ using System.Xml.Schema;
namespace StgSharp.RegularAnalysis.Abstraction
{
- public sealed class AbstractSyntaxTree where TNode : ISyntaxNode
- where TType : unmanaged
+ public sealed class AbstractSyntaxTree where TNode: ISyntaxNode where TType: unmanaged
{
private HashSet _allToken = new();
@@ -65,19 +68,68 @@ namespace StgSharp.RegularAnalysis.Abstraction
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool AddNode(
- TNode node
- )
+ public bool AddNode(TNode node)
{
return _allToken.Add(node);
}
- public bool Contains(
- TNode node
- )
+ public bool Contains(TNode node)
{
return _allToken.Contains(node);
}
+ public IEnumerator GetEnumerator()
+ {
+ return new Enumerator(this);
+ }
+
+ private class Enumerator : IEnumerator
+ {
+
+ private int _state,_target; // 0: not started, 1: on right, 2: ended
+ private Stack _stack, _listStack;
+ private TNode _lRoot;
+ private TNode _root;
+
+ public Enumerator(AbstractSyntaxTree tree)
+ {
+ _root = tree._root;
+ _stack = [];
+ _listStack = [];
+ }
+
+ public TNode Current
+ {
+ get
+ {
+ if (_stack.TryPeek(out TNode? node)) {
+ return node;
+ }
+ return TNode.Empty;
+ }
+ }
+
+ public void Dispose()
+ {
+ return;
+ }
+
+ public bool MoveNext()
+ {
+ if (_stack.TryPop(out TNode? cur)) { }
+ _stack.Push(_root);
+ return true;
+ }
+
+ public void Reset()
+ {
+ _stack.Clear();
+ _state = 0;
+ }
+
+ object IEnumerator.Current => Current;
+
+ }
+
}
}
diff --git a/StgSharp.RegularAnalysis/Abstraction/ISyntaxNode.cs b/StgSharp.RegularAnalysis/Abstraction/ISyntaxNode.cs
index 2279c1cc..45e7410f 100644
--- a/StgSharp.RegularAnalysis/Abstraction/ISyntaxNode.cs
+++ b/StgSharp.RegularAnalysis/Abstraction/ISyntaxNode.cs
@@ -1,9 +1,9 @@
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ISyntaxNode"
// Project: StgSharp
-// AuthorGroup: Nitload
-// Copyright (c) Nitload. All rights reserved.
+// AuthorGroup: Nitload Space
+// Copyright (c) Nitload Space. 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
@@ -35,8 +35,7 @@ using System.Text;
namespace StgSharp.RegularAnalysis.Abstraction
{
- public interface ISyntaxNode where TNode : ISyntaxNode
- where TLabel : unmanaged
+ public interface ISyntaxNode where TNode: ISyntaxNode where TLabel: unmanaged
{
long NodeFlag { get; }
@@ -59,13 +58,11 @@ namespace StgSharp.RegularAnalysis.Abstraction
Token Source { get; }
- void AppendNode(
- TNode nextToken
- );
+ protected internal int EnumState { get; set; }
- void PrependNode(
- TNode previousNode
- );
+ void AppendNode(TNode nextToken);
+
+ void PrependNode(TNode previousNode);
}
}
diff --git a/StgSharp.RegularAnalysis/TextRegex/RegexAstNode.cs b/StgSharp.RegularAnalysis/TextRegex/RegexAstNode.cs
index d987ec0a..a3b14ad2 100644
--- a/StgSharp.RegularAnalysis/TextRegex/RegexAstNode.cs
+++ b/StgSharp.RegularAnalysis/TextRegex/RegexAstNode.cs
@@ -1,27 +1,30 @@
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
-// file="RegexAstNode"
+// file="RegexAstNode.cs"
// Project: StgSharp
-// AuthorGroup: Nitload
-// Copyright (c) Nitload. All rights reserved.
+// AuthorGroup: Nitload Space
+// Copyright (c) Nitload Space. 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -36,18 +39,18 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
{
- public class RegexAstNode : ISyntaxNode
+ internal class RegexAstNode : ISyntaxNode
{
private RegexAstNode _left, _right;
- public RegexAstNode(
- Token source
- )
+ public RegexAstNode(Token source)
{
Source = source;
}
+ public int EnumState { get; set; }
+
public long NodeFlag => (long)Source.Flag;
public static RegexAstNode Empty
@@ -92,15 +95,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
public Token Source { get; }
- public void AppendNode(
- RegexAstNode nextToken
- ) { }
+ public void AppendNode(RegexAstNode nextToken) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static (bool Valid,int Value) ComparePrecedence(
- RegexElementLabel left,
- RegexElementLabel right
- )
+ public static (bool Valid,int Value) ComparePrecedence(RegexElementLabel left, RegexElementLabel right)
{
left |= RegexElementLabel.OPERATOR;
right |= RegexElementLabel.OPERATOR;
@@ -111,15 +109,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
return (true,v_left - v_right);
}
- public void PrependNode(
- RegexAstNode previousNode
- ) { }
+ public void PrependNode(RegexAstNode previousNode) { }
- public static bool TryMergeUnit(
- RegexAstNode prev,
- RegexAstNode next,
- out RegexAstNode result
- )
+ public static bool TryMergeUnit(RegexAstNode prev, RegexAstNode next, out RegexAstNode result)
{
Token pSource = prev.Source;
Token nSource = next.Source;
diff --git a/StgSharp.RegularAnalysis/TextRegex/RegexInterpreter.cs b/StgSharp.RegularAnalysis/TextRegex/RegexInterpreter.cs
index a1b924bc..b1cfed9c 100644
--- a/StgSharp.RegularAnalysis/TextRegex/RegexInterpreter.cs
+++ b/StgSharp.RegularAnalysis/TextRegex/RegexInterpreter.cs
@@ -1,9 +1,9 @@
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter"
// Project: StgSharp
-// AuthorGroup: Nitload
-// Copyright (c) Nitload. All rights reserved.
+// AuthorGroup: Nitload Space
+// Copyright (c) Nitload Space. 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
@@ -29,6 +29,7 @@ using StgSharp.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -39,19 +40,29 @@ namespace StgSharp.RegularAnalysis.TextRegex
{
private AbstractSyntaxTree _tree = new();
-
private CompileStack _stack = new();
+ private Exception _interpreteException;
private string _source;
- public static AbstractSyntaxTree Interpret(
- string regex
- )
+ private RegexInterpreter() { }
+
+ public static RegexInterpreter Analyze(string regex)
{
RegexInterpreter interpreter = new RegexInterpreter();
interpreter._source = regex;
- interpreter.Analyze();
- interpreter.OptimizeTree();
- return interpreter._tree;
+ bool isSuccess;
+ try
+ {
+ interpreter.Analyze();
+ interpreter.OptimizeTree();
+ isSuccess = true;
+ }
+ catch (Exception ex)
+ {
+ interpreter._interpreteException = ex;
+ isSuccess = false;
+ }
+ return interpreter;
}
private void Analyze()
@@ -144,34 +155,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
private void OptimizeTree(
)
{
- // phase0P: ALT flatten
- int opCount = 0;
- do
- {
- opCount = 1;
- } while (opCount != 0);
-
- // phase1: ALT merge
- opCount = 0;
- do
- {
- opCount = 1;
- } while (opCount != 0);
-
- // phase2: CONCAT merge
- // phase3: scan container and remove no parent nodes
+ // phase0: scan container and remove no parent nodes
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
+
+ // phase1: ALT flatten
+ int opCount = 0, cycle = 0;
+ do
+ {
+ cycle++;
+ } while (opCount != 0 && cycle < 16);
+
+ // phase2: ALT merge
+ opCount = 0;
+ cycle = 0;
+ do
+ {
+ opCount = 1;
+ } while (opCount != 0);
+
+ // phase3: CONCAT merge
}
- private void ProcessOperator(
- RegexAstNode op
- )
+ private void ProcessOperator(RegexAstNode op)
{
Console.WriteLine($"Processing a {op.Source.Flag} operator");
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
{
- if (_stack.TryPopOperand(out RegexAstNode? _1) &&
- _stack.TryPopOperand(out RegexAstNode? _2))
+ if (_stack.TryPopOperand(out RegexAstNode? _1) && _stack.TryPopOperand(out RegexAstNode? _2))
{
op.Left = _2;
op.Right = _1;
@@ -197,8 +207,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
{
- if (_stack.TryPopOperand(out RegexAstNode? _1) &&
- _stack.TryPopOperand(out RegexAstNode? _2))
+ if (_stack.TryPopOperand(out RegexAstNode? _1) && _stack.TryPopOperand(out RegexAstNode? _2))
{
op.Left = _1;
op.Right = _2;
diff --git a/StgSharpDebug/TestRegexAnalyse.cs b/StgSharpDebug/TestRegexAnalyse.cs
index 8c7df3d9..cd31e1eb 100644
--- a/StgSharpDebug/TestRegexAnalyse.cs
+++ b/StgSharpDebug/TestRegexAnalyse.cs
@@ -1,9 +1,9 @@
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TestRegexAnalyse"
// Project: StgSharp
-// AuthorGroup: Nitload
-// Copyright (c) Nitload. All rights reserved.
+// AuthorGroup: Nitload Space
+// Copyright (c) Nitload Space. 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
@@ -42,8 +42,7 @@ namespace StgSharp
public static void Run()
{
- RegexInterpreter interpreter = new RegexInterpreter();
- AbstractSyntaxTree tree = interpreter.Analyze(regex);
+ RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
}
}