// ----------------------------------------------------------------------------- // 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 ast, List base_ir, List 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 Ast { get; } internal List BaseIR { get; } internal List IRs { get; } } public static partial class RegexAnalyzer { public static TextRegexSource Analyze( string regex ) { #pragma warning disable CA1031 // RegexAnalyzer interpreter = new(); // interpreter._source = regex; AbstractSyntaxTree tree; try { tree = GenerateAST(regex); OptimizeTree(tree); } catch (Exception ex) { return new TextRegexSource(regex, null, null, null, ex); } List base_ir; try { base_ir = GenerateIR(tree); } catch (Exception ex) { return new TextRegexSource(regex, tree, null, null, ex); } try { ScanAndOptimizeIR(base_ir, out List opt_ir); List 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 } } }