add a scanner, but this seems to be a wrong solution. Change open-source head
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="RegexAnalyzer"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using StgSharp.RegularAnalysis.Abstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.Text
|
||||
{
|
||||
public record TextRegexSource
|
||||
{
|
||||
|
||||
internal TextRegexSource(
|
||||
string pattern,
|
||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> ast,
|
||||
List<RegexIR> base_ir,
|
||||
List<RegexIR> ir,
|
||||
Exception? analyseException = null
|
||||
)
|
||||
{
|
||||
Pattern = pattern;
|
||||
Ast = ast;
|
||||
IRs = ir;
|
||||
BaseIR = base_ir;
|
||||
AnalyseException = analyseException;
|
||||
}
|
||||
|
||||
public Exception? AnalyseException { get; set; }
|
||||
|
||||
public string Pattern { get; }
|
||||
|
||||
internal AbstractSyntaxTree<RegexAstNode, RegexElementLabel> Ast { get; }
|
||||
|
||||
internal List<RegexIR> BaseIR { get; }
|
||||
|
||||
internal List<RegexIR> IRs { get; }
|
||||
|
||||
}
|
||||
|
||||
public static partial class RegexAnalyzer
|
||||
{
|
||||
|
||||
public static TextRegexSource Analyze(
|
||||
string regex
|
||||
)
|
||||
{
|
||||
#pragma warning disable CA1031
|
||||
// RegexAnalyzer interpreter = new();
|
||||
// interpreter._source = regex;
|
||||
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree;
|
||||
try
|
||||
{
|
||||
tree = GenerateAST(regex);
|
||||
OptimizeTree(tree);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new TextRegexSource(regex, null, null, null, ex);
|
||||
}
|
||||
List<RegexIR> base_ir;
|
||||
try
|
||||
{
|
||||
base_ir = GenerateIR(tree);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new TextRegexSource(regex, tree, null, null, ex);
|
||||
}
|
||||
try
|
||||
{
|
||||
ScanAndOptimizeIR(base_ir, out List<RegexIR> opt_ir);
|
||||
List<RegexIR> prefix_ir = EmitPrefix(opt_ir);
|
||||
return new TextRegexSource(regex, tree, base_ir, prefix_ir);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new TextRegexSource(regex, tree, base_ir, null, ex);
|
||||
}
|
||||
#pragma warning restore CA1031
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user