Skip to content

Commit

Permalink
Xml comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Feb 18, 2025
1 parent 7254e82 commit 204097a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Compilation.FSharp/FSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using global::FSharp.Compiler.Diagnostics;
using global::FSharp.Compiler.IO;

/// <summary>
/// F# compiler.
/// </summary>
public class FSharpCompiler : ICompiler
{
private class FileSystemContext : Disposable
Expand Down Expand Up @@ -81,6 +84,9 @@ protected override void DisposeManaged()

private readonly FSharpChecker _checker;

/// <summary>
/// Initializes a new instance of the <see cref="FSharpCompiler"/> class.
/// </summary>
public FSharpCompiler()
{
_checker = FSharpChecker.Create(
Expand Down
10 changes: 10 additions & 0 deletions Compilation.Python/PythonCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
using IronPython.Hosting;
using IronPython.Compiler;

/// <summary>
/// Python compiler.
/// </summary>
public class PythonCompiler : ICompiler
{
private class CustomErrorListener(IList<CompilationError> errors) : ErrorListener
Expand All @@ -32,11 +35,18 @@ public override void ErrorReported(ScriptSource source, string message, SourceSp

private readonly ScriptEngine _engine;

/// <summary>
/// Initializes a new instance of the <see cref="PythonCompiler"/> class.
/// </summary>
public PythonCompiler()
: this(Python.CreateEngine())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PythonCompiler"/> class.
/// </summary>
/// <param name="engine"><see cref="ScriptEngine"/></param>
public PythonCompiler(ScriptEngine engine)
{
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
Expand Down
81 changes: 81 additions & 0 deletions Compilation.Python/PythonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using IronPython.Runtime;
using IronPython.Runtime.Types;

/// <summary>
/// Provides extension methods for working with IronPython types and functions.
/// </summary>
public static class PythonExtensions
{
private const BindingFlags _nonPublic = BindingFlags.Instance | BindingFlags.NonPublic;
Expand All @@ -24,10 +27,22 @@ public static class PythonExtensions
private static readonly PropertyInfo _setters = typeof(ReflectedGetterSetter).GetProperty("Setter", _nonPublic);
private static readonly FieldInfo _propInfo = typeof(ReflectedProperty).GetField("_info", _nonPublic);

/// <summary>
/// Gets the name of the specified Python type.
/// </summary>
/// <param name="type">The Python type.</param>
/// <returns>The name of the Python type.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> is null.</exception>
[CLSCompliant(false)]
public static string GetName(this PythonType type)
=> (string)_nameProp.GetValue(type ?? throw new ArgumentNullException(nameof(type)));

/// <summary>
/// Gets the underlying system type for the specified Python type.
/// </summary>
/// <param name="type">The Python type.</param>
/// <returns>The underlying .NET type.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> is null.</exception>
[CLSCompliant(false)]
public static Type GetUnderlyingSystemType(this PythonType type)
{
Expand All @@ -42,10 +57,23 @@ public static Type GetUnderlyingSystemType(this PythonType type)
return retVal;
}

/// <summary>
/// Determines whether the specified Python type is assignable to the base type <typeparamref name="TBase"/>.
/// </summary>
/// <typeparam name="TBase">The base type to check against.</typeparam>
/// <param name="type">The Python type.</param>
/// <returns><c>true</c> if the Python type is assignable to <typeparamref name="TBase"/>; otherwise, <c>false</c>.</returns>
[CLSCompliant(false)]
public static bool Is<TBase>(this PythonType type)
=> type.Is(typeof(TBase));

/// <summary>
/// Determines whether the specified Python type is assignable to the provided base type.
/// </summary>
/// <param name="type">The Python type.</param>
/// <param name="baseType">The base type to check against.</param>
/// <returns><c>true</c> if the Python type is assignable to <paramref name="baseType"/>; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> or <paramref name="baseType"/> is null.</exception>
[CLSCompliant(false)]
public static bool Is(this PythonType type, Type baseType)
{
Expand All @@ -57,6 +85,12 @@ public static bool Is(this PythonType type, Type baseType)
return underlying?.Is(baseType, false) == true;
}

/// <summary>
/// Retrieves all Python types defined in the specified script scope.
/// </summary>
/// <param name="scope">The script scope.</param>
/// <returns>An enumerable of Python types contained in the scope.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="scope"/> is null.</exception>
[CLSCompliant(false)]
public static IEnumerable<PythonType> GetTypes(this ScriptScope scope)
{
Expand All @@ -66,6 +100,11 @@ public static IEnumerable<PythonType> GetTypes(this ScriptScope scope)
return scope.GetVariableNames().Select(scope.GetVariable).OfType<PythonType>();
}

/// <summary>
/// Converts a <see cref="Severity"/> value to its corresponding <see cref="CompilationErrorTypes"/>.
/// </summary>
/// <param name="severity">The severity of the error or warning.</param>
/// <returns>The corresponding <see cref="CompilationErrorTypes"/> value.</returns>
public static CompilationErrorTypes ToErrorType(this Severity severity)
=> severity switch
{
Expand All @@ -74,6 +113,12 @@ public static CompilationErrorTypes ToErrorType(this Severity severity)
_ => CompilationErrorTypes.Info,
};

/// <summary>
/// Determines whether the specified object is an IronPython object.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><c>true</c> if the object implements <see cref="IPythonObject"/>; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="obj"/> is null.</exception>
public static bool IsPythonObject(this object obj)
{
if (obj is null)
Expand All @@ -82,6 +127,12 @@ public static bool IsPythonObject(this object obj)
return obj is IPythonObject;
}

/// <summary>
/// Determines whether the specified type is an IronPython type.
/// </summary>
/// <param name="type">The type to test.</param>
/// <returns><c>true</c> if the type is an IronPython type; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> is null.</exception>
public static bool IsPythonType(this Type type)
{
if (type is null)
Expand All @@ -90,14 +141,30 @@ public static bool IsPythonType(this Type type)
return type.FullName?.StartsWith(nameof(IronPython)) ?? false;
}

/// <summary>
/// Gets the setter methods of the reflected property.
/// </summary>
/// <param name="property">The reflected property.</param>
/// <returns>An array of setter <see cref="MethodInfo"/> objects.</returns>
[CLSCompliant(false)]
public static MethodInfo[] GetSetters(this ReflectedProperty property)
=> (MethodInfo[])_setters.GetValue(property);

/// <summary>
/// Gets the underlying property information for the reflected property.
/// </summary>
/// <param name="property">The reflected property.</param>
/// <returns>The underlying <see cref="PropertyInfo"/> object.</returns>
[CLSCompliant(false)]
public static PropertyInfo GetPropInfo(this ReflectedProperty property)
=> (PropertyInfo)_propInfo.GetValue(property);

/// <summary>
/// Gets the .NET type corresponding to the specified Python type.
/// </summary>
/// <param name="type">The Python type.</param>
/// <returns>The underlying .NET type; or <see cref="object"/> if not found.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> is null.</exception>
[CLSCompliant(false)]
public static Type GetDotNetType(this PythonType type)
{
Expand All @@ -112,6 +179,14 @@ public static Type GetDotNetType(this PythonType type)
return baseType ?? typeof(object);
}

/// <summary>
/// Retrieves the parameters of the specified Python function along with their corresponding types.
/// </summary>
/// <param name="function">The Python function.</param>
/// <returns>
/// An enumerable of tuples where each tuple contains the name of the parameter and its corresponding type.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="function"/> is null.</exception>
[CLSCompliant(false)]
public static IEnumerable<(string name, Type type)> GetParams(this PythonFunction function)
{
Expand Down Expand Up @@ -146,6 +221,12 @@ public static Type GetDotNetType(this PythonType type)
}
}

/// <summary>
/// Determines whether the specified Python function is static.
/// </summary>
/// <param name="function">The Python function.</param>
/// <returns><c>true</c> if the function is static; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="function"/> is null.</exception>
[CLSCompliant(false)]
public static bool IsStatic(this PythonFunction function)
{
Expand Down
8 changes: 8 additions & 0 deletions Compilation.Roslyn/BannedApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp.BannedApiAnalyzers;

/// <summary>
/// Banned API extensions.
/// </summary>
public static class BannedApiExtensions
{
private class BannedSymbolsAdditionalText(string content) : AdditionalText
Expand All @@ -19,6 +22,11 @@ public override SourceText GetText(CancellationToken cancellationToken)
=> SourceText.From(_content);
}

/// <summary>
/// Converts banned symbols to a banned symbols analyzer.
/// </summary>
/// <param name="bannedSymbols">Banned symbols.</param>
/// <returns>Banned symbols analyzer.</returns>
public static (DiagnosticAnalyzer analyzer, AdditionalText bannedSymbolsTxt) ToBannedSymbolsAnalyzer(this string bannedSymbols)
=> (new CSharpSymbolIsBannedAnalyzer(), new BannedSymbolsAdditionalText(bannedSymbols));
}
36 changes: 35 additions & 1 deletion Compilation.Roslyn/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -18,6 +17,10 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.VisualBasic;

/// <summary>
/// Represents a Roslyn compiler.
/// </summary>
/// <param name="extension">The file extension of the source files that the compiler supports.</param>
public abstract class RoslynCompiler(string extension) : ICompiler
{
private static readonly Dictionary<string, string> _redirects = new()
Expand Down Expand Up @@ -45,8 +48,11 @@ static RoslynCompiler()
bool ICompiler.IsAssemblyPersistable { get; } = true;
string ICompiler.Extension { get; } = extension;

/// <inheritdoc />
public abstract bool IsTabsSupported { get; }
/// <inheritdoc />
public abstract bool IsCaseSensitive { get; }
/// <inheritdoc />
public abstract bool IsReferencesSupported { get; }

private Compilation Create(string name, IEnumerable<string> sources, IEnumerable<(string name, byte[] body)> refs, CancellationToken cancellationToken)
Expand All @@ -64,6 +70,14 @@ private Compilation Create(string name, IEnumerable<string> sources, IEnumerable
return Create(assemblyName, sources, references, cancellationToken);
}

/// <summary>
/// Creates a new compilation instance.
/// </summary>
/// <param name="assemblyName">The name of the assembly.</param>
/// <param name="sources">The source code files as strings.</param>
/// <param name="references">A collection of references.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns><see cref="Compilation"/></returns>
protected abstract Compilation Create(
string assemblyName,
IEnumerable<string> sources,
Expand Down Expand Up @@ -151,17 +165,27 @@ byte[] getBody()
ICompilerContext ICompiler.CreateContext() => new AssemblyLoadContextTracker();
}

/// <summary>
/// Represents a C# compiler that supports compiling source files into an assembly.
/// </summary>
public class CSharpCompiler : RoslynCompiler
{
/// <summary>
/// Initializes a new instance of the <see cref="CSharpCompiler"/> class.
/// </summary>
public CSharpCompiler()
: base(FileExts.CSharp)
{
}

/// <inheritdoc />
public override bool IsTabsSupported => true;
/// <inheritdoc />
public override bool IsCaseSensitive => true;
/// <inheritdoc />
public override bool IsReferencesSupported => true;

/// <inheritdoc />
protected override Compilation Create(string assemblyName, IEnumerable<string> sources, PortableExecutableReference[] references, CancellationToken cancellationToken)
=> CSharpCompilation.Create(
assemblyName,
Expand All @@ -171,17 +195,27 @@ protected override Compilation Create(string assemblyName, IEnumerable<string> s
);
}

/// <summary>
/// Represents a Visual Basic compiler that supports compiling source files into an assembly.
/// </summary>
public class VisualBasicCompiler : RoslynCompiler
{
/// <summary>
/// Initializes a new instance of the <see cref="VisualBasicCompiler"/> class.
/// </summary>
public VisualBasicCompiler()
: base(FileExts.VisualBasic)
{
}

/// <inheritdoc />
public override bool IsTabsSupported => true;
/// <inheritdoc />
public override bool IsCaseSensitive => false;
/// <inheritdoc />
public override bool IsReferencesSupported => true;

/// <inheritdoc />
protected override Compilation Create(string assemblyName, IEnumerable<string> sources, PortableExecutableReference[] references, CancellationToken cancellationToken)
=> VisualBasicCompilation.Create(
assemblyName,
Expand Down
12 changes: 12 additions & 0 deletions common.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<IsSourcesAllow>false</IsSourcesAllow>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="
Expand All @@ -39,6 +40,17 @@
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="
'$(MSBuildProjectName)' == 'Common' or
'$(MSBuildProjectName)' == 'Collections' or
'$(MSBuildProjectName)' == 'Net.HPSocket' or
'$(MSBuildProjectName)' == 'SmartFormat' or
'$(MSBuildProjectName)' == 'StringSearch' or
'$(MSBuildProjectName)' == 'Tests'
">
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(StockSharpTargets)' != ''">
<TargetFrameworks>$(StockSharpTargets)</TargetFrameworks>
</PropertyGroup>
Expand Down

0 comments on commit 204097a

Please sign in to comment.