261 lines
7.1 KiB
C#
261 lines
7.1 KiB
C#
//-----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
// file="RegexIR"
|
|
// Project: StgSharp
|
|
// AuthorGroup: Nitload
|
|
// Copyright (c) Nitload. All rights reserved.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
//
|
|
// -----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StgSharp.RegularAnalysis.Text
|
|
{
|
|
internal enum RegexIRCommand
|
|
{
|
|
|
|
Match_Seq = 0,
|
|
Match_Set = 1,
|
|
Condition = 3,
|
|
Try = 4,
|
|
Count = 5,
|
|
Count_Seq = 6,
|
|
Count_Set = 7,
|
|
Pop = 8,
|
|
Pack = 9,
|
|
|
|
}
|
|
|
|
internal abstract class RegexHIR
|
|
{
|
|
|
|
internal abstract RegexIRCommand Command { get; }
|
|
|
|
internal abstract string AsCommand(
|
|
int lineNum
|
|
);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Searches for a certain string pattern or charset from current position.
|
|
/// </summary>
|
|
internal sealed class RegexMatchSeqHIR(
|
|
string pattern
|
|
) : RegexHIR
|
|
{
|
|
|
|
public string Pattern { get; } = pattern;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Match_Seq;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t MATCH_SEQ \"{(string.IsNullOrEmpty(Pattern) ? "<EPSILON>" : Pattern)}\"";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexMatchSetHIR(
|
|
string charset
|
|
) : RegexHIR
|
|
{
|
|
|
|
public string Charset { get; } = charset;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Match_Set;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t MATCH_SET \"{Charset}\"";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexConditionHIR(
|
|
int success,
|
|
int fail
|
|
) : RegexHIR
|
|
{
|
|
|
|
public int SuccessCase { get; } = success;
|
|
|
|
public int FailCase { get; } = fail;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Condition;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t CONDITION SUCCESS: {SuccessCase} FAIL: {FailCase}";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexPackHIR(
|
|
bool condition,
|
|
string name = ""
|
|
) : RegexHIR
|
|
{
|
|
|
|
public bool Condition { get; } = condition;
|
|
|
|
public string GroupName { get; } = name;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Pack;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t PACK {(Condition ? "SUCCESS" : "FAIL")} GROUP: \"{GroupName}\"";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexCountHIR(
|
|
int min,
|
|
int max,
|
|
bool isGreedy,
|
|
int operation
|
|
) : RegexHIR
|
|
{
|
|
|
|
public bool IsGreedy { get; } = isGreedy;
|
|
|
|
public int Min { get; } = min;
|
|
|
|
public int Max { get; } = max;
|
|
|
|
public int OperationIndex { get; } = operation;
|
|
|
|
internal override RegexIRCommand Command { get; } = RegexIRCommand.Count;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t COUNT {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} OPERATION: {OperationIndex}";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexCountSeqHIR(
|
|
int min,
|
|
int max,
|
|
bool isGreedy,
|
|
string seq
|
|
) : RegexHIR
|
|
{
|
|
|
|
public bool IsGreedy { get; } = isGreedy;
|
|
|
|
public int Min { get; } = min;
|
|
|
|
public int Max { get; } = max;
|
|
|
|
public string Sequence { get; } = seq;
|
|
|
|
internal override RegexIRCommand Command { get; } = RegexIRCommand.Count_Seq;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t COUNT_SEQ {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SEQ: \"{Sequence}\"";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexCountSetHIR(
|
|
int min,
|
|
int max,
|
|
bool isGreedy,
|
|
string set
|
|
) : RegexHIR
|
|
{
|
|
|
|
public bool IsGreedy { get; } = isGreedy;
|
|
|
|
public int Min { get; } = min;
|
|
|
|
public int Max { get; } = max;
|
|
|
|
public string CharSet { get; } = set;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Count_Set;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t COUNT_SET {(IsGreedy ? "GREEDY" : "NON_GREEDY")} MIN: {Min} MAX: {Max} SET: \"{CharSet}\"";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexTryHIR(
|
|
int count
|
|
) : RegexHIR
|
|
{
|
|
|
|
public int CountOfCommand { get; } = count;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Try;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t TRY {CountOfCommand}";
|
|
}
|
|
|
|
}
|
|
|
|
internal sealed class RegexPopHIR(
|
|
bool isSuccess
|
|
) : RegexHIR
|
|
{
|
|
|
|
public bool IsSuccess { get; } = isSuccess;
|
|
|
|
internal override RegexIRCommand Command => RegexIRCommand.Pop;
|
|
|
|
internal override string AsCommand(
|
|
int lineNum
|
|
)
|
|
{
|
|
return $"[{lineNum}]\t POP {(IsSuccess ? "SUCCESS" : "FAIL")}";
|
|
}
|
|
|
|
}
|
|
}
|