Daily update on 20260209-005019 by Nitload Youmu. Additional Message : Start NGRA, AST build success, working on optimizing.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2026-02-09 00:50:19 +08:00
parent ed7972f1da
commit 909bf50b9d
6 changed files with 168 additions and 119 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> <PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -1,33 +1,37 @@
//-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // file="AbstractSyntaxTree.cs"
// file="AbstractSyntaxTree"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person
// of this software and associated documentation files (the "Software"), to deal // obtaining a copy of this software and associated documentation
// in the Software without restriction, including without limitation the rights // files (the “Software”), to deal in the Software without restriction,
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // including without limitation the rights to use, copy, modify, merge,
// copies of the Software, and to permit persons to whom the Software is // publish, distribute, sublicense, and/or sell copies of the Software,
// furnished to do so, subject to the following conditions: // 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 // The above copyright notice and
// copies or substantial portions of the Software. // 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 // THE SOFTWARE IS PROVIDED “AS IS”,
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// SOFTWARE. // ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Collections; using StgSharp.Collections;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
@@ -35,8 +39,7 @@ using System.Xml.Schema;
namespace StgSharp.RegularAnalysis.Abstraction namespace StgSharp.RegularAnalysis.Abstraction
{ {
public sealed class AbstractSyntaxTree<TNode, TType> where TNode : ISyntaxNode<TNode, TType> public sealed class AbstractSyntaxTree<TNode, TType> where TNode: ISyntaxNode<TNode, TType> where TType: unmanaged
where TType : unmanaged
{ {
private HashSet<TNode> _allToken = new(); private HashSet<TNode> _allToken = new();
@@ -65,19 +68,68 @@ namespace StgSharp.RegularAnalysis.Abstraction
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddNode( public bool AddNode(TNode node)
TNode node
)
{ {
return _allToken.Add(node); return _allToken.Add(node);
} }
public bool Contains( public bool Contains(TNode node)
TNode node
)
{ {
return _allToken.Contains(node); return _allToken.Contains(node);
} }
public IEnumerator<TNode> GetEnumerator()
{
return new Enumerator(this);
}
private class Enumerator : IEnumerator<TNode>
{
private int _state,_target; // 0: not started, 1: on right, 2: ended
private Stack<TNode> _stack, _listStack;
private TNode _lRoot;
private TNode _root;
public Enumerator(AbstractSyntaxTree<TNode, TType> 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;
}
} }
} }
@@ -1,9 +1,9 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="ISyntaxNode" // file="ISyntaxNode"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -35,8 +35,7 @@ using System.Text;
namespace StgSharp.RegularAnalysis.Abstraction namespace StgSharp.RegularAnalysis.Abstraction
{ {
public interface ISyntaxNode<TNode, TLabel> where TNode : ISyntaxNode<TNode, TLabel> public interface ISyntaxNode<TNode, TLabel> where TNode: ISyntaxNode<TNode, TLabel> where TLabel: unmanaged
where TLabel : unmanaged
{ {
long NodeFlag { get; } long NodeFlag { get; }
@@ -59,13 +58,11 @@ namespace StgSharp.RegularAnalysis.Abstraction
Token<TLabel> Source { get; } Token<TLabel> Source { get; }
void AppendNode( protected internal int EnumState { get; set; }
TNode nextToken
);
void PrependNode( void AppendNode(TNode nextToken);
TNode previousNode
); void PrependNode(TNode previousNode);
} }
} }
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // file="RegexAstNode.cs"
// file="RegexAstNode"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person
// of this software and associated documentation files (the "Software"), to deal // obtaining a copy of this software and associated documentation
// in the Software without restriction, including without limitation the rights // files (the “Software”), to deal in the Software without restriction,
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // including without limitation the rights to use, copy, modify, merge,
// copies of the Software, and to permit persons to whom the Software is // publish, distribute, sublicense, and/or sell copies of the Software,
// furnished to do so, subject to the following conditions: // 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 // The above copyright notice and
// copies or substantial portions of the Software. // 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 // THE SOFTWARE IS PROVIDED “AS IS”,
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// SOFTWARE. // 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 namespace StgSharp.RegularAnalysis.TextRegex
{ {
public class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel> internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
{ {
private RegexAstNode _left, _right; private RegexAstNode _left, _right;
public RegexAstNode( public RegexAstNode(Token<RegexElementLabel> source)
Token<RegexElementLabel> source
)
{ {
Source = source; Source = source;
} }
public int EnumState { get; set; }
public long NodeFlag => (long)Source.Flag; public long NodeFlag => (long)Source.Flag;
public static RegexAstNode Empty public static RegexAstNode Empty
@@ -92,15 +95,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
public Token<RegexElementLabel> Source { get; } public Token<RegexElementLabel> Source { get; }
public void AppendNode( public void AppendNode(RegexAstNode nextToken) { }
RegexAstNode nextToken
) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static (bool Valid,int Value) ComparePrecedence( public static (bool Valid,int Value) ComparePrecedence(RegexElementLabel left, RegexElementLabel right)
RegexElementLabel left,
RegexElementLabel right
)
{ {
left |= RegexElementLabel.OPERATOR; left |= RegexElementLabel.OPERATOR;
right |= RegexElementLabel.OPERATOR; right |= RegexElementLabel.OPERATOR;
@@ -111,15 +109,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
return (true,v_left - v_right); return (true,v_left - v_right);
} }
public void PrependNode( public void PrependNode(RegexAstNode previousNode) { }
RegexAstNode previousNode
) { }
public static bool TryMergeUnit( public static bool TryMergeUnit(RegexAstNode prev, RegexAstNode next, out RegexAstNode result)
RegexAstNode prev,
RegexAstNode next,
out RegexAstNode result
)
{ {
Token<RegexElementLabel> pSource = prev.Source; Token<RegexElementLabel> pSource = prev.Source;
Token<RegexElementLabel> nSource = next.Source; Token<RegexElementLabel> nSource = next.Source;
@@ -1,9 +1,9 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="RegexInterpreter" // file="RegexInterpreter"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -29,6 +29,7 @@ using StgSharp.RegularAnalysis.Abstraction;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -39,19 +40,29 @@ namespace StgSharp.RegularAnalysis.TextRegex
{ {
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new(); private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
private CompileStack<RegexAstNode, RegexElementLabel> _stack = new(); private CompileStack<RegexAstNode, RegexElementLabel> _stack = new();
private Exception _interpreteException;
private string _source; private string _source;
public static AbstractSyntaxTree<RegexAstNode, RegexElementLabel> Interpret( private RegexInterpreter() { }
string regex
) public static RegexInterpreter Analyze(string regex)
{ {
RegexInterpreter interpreter = new RegexInterpreter(); RegexInterpreter interpreter = new RegexInterpreter();
interpreter._source = regex; interpreter._source = regex;
bool isSuccess;
try
{
interpreter.Analyze(); interpreter.Analyze();
interpreter.OptimizeTree(); interpreter.OptimizeTree();
return interpreter._tree; isSuccess = true;
}
catch (Exception ex)
{
interpreter._interpreteException = ex;
isSuccess = false;
}
return interpreter;
} }
private void Analyze() private void Analyze()
@@ -144,34 +155,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
private void OptimizeTree( private void OptimizeTree(
) )
{ {
// phase0P: ALT flatten // phase0: scan container and remove no parent nodes
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
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root); _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( private void ProcessOperator(RegexAstNode op)
RegexAstNode op
)
{ {
Console.WriteLine($"Processing a {op.Source.Flag} operator"); 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) && _stack.TryPopOperand(out RegexAstNode? _2))
_stack.TryPopOperand(out RegexAstNode? _2))
{ {
op.Left = _2; op.Left = _2;
op.Right = _1; op.Right = _1;
@@ -197,8 +207,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
} }
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT) } else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
{ {
if (_stack.TryPopOperand(out RegexAstNode? _1) && if (_stack.TryPopOperand(out RegexAstNode? _1) && _stack.TryPopOperand(out RegexAstNode? _2))
_stack.TryPopOperand(out RegexAstNode? _2))
{ {
op.Left = _1; op.Left = _1;
op.Right = _2; op.Right = _2;
+4 -5
View File
@@ -1,9 +1,9 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="TestRegexAnalyse" // file="TestRegexAnalyse"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -42,8 +42,7 @@ namespace StgSharp
public static void Run() public static void Run()
{ {
RegexInterpreter interpreter = new RegexInterpreter(); RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree = interpreter.Analyze(regex);
} }
} }