fix errors in interpreter. add a enumerator for tree. working on tree optimizer
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexInterpreter"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -29,6 +29,7 @@ using StgSharp.RegularAnalysis.Abstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
@@ -46,7 +47,20 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
|
||||
private RegexInterpreter() { }
|
||||
|
||||
public static RegexInterpreter Analyze(string regex)
|
||||
internal AbstractSyntaxTree<RegexAstNode, RegexElementLabel> Tree
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_interpreteException != null) {
|
||||
throw new InvalidOperationException("The regex interpretation failed.", _interpreteException);
|
||||
}
|
||||
return _tree;
|
||||
}
|
||||
}
|
||||
|
||||
public static RegexInterpreter Analyze(
|
||||
string regex
|
||||
)
|
||||
{
|
||||
RegexInterpreter interpreter = new RegexInterpreter();
|
||||
interpreter._source = regex;
|
||||
@@ -114,7 +128,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
// process operator by precedence
|
||||
while (_stack.TryPeekOperator(out RegexAstNode topOp))
|
||||
{
|
||||
(bool Valid, int Value) cmp = RegexAstNode.ComparePrecedence(token.Flag, topOp.Source.Flag);
|
||||
(bool Valid, int Value) cmp = RegexAstNode.ComparePrecedence(topOp.Source.Flag, token.Flag);
|
||||
|
||||
if (!cmp.Valid) {
|
||||
throw new InvalidOperationException("Operators with same precedence are not supported.");
|
||||
@@ -157,31 +171,53 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
// phase0: scan container and remove no parent nodes
|
||||
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
|
||||
HashSet<RegexAstNode>.Enumerator enumerator = _tree.AllNodes.GetEnumerator();
|
||||
|
||||
// phase1: ALT flatten
|
||||
int opCount = 0, cycle = 0;
|
||||
do
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
cycle++;
|
||||
} while (opCount != 0 && cycle < 16);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
@@ -207,7 +243,8 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user