Exemple #1
0
 public static MemberDeclarationSyntax IFormattableToStringSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier("ToString"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("format"))
     .WithType(PredefinedType(Token(SyntaxKind.StringKeyword))),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("formatProvider"))
     .WithType(IdentifierName("IFormatProvider"))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     ThisExpression(),
                     IdentifierName(descriptor.InnerName)),
                 IdentifierName("ToString")))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(IdentifierName("format")),
     Token(SyntaxKind.CommaToken),
     Argument(IdentifierName("formatProvider"))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #2
0
 static MemberDeclarationSyntax IConvertibleConvertToSyntax(this PrimitiveDescriptor descriptor, TypeSyntax returnType, string name)
 => MethodDeclaration(returnType, Identifier(name))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(
         SingletonSeparatedList(
             Parameter(Identifier("provider"))
             .WithType(descriptor.TryWrapInNullableTypeSyntax(IdentifierName("IFormatProvider"))))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("Convert"),
                 IdentifierName(name)))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken),
     Argument(IdentifierName("provider"))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #3
0
 public static MemberDeclarationSyntax StringIComparableTCompareToSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 PredefinedType(Token(SyntaxKind.StringKeyword)),
                 IdentifierName("Compare")))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken),
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("other"),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken),
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("StringComparison"),
             IdentifierName(stringComparison.ToString())))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #4
0
 public static MemberDeclarationSyntax ImplicitCastFromPrimitiveSyntax(this PrimitiveDescriptor descriptor)
 => ConversionOperatorDeclaration(Token(SyntaxKind.ImplicitKeyword), IdentifierName(descriptor.Name))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(descriptor.InnerName).WithType(descriptor.InnerType))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(IdentifierName("From" + descriptor.InnerTypeName))
         .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName(descriptor.InnerName)))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #5
0
 public static MemberDeclarationSyntax StringExplicitCastToPrimitiveSyntax(this PrimitiveDescriptor descriptor)
 => ConversionOperatorDeclaration(Token(SyntaxKind.ExplicitKeyword), descriptor.InnerType)
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("value")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("value"),
             IdentifierName(descriptor.InnerName))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #6
0
 public static MemberDeclarationSyntax ToStringSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier("ToString"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName(descriptor.InnerName),
                 IdentifierName("ToString")))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #7
0
 public static MemberDeclarationSyntax ConstructorSyntax(this PrimitiveDescriptor descriptor)
 => ConstructorDeclaration(descriptor.Name)
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(descriptor.InnerName).WithType(descriptor.InnerType))))
 .WithExpressionBody(
     ArrowExpressionClause(
         AssignmentExpression(
             SyntaxKind.SimpleAssignmentExpression,
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 ThisExpression(),
                 IdentifierName(descriptor.InnerName)),
             IdentifierName(descriptor.InnerName))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #8
0
 public static MemberDeclarationSyntax IEquatableTEqualsSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("Equals"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         BinaryExpression(
             SyntaxKind.EqualsExpression,
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 ThisExpression(),
                 IdentifierName(descriptor.InnerName)),
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("other"),
                 IdentifierName(descriptor.InnerName)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #9
0
 public static MemberDeclarationSyntax IConvertibleGetTypeCodeSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(IdentifierName("TypeCode"), Identifier("GetTypeCode"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("Convert"),
                 IdentifierName("GetTypeCode")))
         .WithArgumentList(
             ArgumentList(
                 SingletonSeparatedList(
                     Argument(
                         MemberAccessExpression(
                             SyntaxKind.SimpleMemberAccessExpression,
                             ThisExpression(),
                             IdentifierName(descriptor.InnerName))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #10
0
 public static MemberDeclarationSyntax EqualsSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("Equals"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithParameterList(
     ParameterList(
         SingletonSeparatedList(
             Parameter(Identifier("obj"))
             .WithType(descriptor.TryWrapInNullableTypeSyntax(PredefinedType(Token(SyntaxKind.ObjectKeyword)))))))
 .WithExpressionBody(
     ArrowExpressionClause(
         BinaryExpression(
             SyntaxKind.LogicalAndExpression,
             IsPatternExpression(
                 IdentifierName("obj"),
                 DeclarationPattern(
                     IdentifierName(descriptor.Name),
                     SingleVariableDesignation(Identifier("value")))),
             InvocationExpression(IdentifierName("Equals"))
             .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("value"))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #11
0
 public static MemberDeclarationSyntax OperatorLessThanOrEqualSyntax(this PrimitiveDescriptor descriptor)
 => OperatorDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Token(SyntaxKind.LessThanEqualsToken))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("value1")).WithType(IdentifierName(descriptor.Name)),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("value2")).WithType(IdentifierName(descriptor.Name))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         BinaryExpression(
             SyntaxKind.LessThanOrEqualExpression,
             InvocationExpression(
                 MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("value1"), IdentifierName("CompareTo")))
             .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("value2"))))),
             LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #12
0
 public static MemberDeclarationSyntax OperatorNotEqualsSyntax(this PrimitiveDescriptor descriptor)
 => OperatorDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Token(SyntaxKind.ExclamationEqualsToken))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("value1")).WithType(IdentifierName(descriptor.Name)),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("value2")).WithType(IdentifierName(descriptor.Name))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         PrefixUnaryExpression(
             SyntaxKind.LogicalNotExpression,
             ParenthesizedExpression(
                 BinaryExpression(
                     SyntaxKind.EqualsExpression,
                     IdentifierName("value1"),
                     IdentifierName("value2"))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #13
0
 public static MemberDeclarationSyntax IComparableCompareToSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName("IComparable")))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("obj")).WithType(PredefinedType(Token(SyntaxKind.ObjectKeyword))))))
 .WithBody(
     Block(
         IfStatement(
             PrefixUnaryExpression(
                 SyntaxKind.LogicalNotExpression,
                 ParenthesizedExpression(
                     IsPatternExpression(
                         IdentifierName("obj"),
                         DeclarationPattern(
                             IdentifierName(descriptor.Name),
                             SingleVariableDesignation(Identifier("other")))))),
             ThrowStatement(
                 ObjectCreationExpression(IdentifierName("ArgumentException"))
                 .WithArgumentList(
                     ArgumentList(
                         SingletonSeparatedList(
                             Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"Object must be of type {descriptor.Name}")))))))),
         ReturnStatement(
             InvocationExpression(
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         ThisExpression(),
                         IdentifierName(descriptor.InnerName)),
                     IdentifierName("CompareTo")))
             .WithArgumentList(
                 ArgumentList(
                     SingletonSeparatedList(
                         Argument(
                             MemberAccessExpression(
                                 SyntaxKind.SimpleMemberAccessExpression,
                                 IdentifierName("other"),
                                 IdentifierName(descriptor.InnerName)))))))));
Exemple #14
0
 public static MemberDeclarationSyntax StringGetHashCodeSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("GetHashCode"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         ConditionalExpression(
             BinaryExpression(
                 SyntaxKind.NotEqualsExpression,
                 IdentifierName(descriptor.InnerName),
                 LiteralExpression(SyntaxKind.NullLiteralExpression)),
             InvocationExpression(
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         IdentifierName("StringComparer"),
                         IdentifierName(stringComparison.ToString())),
                     IdentifierName("GetHashCode")))
             .WithArgumentList(
                 ArgumentList(
                     SingletonSeparatedList(Argument(IdentifierName(descriptor.InnerName))))),
             LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #15
0
 public static MemberDeclarationSyntax IComparableTCompareToSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     ThisExpression(),
                     IdentifierName(descriptor.InnerName)),
                 IdentifierName("CompareTo")))
         .WithArgumentList(
             ArgumentList(
                 SingletonSeparatedList(
                     Argument(
                         MemberAccessExpression(
                             SyntaxKind.SimpleMemberAccessExpression,
                             IdentifierName("other"),
                             IdentifierName(descriptor.InnerName))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemple #16
0
 public static MemberDeclarationSyntax NumberTryParseStringSyntax(this PrimitiveDescriptor descriptor)
 => descriptor.NumberTryParseSyntax(PredefinedType(Token(SyntaxKind.StringKeyword)));
Exemple #17
0
 static TypeSyntax TryWrapInNullableTypeSyntax(this PrimitiveDescriptor descriptor, TypeSyntax typeSyntax)
 => descriptor.IsInnerTypeNullableReferenceType() ? NullableType(typeSyntax) : typeSyntax;
Exemple #18
0
 static MemberDeclarationSyntax NumberTryParseSyntax(this PrimitiveDescriptor descriptor, TypeSyntax inputType)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("TryParse"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("s"))
     .WithType(inputType),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("numberStyles"))
     .WithType(IdentifierName("NumberStyles")),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("formatProvider"))
     .WithType(IdentifierName("IFormatProvider")),
     Token(SyntaxKind.CommaToken),
     Parameter(Identifier("value"))
     .WithModifiers(TokenList(Token(SyntaxKind.OutKeyword)))
     .WithType(IdentifierName(descriptor.Name))
 })))
 .WithBody(
     Block(
         IfStatement(
             PrefixUnaryExpression(
                 SyntaxKind.LogicalNotExpression,
                 InvocationExpression(
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         PredefinedType(Token(descriptor.InnerKnownType.ToTypeKeyword())),
                         IdentifierName("TryParse")))
                 .WithArgumentList(
                     ArgumentList(
                         SeparatedList <ArgumentSyntax>(
                             new SyntaxNodeOrToken[]
 {
     Argument(IdentifierName("s")),
     Token(SyntaxKind.CommaToken),
     Argument(IdentifierName("numberStyles")),
     Token(SyntaxKind.CommaToken),
     Argument(IdentifierName("formatProvider")),
     Token(SyntaxKind.CommaToken),
     Argument(
         DeclarationExpression(
             PredefinedType(Token(descriptor.InnerKnownType.ToTypeKeyword())),
             SingleVariableDesignation(Identifier("result"))))
     .WithRefKindKeyword(Token(SyntaxKind.OutKeyword))
 })))),
             Block(
                 ExpressionStatement(
                     AssignmentExpression(
                         SyntaxKind.SimpleAssignmentExpression,
                         IdentifierName("value"),
                         LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))),
                 ReturnStatement(LiteralExpression(SyntaxKind.FalseLiteralExpression)))),
         ExpressionStatement(
             AssignmentExpression(
                 SyntaxKind.SimpleAssignmentExpression,
                 IdentifierName("value"),
                 ObjectCreationExpression(IdentifierName(descriptor.Name))
                 .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("result"))))))),
         ReturnStatement(LiteralExpression(SyntaxKind.TrueLiteralExpression))));
Exemple #19
0
 public static MemberDeclarationSyntax TimeSpanTryParseStringSyntax(this PrimitiveDescriptor descriptor)
 => descriptor.DateTimeTryParseSyntax("TimeSpanStyles", PredefinedType(Token(SyntaxKind.StringKeyword)));
Exemple #20
0
 public static MemberDeclarationSyntax TimeSpanTryParseSpanSyntax(this PrimitiveDescriptor descriptor)
 => descriptor.DateTimeTryParseSyntax("TimeSpanStyles", ReadOnlySpanChar);
Exemple #21
0
 public static MemberDeclarationSyntax NumberTryParseSpanSyntax(this PrimitiveDescriptor descriptor)
 => descriptor.NumberTryParseSyntax(ReadOnlySpanChar);
Exemple #22
0
 public static bool IsInnerTypeNullableReferenceType(this PrimitiveDescriptor descriptor)
 => descriptor.InnerType is NullableTypeSyntax && descriptor.InnerTypeSymbol.IsReferenceType;
Exemple #23
0
 public static MemberDeclarationSyntax IConvertibleConvertToSyntax(this PrimitiveDescriptor descriptor, string returnType, string name)
 => descriptor.IConvertibleConvertToSyntax(IdentifierName(returnType), name);
Exemple #24
0
 public static MemberDeclarationSyntax IConvertibleConvertToSyntax(this PrimitiveDescriptor descriptor, SyntaxKind returnType, string name)
 => descriptor.IConvertibleConvertToSyntax(PredefinedType(Token(returnType)), name);
Exemple #25
0
 public static MemberDeclarationSyntax ToPrimitiveSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(descriptor.InnerType, Identifier("To" + descriptor.InnerTypeName))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithExpressionBody(ArrowExpressionClause(IdentifierName(descriptor.InnerName)))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));