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
@@ -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");
}
op.Right = _stack.PopOperand();
_stack.PushOperand(op);
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;
}
}