Skip to content

Commit

Permalink
Add utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
winscripter authored Jul 25, 2024
1 parent 87db902 commit 63198eb
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 311 deletions.
42 changes: 21 additions & 21 deletions ILSourceParser/Utilities/AsByteExtension.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using ILSourceParser.Syntax;
using System.Globalization;

namespace ILSourceParser.Utilities;

/// <summary>
/// Represents a single extension method that provides the <see cref="AsByte(ByteSyntax)"/> method
/// to <see cref="ByteSyntax"/>.
/// </summary>
public static class AsByteExtension
{
private const NumberStyles s_preferredAsByteNumberStyle = NumberStyles.HexNumber;

/// <summary>
/// Converts the string value of <see cref="ByteSyntax"/> as <see cref="byte"/>.
/// </summary>
/// <param name="syntax">The input <see cref="ByteSyntax"/>.</param>
/// <returns><see cref="ByteSyntax"/> value converted to <see cref="byte"/>.</returns>
public static byte AsByte(this ByteSyntax syntax)
=> byte.Parse(syntax.Value, s_preferredAsByteNumberStyle);
}
using ILSourceParser.Syntax;
using System.Globalization;

namespace ILSourceParser.Utilities;

/// <summary>
/// Represents a single extension method that provides the <see cref="AsByte(ByteSyntax)"/> method
/// to <see cref="ByteSyntax"/>.
/// </summary>
public static class AsByteExtension
{
private const NumberStyles s_preferredAsByteNumberStyle = NumberStyles.HexNumber;

/// <summary>
/// Converts the string value of <see cref="ByteSyntax"/> as <see cref="byte"/>.
/// </summary>
/// <param name="syntax">The input <see cref="ByteSyntax"/>.</param>
/// <returns><see cref="ByteSyntax"/> value converted to <see cref="byte"/>.</returns>
public static byte AsByte(this ByteSyntax syntax)
=> byte.Parse(syntax.Value, s_preferredAsByteNumberStyle);
}
46 changes: 23 additions & 23 deletions ILSourceParser/Utilities/AsParameterMarshalExtension.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using ILSourceParser.Syntax.Marshaling;

namespace ILSourceParser.Utilities;

/// <summary>
/// Converts <see cref="MarshalTypeSyntax"/> to <see cref="ParameterMarshalSyntax"/>.
/// </summary>
public static class AsParameterMarshalExtension
{
/// <summary>
/// Converts <see cref="MarshalTypeSyntax"/> to <see cref="ParameterMarshalSyntax"/>.
/// </summary>
/// <param name="marshalType">The input type of marshal to process.</param>
/// <returns>An output marshal.</returns>
public static ParameterMarshalSyntax AsParameterMarshal(
this MarshalTypeSyntax marshalType)
{
return new ParameterMarshalSyntax(
leadingTrivia: marshalType.LeadingTrivia,
trailingTrivia: marshalType.TrailingTrivia,
marshalType: marshalType);
}
}
using ILSourceParser.Syntax.Marshaling;

namespace ILSourceParser.Utilities;

/// <summary>
/// Converts <see cref="MarshalTypeSyntax"/> to <see cref="ParameterMarshalSyntax"/>.
/// </summary>
public static class AsParameterMarshalExtension
{
/// <summary>
/// Converts <see cref="MarshalTypeSyntax"/> to <see cref="ParameterMarshalSyntax"/>.
/// </summary>
/// <param name="marshalType">The input type of marshal to process.</param>
/// <returns>An output marshal.</returns>
public static ParameterMarshalSyntax AsParameterMarshal(
this MarshalTypeSyntax marshalType)
{
return new ParameterMarshalSyntax(
leadingTrivia: marshalType.LeadingTrivia,
trailingTrivia: marshalType.TrailingTrivia,
marshalType: marshalType);
}
}
17 changes: 17 additions & 0 deletions ILSourceParser/Utilities/AsVersionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ILSourceParser.Syntax;

namespace ILSourceParser.Utilities;

/// <summary>
/// Represents a single extension that provides the method <see cref="AsVersion(VerDirectiveSyntax)"/> to <see cref="VerDirectiveSyntax"/>.
/// </summary>
public static class AsVersionExtension
{
/// <summary>
/// Converts values of the <c>.ver</c> directive into <see cref="Version"/>.
/// </summary>
/// <param name="directive">The information about the <c>.ver</c> IL directive.</param>
/// <returns>A new instance of <see cref="Version"/>.</returns>
public static Version AsVersion(this VerDirectiveSyntax directive) =>
new(directive.Major - '0', directive.Minor - '0', directive.Build - '0', directive.Revision - '0');
}
10 changes: 5 additions & 5 deletions ILSourceParser/Utilities/CommonNodeUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace ILSourceParser.Utilities;

public static partial class CommonNodeUtilities
{
}
namespace ILSourceParser.Utilities;

public static partial class CommonNodeUtilities
{
}
236 changes: 118 additions & 118 deletions ILSourceParser/Utilities/GetAssemblyNameExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,118 +1,118 @@
using ILSourceParser.Syntax;

namespace ILSourceParser.Utilities;

/// <summary>
/// A bunch of extension methods that retrieve names of assemblies from
/// given syntax nodes.
/// </summary>
public static class GetAssemblyNameExtensions
{
/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="attribute">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this AnonymousCustomAttributeSyntax attribute) =>
attribute.AttributeConstructorTarget.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="customAttribute">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this CustomAttributeSyntax customAttribute) =>
customAttribute.AttributeConstructorTarget.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="reference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this NonGenericTypeReferenceSyntax reference) =>
reference.AssemblyReference?.AssemblyName;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="reference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this GenericTypeReferenceSyntax reference) =>
reference.AssemblyReference?.AssemblyName;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="typeReference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this TypeReferenceSyntax typeReference) =>
typeReference is NonGenericTypeReferenceSyntax nonGeneric
? nonGeneric.AssemblyReference?.AssemblyName
: typeReference is GenericTypeReferenceSyntax generic
? generic.AssemblyReference?.AssemblyName
: null;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="type">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this TypeSyntax type) =>
type is TypeReferenceSyntax typeReference ? typeReference.GetAssemblyName() : null;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="invocation">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this MethodInvocationSyntax invocation) =>
invocation.TypeReference.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="call">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this MethodCallSyntax call) =>
call.MethodInvocation.GetAssemblyName();
}
using ILSourceParser.Syntax;

namespace ILSourceParser.Utilities;

/// <summary>
/// A bunch of extension methods that retrieve names of assemblies from
/// given syntax nodes.
/// </summary>
public static class GetAssemblyNameExtensions
{
/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="attribute">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this AnonymousCustomAttributeSyntax attribute) =>
attribute.AttributeConstructorTarget.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="customAttribute">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this CustomAttributeSyntax customAttribute) =>
customAttribute.AttributeConstructorTarget.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="reference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this NonGenericTypeReferenceSyntax reference) =>
reference.AssemblyReference?.AssemblyName;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="reference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this GenericTypeReferenceSyntax reference) =>
reference.AssemblyReference?.AssemblyName;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="typeReference">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this TypeReferenceSyntax typeReference) =>
typeReference is NonGenericTypeReferenceSyntax nonGeneric
? nonGeneric.AssemblyReference?.AssemblyName
: typeReference is GenericTypeReferenceSyntax generic
? generic.AssemblyReference?.AssemblyName
: null;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="type">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this TypeSyntax type) =>
type is TypeReferenceSyntax typeReference ? typeReference.GetAssemblyName() : null;

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="invocation">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this MethodInvocationSyntax invocation) =>
invocation.TypeReference.GetAssemblyName();

/// <summary>
/// Returns the explicit name of the assembly from this syntax node. For example,
/// if the type reference is prefixed with <c>[System.Private.CoreLib]</c>, this
/// method will return <c>System.Private.CoreLib</c> as a string. If assembly reference
/// is omitted, this method returns <see langword="NULL"/>.
/// </summary>
/// <param name="call">The syntax node to get assembly name from.</param>
/// <returns>
/// If assembly name is explicitly emitted, returns the name of the assembly. Otherwise, returns <see langword="NULL"/>
/// </returns>
public static string? GetAssemblyName(this MethodCallSyntax call) =>
call.MethodInvocation.GetAssemblyName();
}
38 changes: 19 additions & 19 deletions ILSourceParser/Utilities/GetByteExtension.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using ILSourceParser.Syntax;

namespace ILSourceParser.Utilities;

/// <summary>
/// Represents a single extension method <see cref="GetBoolean(BooleanLiteralSyntax)"/>
/// for <see cref="BooleanLiteralSyntax"/>.
/// </summary>
public static class GetByteExtension
{
/// <summary>
/// Returns the boolean for <see cref="BooleanLiteralSyntax"/> based on the value
/// of the literal, whether it is <c>true</c> or <c>false</c>.
/// </summary>
/// <param name="literal">The input boolean literal.</param>
/// <returns>A boolean from the boolean literal.</returns>
public static bool GetBoolean(this BooleanLiteralSyntax literal) =>
literal.Value.Equals(BooleanLiteralSyntax.TrueEqualityString);
}
using ILSourceParser.Syntax;

namespace ILSourceParser.Utilities;

/// <summary>
/// Represents a single extension method <see cref="GetBoolean(BooleanLiteralSyntax)"/>
/// for <see cref="BooleanLiteralSyntax"/>.
/// </summary>
public static class GetByteExtension
{
/// <summary>
/// Returns the boolean for <see cref="BooleanLiteralSyntax"/> based on the value
/// of the literal, whether it is <c>true</c> or <c>false</c>.
/// </summary>
/// <param name="literal">The input boolean literal.</param>
/// <returns>A boolean from the boolean literal.</returns>
public static bool GetBoolean(this BooleanLiteralSyntax literal) =>
literal.Value.Equals(BooleanLiteralSyntax.TrueEqualityString);
}
Loading

0 comments on commit 63198eb

Please sign in to comment.