117 lines
3.4 KiB
C#
117 lines
3.4 KiB
C#
// -----------------------------------------------------------------------------
|
|
// file="TextRegexSourceGen.Source"
|
|
// Project: StgSharp
|
|
// Copyright (c) Nitload.
|
|
// SPDX-License-Identifier: MIT
|
|
// -----------------------------------------------------------------------------
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StgSharp.RegularAnalysis.Text
|
|
{
|
|
internal partial class TextRegexSourceGen
|
|
{
|
|
|
|
private record struct SourceRecord(
|
|
|
|
bool IsClassStatic,
|
|
bool IsMethodStatic,
|
|
string Pattern,
|
|
string MethodName,
|
|
string NamespaceName,
|
|
Accessibility MethodAccessibility,
|
|
TypeSource ContainingType
|
|
);
|
|
|
|
private sealed record TypeSource(
|
|
bool IsStatic,
|
|
string TypeName,
|
|
TypeKind DeclType,
|
|
Accessibility Accessibility,
|
|
TypeSource? InnerType
|
|
);
|
|
|
|
private struct Source
|
|
{
|
|
|
|
public Accessibility MethodAccessibility { get; set; }
|
|
|
|
public bool IsClassStatic { get; set; }
|
|
|
|
public bool IsMethodStatic { get; set; }
|
|
|
|
public string NameSpaceName { get; set; }
|
|
|
|
public string Pattern { get; set; }
|
|
|
|
public string MethodName { get; set; }
|
|
|
|
public TypeSource ContainingType { get; set; }
|
|
|
|
public readonly SourceRecord AsRecord()
|
|
{
|
|
return new SourceRecord(IsClassStatic, IsMethodStatic, Pattern, MethodName,
|
|
NameSpaceName, MethodAccessibility, ContainingType);
|
|
}
|
|
|
|
}
|
|
|
|
private class TextRegexBinding
|
|
{
|
|
|
|
private readonly List<Diagnostic> _diagnostics = [];
|
|
|
|
public bool IsValid { get; private set; } = true;
|
|
|
|
public IReadOnlyList<Diagnostic> RegexDiagnostic => _diagnostics;
|
|
|
|
public SourceRecord Source { get; set; }
|
|
|
|
public void AddDiagnostic(
|
|
Diagnostic diag
|
|
)
|
|
{
|
|
_diagnostics.Add(diag);
|
|
IsValid = IsValid && (diag.DefaultSeverity != DiagnosticSeverity.Error);
|
|
}
|
|
|
|
public void AddDiagnostic(
|
|
DiagnosticDescriptor descriptor,
|
|
Location? location,
|
|
params object[] messageArgs
|
|
)
|
|
{
|
|
Diagnostic diag = Diagnostic.Create(descriptor, location, messageArgs);
|
|
_diagnostics.Add(diag);
|
|
IsValid = IsValid && (diag.DefaultSeverity != DiagnosticSeverity.Error);
|
|
}
|
|
|
|
public TextRegexBinding Clone()
|
|
{
|
|
TextRegexBinding binding = new()
|
|
{
|
|
IsValid = this.IsValid,
|
|
};
|
|
binding._diagnostics.AddRange(this._diagnostics);
|
|
return binding;
|
|
}
|
|
|
|
public static TextRegexBinding Invalid()
|
|
{
|
|
return new TextRegexBinding
|
|
{
|
|
IsValid = false
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|