Normalize all existing files to LF
This commit is contained in:
@@ -1,212 +1,212 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexAstNode"
|
||||
// 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.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
|
||||
{
|
||||
|
||||
// private RegexAstNode _left, _right;
|
||||
|
||||
public RegexAstNode(
|
||||
Token<RegexElementLabel> source
|
||||
)
|
||||
{
|
||||
Source = source;
|
||||
}
|
||||
|
||||
public int EnumState { get; set; }
|
||||
|
||||
public long NodeFlag => (long)Source.Flag;
|
||||
|
||||
public static RegexAstNode Empty { get; }
|
||||
= new EmptyRegexAstNode();
|
||||
|
||||
public RegexAstNode Previous { get; set; }
|
||||
|
||||
public RegexAstNode Next { get; set; }
|
||||
|
||||
public RegexAstNode Parent { get; set; }
|
||||
|
||||
public RegexAstNode Left
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
_ = (field?.Parent = Empty);
|
||||
field = value;
|
||||
if (!IsNullOrEmpty(value)) {
|
||||
value.Parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RegexAstNode Right
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
_ = (field?.Parent = Empty);
|
||||
field = value;
|
||||
if (!IsNullOrEmpty(value)) {
|
||||
value.Parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RegexElementLabel EqualityTypeConvert => Source.Flag;
|
||||
|
||||
public string Value => Source.Value;
|
||||
|
||||
public string CodeConvertTemplate { get; }
|
||||
|
||||
public Token<RegexElementLabel> Source { get; private protected set; }
|
||||
|
||||
public void AppendNode(
|
||||
RegexAstNode nextToken
|
||||
)
|
||||
{
|
||||
Next = nextToken;
|
||||
nextToken.Previous = this;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static (bool Valid, int Value) ComparePrecedence(
|
||||
RegexElementLabel left,
|
||||
RegexElementLabel right
|
||||
)
|
||||
{
|
||||
left &= RegexElementLabel.OPERATOR;
|
||||
right &= RegexElementLabel.OPERATOR;
|
||||
int v_left = (int)left, v_right = (int)right;
|
||||
if (v_left == 0 || v_right == 0) {
|
||||
return (false,0);
|
||||
}
|
||||
return (true,v_left - v_right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsNullOrEmpty(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
return node is null || ReferenceEquals(node, Empty);
|
||||
}
|
||||
/**/
|
||||
|
||||
public void PrependNode(
|
||||
RegexAstNode previousNode
|
||||
)
|
||||
{
|
||||
Previous = previousNode;
|
||||
previousNode.Next = this;
|
||||
}
|
||||
|
||||
public void RemoveChild(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
if (Left == node)
|
||||
{
|
||||
Left = Empty;
|
||||
} else if (Right == node) {
|
||||
Right = Empty;
|
||||
}
|
||||
node.Parent = Empty;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static RegexAstNode NewEmptyNode(
|
||||
)
|
||||
{
|
||||
return new EmptyRegexAstNode();
|
||||
}
|
||||
|
||||
internal void RemoveAndAsChild(
|
||||
bool useLeft
|
||||
)
|
||||
{
|
||||
if (useLeft)
|
||||
{
|
||||
Source = Left.Source;
|
||||
Left.Parent = Empty;
|
||||
Left = Empty;
|
||||
} else
|
||||
{
|
||||
Source = Right.Source;
|
||||
Right.Parent = Empty;
|
||||
Right = Empty;
|
||||
}
|
||||
}
|
||||
/**/
|
||||
|
||||
internal void ReplaceBy(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
Source = node.Source;
|
||||
Left = node.Left;
|
||||
Right = node.Right;
|
||||
if (!IsNullOrEmpty(Left)) {
|
||||
Left.Parent = this;
|
||||
}
|
||||
if (!IsNullOrEmpty(Right)) {
|
||||
Right.Parent = this;
|
||||
}
|
||||
Previous = node.Previous;
|
||||
Next = node.Next;
|
||||
|
||||
RegexAstNode prev = Previous;
|
||||
RegexAstNode next = Next;
|
||||
if (!IsNullOrEmpty(prev)) {
|
||||
prev.Next = this;
|
||||
}
|
||||
if (!IsNullOrEmpty(next)) {
|
||||
next.Previous = this;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class EmptyRegexAstNode : RegexAstNode
|
||||
|
||||
{
|
||||
|
||||
public EmptyRegexAstNode() : base(Token<RegexElementLabel>.Empty) { }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexAstNode"
|
||||
// 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.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexAstNode : ISyntaxNode<RegexAstNode, RegexElementLabel>
|
||||
{
|
||||
|
||||
// private RegexAstNode _left, _right;
|
||||
|
||||
public RegexAstNode(
|
||||
Token<RegexElementLabel> source
|
||||
)
|
||||
{
|
||||
Source = source;
|
||||
}
|
||||
|
||||
public int EnumState { get; set; }
|
||||
|
||||
public long NodeFlag => (long)Source.Flag;
|
||||
|
||||
public static RegexAstNode Empty { get; }
|
||||
= new EmptyRegexAstNode();
|
||||
|
||||
public RegexAstNode Previous { get; set; }
|
||||
|
||||
public RegexAstNode Next { get; set; }
|
||||
|
||||
public RegexAstNode Parent { get; set; }
|
||||
|
||||
public RegexAstNode Left
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
_ = (field?.Parent = Empty);
|
||||
field = value;
|
||||
if (!IsNullOrEmpty(value)) {
|
||||
value.Parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RegexAstNode Right
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
_ = (field?.Parent = Empty);
|
||||
field = value;
|
||||
if (!IsNullOrEmpty(value)) {
|
||||
value.Parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RegexElementLabel EqualityTypeConvert => Source.Flag;
|
||||
|
||||
public string Value => Source.Value;
|
||||
|
||||
public string CodeConvertTemplate { get; }
|
||||
|
||||
public Token<RegexElementLabel> Source { get; private protected set; }
|
||||
|
||||
public void AppendNode(
|
||||
RegexAstNode nextToken
|
||||
)
|
||||
{
|
||||
Next = nextToken;
|
||||
nextToken.Previous = this;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static (bool Valid, int Value) ComparePrecedence(
|
||||
RegexElementLabel left,
|
||||
RegexElementLabel right
|
||||
)
|
||||
{
|
||||
left &= RegexElementLabel.OPERATOR;
|
||||
right &= RegexElementLabel.OPERATOR;
|
||||
int v_left = (int)left, v_right = (int)right;
|
||||
if (v_left == 0 || v_right == 0) {
|
||||
return (false,0);
|
||||
}
|
||||
return (true,v_left - v_right);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsNullOrEmpty(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
return node is null || ReferenceEquals(node, Empty);
|
||||
}
|
||||
/**/
|
||||
|
||||
public void PrependNode(
|
||||
RegexAstNode previousNode
|
||||
)
|
||||
{
|
||||
Previous = previousNode;
|
||||
previousNode.Next = this;
|
||||
}
|
||||
|
||||
public void RemoveChild(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
if (Left == node)
|
||||
{
|
||||
Left = Empty;
|
||||
} else if (Right == node) {
|
||||
Right = Empty;
|
||||
}
|
||||
node.Parent = Empty;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static RegexAstNode NewEmptyNode(
|
||||
)
|
||||
{
|
||||
return new EmptyRegexAstNode();
|
||||
}
|
||||
|
||||
internal void RemoveAndAsChild(
|
||||
bool useLeft
|
||||
)
|
||||
{
|
||||
if (useLeft)
|
||||
{
|
||||
Source = Left.Source;
|
||||
Left.Parent = Empty;
|
||||
Left = Empty;
|
||||
} else
|
||||
{
|
||||
Source = Right.Source;
|
||||
Right.Parent = Empty;
|
||||
Right = Empty;
|
||||
}
|
||||
}
|
||||
/**/
|
||||
|
||||
internal void ReplaceBy(
|
||||
RegexAstNode node
|
||||
)
|
||||
{
|
||||
Source = node.Source;
|
||||
Left = node.Left;
|
||||
Right = node.Right;
|
||||
if (!IsNullOrEmpty(Left)) {
|
||||
Left.Parent = this;
|
||||
}
|
||||
if (!IsNullOrEmpty(Right)) {
|
||||
Right.Parent = this;
|
||||
}
|
||||
Previous = node.Previous;
|
||||
Next = node.Next;
|
||||
|
||||
RegexAstNode prev = Previous;
|
||||
RegexAstNode next = Next;
|
||||
if (!IsNullOrEmpty(prev)) {
|
||||
prev.Next = this;
|
||||
}
|
||||
if (!IsNullOrEmpty(next)) {
|
||||
next.Previous = this;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class EmptyRegexAstNode : RegexAstNode
|
||||
|
||||
{
|
||||
|
||||
public EmptyRegexAstNode() : base(Token<RegexElementLabel>.Empty) { }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexElementLabel"
|
||||
// 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.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
[Flags]
|
||||
public enum RegexElementLabel
|
||||
{
|
||||
|
||||
NONE = 0,
|
||||
UNIT = 1,
|
||||
UNIT_SET = 2,
|
||||
UNIT_SPAN = 4,
|
||||
GROUP_BEGIN = 8,
|
||||
GROUP_END = 16,
|
||||
COUNT = 32,
|
||||
CONCAT = 64,
|
||||
ALT = 128,
|
||||
SEQUENCE = UNIT | UNIT_SPAN | UNIT_SET,
|
||||
SINGLE = UNIT | UNIT_SET,
|
||||
OPERATOR = COUNT | ALT | CONCAT,
|
||||
ATOM_BEGIN = SEQUENCE | GROUP_BEGIN ,
|
||||
ATOM_END = SEQUENCE | GROUP_END | COUNT,
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexElementLabel"
|
||||
// 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.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
[Flags]
|
||||
public enum RegexElementLabel
|
||||
{
|
||||
|
||||
NONE = 0,
|
||||
UNIT = 1,
|
||||
UNIT_SET = 2,
|
||||
UNIT_SPAN = 4,
|
||||
GROUP_BEGIN = 8,
|
||||
GROUP_END = 16,
|
||||
COUNT = 32,
|
||||
CONCAT = 64,
|
||||
ALT = 128,
|
||||
SEQUENCE = UNIT | UNIT_SPAN | UNIT_SET,
|
||||
SINGLE = UNIT | UNIT_SET,
|
||||
OPERATOR = COUNT | ALT | CONCAT,
|
||||
ATOM_BEGIN = SEQUENCE | GROUP_BEGIN ,
|
||||
ATOM_END = SEQUENCE | GROUP_END | COUNT,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,417 +1,417 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// 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.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Security;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
public partial class RegexInterpreter
|
||||
{
|
||||
|
||||
private static RegexAstNode FlattenAlt(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
||||
List<RegexAstNode> cases = [];
|
||||
RegexAstNode current;
|
||||
_ = enumerator.MoveNext(); // skip root
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert != RegexElementLabel.ALT)
|
||||
{
|
||||
cases.Add(current);
|
||||
enumerator.SkipNode(current);
|
||||
}
|
||||
current.Parent.RemoveChild(current);
|
||||
}
|
||||
for (int i = 0; i < cases.Count - 1; i++)
|
||||
{
|
||||
cases[i].AppendNode(cases[i + 1]);
|
||||
cases[i].Parent = root;
|
||||
}
|
||||
cases[^1].Parent = root;
|
||||
return cases[0];
|
||||
}
|
||||
|
||||
private static bool MergeConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* concat → 'a'
|
||||
* ╱
|
||||
* 'a'
|
||||
*/
|
||||
if ((root.Left.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||
RegexAstNode.IsNullOrEmpty(root.Right))
|
||||
{
|
||||
root.RemoveAndAsChild(true);
|
||||
return true;
|
||||
}
|
||||
if ((root.Right.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||
RegexAstNode.IsNullOrEmpty(root.Left))
|
||||
{
|
||||
root.RemoveAndAsChild(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* concat → concat
|
||||
* ╱ ╲ /
|
||||
* 'a' concat "abc"
|
||||
* / \
|
||||
* 'b' 'c'
|
||||
*/
|
||||
List<string> buffer = [];
|
||||
int length = 0;
|
||||
RegexAstNode right = root.Right;
|
||||
RegexAstNode left = root.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT) {
|
||||
return false;
|
||||
}
|
||||
string str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
while (!RegexAstNode.IsNullOrEmpty(right) &&
|
||||
right.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
left = right.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT)
|
||||
{
|
||||
break;
|
||||
}
|
||||
str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
root.Right = right.Right;
|
||||
right.Parent = RegexAstNode.Empty;
|
||||
right = root.Right;
|
||||
right.Parent = root;
|
||||
}
|
||||
if (right.EqualityTypeConvert == RegexElementLabel.UNIT_SPAN ||
|
||||
right.EqualityTypeConvert == RegexElementLabel.UNIT)
|
||||
{
|
||||
root.Right = RegexAstNode.Empty;
|
||||
buffer.Add(right.Value);
|
||||
length += right.Value.Length;
|
||||
}
|
||||
str = string.Create(length, buffer, static(
|
||||
span,
|
||||
buffer
|
||||
) => {
|
||||
int offset = 0;
|
||||
for (int i = 0; i < buffer.Count; i++)
|
||||
{
|
||||
buffer[i].AsSpan().CopyTo(span[offset..]);
|
||||
offset += buffer[i].Length;
|
||||
}
|
||||
});
|
||||
root.Left = new RegexAstNode(new Token<RegexElementLabel>(str, left.Source.Line,
|
||||
left.Source.Column,
|
||||
str.Length == 1 ?
|
||||
RegexElementLabel.UNIT :
|
||||
RegexElementLabel.UNIT_SPAN));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void RotateConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
RegexAstNode left = root.Left;
|
||||
while (!RegexAstNode.IsNullOrEmpty(left) &&
|
||||
left.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
/*
|
||||
* root → root
|
||||
* ╱ ╲ ╱ ╲
|
||||
* left c a left
|
||||
* ╱ ╲ ╱ ╲
|
||||
* a b b c
|
||||
*/
|
||||
RegexAstNode a = left.Left;
|
||||
RegexAstNode b = left.Right;
|
||||
RegexAstNode c = root.Right;
|
||||
root.Left = a;
|
||||
if (RegexAstNode.IsNullOrEmpty(a)) {
|
||||
a.Parent = root;
|
||||
}
|
||||
|
||||
root.Right = left;
|
||||
left.Left = b;
|
||||
left.Right = c;
|
||||
if (RegexAstNode.IsNullOrEmpty(b)) {
|
||||
b.Parent = left;
|
||||
}
|
||||
|
||||
if (RegexAstNode.IsNullOrEmpty(c)) {
|
||||
c.Parent = left;
|
||||
}
|
||||
left = root.Left;
|
||||
}
|
||||
}
|
||||
|
||||
#region merge alt
|
||||
|
||||
/// <summary>
|
||||
/// Get union beginning part of cases
|
||||
/// </summary>
|
||||
/// <param name="cases">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static RegexAstNode MergeAlt(
|
||||
List<RegexAstNode> cases
|
||||
)
|
||||
{
|
||||
UnionAlt union = new(0, cases);
|
||||
RegexAstNode root = union.AsNormal();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private class UnionAlt : RegexAstNode
|
||||
{
|
||||
|
||||
private const int MaxLevel = 8;
|
||||
|
||||
private UnionAlt(
|
||||
int level,
|
||||
RegexAstNode node
|
||||
)
|
||||
: base(node.Source)
|
||||
{
|
||||
Level = level;
|
||||
Cases = new List<RegexAstNode>();
|
||||
|
||||
RegexAstNode right = IsNullOrEmpty(node.Right) ?
|
||||
new RegexAstNode(
|
||||
new Token<RegexElementLabel>(node.Value, 0, node.Source.Column,
|
||||
RegexElementLabel.UNIT)
|
||||
) :
|
||||
node.Right;
|
||||
Cases.Add(right);
|
||||
|
||||
node.PrependNode(Empty);
|
||||
node.AppendNode(Empty);
|
||||
UnionBegin = node;
|
||||
UnionEnd = node;
|
||||
}
|
||||
|
||||
public UnionAlt(
|
||||
int level,
|
||||
List<RegexAstNode> c
|
||||
)
|
||||
: base(c[0].Source)
|
||||
{
|
||||
Cases = c;
|
||||
Level = level;
|
||||
}
|
||||
|
||||
public List<RegexAstNode> Cases { get; set; }
|
||||
|
||||
public RegexAstNode UnionBegin { get; set; }
|
||||
|
||||
public RegexAstNode UnionEnd { get; set; }
|
||||
|
||||
private int Level { get; set; }
|
||||
|
||||
public RegexAstNode AsNormal()
|
||||
{
|
||||
if (Level >= MaxLevel)
|
||||
{
|
||||
goto build;
|
||||
}
|
||||
merge:
|
||||
List<RegexAstNode> buffer = [];
|
||||
for (int i = 0; i < Cases.Count; i++)
|
||||
{
|
||||
if ((Cases[i].EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
RegexAstNode concat = new RegexAstNode(new Token<RegexElementLabel>(string.Empty,
|
||||
0,
|
||||
Cases[i].Source.Column,
|
||||
RegexElementLabel.CONCAT));
|
||||
concat.Left = Cases[i];
|
||||
Cases[i].Parent = concat;
|
||||
concat.Right = Empty;
|
||||
Cases[i] = concat;
|
||||
}
|
||||
RegexAstNode current = Cases[i];
|
||||
int j = 0;
|
||||
for (; j < buffer.Count; j++)
|
||||
{
|
||||
if (CanMerge(current, buffer[j]))
|
||||
{
|
||||
// find a case in buffer that can merge with current case,
|
||||
// then merge them and break
|
||||
RegexAstNode b = buffer[j];
|
||||
if (b is not UnionAlt alt)
|
||||
{
|
||||
alt = new UnionAlt(Level + 1, b);
|
||||
alt.AddCase(current);
|
||||
buffer[j] = alt;
|
||||
} else
|
||||
{
|
||||
alt.AddCase(current);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j >= buffer.Count) {
|
||||
buffer.Add(current);
|
||||
}
|
||||
}
|
||||
if (Cases.Count != buffer.Count) {
|
||||
Cases = buffer;
|
||||
}
|
||||
|
||||
if (buffer.Count == 1)
|
||||
{
|
||||
// if there is only one case,
|
||||
// then we suppose that its contained cases has a same beginning,
|
||||
// so we attach it to UnionEnd and stay level unchanged
|
||||
UnionAlt alt = (Cases[0] as UnionAlt)!;
|
||||
if (IsNullOrEmpty(UnionBegin))
|
||||
{
|
||||
UnionBegin = alt.UnionBegin;
|
||||
UnionEnd = alt.UnionEnd;
|
||||
} else
|
||||
{
|
||||
alt.UnionBegin.Parent = UnionEnd;
|
||||
UnionEnd.Right = alt.UnionBegin;
|
||||
UnionEnd = alt.UnionBegin;
|
||||
}
|
||||
|
||||
Cases = alt.Cases;
|
||||
goto merge;
|
||||
}
|
||||
|
||||
build:
|
||||
|
||||
// suppose that from UnionBegin to UnionEnd is a normal form without union
|
||||
for (int i = 0; i < Cases.Count; i++)
|
||||
{
|
||||
if (Cases[i] is UnionAlt union) {
|
||||
Cases[i] = union.AsNormal();
|
||||
}
|
||||
}
|
||||
|
||||
RegexAstNode branch;
|
||||
if (IsNullOrEmpty(UnionEnd))
|
||||
{
|
||||
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
Source.Column,
|
||||
RegexElementLabel.ALT));
|
||||
UnionBegin = branch;
|
||||
UnionEnd = branch;
|
||||
} else
|
||||
{
|
||||
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
UnionEnd.Source.Column,
|
||||
RegexElementLabel.ALT));
|
||||
UnionEnd.Right = branch;
|
||||
branch.Parent = UnionEnd;
|
||||
UnionEnd = branch;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Cases.Count - 1; i++)
|
||||
{
|
||||
Cases[i] ??= NewEmptyNode();
|
||||
Cases[i].AppendNode(Cases[i + 1]);
|
||||
Cases[i].Parent = UnionEnd;
|
||||
}
|
||||
UnionEnd.Right = Cases[0];
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(UnionBegin);
|
||||
|
||||
return UnionBegin;
|
||||
}
|
||||
|
||||
private void AddCase(
|
||||
RegexAstNode _case
|
||||
)
|
||||
{
|
||||
RegexAstNode right = IsNullOrEmpty(_case.Right) ?
|
||||
new RegexAstNode(
|
||||
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column,
|
||||
RegexElementLabel.UNIT)
|
||||
) :
|
||||
_case.Right;
|
||||
Cases.Add(right);
|
||||
}
|
||||
|
||||
private static bool CanMerge(
|
||||
RegexAstNode origin,
|
||||
RegexAstNode target
|
||||
)
|
||||
{
|
||||
// suppose that origin is not a union,
|
||||
if (target is UnionAlt alt)
|
||||
{
|
||||
// then we suppose that alt has only one are same
|
||||
return origin .EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
(origin.Left.EqualityTypeConvert & RegexElementLabel.SINGLE) != 0 &&
|
||||
origin.Left.EqualityTypeConvert == alt.UnionEnd.Left.EqualityTypeConvert &&
|
||||
origin.Left.Value == alt.UnionEnd.Left.Value;
|
||||
} else
|
||||
{
|
||||
return origin.EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
target.EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
(origin.Left.EqualityTypeConvert & RegexElementLabel.SINGLE) != 0 &&
|
||||
origin.Left.EqualityTypeConvert == target.Left.EqualityTypeConvert &&
|
||||
origin.Left.Value == target.Left.Value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/**/
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// 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.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Security;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
public partial class RegexInterpreter
|
||||
{
|
||||
|
||||
private static RegexAstNode FlattenAlt(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
|
||||
List<RegexAstNode> cases = [];
|
||||
RegexAstNode current;
|
||||
_ = enumerator.MoveNext(); // skip root
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert != RegexElementLabel.ALT)
|
||||
{
|
||||
cases.Add(current);
|
||||
enumerator.SkipNode(current);
|
||||
}
|
||||
current.Parent.RemoveChild(current);
|
||||
}
|
||||
for (int i = 0; i < cases.Count - 1; i++)
|
||||
{
|
||||
cases[i].AppendNode(cases[i + 1]);
|
||||
cases[i].Parent = root;
|
||||
}
|
||||
cases[^1].Parent = root;
|
||||
return cases[0];
|
||||
}
|
||||
|
||||
private static bool MergeConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* concat → 'a'
|
||||
* ╱
|
||||
* 'a'
|
||||
*/
|
||||
if ((root.Left.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||
RegexAstNode.IsNullOrEmpty(root.Right))
|
||||
{
|
||||
root.RemoveAndAsChild(true);
|
||||
return true;
|
||||
}
|
||||
if ((root.Right.EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0 &&
|
||||
RegexAstNode.IsNullOrEmpty(root.Left))
|
||||
{
|
||||
root.RemoveAndAsChild(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* concat → concat
|
||||
* ╱ ╲ /
|
||||
* 'a' concat "abc"
|
||||
* / \
|
||||
* 'b' 'c'
|
||||
*/
|
||||
List<string> buffer = [];
|
||||
int length = 0;
|
||||
RegexAstNode right = root.Right;
|
||||
RegexAstNode left = root.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT) {
|
||||
return false;
|
||||
}
|
||||
string str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
while (!RegexAstNode.IsNullOrEmpty(right) &&
|
||||
right.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
left = right.Left;
|
||||
if (left.EqualityTypeConvert != RegexElementLabel.UNIT_SPAN &&
|
||||
left.EqualityTypeConvert != RegexElementLabel.UNIT)
|
||||
{
|
||||
break;
|
||||
}
|
||||
str = left.Value;
|
||||
buffer.Add(str);
|
||||
length += str.Length;
|
||||
root.Right = right.Right;
|
||||
right.Parent = RegexAstNode.Empty;
|
||||
right = root.Right;
|
||||
right.Parent = root;
|
||||
}
|
||||
if (right.EqualityTypeConvert == RegexElementLabel.UNIT_SPAN ||
|
||||
right.EqualityTypeConvert == RegexElementLabel.UNIT)
|
||||
{
|
||||
root.Right = RegexAstNode.Empty;
|
||||
buffer.Add(right.Value);
|
||||
length += right.Value.Length;
|
||||
}
|
||||
str = string.Create(length, buffer, static(
|
||||
span,
|
||||
buffer
|
||||
) => {
|
||||
int offset = 0;
|
||||
for (int i = 0; i < buffer.Count; i++)
|
||||
{
|
||||
buffer[i].AsSpan().CopyTo(span[offset..]);
|
||||
offset += buffer[i].Length;
|
||||
}
|
||||
});
|
||||
root.Left = new RegexAstNode(new Token<RegexElementLabel>(str, left.Source.Line,
|
||||
left.Source.Column,
|
||||
str.Length == 1 ?
|
||||
RegexElementLabel.UNIT :
|
||||
RegexElementLabel.UNIT_SPAN));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void RotateConcat(
|
||||
RegexAstNode root
|
||||
)
|
||||
{
|
||||
RegexAstNode left = root.Left;
|
||||
while (!RegexAstNode.IsNullOrEmpty(left) &&
|
||||
left.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
/*
|
||||
* root → root
|
||||
* ╱ ╲ ╱ ╲
|
||||
* left c a left
|
||||
* ╱ ╲ ╱ ╲
|
||||
* a b b c
|
||||
*/
|
||||
RegexAstNode a = left.Left;
|
||||
RegexAstNode b = left.Right;
|
||||
RegexAstNode c = root.Right;
|
||||
root.Left = a;
|
||||
if (RegexAstNode.IsNullOrEmpty(a)) {
|
||||
a.Parent = root;
|
||||
}
|
||||
|
||||
root.Right = left;
|
||||
left.Left = b;
|
||||
left.Right = c;
|
||||
if (RegexAstNode.IsNullOrEmpty(b)) {
|
||||
b.Parent = left;
|
||||
}
|
||||
|
||||
if (RegexAstNode.IsNullOrEmpty(c)) {
|
||||
c.Parent = left;
|
||||
}
|
||||
left = root.Left;
|
||||
}
|
||||
}
|
||||
|
||||
#region merge alt
|
||||
|
||||
/// <summary>
|
||||
/// Get union beginning part of cases
|
||||
/// </summary>
|
||||
/// <param name="cases">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static RegexAstNode MergeAlt(
|
||||
List<RegexAstNode> cases
|
||||
)
|
||||
{
|
||||
UnionAlt union = new(0, cases);
|
||||
RegexAstNode root = union.AsNormal();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private class UnionAlt : RegexAstNode
|
||||
{
|
||||
|
||||
private const int MaxLevel = 8;
|
||||
|
||||
private UnionAlt(
|
||||
int level,
|
||||
RegexAstNode node
|
||||
)
|
||||
: base(node.Source)
|
||||
{
|
||||
Level = level;
|
||||
Cases = new List<RegexAstNode>();
|
||||
|
||||
RegexAstNode right = IsNullOrEmpty(node.Right) ?
|
||||
new RegexAstNode(
|
||||
new Token<RegexElementLabel>(node.Value, 0, node.Source.Column,
|
||||
RegexElementLabel.UNIT)
|
||||
) :
|
||||
node.Right;
|
||||
Cases.Add(right);
|
||||
|
||||
node.PrependNode(Empty);
|
||||
node.AppendNode(Empty);
|
||||
UnionBegin = node;
|
||||
UnionEnd = node;
|
||||
}
|
||||
|
||||
public UnionAlt(
|
||||
int level,
|
||||
List<RegexAstNode> c
|
||||
)
|
||||
: base(c[0].Source)
|
||||
{
|
||||
Cases = c;
|
||||
Level = level;
|
||||
}
|
||||
|
||||
public List<RegexAstNode> Cases { get; set; }
|
||||
|
||||
public RegexAstNode UnionBegin { get; set; }
|
||||
|
||||
public RegexAstNode UnionEnd { get; set; }
|
||||
|
||||
private int Level { get; set; }
|
||||
|
||||
public RegexAstNode AsNormal()
|
||||
{
|
||||
if (Level >= MaxLevel)
|
||||
{
|
||||
goto build;
|
||||
}
|
||||
merge:
|
||||
List<RegexAstNode> buffer = [];
|
||||
for (int i = 0; i < Cases.Count; i++)
|
||||
{
|
||||
if ((Cases[i].EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
RegexAstNode concat = new RegexAstNode(new Token<RegexElementLabel>(string.Empty,
|
||||
0,
|
||||
Cases[i].Source.Column,
|
||||
RegexElementLabel.CONCAT));
|
||||
concat.Left = Cases[i];
|
||||
Cases[i].Parent = concat;
|
||||
concat.Right = Empty;
|
||||
Cases[i] = concat;
|
||||
}
|
||||
RegexAstNode current = Cases[i];
|
||||
int j = 0;
|
||||
for (; j < buffer.Count; j++)
|
||||
{
|
||||
if (CanMerge(current, buffer[j]))
|
||||
{
|
||||
// find a case in buffer that can merge with current case,
|
||||
// then merge them and break
|
||||
RegexAstNode b = buffer[j];
|
||||
if (b is not UnionAlt alt)
|
||||
{
|
||||
alt = new UnionAlt(Level + 1, b);
|
||||
alt.AddCase(current);
|
||||
buffer[j] = alt;
|
||||
} else
|
||||
{
|
||||
alt.AddCase(current);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j >= buffer.Count) {
|
||||
buffer.Add(current);
|
||||
}
|
||||
}
|
||||
if (Cases.Count != buffer.Count) {
|
||||
Cases = buffer;
|
||||
}
|
||||
|
||||
if (buffer.Count == 1)
|
||||
{
|
||||
// if there is only one case,
|
||||
// then we suppose that its contained cases has a same beginning,
|
||||
// so we attach it to UnionEnd and stay level unchanged
|
||||
UnionAlt alt = (Cases[0] as UnionAlt)!;
|
||||
if (IsNullOrEmpty(UnionBegin))
|
||||
{
|
||||
UnionBegin = alt.UnionBegin;
|
||||
UnionEnd = alt.UnionEnd;
|
||||
} else
|
||||
{
|
||||
alt.UnionBegin.Parent = UnionEnd;
|
||||
UnionEnd.Right = alt.UnionBegin;
|
||||
UnionEnd = alt.UnionBegin;
|
||||
}
|
||||
|
||||
Cases = alt.Cases;
|
||||
goto merge;
|
||||
}
|
||||
|
||||
build:
|
||||
|
||||
// suppose that from UnionBegin to UnionEnd is a normal form without union
|
||||
for (int i = 0; i < Cases.Count; i++)
|
||||
{
|
||||
if (Cases[i] is UnionAlt union) {
|
||||
Cases[i] = union.AsNormal();
|
||||
}
|
||||
}
|
||||
|
||||
RegexAstNode branch;
|
||||
if (IsNullOrEmpty(UnionEnd))
|
||||
{
|
||||
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
Source.Column,
|
||||
RegexElementLabel.ALT));
|
||||
UnionBegin = branch;
|
||||
UnionEnd = branch;
|
||||
} else
|
||||
{
|
||||
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
|
||||
UnionEnd.Source.Column,
|
||||
RegexElementLabel.ALT));
|
||||
UnionEnd.Right = branch;
|
||||
branch.Parent = UnionEnd;
|
||||
UnionEnd = branch;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Cases.Count - 1; i++)
|
||||
{
|
||||
Cases[i] ??= NewEmptyNode();
|
||||
Cases[i].AppendNode(Cases[i + 1]);
|
||||
Cases[i].Parent = UnionEnd;
|
||||
}
|
||||
UnionEnd.Right = Cases[0];
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(UnionBegin);
|
||||
|
||||
return UnionBegin;
|
||||
}
|
||||
|
||||
private void AddCase(
|
||||
RegexAstNode _case
|
||||
)
|
||||
{
|
||||
RegexAstNode right = IsNullOrEmpty(_case.Right) ?
|
||||
new RegexAstNode(
|
||||
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column,
|
||||
RegexElementLabel.UNIT)
|
||||
) :
|
||||
_case.Right;
|
||||
Cases.Add(right);
|
||||
}
|
||||
|
||||
private static bool CanMerge(
|
||||
RegexAstNode origin,
|
||||
RegexAstNode target
|
||||
)
|
||||
{
|
||||
// suppose that origin is not a union,
|
||||
if (target is UnionAlt alt)
|
||||
{
|
||||
// then we suppose that alt has only one are same
|
||||
return origin .EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
(origin.Left.EqualityTypeConvert & RegexElementLabel.SINGLE) != 0 &&
|
||||
origin.Left.EqualityTypeConvert == alt.UnionEnd.Left.EqualityTypeConvert &&
|
||||
origin.Left.Value == alt.UnionEnd.Left.Value;
|
||||
} else
|
||||
{
|
||||
return origin.EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
target.EqualityTypeConvert == RegexElementLabel.CONCAT &&
|
||||
(origin.Left.EqualityTypeConvert & RegexElementLabel.SINGLE) != 0 &&
|
||||
origin.Left.EqualityTypeConvert == target.Left.EqualityTypeConvert &&
|
||||
origin.Left.Value == target.Left.Value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/**/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,273 +1,273 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexInterpreter"
|
||||
// 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.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
public partial class RegexInterpreter
|
||||
{
|
||||
|
||||
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
|
||||
private CompileStack<RegexAstNode, RegexElementLabel> _stack = new();
|
||||
private Exception _interpreteException;
|
||||
private string _source;
|
||||
|
||||
private RegexInterpreter() { }
|
||||
|
||||
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;
|
||||
bool isSuccess;
|
||||
try
|
||||
{
|
||||
interpreter.Analyze();
|
||||
interpreter.OptimizeTree();
|
||||
isSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
interpreter._interpreteException = ex;
|
||||
isSuccess = false;
|
||||
}
|
||||
return interpreter;
|
||||
}
|
||||
|
||||
private void Analyze()
|
||||
{
|
||||
RegexTokenReader reader = new RegexTokenReader(_source);
|
||||
TokenParser<RegexElementLabel, RegexElementLabel> lexer = reader.Pipe(() => new RegexTokenParser());
|
||||
|
||||
while (lexer.TryReadToken(out Token<RegexElementLabel> token))
|
||||
{
|
||||
if ((token.Flag & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
// char or charset
|
||||
_stack.PushOperand(new RegexAstNode(token));
|
||||
} else if ((token.Flag & RegexElementLabel.GROUP_BEGIN) != 0)
|
||||
{
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
_stack.IncreaseDepth(0);
|
||||
} else if ((token.Flag & RegexElementLabel.GROUP_END) != 0)
|
||||
{
|
||||
// close all nodes here
|
||||
RegexAstNode op;
|
||||
while (_stack.TryPopOperator(out op)) {
|
||||
ProcessOperator(op);
|
||||
}
|
||||
if (_stack.OperandInDepthCount != 1) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
_stack.DecreaseDepth();
|
||||
op = _stack.PopOperator();
|
||||
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)
|
||||
{
|
||||
if (_stack.OperatorInDepthCount == 0)
|
||||
{
|
||||
// the first operator in stack
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
} else
|
||||
{
|
||||
// process operator by precedence
|
||||
while (_stack.TryPeekOperator(out RegexAstNode topOp))
|
||||
{
|
||||
(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.");
|
||||
}
|
||||
if (cmp.Value < 0)
|
||||
{
|
||||
_stack.PopOperator();
|
||||
ProcessOperator(topOp);
|
||||
continue;
|
||||
} else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_stack.Depth == 1)
|
||||
{
|
||||
if (_stack.OperandInDepthCount != 1 || _stack.OperatorInDepthCount != 0)
|
||||
{
|
||||
while (_stack.TryPopOperator(out RegexAstNode? op)) {
|
||||
ProcessOperator(op);
|
||||
}
|
||||
}
|
||||
if (_stack.OperandInDepthCount == 1 && _stack.OperatorInDepthCount == 0)
|
||||
{
|
||||
_tree.Root = _stack.PopOperand();
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Invalid regular expression syntax.");
|
||||
}
|
||||
|
||||
private void OptimizeTree(
|
||||
)
|
||||
{
|
||||
// phase0: scan container and remove no parent nodes
|
||||
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(_tree.Root);
|
||||
RegexAstNode current;
|
||||
|
||||
// phase1: ALT flatten
|
||||
// phase3: CONCAT normalize
|
||||
int i = 0;
|
||||
List<RegexAstNode> altNodes = [];
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
RegexAstNode union = FlattenAlt(current);
|
||||
altNodes.Add(current);
|
||||
current.Right = union;
|
||||
current.Left = RegexAstNode.Empty;
|
||||
} else if (current.EqualityTypeConvert == RegexElementLabel.CONCAT) {
|
||||
RotateConcat(current);
|
||||
}
|
||||
}
|
||||
|
||||
List<RegexAstNode> cases = [];
|
||||
|
||||
// phase2: then ALT union
|
||||
foreach (RegexAstNode item in altNodes)
|
||||
{
|
||||
RegexAstNode _case = item.Right;
|
||||
do
|
||||
{
|
||||
cases.Add(_case);
|
||||
_case = _case.Next;
|
||||
} while (!RegexAstNode.IsNullOrEmpty(_case));
|
||||
RegexAstNode newAlt = MergeAlt(cases);
|
||||
item.ReplaceBy(newAlt);
|
||||
}
|
||||
|
||||
// return;
|
||||
|
||||
// phase4: CONCAT merge
|
||||
enumerator.Reset();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
// suppose all concat is now in normalized form,
|
||||
// we can merge adjacent unit nodes into a single unit node with string value
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
if (MergeConcat(current)) {
|
||||
enumerator.SkipOnce();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessOperator(
|
||||
RegexAstNode op
|
||||
)
|
||||
{
|
||||
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||
_stack.TryPopOperand(out RegexAstNode? _2))
|
||||
{
|
||||
op.Left = _2;
|
||||
op.Right = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(_2);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
} else if (op.EqualityTypeConvert == RegexElementLabel.COUNT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1))
|
||||
{
|
||||
op.Left = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(op);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||
_stack.TryPopOperand(out RegexAstNode? _2))
|
||||
{
|
||||
op.Left = _2;
|
||||
op.Right = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(_2);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexInterpreter"
|
||||
// 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.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
public partial class RegexInterpreter
|
||||
{
|
||||
|
||||
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
|
||||
private CompileStack<RegexAstNode, RegexElementLabel> _stack = new();
|
||||
private Exception _interpreteException;
|
||||
private string _source;
|
||||
|
||||
private RegexInterpreter() { }
|
||||
|
||||
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;
|
||||
bool isSuccess;
|
||||
try
|
||||
{
|
||||
interpreter.Analyze();
|
||||
interpreter.OptimizeTree();
|
||||
isSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
interpreter._interpreteException = ex;
|
||||
isSuccess = false;
|
||||
}
|
||||
return interpreter;
|
||||
}
|
||||
|
||||
private void Analyze()
|
||||
{
|
||||
RegexTokenReader reader = new RegexTokenReader(_source);
|
||||
TokenParser<RegexElementLabel, RegexElementLabel> lexer = reader.Pipe(() => new RegexTokenParser());
|
||||
|
||||
while (lexer.TryReadToken(out Token<RegexElementLabel> token))
|
||||
{
|
||||
if ((token.Flag & RegexElementLabel.SEQUENCE) != 0)
|
||||
{
|
||||
// char or charset
|
||||
_stack.PushOperand(new RegexAstNode(token));
|
||||
} else if ((token.Flag & RegexElementLabel.GROUP_BEGIN) != 0)
|
||||
{
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
_stack.IncreaseDepth(0);
|
||||
} else if ((token.Flag & RegexElementLabel.GROUP_END) != 0)
|
||||
{
|
||||
// close all nodes here
|
||||
RegexAstNode op;
|
||||
while (_stack.TryPopOperator(out op)) {
|
||||
ProcessOperator(op);
|
||||
}
|
||||
if (_stack.OperandInDepthCount != 1) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
_stack.DecreaseDepth();
|
||||
op = _stack.PopOperator();
|
||||
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)
|
||||
{
|
||||
if (_stack.OperatorInDepthCount == 0)
|
||||
{
|
||||
// the first operator in stack
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
} else
|
||||
{
|
||||
// process operator by precedence
|
||||
while (_stack.TryPeekOperator(out RegexAstNode topOp))
|
||||
{
|
||||
(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.");
|
||||
}
|
||||
if (cmp.Value < 0)
|
||||
{
|
||||
_stack.PopOperator();
|
||||
ProcessOperator(topOp);
|
||||
continue;
|
||||
} else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
_stack.PushOperator(new RegexAstNode(token));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_stack.Depth == 1)
|
||||
{
|
||||
if (_stack.OperandInDepthCount != 1 || _stack.OperatorInDepthCount != 0)
|
||||
{
|
||||
while (_stack.TryPopOperator(out RegexAstNode? op)) {
|
||||
ProcessOperator(op);
|
||||
}
|
||||
}
|
||||
if (_stack.OperandInDepthCount == 1 && _stack.OperatorInDepthCount == 0)
|
||||
{
|
||||
_tree.Root = _stack.PopOperand();
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Invalid regular expression syntax.");
|
||||
}
|
||||
|
||||
private void OptimizeTree(
|
||||
)
|
||||
{
|
||||
// phase0: scan container and remove no parent nodes
|
||||
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
|
||||
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new TreeEnumerator<RegexAstNode, RegexElementLabel>(_tree.Root);
|
||||
RegexAstNode current;
|
||||
|
||||
// phase1: ALT flatten
|
||||
// phase3: CONCAT normalize
|
||||
int i = 0;
|
||||
List<RegexAstNode> altNodes = [];
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
RegexAstNode union = FlattenAlt(current);
|
||||
altNodes.Add(current);
|
||||
current.Right = union;
|
||||
current.Left = RegexAstNode.Empty;
|
||||
} else if (current.EqualityTypeConvert == RegexElementLabel.CONCAT) {
|
||||
RotateConcat(current);
|
||||
}
|
||||
}
|
||||
|
||||
List<RegexAstNode> cases = [];
|
||||
|
||||
// phase2: then ALT union
|
||||
foreach (RegexAstNode item in altNodes)
|
||||
{
|
||||
RegexAstNode _case = item.Right;
|
||||
do
|
||||
{
|
||||
cases.Add(_case);
|
||||
_case = _case.Next;
|
||||
} while (!RegexAstNode.IsNullOrEmpty(_case));
|
||||
RegexAstNode newAlt = MergeAlt(cases);
|
||||
item.ReplaceBy(newAlt);
|
||||
}
|
||||
|
||||
// return;
|
||||
|
||||
// phase4: CONCAT merge
|
||||
enumerator.Reset();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
// suppose all concat is now in normalized form,
|
||||
// we can merge adjacent unit nodes into a single unit node with string value
|
||||
current = enumerator.Current;
|
||||
if (current.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
if (MergeConcat(current)) {
|
||||
enumerator.SkipOnce();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessOperator(
|
||||
RegexAstNode op
|
||||
)
|
||||
{
|
||||
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||
_stack.TryPopOperand(out RegexAstNode? _2))
|
||||
{
|
||||
op.Left = _2;
|
||||
op.Right = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(_2);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
} else if (op.EqualityTypeConvert == RegexElementLabel.COUNT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1))
|
||||
{
|
||||
op.Left = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(op);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
|
||||
{
|
||||
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
|
||||
_stack.TryPopOperand(out RegexAstNode? _2))
|
||||
{
|
||||
op.Left = _2;
|
||||
op.Right = _1;
|
||||
_stack.PushOperand(op);
|
||||
_tree.AddNode(op);
|
||||
_tree.AddNode(_1);
|
||||
_tree.AddNode(_2);
|
||||
} else
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient operands for CONCAT operator.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +1,89 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenParser"
|
||||
// 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.Diagnostics.Tracing;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexTokenParser : TokenParser<RegexElementLabel, RegexElementLabel>
|
||||
{
|
||||
|
||||
private readonly Queue<Token<RegexElementLabel>> _buffer = [];
|
||||
|
||||
private RegexElementLabel _prev;
|
||||
|
||||
public override bool IsEmpty => Source.IsEmpty && _buffer.Count == 0;
|
||||
|
||||
public override Token<RegexElementLabel> ReadToken()
|
||||
{
|
||||
if (_buffer.TryDequeue(out Token<RegexElementLabel> token)) {
|
||||
return token;
|
||||
}
|
||||
if (Source.TryReadToken(out Token<RegexElementLabel> orig))
|
||||
{
|
||||
if (((int)(orig.Flag & RegexElementLabel.ATOM_BEGIN) != 0) && ((int)_prev != 0))
|
||||
{
|
||||
Token<RegexElementLabel> ret = new Token<RegexElementLabel>(string.Empty,
|
||||
orig.Line,
|
||||
orig.Column,
|
||||
RegexElementLabel.CONCAT);
|
||||
_buffer.Enqueue(orig);
|
||||
_prev = orig.Flag & RegexElementLabel.ATOM_END;
|
||||
return ret;
|
||||
} else
|
||||
{
|
||||
_prev = orig.Flag & RegexElementLabel.ATOM_END;
|
||||
return orig;
|
||||
}
|
||||
} else
|
||||
{
|
||||
return Token<RegexElementLabel>.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool TryReadToken(
|
||||
out Token<RegexElementLabel> t
|
||||
)
|
||||
{
|
||||
if (IsEmpty)
|
||||
{
|
||||
t = Token<RegexElementLabel>.Empty;
|
||||
return false;
|
||||
}
|
||||
t = ReadToken();
|
||||
return t == Token<RegexElementLabel>.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenParser"
|
||||
// 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.Diagnostics.Tracing;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexTokenParser : TokenParser<RegexElementLabel, RegexElementLabel>
|
||||
{
|
||||
|
||||
private readonly Queue<Token<RegexElementLabel>> _buffer = [];
|
||||
|
||||
private RegexElementLabel _prev;
|
||||
|
||||
public override bool IsEmpty => Source.IsEmpty && _buffer.Count == 0;
|
||||
|
||||
public override Token<RegexElementLabel> ReadToken()
|
||||
{
|
||||
if (_buffer.TryDequeue(out Token<RegexElementLabel> token)) {
|
||||
return token;
|
||||
}
|
||||
if (Source.TryReadToken(out Token<RegexElementLabel> orig))
|
||||
{
|
||||
if (((int)(orig.Flag & RegexElementLabel.ATOM_BEGIN) != 0) && ((int)_prev != 0))
|
||||
{
|
||||
Token<RegexElementLabel> ret = new Token<RegexElementLabel>(string.Empty,
|
||||
orig.Line,
|
||||
orig.Column,
|
||||
RegexElementLabel.CONCAT);
|
||||
_buffer.Enqueue(orig);
|
||||
_prev = orig.Flag & RegexElementLabel.ATOM_END;
|
||||
return ret;
|
||||
} else
|
||||
{
|
||||
_prev = orig.Flag & RegexElementLabel.ATOM_END;
|
||||
return orig;
|
||||
}
|
||||
} else
|
||||
{
|
||||
return Token<RegexElementLabel>.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool TryReadToken(
|
||||
out Token<RegexElementLabel> t
|
||||
)
|
||||
{
|
||||
if (IsEmpty)
|
||||
{
|
||||
t = Token<RegexElementLabel>.Empty;
|
||||
return false;
|
||||
}
|
||||
t = ReadToken();
|
||||
return t == Token<RegexElementLabel>.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,306 +1,306 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenReader"
|
||||
// Project: StgSharp
|
||||
// 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:
|
||||
//
|
||||
// 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.Runtime.InteropServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexTokenReader : ITokenReader<RegexElementLabel>
|
||||
{
|
||||
|
||||
private static string _source;
|
||||
|
||||
private int position = 0;
|
||||
|
||||
public RegexTokenReader() { }
|
||||
|
||||
public RegexTokenReader(string source)
|
||||
{
|
||||
_source = source;
|
||||
}
|
||||
|
||||
public bool IsEmpty => string.IsNullOrEmpty(_source) || position >= _source.Length;
|
||||
|
||||
public Token<RegexElementLabel> ReadToken()
|
||||
{
|
||||
int pos = position;
|
||||
switch (_source[pos])
|
||||
{
|
||||
case '\\':
|
||||
if (pos + 1 == _source.Length) {
|
||||
throw new InvalidOperationException("Unclosed char");
|
||||
}
|
||||
if (_source[pos + 1] == 'x')
|
||||
{
|
||||
string value = _source[(pos + 1)..(pos + 3)];
|
||||
pos += 4;
|
||||
if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result))
|
||||
{
|
||||
return new Token<RegexElementLabel>(result.ToString(), 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Failed to convert {value} to a valid ascii code point.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'u')
|
||||
{
|
||||
string value = _source[(pos + 1)..(pos + 5)];
|
||||
pos += 6;
|
||||
if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result))
|
||||
{
|
||||
return new Token<RegexElementLabel>(result.ToString(), 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Failed to convert {value} to a valid unicode code point.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'p')
|
||||
{
|
||||
int i = pos + 2;
|
||||
if (_source[i] != '{') {
|
||||
throw new InvalidOperationException("Invalid Unicode property escape sequence.");
|
||||
}
|
||||
for (; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed Unicode property escape sequence.");
|
||||
} else
|
||||
{
|
||||
position = pos + 2;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 2)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
case '[':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == ']' && _source[i - 1] != '\\')
|
||||
{
|
||||
int length = (i - pos - 1);
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, position,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed character set");
|
||||
case '|':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.ALT);
|
||||
case '(':
|
||||
|
||||
// very comlex here
|
||||
return ReadGroupBeginToken();
|
||||
case ')':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.GROUP_END);
|
||||
case '{':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||
RegexElementLabel.COUNT);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed count specifier");
|
||||
case '*' or '+':
|
||||
int count = _source[pos + 1] == '?' ? 2 : 1;
|
||||
position = pos + count;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + count)], 0, pos, RegexElementLabel.COUNT);
|
||||
case '?':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.COUNT);
|
||||
case '.':
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT);
|
||||
default:
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryReadToken(out Token<RegexElementLabel> t)
|
||||
{
|
||||
if (IsEmpty)
|
||||
{
|
||||
t = Token<RegexElementLabel>.Empty;
|
||||
return false;
|
||||
}
|
||||
t = ReadToken();
|
||||
return Token<RegexElementLabel>.Empty == t;
|
||||
}
|
||||
|
||||
private Token<RegexElementLabel> ReadGroupBeginToken()
|
||||
{
|
||||
if (IsEmpty) {
|
||||
throw new InvalidOperationException("Source is null.");
|
||||
}
|
||||
|
||||
int start = position;
|
||||
|
||||
if (start >= _source.Length || _source[start] != '(') {
|
||||
throw new InvalidOperationException("ReadGroupBeginToken called when current char is not '('.");
|
||||
}
|
||||
|
||||
// Case 1: plain capturing group "("
|
||||
if (start + 1 >= _source.Length || _source[start + 1] != '?')
|
||||
{
|
||||
position = start + 1;
|
||||
return new Token<RegexElementLabel>("(", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// From here we have "(?"
|
||||
if (start + 2 >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?'.");
|
||||
}
|
||||
|
||||
char c2 = _source[start + 2];
|
||||
|
||||
// Case 2: non-capturing group "(?:"
|
||||
if (c2 == ':')
|
||||
{
|
||||
position = start + 3;
|
||||
return new Token<RegexElementLabel>("(?:", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Assertions (write the first-level cases, but not supported yet)
|
||||
if (c2 == '=')
|
||||
{
|
||||
// "(?="
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Positive lookahead '(?=...)' is not supported yet.");
|
||||
}
|
||||
if (c2 == '!')
|
||||
{
|
||||
// "(?!"
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Negative lookahead '(?!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Named capturing group or lookbehind starts with "(?<"
|
||||
if (c2 == '<')
|
||||
{
|
||||
if (start + 3 >= _source.Length) {
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?<'.");
|
||||
}
|
||||
|
||||
char c3 = _source[start + 3];
|
||||
|
||||
// Lookbehind prefixes: "(?<=" and "(?<!"
|
||||
if (c3 == '=')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Positive lookbehind '(?<=...)' is not supported yet.");
|
||||
}
|
||||
if (c3 == '!')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Negative lookbehind '(?<!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Otherwise: "(?<name>"
|
||||
int nameStart = start + 3;
|
||||
|
||||
// Read name until '>'
|
||||
int i = nameStart;
|
||||
|
||||
// Name must start with [A-Za-z_]
|
||||
if (i >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished named group prefix.");
|
||||
}
|
||||
|
||||
char first = _source[i];
|
||||
if (!(first == '_' || char.IsLetter(first))) {
|
||||
throw new InvalidOperationException($"Invalid named group name start '{first}' at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
while (i < _source.Length)
|
||||
{
|
||||
char ch = _source[i];
|
||||
if (ch == '>')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(ch == '_' || char.IsLetterOrDigit(ch))) {
|
||||
throw new InvalidOperationException($"Invalid character '{ch}' in group name at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= _source.Length || _source[i] != '>') {
|
||||
throw new InvalidOperationException("Unclosed named group prefix; missing '>'.");
|
||||
}
|
||||
|
||||
// Consume "(?<name>"
|
||||
int consumedLen = (i - start) + 1; // include '>'
|
||||
string text = _source.Substring(start, consumedLen);
|
||||
position = start + consumedLen;
|
||||
|
||||
return new Token<RegexElementLabel>(text, 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Optional: comments "(?#...)" or inline options "(?i:...)" etc.
|
||||
// First-level cases listed but not supported yet:
|
||||
if (c2 == '#')
|
||||
{
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Comment group '(?#...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Inline options: (?i:...), (?m:...), (?-i:...), (?im), etc.
|
||||
// Too many dialect differences; reserve the prefix but mark unsupported.
|
||||
if (char.IsLetter(c2) || c2 == '-')
|
||||
{
|
||||
// We don't know how long the option spec is here; do NOT advance position too much.
|
||||
// Keep position at '(' so caller can decide how to handle, or just throw.
|
||||
position = start;
|
||||
throw new NotSupportedException("Inline option groups like '(?i:...)' are not supported yet.");
|
||||
}
|
||||
|
||||
// Unknown "(?X" prefix
|
||||
position = start;
|
||||
throw new InvalidOperationException($"Unknown or unsupported group prefix starting at position {start}: '{_source.Substring(start, Math.Min(4, _source.Length - start))}'");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenReader"
|
||||
// Project: StgSharp
|
||||
// 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:
|
||||
//
|
||||
// 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.Runtime.InteropServices;
|
||||
using System.Security.Permissions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.TextRegex
|
||||
{
|
||||
internal class RegexTokenReader : ITokenReader<RegexElementLabel>
|
||||
{
|
||||
|
||||
private static string _source;
|
||||
|
||||
private int position = 0;
|
||||
|
||||
public RegexTokenReader() { }
|
||||
|
||||
public RegexTokenReader(string source)
|
||||
{
|
||||
_source = source;
|
||||
}
|
||||
|
||||
public bool IsEmpty => string.IsNullOrEmpty(_source) || position >= _source.Length;
|
||||
|
||||
public Token<RegexElementLabel> ReadToken()
|
||||
{
|
||||
int pos = position;
|
||||
switch (_source[pos])
|
||||
{
|
||||
case '\\':
|
||||
if (pos + 1 == _source.Length) {
|
||||
throw new InvalidOperationException("Unclosed char");
|
||||
}
|
||||
if (_source[pos + 1] == 'x')
|
||||
{
|
||||
string value = _source[(pos + 1)..(pos + 3)];
|
||||
pos += 4;
|
||||
if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result))
|
||||
{
|
||||
return new Token<RegexElementLabel>(result.ToString(), 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Failed to convert {value} to a valid ascii code point.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'u')
|
||||
{
|
||||
string value = _source[(pos + 1)..(pos + 5)];
|
||||
pos += 6;
|
||||
if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result))
|
||||
{
|
||||
return new Token<RegexElementLabel>(result.ToString(), 0, pos, RegexElementLabel.UNIT);
|
||||
} else
|
||||
{
|
||||
throw new InvalidCastException($"Failed to convert {value} to a valid unicode code point.");
|
||||
}
|
||||
} else if (_source[pos + 1] == 'p')
|
||||
{
|
||||
int i = pos + 2;
|
||||
if (_source[i] != '{') {
|
||||
throw new InvalidOperationException("Invalid Unicode property escape sequence.");
|
||||
}
|
||||
for (; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed Unicode property escape sequence.");
|
||||
} else
|
||||
{
|
||||
position = pos + 2;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 2)], 0, pos,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
case '[':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == ']' && _source[i - 1] != '\\')
|
||||
{
|
||||
int length = (i - pos - 1);
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, position,
|
||||
RegexElementLabel.UNIT_SET);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed character set");
|
||||
case '|':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.ALT);
|
||||
case '(':
|
||||
|
||||
// very comlex here
|
||||
return ReadGroupBeginToken();
|
||||
case ')':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.GROUP_END);
|
||||
case '{':
|
||||
for (int i = pos + 1; i < _source.Length; i++)
|
||||
{
|
||||
if (_source[i] == '}')
|
||||
{
|
||||
position = i + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(i + 1)], 0, pos,
|
||||
RegexElementLabel.COUNT);
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException("Unclosed count specifier");
|
||||
case '*' or '+':
|
||||
int count = _source[pos + 1] == '?' ? 2 : 1;
|
||||
position = pos + count;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + count)], 0, pos, RegexElementLabel.COUNT);
|
||||
case '?':
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.COUNT);
|
||||
case '.':
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT);
|
||||
default:
|
||||
position = pos + 1;
|
||||
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryReadToken(out Token<RegexElementLabel> t)
|
||||
{
|
||||
if (IsEmpty)
|
||||
{
|
||||
t = Token<RegexElementLabel>.Empty;
|
||||
return false;
|
||||
}
|
||||
t = ReadToken();
|
||||
return Token<RegexElementLabel>.Empty == t;
|
||||
}
|
||||
|
||||
private Token<RegexElementLabel> ReadGroupBeginToken()
|
||||
{
|
||||
if (IsEmpty) {
|
||||
throw new InvalidOperationException("Source is null.");
|
||||
}
|
||||
|
||||
int start = position;
|
||||
|
||||
if (start >= _source.Length || _source[start] != '(') {
|
||||
throw new InvalidOperationException("ReadGroupBeginToken called when current char is not '('.");
|
||||
}
|
||||
|
||||
// Case 1: plain capturing group "("
|
||||
if (start + 1 >= _source.Length || _source[start + 1] != '?')
|
||||
{
|
||||
position = start + 1;
|
||||
return new Token<RegexElementLabel>("(", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// From here we have "(?"
|
||||
if (start + 2 >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?'.");
|
||||
}
|
||||
|
||||
char c2 = _source[start + 2];
|
||||
|
||||
// Case 2: non-capturing group "(?:"
|
||||
if (c2 == ':')
|
||||
{
|
||||
position = start + 3;
|
||||
return new Token<RegexElementLabel>("(?:", 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Assertions (write the first-level cases, but not supported yet)
|
||||
if (c2 == '=')
|
||||
{
|
||||
// "(?="
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Positive lookahead '(?=...)' is not supported yet.");
|
||||
}
|
||||
if (c2 == '!')
|
||||
{
|
||||
// "(?!"
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Negative lookahead '(?!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Named capturing group or lookbehind starts with "(?<"
|
||||
if (c2 == '<')
|
||||
{
|
||||
if (start + 3 >= _source.Length) {
|
||||
throw new InvalidOperationException("Unfinished group prefix '(?<'.");
|
||||
}
|
||||
|
||||
char c3 = _source[start + 3];
|
||||
|
||||
// Lookbehind prefixes: "(?<=" and "(?<!"
|
||||
if (c3 == '=')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Positive lookbehind '(?<=...)' is not supported yet.");
|
||||
}
|
||||
if (c3 == '!')
|
||||
{
|
||||
position = start + 4;
|
||||
throw new NotSupportedException("Negative lookbehind '(?<!...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Otherwise: "(?<name>"
|
||||
int nameStart = start + 3;
|
||||
|
||||
// Read name until '>'
|
||||
int i = nameStart;
|
||||
|
||||
// Name must start with [A-Za-z_]
|
||||
if (i >= _source.Length)
|
||||
{
|
||||
throw new InvalidOperationException("Unfinished named group prefix.");
|
||||
}
|
||||
|
||||
char first = _source[i];
|
||||
if (!(first == '_' || char.IsLetter(first))) {
|
||||
throw new InvalidOperationException($"Invalid named group name start '{first}' at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
while (i < _source.Length)
|
||||
{
|
||||
char ch = _source[i];
|
||||
if (ch == '>')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(ch == '_' || char.IsLetterOrDigit(ch))) {
|
||||
throw new InvalidOperationException($"Invalid character '{ch}' in group name at position {i}.");
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= _source.Length || _source[i] != '>') {
|
||||
throw new InvalidOperationException("Unclosed named group prefix; missing '>'.");
|
||||
}
|
||||
|
||||
// Consume "(?<name>"
|
||||
int consumedLen = (i - start) + 1; // include '>'
|
||||
string text = _source.Substring(start, consumedLen);
|
||||
position = start + consumedLen;
|
||||
|
||||
return new Token<RegexElementLabel>(text, 0, start, RegexElementLabel.GROUP_BEGIN);
|
||||
}
|
||||
|
||||
// Optional: comments "(?#...)" or inline options "(?i:...)" etc.
|
||||
// First-level cases listed but not supported yet:
|
||||
if (c2 == '#')
|
||||
{
|
||||
position = start + 3;
|
||||
throw new NotSupportedException("Comment group '(?#...)' is not supported yet.");
|
||||
}
|
||||
|
||||
// Inline options: (?i:...), (?m:...), (?-i:...), (?im), etc.
|
||||
// Too many dialect differences; reserve the prefix but mark unsupported.
|
||||
if (char.IsLetter(c2) || c2 == '-')
|
||||
{
|
||||
// We don't know how long the option spec is here; do NOT advance position too much.
|
||||
// Keep position at '(' so caller can decide how to handle, or just throw.
|
||||
position = start;
|
||||
throw new NotSupportedException("Inline option groups like '(?i:...)' are not supported yet.");
|
||||
}
|
||||
|
||||
// Unknown "(?X" prefix
|
||||
position = start;
|
||||
throw new InvalidOperationException($"Unknown or unsupported group prefix starting at position {start}: '{_source.Substring(start, Math.Min(4, _source.Length - start))}'");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenUtil"
|
||||
// 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
|
||||
{
|
||||
internal static class RegexTokenUtil
|
||||
{
|
||||
|
||||
public static bool IsAccurateChar(
|
||||
this Token<RegexElementLabel> token
|
||||
)
|
||||
{
|
||||
return ((token.Flag & RegexElementLabel.UNIT) != 0 && token.Value.Length == 1) ||
|
||||
(token.Flag & RegexElementLabel.UNIT_SPAN) != 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="RegexTokenUtil"
|
||||
// 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
|
||||
{
|
||||
internal static class RegexTokenUtil
|
||||
{
|
||||
|
||||
public static bool IsAccurateChar(
|
||||
this Token<RegexElementLabel> token
|
||||
)
|
||||
{
|
||||
return ((token.Flag & RegexElementLabel.UNIT) != 0 && token.Value.Length == 1) ||
|
||||
(token.Flag & RegexElementLabel.UNIT_SPAN) != 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user