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>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>
<ItemGroup>
@@ -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:
// 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 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.
// 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<TNode, TType> where TNode : ISyntaxNode<TNode, TType>
where TType : unmanaged
public sealed class AbstractSyntaxTree<TNode, TType> where TNode: ISyntaxNode<TNode, TType> where TType: unmanaged
{
private HashSet<TNode> _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<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"
// 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<TNode, TLabel> where TNode : ISyntaxNode<TNode, TLabel>
where TLabel : unmanaged
public interface ISyntaxNode<TNode, TLabel> where TNode: ISyntaxNode<TNode, TLabel> where TLabel: unmanaged
{
long NodeFlag { get; }
@@ -59,13 +58,11 @@ namespace StgSharp.RegularAnalysis.Abstraction
Token<TLabel> Source { get; }
void AppendNode(
TNode nextToken
);
protected internal int EnumState { get; set; }
void PrependNode(
TNode previousNode
);
void AppendNode(TNode nextToken);
void PrependNode(TNode previousNode);
}
}
@@ -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:
// 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 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.
// 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<RegexAstNode, RegexElementLabel>
internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
{
private RegexAstNode _left, _right;
public RegexAstNode(
Token<RegexElementLabel> source
)
public RegexAstNode(Token<RegexElementLabel> 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<RegexElementLabel> 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<RegexElementLabel> pSource = prev.Source;
Token<RegexElementLabel> nSource = next.Source;
@@ -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<RegexAstNode, RegexElementLabel> _tree = new();
private CompileStack<RegexAstNode, RegexElementLabel> _stack = new();
private Exception _interpreteException;
private string _source;
public static AbstractSyntaxTree<RegexAstNode, RegexElementLabel> 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;
+4 -5
View File
@@ -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<RegexAstNode, RegexElementLabel> tree = interpreter.Analyze(regex);
RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
}
}