fix errors in interpreter. add a enumerator for tree. working on tree optimizer
This commit is contained in:
+17
-6
@@ -1,6 +1,6 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="IBidrirectionalDictionary"
|
// file="IBidirectionalDictionary"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
@@ -32,7 +32,9 @@ namespace StgSharp.Collections
|
|||||||
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
|
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
|
||||||
{
|
{
|
||||||
|
|
||||||
TFirst this[TSecond key] { get; set; }
|
TFirst this[
|
||||||
|
TSecond key
|
||||||
|
] { get; set; }
|
||||||
|
|
||||||
ICollection<TFirst> FirstIndex { get; }
|
ICollection<TFirst> FirstIndex { get; }
|
||||||
|
|
||||||
@@ -53,7 +55,9 @@ namespace StgSharp.Collections
|
|||||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||||
/// the specified key; otherwise, false.
|
/// the specified key; otherwise, false.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
bool Contains(TSecond key);
|
bool Contains(
|
||||||
|
TSecond key
|
||||||
|
);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
||||||
@@ -66,16 +70,23 @@ namespace StgSharp.Collections
|
|||||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||||
/// the specified key; otherwise, false.
|
/// the specified key; otherwise, false.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
bool Contains(TFirst key);
|
bool Contains(
|
||||||
|
TFirst key
|
||||||
|
);
|
||||||
|
|
||||||
bool TryGetValue(TSecond key, out TFirst value);
|
bool TryGetValue(
|
||||||
|
TSecond key,
|
||||||
|
out TFirst value
|
||||||
|
);
|
||||||
|
|
||||||
#pragma warning disable CA1033
|
#pragma warning disable CA1033
|
||||||
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
|
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
|
||||||
|
|
||||||
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
|
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
|
||||||
|
|
||||||
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key)
|
bool IDictionary<TFirst, TSecond>.ContainsKey(
|
||||||
|
TFirst key
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return Contains(key);
|
return Contains(key);
|
||||||
}
|
}
|
||||||
@@ -1,30 +1,27 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="AbstractSyntaxTree.cs"
|
// file="AbstractSyntaxTree"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// obtaining a copy of this software and associated documentation
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
// files (the “Software”), to deal in the Software without restriction,
|
// in the Software without restriction, including without limitation the rights
|
||||||
// including without limitation the rights to use, copy, modify, merge,
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
// and to permit persons to whom the Software is furnished to do so,
|
// furnished to do so, subject to the following conditions:
|
||||||
// subject to the following conditions:
|
|
||||||
//
|
//
|
||||||
// The above copyright notice and
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
// this permission notice shall be included in all copies
|
// copies or substantial portions of the Software.
|
||||||
// or substantial portions of the Software.
|
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
// SOFTWARE.
|
||||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -34,18 +31,18 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml.Schema;
|
using System.Xml.Schema;
|
||||||
|
|
||||||
namespace StgSharp.RegularAnalysis.Abstraction
|
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();
|
private HashSet<TNode> _allToken = new();
|
||||||
|
|
||||||
private TNode _root;
|
|
||||||
|
|
||||||
public HashSet<TNode> AllNodes
|
public HashSet<TNode> AllNodes
|
||||||
{
|
{
|
||||||
get { return _allToken; }
|
get { return _allToken; }
|
||||||
@@ -68,12 +65,16 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool AddNode(TNode node)
|
public bool AddNode(
|
||||||
|
TNode node
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return _allToken.Add(node);
|
return _allToken.Add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Contains(TNode node)
|
public bool Contains(
|
||||||
|
TNode node
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return _allToken.Contains(node);
|
return _allToken.Contains(node);
|
||||||
}
|
}
|
||||||
@@ -87,23 +88,26 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
{
|
{
|
||||||
|
|
||||||
private int _state,_target; // 0: not started, 1: on right, 2: ended
|
private int _state,_target; // 0: not started, 1: on right, 2: ended
|
||||||
private Stack<TNode> _stack, _listStack;
|
private Stack<Record> _recycle;
|
||||||
|
private Stack<Record> _stack;
|
||||||
private TNode _lRoot;
|
private TNode _lRoot;
|
||||||
private TNode _root;
|
private TNode _root;
|
||||||
|
|
||||||
public Enumerator(AbstractSyntaxTree<TNode, TType> tree)
|
public Enumerator(
|
||||||
|
AbstractSyntaxTree<TNode, TType> tree
|
||||||
|
)
|
||||||
{
|
{
|
||||||
_root = tree._root;
|
_root = tree.Root;
|
||||||
_stack = [];
|
_stack = [];
|
||||||
_listStack = [];
|
_recycle = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public TNode Current
|
public TNode Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_stack.TryPeek(out TNode? node)) {
|
if (_stack.TryPeek(out Record? r)) {
|
||||||
return node;
|
return r.Node;
|
||||||
}
|
}
|
||||||
return TNode.Empty;
|
return TNode.Empty;
|
||||||
}
|
}
|
||||||
@@ -116,8 +120,48 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (_stack.TryPop(out TNode? cur)) { }
|
while (_stack.TryPeek(out Record? cur))
|
||||||
_stack.Push(_root);
|
{
|
||||||
|
switch (cur.State)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
cur.State++;
|
||||||
|
if (TNode.IsNullOrEmpty(cur.Node.Left))
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
_stack.Push(GetNewRecord(cur.Node.Left));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
cur.State++;
|
||||||
|
if (TNode.IsNullOrEmpty(cur.Node.Right))
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
_stack.Push(GetNewRecord(cur.Node.Right));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (TNode.IsNullOrEmpty(cur.Node.Next))
|
||||||
|
{
|
||||||
|
_recycle.Push(_stack.Pop());
|
||||||
|
if (_stack.Count == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
_stack.Push(GetNewRecord(cur.Node.Next));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_stack.Push(GetNewRecord(_root));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,8 +171,33 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
_state = 0;
|
_state = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Record GetNewRecord(
|
||||||
|
TNode node
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (_recycle.TryPop(out Record? r))
|
||||||
|
{
|
||||||
|
r.State = 0;
|
||||||
|
r.Node = node;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
return new Record(node, 0);
|
||||||
|
}
|
||||||
|
|
||||||
object IEnumerator.Current => Current;
|
object IEnumerator.Current => Current;
|
||||||
|
|
||||||
|
private class Record(
|
||||||
|
TNode node,
|
||||||
|
int state
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
|
public int State { get; set; } = state;
|
||||||
|
|
||||||
|
public TNode Node { get; set; } = node;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
public TNode PopOperand(
|
public TNode PopOperand(
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (OperatorInDepthCount > 0) {
|
if (OperandInDepthCount > 0) {
|
||||||
return _operands.Pop();
|
return _operands.Pop();
|
||||||
}
|
}
|
||||||
return TNode.Empty;
|
return TNode.Empty;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="ISyntaxNode"
|
// file="ISyntaxNode"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. 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
|
||||||
@@ -31,11 +31,13 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace StgSharp.RegularAnalysis.Abstraction
|
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; }
|
long NodeFlag { get; }
|
||||||
@@ -60,9 +62,21 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
|||||||
|
|
||||||
protected internal int EnumState { get; set; }
|
protected internal int EnumState { get; set; }
|
||||||
|
|
||||||
void AppendNode(TNode nextToken);
|
void AppendNode(
|
||||||
|
TNode nextToken
|
||||||
|
);
|
||||||
|
|
||||||
void PrependNode(TNode previousNode);
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static virtual bool IsNullOrEmpty(
|
||||||
|
TNode node
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return node is null || node.Equals(TNode.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrependNode(
|
||||||
|
TNode previousNode
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
//-----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// file="Global"
|
||||||
|
// 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 System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("StgSharpDebug")]
|
||||||
@@ -1,30 +1,27 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="RegexAstNode.cs"
|
// file="RegexAstNode"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// obtaining a copy of this software and associated documentation
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
// files (the “Software”), to deal in the Software without restriction,
|
// in the Software without restriction, including without limitation the rights
|
||||||
// including without limitation the rights to use, copy, modify, merge,
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
// and to permit persons to whom the Software is furnished to do so,
|
// furnished to do so, subject to the following conditions:
|
||||||
// subject to the following conditions:
|
|
||||||
//
|
//
|
||||||
// The above copyright notice and
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
// this permission notice shall be included in all copies
|
// copies or substantial portions of the Software.
|
||||||
// or substantial portions of the Software.
|
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
// SOFTWARE.
|
||||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -44,7 +41,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
|
|
||||||
private RegexAstNode _left, _right;
|
private RegexAstNode _left, _right;
|
||||||
|
|
||||||
public RegexAstNode(Token<RegexElementLabel> source)
|
public RegexAstNode(
|
||||||
|
Token<RegexElementLabel> source
|
||||||
|
)
|
||||||
{
|
{
|
||||||
Source = source;
|
Source = source;
|
||||||
}
|
}
|
||||||
@@ -95,13 +94,18 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
|
|
||||||
public Token<RegexElementLabel> Source { get; }
|
public Token<RegexElementLabel> Source { get; }
|
||||||
|
|
||||||
public void AppendNode(RegexAstNode nextToken) { }
|
public void AppendNode(
|
||||||
|
RegexAstNode nextToken
|
||||||
|
) { }
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[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;
|
left &= RegexElementLabel.OPERATOR;
|
||||||
right |= RegexElementLabel.OPERATOR;
|
right &= RegexElementLabel.OPERATOR;
|
||||||
int v_left = (int)left, v_right = (int)right;
|
int v_left = (int)left, v_right = (int)right;
|
||||||
if (v_left == 0 || v_right == 0) {
|
if (v_left == 0 || v_right == 0) {
|
||||||
return (false,0);
|
return (false,0);
|
||||||
@@ -109,9 +113,15 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
return (true,v_left - v_right);
|
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> pSource = prev.Source;
|
||||||
Token<RegexElementLabel> nSource = next.Source;
|
Token<RegexElementLabel> nSource = next.Source;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
SEQUENCE = UNIT | UNIT_SPAN | UNIT_SET,
|
SEQUENCE = UNIT | UNIT_SPAN | UNIT_SET,
|
||||||
OPERATOR = COUNT | ALT | CONCAT,
|
OPERATOR = COUNT | ALT | CONCAT,
|
||||||
ATOM_BEGIN = SEQUENCE | GROUP_BEGIN ,
|
ATOM_BEGIN = SEQUENCE | GROUP_BEGIN ,
|
||||||
ATOM_END = SEQUENCE | GROUP_END,
|
ATOM_END = SEQUENCE | GROUP_END | COUNT,
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="RegexInterpreter"
|
// file="RegexInterpreter"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. 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.Reflection.Emit;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -46,7 +47,20 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
|
|
||||||
private RegexInterpreter() { }
|
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();
|
RegexInterpreter interpreter = new RegexInterpreter();
|
||||||
interpreter._source = regex;
|
interpreter._source = regex;
|
||||||
@@ -114,7 +128,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
// process operator by precedence
|
// process operator by precedence
|
||||||
while (_stack.TryPeekOperator(out RegexAstNode topOp))
|
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) {
|
if (!cmp.Valid) {
|
||||||
throw new InvalidOperationException("Operators with same precedence are not supported.");
|
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
|
// phase0: 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);
|
||||||
|
HashSet<RegexAstNode>.Enumerator enumerator = _tree.AllNodes.GetEnumerator();
|
||||||
|
|
||||||
// phase1: ALT flatten
|
// phase1: ALT flatten
|
||||||
int opCount = 0, cycle = 0;
|
while (enumerator.MoveNext())
|
||||||
do
|
|
||||||
{
|
{
|
||||||
cycle++;
|
RegexAstNode node = enumerator.Current;
|
||||||
} while (opCount != 0 && cycle < 16);
|
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
|
// phase2: ALT merge
|
||||||
opCount = 0;
|
|
||||||
cycle = 0;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
opCount = 1;
|
|
||||||
} while (opCount != 0);
|
|
||||||
|
|
||||||
// phase3: CONCAT merge
|
// phase3: CONCAT merge
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessOperator(RegexAstNode op)
|
private void ProcessOperator(
|
||||||
|
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) && _stack.TryPopOperand(out RegexAstNode? _2))
|
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||||
|
_stack.TryPopOperand(out RegexAstNode? _2))
|
||||||
{
|
{
|
||||||
op.Left = _2;
|
op.Left = _2;
|
||||||
op.Right = _1;
|
op.Right = _1;
|
||||||
@@ -207,7 +243,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
}
|
}
|
||||||
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
|
} 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.Left = _1;
|
||||||
op.Right = _2;
|
op.Right = _2;
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
{
|
{
|
||||||
int length = (i - pos - 1);
|
int length = (i - pos - 1);
|
||||||
position = i + 1;
|
position = i + 1;
|
||||||
return new Token<RegexElementLabel>(_source[(pos - 1)..(i - 1)], 0,
|
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0,
|
||||||
position,
|
position,
|
||||||
RegexElementLabel.UNIT_SET);
|
RegexElementLabel.UNIT_SET);
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
|
|||||||
if (_source[i] == '}')
|
if (_source[i] == '}')
|
||||||
{
|
{
|
||||||
position = i + 1;
|
position = i + 1;
|
||||||
return new Token<RegexElementLabel>(_source[(pos)..(i - 1)], 0, pos,
|
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||||
RegexElementLabel.COUNT);
|
RegexElementLabel.COUNT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="TestRegexAnalyse"
|
// file="TestRegexAnalyse"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload Space
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload Space. All rights reserved.
|
// Copyright (c) Nitload. 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
|
||||||
@@ -38,11 +38,17 @@ namespace StgSharp
|
|||||||
public class TestRegexAnalyse
|
public class TestRegexAnalyse
|
||||||
{
|
{
|
||||||
|
|
||||||
private const string regex = @"abc(?<name>abc)";
|
private const string regex = @"ab[1-9]+(?<name>abc)";
|
||||||
|
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
|
RegexInterpreter interpreter = RegexInterpreter.Analyze(regex);
|
||||||
|
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree = interpreter.Tree;
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine();
|
||||||
|
foreach (RegexAstNode node in tree) {
|
||||||
|
Console.WriteLine($"{node.Source.Value}\t{node.Source.Flag}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user