fix errors in interpreter. add a enumerator for tree. working on tree optimizer

This commit is contained in:
Youmu Liu
2026-02-10 18:31:30 +08:00
parent 807658111c
commit 4735b987f4
4 changed files with 221 additions and 136 deletions
@@ -33,6 +33,7 @@ using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
namespace StgSharp.RegularAnalysis.Abstraction
@@ -81,26 +82,29 @@ namespace StgSharp.RegularAnalysis.Abstraction
public IEnumerator<TNode> GetEnumerator()
{
return new Enumerator(this);
return new TreeEnumerator<TNode, TType>(this);
}
private class Enumerator : IEnumerator<TNode>
}
internal class TreeEnumerator<TNode, TType>(
TNode root
) : IEnumerator<TNode> where TNode : ISyntaxNode<TNode, TType> where TType : unmanaged
{
private bool _completed = false;
private int _state, _target; // 0: not started, 1: on right, 2: ended
private Stack<Record> _recycle;
private Stack<Record> _stack;
private TNode _lRoot;
private TNode _root;
private Stack<Record> _recycle = [];
private Stack<Record> _stack = [];
public Enumerator(
// private TNode _lRoot;
private TNode _root = root;
internal TreeEnumerator(
AbstractSyntaxTree<TNode, TType> tree
)
{
_root = tree.Root;
_stack = [];
_recycle = [];
}
: this(tree.Root) { }
public TNode Current
{
@@ -120,6 +124,9 @@ namespace StgSharp.RegularAnalysis.Abstraction
public bool MoveNext()
{
if (_completed) {
return false;
}
while (_stack.TryPeek(out Record? cur))
{
switch (cur.State)
@@ -148,7 +155,9 @@ namespace StgSharp.RegularAnalysis.Abstraction
if (TNode.IsNullOrEmpty(cur.Node.Next))
{
_recycle.Push(_stack.Pop());
if (_stack.Count == 0) {
if (_stack.Count == 0)
{
_completed = true;
return false;
}
} else
@@ -167,10 +176,50 @@ namespace StgSharp.RegularAnalysis.Abstraction
public void Reset()
{
_completed = false;
_stack.Clear();
_state = 0;
}
public void SkipNode()
{
if (_stack.TryPop(out Record? r)) {
_recycle.Push(r);
}
}
public void SkipOnce()
{
if (_stack.TryPeek(out Record? cur))
{
switch (cur.State)
{
case 0:
cur.State++;
break;
case 1:
cur.State++;
break;
case 2:
if (TNode.IsNullOrEmpty(cur.Node.Next))
{
_recycle.Push(_stack.Pop());
if (_stack.Count == 0)
{
_completed = true;
return;
}
} else
{
_stack.Push(GetNewRecord(cur.Node.Next));
}
break;
default:
break;
}
}
}
private Record GetNewRecord(
TNode node
)
@@ -199,6 +248,4 @@ namespace StgSharp.RegularAnalysis.Abstraction
}
}
}
}
@@ -0,0 +1,53 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter.Optimize"
// 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.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
{
public partial class RegexInterpreter
{
private static RegexAstNode FlattenAlt(
RegexAstNode root
)
{
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
List<RegexAstNode> cases = [];
while (enumerator.MoveNext())
{
RegexAstNode current = enumerator.Current;
}
}
}
}
@@ -37,7 +37,7 @@ using System.Threading.Tasks;
namespace StgSharp.RegularAnalysis.TextRegex
{
public class RegexInterpreter
public partial class RegexInterpreter
{
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
@@ -113,8 +113,11 @@ namespace StgSharp.RegularAnalysis.TextRegex
if (op.Source.Flag != RegexElementLabel.GROUP_BEGIN) {
throw new InvalidOperationException("Unmatched group begin and end symbol");
}
if (op.Source.Value.Length > 1)
{
op.Right = _stack.PopOperand();
_stack.PushOperand(op);
}
} else if ((token.Flag & RegexElementLabel.OPERATOR) != 0)
{
Console.Write($"Operator {token.Value} with flag {token.Flag}, ");
@@ -179,27 +182,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
RegexAstNode node = enumerator.Current;
if ((RegexElementLabel)node.NodeFlag == RegexElementLabel.ALT)
{
RegexAstNode right, left = node.Left;
while (node.Right.Source.Flag == RegexElementLabel.ALT)
{
// flatten a alternation to left linked list of root ALT
right = node.Right;
RegexAstNode nextAlt = right.Left;
left.Next = nextAlt;
nextAlt.Previous = left;
nextAlt.Parent = node;
right.Left = RegexAstNode.Empty;
left = nextAlt;
// remove the right ALT node and connect its right child to current node
node.Right = right.Right;
right.Right.Parent = node;
}
// connect the last ALT node to the right child of current node
right = node.Right;
left.Next = right;
right.Previous = left;
RegexAstNode list = FlattenAlt(node);
node.Left = list;
node.Right = RegexAstNode.Empty;
}
}
+1 -1
View File
@@ -38,7 +38,7 @@ namespace StgSharp
public class TestRegexAnalyse
{
private const string regex = @"ab[1-9]+(?<name>abc)";
private const string regex = @"(ab[1-9])+(?<name>abc)";
public static void Run()
{