private static SyntaxList <ArrayRankSpecifierSyntax> ArrayRankSpecifier(ExpressionSyntax expressionSyntax) { var literalExpressionSyntaxList = SyntaxFactory.SingletonSeparatedList(expressionSyntax); var arrayRankSpecifierSyntax = SyntaxFactory.ArrayRankSpecifier(literalExpressionSyntaxList); return(SyntaxFactory.SingletonList(arrayRankSpecifierSyntax)); }
internal SyntaxList <ArrayRankSpecifierSyntax> ConvertArrayRankSpecifierSyntaxes( SyntaxList <VBSyntax.ArrayRankSpecifierSyntax> arrayRankSpecifierSyntaxs, ArgumentListSyntax nodeArrayBounds, bool withSizes = true) { var bounds = SyntaxFactory.List(arrayRankSpecifierSyntaxs.Select(r => (ArrayRankSpecifierSyntax)r.Accept(_nodesVisitor))); if (nodeArrayBounds != null) { var sizesSpecified = nodeArrayBounds.Arguments.Any(a => !a.IsOmitted); var rank = nodeArrayBounds.Arguments.Count; if (!sizesSpecified) { rank += 1; } var convertedArrayBounds = withSizes && sizesSpecified?ConvertArrayBounds(nodeArrayBounds) : Enumerable.Repeat(SyntaxFactory.OmittedArraySizeExpression(), rank); var arrayRankSpecifierSyntax = SyntaxFactory.ArrayRankSpecifier( SyntaxFactory.SeparatedList( convertedArrayBounds)); bounds = bounds.Insert(0, arrayRankSpecifierSyntax); } return(bounds); }
public static ArrowExpressionClauseSyntax GenerateSpanNameValues(MemberDeclarationMeta memberdecl) //=> { byte, byte, byte, byte} { SeparatedSyntaxList <ExpressionSyntax> ArrayInitExpr(MemberDeclarationMeta memberdecl) { SeparatedSyntaxList <ExpressionSyntax> expr = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var byteItem in Encoding.UTF8.GetBytes(memberdecl.StringFieldNameAlias)) { expr = expr.Add(Basics.NumberLiteral(byteItem)); } return(expr); } ArrayRankSpecifierSyntax ArrayRank(MemberDeclarationMeta memberdecl) { return(SF.ArrayRankSpecifier().AddSizes(Basics.NumberLiteral(memberdecl.StringFieldNameAlias.Length))); } return(SF.ArrowExpressionClause( SF.ArrayCreationExpression( SF.ArrayType( SF.PredefinedType(SF.Token(SyntaxKind.ByteKeyword)), SF.SingletonList <ArrayRankSpecifierSyntax>(ArrayRank(memberdecl))), SF.InitializerExpression(SyntaxKind.ArrayInitializerExpression, ArrayInitExpr(memberdecl)) ) )); }
private PropertyDeclarationSyntax CreateArray(SymbolData symbol) { var type = SF.ArrayType( SF.ParseTypeName(SymbolTypeConverter.Convert(symbol)), SF.List(new ArrayRankSpecifierSyntax[] { SF.ArrayRankSpecifier(), })); return(CreatePropertyImpl(type, symbol)); }
private ExpressionStatementSyntax CreateNewArrayAssignment(VBSyntax.ExpressionSyntax vbArrayExpression, ExpressionSyntax csArrayExpression, List <ExpressionSyntax> convertedBounds, int nodeSpanStart) { var arrayRankSpecifierSyntax = SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SeparatedList(convertedBounds)); var convertedType = (IArrayTypeSymbol)_semanticModel.GetTypeInfo(vbArrayExpression).ConvertedType; var typeSyntax = GetTypeSyntaxFromTypeSymbol(convertedType.ElementType, nodeSpanStart); var arrayCreation = SyntaxFactory.ArrayCreationExpression(SyntaxFactory.ArrayType(typeSyntax, SyntaxFactory.SingletonList(arrayRankSpecifierSyntax))); var assignmentExpressionSyntax = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, csArrayExpression, arrayCreation); var newArrayAssignment = SyntaxFactory.ExpressionStatement(assignmentExpressionSyntax); return(newArrayAssignment); }
/// <summary> /// Create a class that implements <see cref="ICommand"/>. /// /// The implementation will execute the given <see cref="method"/>. /// </summary> /// <param name="id">The unique identifier of the command for <see cref="ICommand.Id"/>.</param> /// <param name="className">The new class name.</param> /// <param name="moduleType">The type that contains the <see cref="method"/>.</param> /// <param name="method">The method that should be executed when <see cref="ICommand.ExecuteAsync"/> is called.</param> /// <returns>The <see cref="ClassDeclarationSyntax"/>.</returns> private ClassDeclarationSyntax CreateCommandClass( string id, IReadOnlyList <string> aliases, string className, Type moduleType, MethodInfo method) { var permission = ModuleUtils.GetPermissionName(moduleType, method); var moduleName = ModuleUtils.GetModuleName(moduleType); var attr = method.GetCustomAttribute <CommandAttribute>(); var guildOnly = attr.GuildOnly; var permissionGroup = attr.PermissionGroup.ToString(); return(S.ClassDeclaration(className) .AddBaseListTypes(S.SimpleBaseType(CommandType)) .AddMembers( // ICommand.Id S.PropertyDeclaration(StringType, nameof(ICommand.Id)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(id)))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.Aliases S.PropertyDeclaration(ReadOnlyStringList, nameof(ICommand.Aliases)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.ArrayCreationExpression( S.ArrayType(StringType) .WithRankSpecifiers(S.SingletonList(S.ArrayRankSpecifier(S.SingletonSeparatedList <ExpressionSyntax>(S.OmittedArraySizeExpression())))), S.InitializerExpression(SyntaxKind.ArrayInitializerExpression, new SeparatedSyntaxList <ExpressionSyntax>() .AddRange(aliases.Select(s => S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(s)))))))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.Module S.PropertyDeclaration(StringType, nameof(ICommand.Module)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(moduleName)))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.Permission S.PropertyDeclaration(StringType, nameof(ICommand.Permission)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(permission)))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.PermissionGroup S.PropertyDeclaration(S.ParseName(nameof(PermissionGroup)), nameof(ICommand.PermissionGroup)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, S.IdentifierName(nameof(PermissionGroup)), S.IdentifierName(permissionGroup)))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.GuildOnly S.PropertyDeclaration(BoolType, nameof(ICommand.GuildOnly)) .AddAccessorListAccessors( S.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddBodyStatements(S.ReturnStatement(S.LiteralExpression(guildOnly ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))) .WithSemicolonToken(S.Token(SyntaxKind.SemicolonToken)) ) .AddModifiers(S.Token(SyntaxKind.PublicKeyword)), // ICommand.ExecuteAsync S.MethodDeclaration(TaskType, nameof(ICommand.ExecuteAsync)) .AddModifiers(S.Token(SyntaxKind.PublicKeyword), S.Token(SyntaxKind.AsyncKeyword)) .AddParameterListParameters(Parameter(ArgumentContext, MessageContextType)) .AddBodyStatements( S.ExpressionStatement(InvokeCommand(moduleType, method)) ) )); }
public ClassDeclarationSyntax GenerateHelper() { if (!HasImports) { return(null); } var xAttributeList = SyntaxFactory.FieldDeclaration( SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.PrivateKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)), CommonConversions.CreateVariableDeclarationAndAssignment( "namespaceAttributes", SyntaxFactory.InitializerExpression( CSSyntaxKind.ArrayInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( from x in _xNamespaceFields let fieldIdentifierName = SyntaxFactory.IdentifierName(x.Declaration.Variables.Single().Identifier) let namespaceNameExpression = SyntaxFactory.MemberAccessExpression(CSSyntaxKind.SimpleMemberAccessExpression, fieldIdentifierName, SyntaxFactory.IdentifierName("NamespaceName")) let attributeNameExpression = fieldIdentifierName.IsEquivalentTo(DefaultIdentifierName) ? CommonConversions.Literal("xmlns") : BuildXmlnsAttributeName(fieldIdentifierName) let arguments = SyntaxFactory.Argument(attributeNameExpression).Yield().Concat(SyntaxFactory.Argument(namespaceNameExpression)) select SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName("XAttribute")).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments))))), SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName("XAttribute"), SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression())))))); var boilerplate = SyntaxFactory.ParseStatement(@" TContainer Apply<TContainer>(TContainer x) where TContainer : XContainer { foreach (var d in x.Descendants()) { foreach (var n in namespaceAttributes) { var a = d.Attribute(n.Name); if (a != null && a.Value == n.Value) { a.Remove(); } } } x.Add(namespaceAttributes); return x; }") as LocalFunctionStatementSyntax; var applyMethod = SyntaxFactory.MethodDeclaration( SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)), boilerplate.ReturnType, null, boilerplate.Identifier, boilerplate.TypeParameterList, boilerplate.ParameterList, boilerplate.ConstraintClauses, boilerplate.Body, boilerplate.ExpressionBody); return(SyntaxFactory.ClassDeclaration( SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)), HelperClassUniqueIdentifierName.Identifier, null, null, SyntaxFactory.List <TypeParameterConstraintClauseSyntax>(), SyntaxFactory.List(_xNamespaceFields.Concat <MemberDeclarationSyntax>(xAttributeList).Concat(applyMethod)) )); }