Esempio n. 1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            ClassDeclarationSyntax classDeclaration,
            SyntaxKind modifierKind,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (classDeclaration == null)
            {
                throw new ArgumentNullException(nameof(classDeclaration));
            }

            SyntaxTokenList modifiers = classDeclaration.Modifiers;

            Debug.Assert(!modifiers.Contains(modifierKind), modifierKind.ToString());

            if (!modifiers.Contains(modifierKind))
            {
                SyntaxToken modifier = Token(modifierKind);

                ClassDeclarationSyntax newClassDeclaration = Inserter.InsertModifier(classDeclaration, modifier);

                return(await document.ReplaceNodeAsync(classDeclaration, newClassDeclaration, cancellationToken).ConfigureAwait(false));
            }
            else
            {
                return(document);
            }
        }
        public async Task ComputeRefactoringsForTokenAsync()
        {
            SyntaxToken token = Root.FindToken(Span.Start);

            SyntaxKind kind = token.Kind();

            if (kind != SyntaxKind.None &&
                token.Span.Contains(Span))
            {
                Debug.WriteLine(kind.ToString());

                switch (kind)
                {
                case SyntaxKind.CloseParenToken:
                {
                    await CloseParenTokenRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.CommaToken:
                {
                    await CommaTokenRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.SemicolonToken:
                {
                    SemicolonTokenRefactoring.ComputeRefactorings(this, token);
                    break;
                }
                }
            }
        }
Esempio n. 3
0
        public static async Task ComputeRefactoringsForTokenAsync(this RefactoringContext context)
        {
            SyntaxToken token = context.FindToken();

            SyntaxKind kind = token.Kind();

            if (kind != SyntaxKind.None &&
                token.Span.Contains(context.Span))
            {
                Debug.WriteLine(kind.ToString());

                switch (kind)
                {
                case SyntaxKind.CloseParenToken:
                {
                    await CloseParenTokenRefactoring.ComputeRefactoringsAsync(context, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.CommaToken:
                {
                    await CommaTokenRefactoring.ComputeRefactoringsAsync(context, token).ConfigureAwait(false);

                    break;
                }
                }
            }
        }
        private static SyntaxKind NegateOperatorTokenKind(this SyntaxKind operatorKind)
        {
            switch (operatorKind)
            {
            case SyntaxKind.EqualsEqualsToken:
                return(SyntaxKind.ExclamationEqualsToken);

            case SyntaxKind.ExclamationEqualsToken:
                return(SyntaxKind.EqualsEqualsToken);

            case SyntaxKind.GreaterThanToken:
                return(SyntaxKind.LessThanEqualsToken);

            case SyntaxKind.GreaterThanEqualsToken:
                return(SyntaxKind.LessThanToken);

            case SyntaxKind.LessThanToken:
                return(SyntaxKind.GreaterThanEqualsToken);

            case SyntaxKind.LessThanEqualsToken:
                return(SyntaxKind.GreaterThanToken);
            }

            Debug.Assert(false, operatorKind.ToString());
            return(SyntaxKind.None);
        }
Esempio n. 5
0
        private static bool RequireSeperator(SyntaxKind kind1, SyntaxKind kind2)
        {
            if (kind1 == SyntaxKind.IdentifierToken || kind2 == SyntaxKind.IdentifierToken)
            {
                return(true);
            }
            var kw1 = kind1.ToString().EndsWith("Keyword");
            var kw2 = kind2.ToString().EndsWith("Keyword");

            if (kw1 && kw2)
            {
                return(true);
            }
            if (kw1 && kind2 == SyntaxKind.IdentifierToken)
            {
                return(true);
            }
            if (kind1 == SyntaxKind.IdentifierToken && kw2)
            {
                return(true);
            }
            if (kind1 == SyntaxKind.NumberToken && kind2 == SyntaxKind.NumberToken)
            {
                return(true);
            }
            if (kind1 == SyntaxKind.NotToken || kind2 == SyntaxKind.NotToken)
            {
                return(true);
            }
            return(false);
        }
        private static SyntaxKind NegateExpressionKind(this SyntaxKind expressionKind)
        {
            switch (expressionKind)
            {
            case SyntaxKind.EqualsExpression:
                return(SyntaxKind.NotEqualsExpression);

            case SyntaxKind.NotEqualsExpression:
                return(SyntaxKind.EqualsExpression);

            case SyntaxKind.GreaterThanExpression:
                return(SyntaxKind.LessThanOrEqualExpression);

            case SyntaxKind.GreaterThanOrEqualExpression:
                return(SyntaxKind.LessThanExpression);

            case SyntaxKind.LessThanExpression:
                return(SyntaxKind.GreaterThanOrEqualExpression);

            case SyntaxKind.LessThanOrEqualExpression:
                return(SyntaxKind.GreaterThanExpression);
            }

            Debug.Assert(false, expressionKind.ToString());
            return(SyntaxKind.None);
        }
Esempio n. 7
0
            private static StatementSyntax CreateJumpStatement(SyntaxKind jumpKind)
            {
                switch (jumpKind)
                {
                case SyntaxKind.ReturnStatement:
                    return(ReturnStatement());

                case SyntaxKind.NullLiteralExpression:
                    return(ReturnStatement(NullLiteralExpression()));

                case SyntaxKind.FalseLiteralExpression:
                    return(ReturnStatement(FalseLiteralExpression()));

                case SyntaxKind.TrueLiteralExpression:
                    return(ReturnStatement(TrueLiteralExpression()));

                case SyntaxKind.DefaultLiteralExpression:
                    return(ReturnStatement(DefaultLiteralExpression()));

                case SyntaxKind.BreakStatement:
                    return(BreakStatement());

                case SyntaxKind.ContinueStatement:
                    return(ContinueStatement());

                case SyntaxKind.ThrowStatement:
                    return(ThrowStatement());

                case SyntaxKind.YieldBreakStatement:
                    return(YieldBreakStatement());

                default:
                    throw new ArgumentException(jumpKind.ToString(), nameof(jumpKind));
                }
            }
Esempio n. 8
0
        private static bool RequiresSeparator(SyntaxKind left, SyntaxKind right)
        {
            var leftIsKeyword  = left.ToString().EndsWith("Keyword");
            var rightIsKeyword = right.ToString().EndsWith("Keyword");


            if (left == SyntaxKind.Identifier || right == SyntaxKind.Identifier)
            {
                return(true);
            }

            if (leftIsKeyword && rightIsKeyword)
            {
                return(true);
            }

            if (leftIsKeyword && right == SyntaxKind.Identifier)
            {
                return(true);
            }

            if (left == SyntaxKind.Identifier && rightIsKeyword)
            {
                return(true);
            }

            if (left == SyntaxKind.Number && right == SyntaxKind.Number)
            {
                return(true);
            }

            if (left == SyntaxKind.BooleanNot && right == SyntaxKind.EqualsToken)
            {
                return(true);
            }

            if (left == SyntaxKind.EqualsToken && right == SyntaxKind.EqualsToken)
            {
                return(true);
            }

            if (left == SyntaxKind.EqualsToken && right == SyntaxKind.DoubleEquals)
            {
                return(true);
            }

            if (left == SyntaxKind.BooleanNot && right == SyntaxKind.DoubleEquals)
            {
                return(true);
            }

            if (left == SyntaxKind.BooleanNot && right == SyntaxKind.IsTextBooleanKeyword)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 9
0
 private SyntaxFactoryWriter WriteArgument(string parameter, SyntaxKind kind, bool closeArgumentList = false)
 {
     _ = this.writer
         .WriteArgumentStart(parameter)
         .Append("SyntaxKind.")
         .Append(kind.ToString())
         .WriteArgumentEnd(closeArgumentList);
     return(this);
 }
Esempio n. 10
0
        private void AddModifier(CodeFixContext context, Diagnostic diagnostic, MemberDeclarationSyntax memberDeclaration, SyntaxKind kind)
        {
            CodeAction codeAction = CodeAction.Create(
                $"Add '{ModifierHelper.GetModifierName(kind)}' modifier",
                cancellationToken => context.Document.InsertModifierAsync(memberDeclaration, kind, ModifierComparer.Instance, cancellationToken),
                GetEquivalenceKey(diagnostic, kind.ToString()));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Esempio n. 11
0
        public async Task ComputeRefactoringsForTokenAsync()
        {
            SyntaxToken token = Root.FindToken(Span.Start);

            SyntaxKind kind = token.Kind();

            if (kind != SyntaxKind.None &&
                token.Span.Contains(Span))
            {
                Debug.WriteLine(kind.ToString());

                switch (kind)
                {
                case SyntaxKind.CloseParenToken:
                {
                    await CloseParenTokenRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.CommaToken:
                {
                    await CommaTokenRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.SemicolonToken:
                {
                    SemicolonTokenRefactoring.ComputeRefactorings(this, token);
                    break;
                }

                case SyntaxKind.PlusToken:
                {
                    await PlusTokenRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);

                    break;
                }

                case SyntaxKind.PublicKeyword:
                case SyntaxKind.InternalKeyword:
                case SyntaxKind.ProtectedKeyword:
                case SyntaxKind.PrivateKeyword:
                {
                    {
                        if (IsRefactoringEnabled(RefactoringIdentifiers.ChangeAccessibility))
                        {
                            await AccessModifierRefactoring.ComputeRefactoringsAsync(this, token).ConfigureAwait(false);
                        }

                        break;
                    }
                }
                }
            }
        }
Esempio n. 12
0
        public static InvocationExpressionSyntax ChangeInvokedMethodName(InvocationExpressionSyntax invocationExpression, string newName)
        {
            ExpressionSyntax expression = invocationExpression.Expression;

            if (expression != null)
            {
                SyntaxKind kind = expression.Kind();

                if (kind == SyntaxKind.SimpleMemberAccessExpression)
                {
                    var memberAccess            = (MemberAccessExpressionSyntax)expression;
                    SimpleNameSyntax simpleName = memberAccess.Name;

                    if (simpleName != null)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName);

                        return(invocationExpression.WithExpression(memberAccess.WithName(newSimpleName)));
                    }
                }
                else if (kind == SyntaxKind.MemberBindingExpression)
                {
                    var memberBinding           = (MemberBindingExpressionSyntax)expression;
                    SimpleNameSyntax simpleName = memberBinding.Name;

                    if (simpleName != null)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName);

                        return(invocationExpression.WithExpression(memberBinding.WithName(newSimpleName)));
                    }
                }
                else
                {
                    if (expression is SimpleNameSyntax simpleName)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName);

                        return(invocationExpression.WithExpression(newSimpleName));
                    }

                    Debug.Fail(kind.ToString());
                }
            }

            return(invocationExpression);

            SimpleNameSyntax ChangeName(SimpleNameSyntax simpleName)
            {
                return(simpleName.WithIdentifier(
                           Identifier(
                               simpleName.GetLeadingTrivia(),
                               newName,
                               simpleName.GetTrailingTrivia())));
            }
        }
Esempio n. 13
0
        public static InvocationExpressionSyntax ChangeInvokedMethodName(InvocationExpressionSyntax invocation, string newName)
        {
            if (invocation == null)
            {
                throw new ArgumentNullException(nameof(invocation));
            }

            ExpressionSyntax expression = invocation.Expression;

            if (expression != null)
            {
                SyntaxKind kind = expression.Kind();

                if (kind == SyntaxKind.SimpleMemberAccessExpression)
                {
                    var memberAccess            = (MemberAccessExpressionSyntax)expression;
                    SimpleNameSyntax simpleName = memberAccess.Name;

                    if (simpleName != null)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName, newName);

                        return(invocation.WithExpression(memberAccess.WithName(newSimpleName)));
                    }
                }
                else if (kind == SyntaxKind.MemberBindingExpression)
                {
                    var memberBinding           = (MemberBindingExpressionSyntax)expression;
                    SimpleNameSyntax simpleName = memberBinding.Name;

                    if (simpleName != null)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName, newName);

                        return(invocation.WithExpression(memberBinding.WithName(newSimpleName)));
                    }
                }
                else
                {
                    var simpleName = expression as SimpleNameSyntax;

                    if (simpleName != null)
                    {
                        SimpleNameSyntax newSimpleName = ChangeName(simpleName, newName);

                        return(invocation.WithExpression(newSimpleName));
                    }

                    Debug.Fail(kind.ToString());
                }
            }

            return(invocation);
        }
Esempio n. 14
0
        private static bool RequiresSeparator(SyntaxKind _t1Kind, SyntaxKind _t2Kind)
        {
            var _t1IsKeyword = _t1Kind.ToString().EndsWith("Keyword");
            var _t2IsKeyword = _t2Kind.ToString().EndsWith("Keyword");

            if (_t1Kind == SyntaxKind.IdentifierToken && _t2Kind == SyntaxKind.IdentifierToken)
            {
                return(true);
            }

            else if (_t1IsKeyword && _t2IsKeyword)
            {
                return(true);
            }

            else if (_t1IsKeyword && _t2Kind == SyntaxKind.IdentifierToken)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.IdentifierToken && _t2IsKeyword)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.NumberToken && _t2Kind == SyntaxKind.NumberToken)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.BangToken && _t2Kind == SyntaxKind.EqualsToken)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.EqualsToken && _t2Kind == SyntaxKind.EqualsToken)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.EqualsToken && _t2Kind == SyntaxKind.EqualsEqualsToken)
            {
                return(true);
            }

            else if (_t1Kind == SyntaxKind.BangToken && _t2Kind == SyntaxKind.EqualsEqualsToken)
            {
                return(true);
            }

            return(false);
        }
        private void RemoveModifier(CodeFixContext context, Diagnostic diagnostic, SyntaxNode node, SyntaxTokenList modifiers, SyntaxKind modifierKind)
        {
            int index = modifiers.IndexOf(modifierKind);

            if (index == -1)
            {
                return;
            }

            SyntaxToken modifier = modifiers[index];

            RemoveModifier(context, diagnostic, node, modifier, modifierKind.ToString());
        }
Esempio n. 16
0
 public Node(SyntaxNode This, HashSet <SyntaxNode> Ancestors, SyntaxNode[] Descendents,
             SyntaxToken[] Leaves, SyntaxKind Kind)
 {
     this.This             = This;
     this.Ancestors        = Ancestors;
     this.Descendents      = Descendents;
     this.AncestorsAndSelf = new HashSet <SyntaxNode>(Ancestors);
     this.AncestorsAndSelf.Add(This);
     this.Leaves   = Leaves;
     this.Depth    = Depth;
     this.Kind     = Kind;
     this.KindName = Kind.ToString();
 }
Esempio n. 17
0
        private static ExpressionSyntax GetName(ExpressionSyntax expression)
        {
            SyntaxKind kind = expression.Kind();

            if (kind == SyntaxKind.SimpleMemberAccessExpression)
            {
                return(((MemberAccessExpressionSyntax)expression).Name);
            }

            Debug.Assert(expression.IsKind(SyntaxKind.IdentifierName), kind.ToString());

            return(expression);
        }
Esempio n. 18
0
        /// <nodoc />
        public static string ToDisplayString(this SyntaxKind kind)
        {
            Scanner.TokenStrings.TryGetValue(kind, out var result);

            // Some kinds should be treated differently. For instance, 'OmittedExpression' should be printed as space.
            if (result == null)
            {
                s_specialSyntaxKinds.TryGetValue(kind, out result);
            }

            // Not all kinds are presented in tokenStrings, so using string representation
            // for other kinds.
            return(result ?? kind.ToString());
        }
            private static void AppendOperatorName(StringBuilder builder, SyntaxKind kind)
            {
                var name = "#op_" + kind.ToString();

                if (name.EndsWith("Keyword", StringComparison.Ordinal))
                {
                    name = name.Substring(0, name.Length - 7);
                }
                else if (name.EndsWith("Token", StringComparison.Ordinal))
                {
                    name = name.Substring(0, name.Length - 5);
                }

                builder.Append(name);
            }
Esempio n. 20
0
 private SyntaxToken Token(SyntaxKind kind, int i1 = 0, int i2 = 0)
 {
     try
     {
         var tokenLength = buffer.Index - tokenStart;
         var tokenValue  = code.Substring(tokenStart + i1, tokenLength + i2 + 1);
         CurrentColumn += tokenLength;
         var charString = code.Substring(tokenStart, 1) == "'";
         return(new SyntaxToken(kind, kind.ToString(), tokenValue, charString, 0, 0, this.CurrentLine, this.CurrentColumn));
     }
     catch
     {
         return(Error("Unexpected end of code. Maybe you forgot to close the '" + kind + "'?"));
     }
 }
Esempio n. 21
0
        public static void AddModifier(
            CodeFixContext context,
            Diagnostic diagnostic,
            MemberDeclarationSyntax memberDeclaration,
            SyntaxKind kind)
        {
            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                $"Add '{ModifierHelper.GetModifierName(kind)}' modifier",
                cancellationToken => AddModifier(document, memberDeclaration, kind, cancellationToken),
                AbstractCodeFixProvider.GetEquivalenceKey(diagnostic, kind.ToString()));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
        public ReduceIfNestingAnalysisResult(SyntaxKind jumpKind, SyntaxNode topNode)
        {
            Debug.Assert(jumpKind == SyntaxKind.None ||
                         jumpKind == SyntaxKind.ReturnStatement ||
                         jumpKind == SyntaxKind.NullLiteralExpression ||
                         jumpKind == SyntaxKind.FalseLiteralExpression ||
                         jumpKind == SyntaxKind.TrueLiteralExpression ||
                         jumpKind == SyntaxKind.BreakStatement ||
                         jumpKind == SyntaxKind.ContinueStatement ||
                         jumpKind == SyntaxKind.ThrowStatement ||
                         jumpKind == SyntaxKind.YieldBreakStatement, jumpKind.ToString());

            JumpKind = jumpKind;
            TopNode  = topNode;
        }
        private static string GetOperatorText(SyntaxKind kind)
        {
            switch (kind)
            {
            case SyntaxKind.PostIncrementExpression:
                return("++");

            case SyntaxKind.PostDecrementExpression:
                return("--");
            }

            Debug.Fail(kind.ToString());

            return("");
        }
Esempio n. 24
0
        private static bool RequiredSeperated(SyntaxKind t1Kind, SyntaxKind t2Kind)
        {
            var t1IsKeyword = t1Kind.ToString().EndsWith("Keyword");
            var t2IsKeyword = t2Kind.ToString().EndsWith("Keyword");

            if (t1Kind == SyntaxKind.IdentifierToken && t2Kind == SyntaxKind.IdentifierToken)
            {
                return(true);
            }
            if (t1IsKeyword && t2IsKeyword)
            {
                return(true);
            }
            if (t1IsKeyword && t2Kind == SyntaxKind.IdentifierToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.LiteralToken && t2Kind == SyntaxKind.LiteralToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.IdentifierToken && t2IsKeyword)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.EqualsToken && t2Kind == SyntaxKind.EqualsToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.AssignEqualsToken && t2Kind == SyntaxKind.AssignEqualsToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.AssignEqualsToken && t2Kind == SyntaxKind.EqualsToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.BangToken && t2Kind == SyntaxKind.EqualsToken)
            {
                return(true);
            }
            if (t1Kind == SyntaxKind.BangToken && t2Kind == SyntaxKind.AssignEqualsToken)
            {
                return(true);
            }
            return(false);
        }
        public static string GetOperatorText(SyntaxKind kind)
        {
            switch (kind)
            {
            case SyntaxKind.PostIncrementExpression:
                return("++");

            case SyntaxKind.PostDecrementExpression:
                return("--");

            default:
            {
                Debug.Assert(false, kind.ToString());
                return("");
            }
            }
        }
        private static string GetBlockDescription(string blockName, SyntaxKind blockKind, string containingTypeName)
        {
            string result;

            switch (blockKind)
            {
            case SyntaxKind.ConstructorDeclaration:
                result = (blockName == ".cctor" ? "Static constructor " : "Constructor ") + (containingTypeName ?? blockName);
                break;

            case SyntaxKind.DestructorDeclaration:
                result = "Destructor " + ('~' + containingTypeName ?? blockName);
                break;

            case SyntaxKind.ConversionOperatorDeclaration:
            case SyntaxKind.OperatorDeclaration:
                // Improve: This could describe Implicit and Explicit conversion operators better (e.g., show converted type name).
                result = "Operator " + TrimPrefix(blockName, "op_");
                break;

            case SyntaxKind.GetAccessorDeclaration:
                // Improve: This could describe indexers better (e.g., report this instead of Item).
                result = "Property " + TrimPrefix(blockName, "get_") + " get accessor";
                break;

            case SyntaxKind.SetAccessorDeclaration:
                // Improve: This could describe indexers better (e.g., report this instead of Item).
                result = "Property " + TrimPrefix(blockName, "set_") + " set accessor";
                break;

            case SyntaxKind.AddAccessorDeclaration:
                result = "Event " + TrimPrefix(blockName, "add_") + " add accessor";
                break;

            case SyntaxKind.RemoveAccessorDeclaration:
                result = "Event " + TrimPrefix(blockName, "remove_") + " remove accessor";
                break;

            default:
                result = TrimSuffix(blockKind.ToString(), "Declaration") + ' ' + blockName;
                break;
            }

            return(result);
        }
Esempio n. 27
0
        /// <summary>
        /// Returns true if a syntax of the specified kind can be an embedded statement.
        /// </summary>
        /// <param name="kind"></param>
        public static bool CanBeEmbeddedStatement(SyntaxKind kind)
        {
            switch (kind)
            {
            case SyntaxKind.Block:
            case SyntaxKind.ExpressionStatement:
            case SyntaxKind.EmptyStatement:
            case SyntaxKind.GotoStatement:
            case SyntaxKind.GotoCaseStatement:
            case SyntaxKind.GotoDefaultStatement:
            case SyntaxKind.BreakStatement:
            case SyntaxKind.ContinueStatement:
            case SyntaxKind.ReturnStatement:
            case SyntaxKind.YieldReturnStatement:
            case SyntaxKind.YieldBreakStatement:
            case SyntaxKind.ThrowStatement:
            case SyntaxKind.WhileStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.ForEachStatement:
            case SyntaxKind.UsingStatement:
            case SyntaxKind.FixedStatement:
            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
            case SyntaxKind.UnsafeStatement:
            case SyntaxKind.LockStatement:
            case SyntaxKind.IfStatement:
            case SyntaxKind.SwitchStatement:
            case SyntaxKind.TryStatement:
            case SyntaxKind.ForEachVariableStatement:
                return(true);

            case SyntaxKind.LocalDeclarationStatement:
            case SyntaxKind.LabeledStatement:
            case SyntaxKind.LocalFunctionStatement:
            case SyntaxKind.GlobalStatement:
                return(false);
            }

            Debug.Assert(!kind.ToString().EndsWith("Statement", StringComparison.Ordinal), kind.ToString());

            return(false);
        }
Esempio n. 28
0
        private Mutator GetMutatorType(SyntaxKind kind)
        {
            string kindString = kind.ToString();

            if (kindString.StartsWith("Logical"))
            {
                return(Mutator.Logical);
            }
            else if (kindString.Contains("Equals") ||
                     kindString.Contains("Greater") ||
                     kindString.Contains("Less"))
            {
                return(Mutator.Equality);
            }
            else
            {
                return(Mutator.Arithmetic);
            }
        }
        public static void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol debugSymbol)
        {
            var invocation = (InvocationExpressionSyntax)context.Node;

            ExpressionSyntax expression = invocation.Expression;

            if (expression == null)
            {
                return;
            }

            if (invocation.SpanContainsDirectives())
            {
                return;
            }

            if (!CanRefactor(invocation, debugSymbol, context.SemanticModel, context.CancellationToken))
            {
                return;
            }

            if (expression.Kind() != SyntaxKind.SimpleMemberAccessExpression &&
                context.SemanticModel
                .GetSpeculativeMethodSymbol(invocation.SpanStart, GetNewInvocation(invocation))?
                .ContainingType?
                .Equals(debugSymbol) != true)
            {
                return;
            }

            SyntaxKind kind = expression.Kind();

            if (kind == SyntaxKind.SimpleMemberAccessExpression)
            {
                expression = ((MemberAccessExpressionSyntax)expression).Name;
            }

            Debug.Assert(expression.Kind() == SyntaxKind.IdentifierName, kind.ToString());

            context.ReportDiagnostic(DiagnosticDescriptors.CallDebugFailInsteadOfDebugAssert, expression);
        }
Esempio n. 30
0
        private static SyntaxKind NegateBinaryOperator(SyntaxKind kind)
        {
            switch (kind)
            {
            case SyntaxKind.LessThanToken:
                return(SyntaxKind.GreaterThanEqualsToken);

            case SyntaxKind.LessThanEqualsToken:
                return(SyntaxKind.GreaterThanToken);

            case SyntaxKind.GreaterThanToken:
                return(SyntaxKind.LessThanEqualsToken);

            case SyntaxKind.GreaterThanEqualsToken:
                return(SyntaxKind.LessThanToken);

            case SyntaxKind.EqualsEqualsToken:
                return(SyntaxKind.ExclamationEqualsToken);

            case SyntaxKind.ExclamationEqualsToken:
                return(SyntaxKind.EqualsEqualsToken);

            case SyntaxKind.AmpersandToken:
                return(SyntaxKind.BarToken);

            case SyntaxKind.BarToken:
                return(SyntaxKind.AmpersandToken);

            case SyntaxKind.BarBarToken:
                return(SyntaxKind.AmpersandAmpersandToken);

            case SyntaxKind.AmpersandAmpersandToken:
                return(SyntaxKind.BarBarToken);

            default:
            {
                Debug.Assert(false, kind.ToString());
                return(kind);
            }
            }
        }
Esempio n. 31
0
        private Token GetExpectedToken(SyntaxKind kind)
        {
            if (ParseExpected(kind))
            {
                return currentToken;
            }
            else if (kind == SyntaxKind.AssignmentOperator && IsBlockContext(contextStack.Peek()))
            {
                ParseErrorAtCurrentPosition(ErrorMessages.InvalidStatementWithoutAssignementOperator);
            }
            else
            {
                ParseErrorAtCurrentPosition(ErrorMessages.MissingToken + kind.ToString());
            }

            return Token.CreateMissingToken(currentToken.End);
        }
Esempio n. 32
0
 public static PredefinedObjectType GetPredefinedObjectType(SyntaxKind kind)
 {
     switch (kind)
     {
         case SyntaxKind.AppendStructuredBufferKeyword:
             return PredefinedObjectType.AppendStructuredBuffer;
         case SyntaxKind.BlendStateKeyword:
             return PredefinedObjectType.BlendState;
         case SyntaxKind.BufferKeyword:
             return PredefinedObjectType.Buffer;
         case SyntaxKind.ByteAddressBufferKeyword:
             return PredefinedObjectType.ByteAddressBuffer;
         case SyntaxKind.ConsumeStructuredBufferKeyword:
             return PredefinedObjectType.ConsumeStructuredBuffer;
         case SyntaxKind.DepthStencilStateKeyword:
             return PredefinedObjectType.DepthStencilState;
         case SyntaxKind.InputPatchKeyword:
             return PredefinedObjectType.InputPatch;
         case SyntaxKind.LineStreamKeyword:
             return PredefinedObjectType.LineStream;
         case SyntaxKind.OutputPatchKeyword:
             return PredefinedObjectType.OutputPatch;
         case SyntaxKind.PointStreamKeyword:
             return PredefinedObjectType.PointStream;
         case SyntaxKind.RasterizerStateKeyword:
             return PredefinedObjectType.RasterizerState;
         case SyntaxKind.RWBufferKeyword:
             return PredefinedObjectType.RWBuffer;
         case SyntaxKind.RWByteAddressBufferKeyword:
             return PredefinedObjectType.RWByteAddressBuffer;
         case SyntaxKind.RWStructuredBufferKeyword:
             return PredefinedObjectType.RWStructuredBuffer;
         case SyntaxKind.RWTexture1DKeyword:
             return PredefinedObjectType.RWTexture1D;
         case SyntaxKind.RWTexture1DArrayKeyword:
             return PredefinedObjectType.RWTexture1DArray;
         case SyntaxKind.RWTexture2DKeyword:
             return PredefinedObjectType.RWTexture2D;
         case SyntaxKind.RWTexture2DArrayKeyword:
             return PredefinedObjectType.RWTexture2DArray;
         case SyntaxKind.RWTexture3DKeyword:
             return PredefinedObjectType.RWTexture3D;
         case SyntaxKind.Sampler1DKeyword:
             return PredefinedObjectType.Sampler1D;
         case SyntaxKind.SamplerKeyword:
             return PredefinedObjectType.Sampler;
         case SyntaxKind.Sampler2DKeyword:
             return PredefinedObjectType.Sampler2D;
         case SyntaxKind.Sampler3DKeyword:
             return PredefinedObjectType.Sampler3D;
         case SyntaxKind.SamplerCubeKeyword:
             return PredefinedObjectType.SamplerCube;
         case SyntaxKind.SamplerStateKeyword:
             return PredefinedObjectType.SamplerState;
         case SyntaxKind.SamplerComparisonStateKeyword:
             return PredefinedObjectType.SamplerComparisonState;
         case SyntaxKind.StructuredBufferKeyword:
             return PredefinedObjectType.StructuredBuffer;
         case SyntaxKind.TextureKeyword:
         case SyntaxKind.Texture2DLegacyKeyword:
         case SyntaxKind.TextureCubeLegacyKeyword:
             return PredefinedObjectType.Texture;
         case SyntaxKind.Texture1DKeyword:
             return PredefinedObjectType.Texture1D;
         case SyntaxKind.Texture1DArrayKeyword:
             return PredefinedObjectType.Texture1DArray;
         case SyntaxKind.Texture2DKeyword:
             return PredefinedObjectType.Texture2D;
         case SyntaxKind.Texture2DArrayKeyword:
             return PredefinedObjectType.Texture2DArray;
         case SyntaxKind.Texture2DMSKeyword:
             return PredefinedObjectType.Texture2DMS;
         case SyntaxKind.Texture2DMSArrayKeyword:
             return PredefinedObjectType.Texture2DMSArray;
         case SyntaxKind.Texture3DKeyword:
             return PredefinedObjectType.Texture3D;
         case SyntaxKind.TextureCubeKeyword:
             return PredefinedObjectType.TextureCube;
         case SyntaxKind.TextureCubeArrayKeyword:
             return PredefinedObjectType.TextureCubeArray;
         case SyntaxKind.TriangleStreamKeyword:
             return PredefinedObjectType.TriangleStream;
         case SyntaxKind.RasterizerOrderedBufferKeyword:
             return PredefinedObjectType.RasterizerOrderedBuffer;
         case SyntaxKind.RasterizerOrderedByteAddressBufferKeyword:
             return PredefinedObjectType.RasterizerOrderedByteAddressBuffer;
         case SyntaxKind.RasterizerOrderedStructuredBufferKeyword:
             return PredefinedObjectType.RasterizerOrderedStructuredBuffer;
         case SyntaxKind.RasterizerOrderedTexture1DArrayKeyword:
             return PredefinedObjectType.RasterizerOrderedTexture1DArray;
         case SyntaxKind.RasterizerOrderedTexture1DKeyword:
             return PredefinedObjectType.RasterizerOrderedTexture1D;
         case SyntaxKind.RasterizerOrderedTexture2DArrayKeyword:
             return PredefinedObjectType.RasterizerOrderedTexture2DArray;
         case SyntaxKind.RasterizerOrderedTexture2DKeyword:
             return PredefinedObjectType.RasterizerOrderedTexture2D;
         case SyntaxKind.RasterizerOrderedTexture3DKeyword:
             return PredefinedObjectType.RasterizerOrderedTexture3D;
         case SyntaxKind.GeometryShaderKeyword:
             return PredefinedObjectType.GeometryShader;
         case SyntaxKind.PixelShaderKeyword:
             return PredefinedObjectType.PixelShader;
         case SyntaxKind.VertexShaderKeyword:
             return PredefinedObjectType.VertexShader;
         default:
             throw new ArgumentOutOfRangeException(nameof(kind), kind.ToString());
     }
 }
Esempio n. 33
0
        /// <summary>
        /// Gets the friendly name that corresponds to the specified <see cref="SyntaxKind"/> value.
        /// </summary>
        private static String GetSyntaxKindFriendlyName(SyntaxKind kind)
        {
            switch (kind)
            {
                case SyntaxKind.EndOfLineTrivia:
                    return "end of line";
                case SyntaxKind.SingleLineCommentTrivia:
                    return "single line comment";
                case SyntaxKind.MultiLineCommentTrivia:
                    return "multi-line comment";
                case SyntaxKind.WhitespaceTrivia:
                    return "white space";
                case SyntaxKind.PlayStoryboardKeyword:
                    return "'play-storyboard' keyword";
                case SyntaxKind.SetHandledKeyword:
                    return "'set-handled' keyword";
                case SyntaxKind.TransitionKeyword:
                    return "'transition' keyword";
                case SyntaxKind.ImportantKeyword:
                    return "'!important' keyword";
                case SyntaxKind.AnimationKeyword:
                    return "'animation' keyword";
                case SyntaxKind.PlaySfxKeyword:
                    return "'play-sfx' keyword";
                case SyntaxKind.PropertyKeyword:
                    return "'property' keyword";
                case SyntaxKind.KeyframeKeyword:
                    return "'keyframe' keyword";
                case SyntaxKind.TriggerKeyword:
                    return "'trigger' keyword";
                case SyntaxKind.HandledKeyword:
                    return "'handled' keyword";
                case SyntaxKind.TargetKeyword:
                    return "'target' keyword";
                case SyntaxKind.EventKeyword:
                    return "'event' keyword";
                case SyntaxKind.SetKeyword:
                    return "'set' keyword";
                case SyntaxKind.AsKeyword:
                    return "'as' keyword";
                case SyntaxKind.IdentifierToken:
                    return "Identifier";
                case SyntaxKind.NumberToken:
                    return "Number";
                case SyntaxKind.CommaToken:
                    return ",";
                case SyntaxKind.ColonToken:
                    return ":";
                case SyntaxKind.SemiColonToken:
                    return ";";
                case SyntaxKind.AtSignToken:
                    return "@";
                case SyntaxKind.HashToken:
                    return "#";
                case SyntaxKind.PeriodToken:
                    return ".";
                case SyntaxKind.ExclamationMarkToken:
                    return "!";
                case SyntaxKind.OpenParenthesesToken:
                    return "(";
                case SyntaxKind.CloseParenthesesToken:
                    return ")";
                case SyntaxKind.OpenCurlyBraceToken:
                    return "{";
                case SyntaxKind.CloseCurlyBraceToken:
                    return "}";
                case SyntaxKind.OpenBracketToken:
                    return "[";
                case SyntaxKind.CloseBracketToken:
                    return "]";
                case SyntaxKind.AsteriskToken:
                    return "*";
                case SyntaxKind.GreaterThanGreaterThanToken:
                    return ">>";
                case SyntaxKind.GreaterThanQuestionMarkToken:
                    return ">?";
                case SyntaxKind.EqualsToken:
                    return "=";
                case SyntaxKind.NotEqualsToken:
                    return "<>";
                case SyntaxKind.LessThanToken:
                    return "<";
                case SyntaxKind.GreaterThanToken:
                    return ">";
                case SyntaxKind.LessThanEqualsToken:
                    return "<=";
                case SyntaxKind.GreaterThanEqualsToken:
                    return ">=";
                case SyntaxKind.PipeToken:
                    return "|";
                case SyntaxKind.PropertyValueToken:
                    return "Property value";
                case SyntaxKind.EndOfFileToken:
                    return "End of file";
                case SyntaxKind.List:
                    return "List";
                case SyntaxKind.Block:
                    return "Block";
                case SyntaxKind.RuleSet:
                    return "Rule set";
                case SyntaxKind.Rule:
                    return "Rule";
                case SyntaxKind.Selector:
                case SyntaxKind.SelectorWithNavigationExpression:
                    return "Selector";
                case SyntaxKind.SelectorWithParentheses:
                    return "Parentheses-enclosed selector";
                case SyntaxKind.SelectorPart:
                    return "Selector part";
                case SyntaxKind.PseudoClass:
                    return "Pseudo-class";
                case SyntaxKind.PropertyName:
                    return "Property name";
                case SyntaxKind.PropertyValue:
                    return "Property value";
                case SyntaxKind.PropertyValueWithBraces:
                    return "Brace-enclosed property value";
                case SyntaxKind.EventName:
                    return "Event name";
                case SyntaxKind.IncompleteTrigger:
                    return "Incomplete trigger";
                case SyntaxKind.EventTrigger:
                    return "Event trigger";
                case SyntaxKind.EventTriggerArgumentList:
                    return "Event trigger argument list";
                case SyntaxKind.PropertyTrigger:
                    return "Property trigger";
                case SyntaxKind.PropertyTriggerCondition:
                    return "Property trigger condition";
                case SyntaxKind.PlayStoryboardTriggerAction:
                    return "play-storyboard trigger action";
                case SyntaxKind.PlaySfxTriggerAction:
                    return "play-sfx trigger action";
                case SyntaxKind.SetTriggerAction:
                    return "set trigger action";
                case SyntaxKind.Transition:
                    return "Transition declaration";
                case SyntaxKind.TransitionArgumentList:
                    return "Transition argument list";
                case SyntaxKind.Storyboard:
                    return "Storyboard declaration";
                case SyntaxKind.StoryboardTarget:
                    return "Storyboard target declaration";
                case SyntaxKind.Animation:
                    return "Animation declaration";
                case SyntaxKind.AnimationKeyframe:
                    return "Keyframe declaration";
                case SyntaxKind.NavigationExpression:
                    return "Navigation expression";
                case SyntaxKind.Identifier:
                    return "Identifier";
                case SyntaxKind.EscapedIdentifier:
                    return "Escaped identifier";
                case SyntaxKind.DirectiveToken:
                case SyntaxKind.UnknownDirective:
                case SyntaxKind.CultureDirective:
                    return "Directive";
            }

            return kind.ToString();
        }
            private static void AppendOperatorName(StringBuilder builder, SyntaxKind kind)
            {
                var name = "#op_" + kind.ToString();
                if (name.EndsWith("Keyword", StringComparison.Ordinal))
                {
                    name = name.Substring(0, name.Length - 7);
                }
                else if (name.EndsWith("Token", StringComparison.Ordinal))
                {
                    name = name.Substring(0, name.Length - 5);
                }

                builder.Append(name);
            }