Exemple #1
0
        public void TestFreeFormTokenFactory_CustomText()
        {
            for (SyntaxKind kind = InternalSyntax.SyntaxToken.FirstTokenWithWellKnownText; kind <= InternalSyntax.SyntaxToken.LastTokenWithWellKnownText; kind++)
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    continue;
                }

                var defaultText = SyntaxFacts.GetText(kind);
                var text        = ToXmlEntities(defaultText);
                var valueText   = defaultText;

                var token = SyntaxFactory.Token(SyntaxTriviaList.Create(SyntaxFactory.ElasticMarker), kind, text, valueText, SyntaxTriviaList.Create(SyntaxFactory.ElasticMarker));

                Assert.Equal(kind, token.Kind());
                Assert.Equal(text, token.Text);
                Assert.Equal(valueText, token.ValueText);

                if (string.IsNullOrEmpty(valueText))
                {
                    Assert.IsType <InternalSyntax.SyntaxToken.SyntaxTokenWithTrivia>(token.Node);
                }
                else
                {
                    Assert.IsType <InternalSyntax.SyntaxToken.SyntaxTokenWithValueAndTrivia <string> >(token.Node);
                }
            }
        }
Exemple #2
0
        public static SyntaxToken Create(SyntaxKind kind, GreenNode leading, GreenNode trailing)
        {
            if (kind > LastTokenWithWellKnownText)
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    throw new ArgumentException(string.Format("@@ThisMethodCanOnlyBeUsedToCreateTokens", kind), nameof(kind));
                }

                return(CreateMissing(kind, leading, trailing));
            }

            if (leading == null)
            {
                if (trailing == null)
                {
                    return(s_tokensWithNoTrivia[(int)kind]);
                }
                else if (trailing == SyntaxFactory.Space)
                {
                    return(s_tokensWithSingleTrailingSpace[(int)kind]);
                }
                else if (trailing == SyntaxFactory.CarriageReturnLineFeed)
                {
                    return(s_tokensWithSingleTrailingCRLF[(int)kind]);
                }
            }

            if (leading == SyntaxFactory.ElasticZeroSpace && trailing == SyntaxFactory.ElasticZeroSpace)
            {
                return(s_tokensWithElasticTrivia[(int)kind]);
            }

            return(new SyntaxTokenWithTrivia(kind, leading, trailing));
        }
Exemple #3
0
        internal static SyntaxToken Create(SyntaxKind kind, CSharpSyntaxNode leading, CSharpSyntaxNode trailing)
        {
            if (kind > LastTokenWithWellKnownText)
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    throw new ArgumentException(string.Format(CSharpResources.ThisMethodCanOnlyBeUsedToCreateTokens, kind), "kind");
                }

                return(CreateMissing(kind, leading, trailing));
            }

            if (leading == null)
            {
                if (trailing == null)
                {
                    return(TokensWithNoTrivia[(int)kind].Value);
                }
                else if (trailing == SyntaxFactory.Space)
                {
                    return(TokensWithSingleTrailingSpace[(int)kind].Value);
                }
                else if (trailing == SyntaxFactory.CarriageReturnLineFeed)
                {
                    return(TokensWithSingleTrailingCRLF[(int)kind].Value);
                }
            }

            if (leading == SyntaxFactory.ElasticZeroSpace && trailing == SyntaxFactory.ElasticZeroSpace)
            {
                return(TokensWithElasticTrivia[(int)kind].Value);
            }

            return(new SyntaxTokenWithTrivia(kind, leading, trailing));
        }
Exemple #4
0
        public void TestFreeFormTokenFactory_DefaultText()
        {
            for (
                SyntaxKind kind = InternalSyntax.SyntaxToken.FirstTokenWithWellKnownText;
                kind <= InternalSyntax.SyntaxToken.LastTokenWithWellKnownText;
                kind++
                )
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    continue;
                }

                var defaultText = SyntaxFacts.GetText(kind);
                var actualRed   = SyntaxFactory.Token(
                    SyntaxTriviaList.Create(SyntaxFactory.ElasticMarker),
                    kind,
                    defaultText,
                    defaultText,
                    SyntaxTriviaList.Create(SyntaxFactory.ElasticMarker)
                    );
                var actualGreen = actualRed.Node;

                var expectedGreen = InternalSyntax.SyntaxFactory.Token(
                    InternalSyntax.SyntaxFactory.ElasticZeroSpace,
                    kind,
                    InternalSyntax.SyntaxFactory.ElasticZeroSpace
                    );

                Assert.Same(expectedGreen, actualGreen); // Don't create a new token if we don't have to.
            }
        }
Exemple #5
0
 protected SyntaxToken EatToken(SyntaxKind kind, ErrorCode code, bool reportError = true)
 {
     Debug.Assert(SyntaxFacts.IsAnyToken(kind));
     if (this.CurrentToken.Kind != kind)
     {
         return(CreateMissingToken(kind, code, reportError));
     }
     else
     {
         return(this.EatToken());
     }
 }
Exemple #6
0
        protected SyntaxToken EatTokenWithPrejudice(SyntaxKind kind)
        {
            var token = this.CurrentToken;

            Debug.Assert(SyntaxFacts.IsAnyToken(kind));
            if (token.Kind != kind)
            {
                token = WithAdditionalDiagnostics(token, this.GetExpectedTokenError(kind, token.Kind));
            }

            this.MoveToNextToken();
            return(token);
        }
Exemple #7
0
        internal static SyntaxToken Token(CSharpSyntaxNode leading, SyntaxKind kind, string text, string valueText, CSharpSyntaxNode trailing)
        {
            Debug.Assert(SyntaxFacts.IsAnyToken(kind));
            Debug.Assert(kind != SyntaxKind.IdentifierToken);
            Debug.Assert(kind != SyntaxKind.CharacterLiteralToken);
            Debug.Assert(kind != SyntaxKind.NumericLiteralToken);

            string defaultText = SyntaxFacts.GetText(kind);

            return(kind >= SyntaxToken.FirstTokenWithWellKnownText && kind <= SyntaxToken.LastTokenWithWellKnownText && text == defaultText && valueText == defaultText
                ? Token(leading, kind, trailing)
                : SyntaxToken.WithValue(kind, leading, text, valueText, trailing));
        }
Exemple #8
0
        internal static SyntaxToken Create(SyntaxKind kind)
        {
            if (kind > LastTokenWithWellKnownText)
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    throw new ArgumentException(string.Format(CSharpResources.ThisMethodCanOnlyBeUsedToCreateTokens, kind), "kind");
                }

                return(CreateMissing(kind, null, null));
            }

            return(TokensWithNoTrivia[(int)kind].Value);
        }
Exemple #9
0
        public static SyntaxToken Create(SyntaxKind kind)
        {
            if (kind > LastTokenWithWellKnownText)
            {
                if (!SyntaxFacts.IsAnyToken(kind))
                {
                    throw new ArgumentException(string.Format("@@ThisMethodCanOnlyBeUsedToCreateTokens", kind), nameof(kind));
                }

                return(CreateMissing(kind, null, null));
            }

            return(s_tokensWithNoTrivia[(int)kind]);
        }
Exemple #10
0
        protected SyntaxToken EatContextualToken(SyntaxKind kind, bool reportError = true)
        {
            Debug.Assert(SyntaxFacts.IsAnyToken(kind));

            var contextualKind = this.CurrentToken.ContextualKind;

            if (contextualKind != kind)
            {
                return(CreateMissingToken(kind, contextualKind, reportError));
            }
            else
            {
                return(ConvertToKeyword(this.EatToken()));
            }
        }
Exemple #11
0
        //this method is called very frequently
        //we should keep it simple so that it can be inlined.
        protected SyntaxToken EatToken(SyntaxKind kind)
        {
            Debug.Assert(SyntaxFacts.IsAnyToken(kind));

            var ct = this.CurrentToken;

            if (ct.Kind == kind)
            {
                MoveToNextToken();
                return(ct);
            }

            //slow part of EatToken(SyntaxKind kind)
            return(CreateMissingToken(kind, this.CurrentToken.Kind, reportError: true));
        }
Exemple #12
0
        // Consume a token if it is the right kind. Otherwise skip a token and replace it with one of the correct kind.
        protected SyntaxToken EatTokenAsKind(SyntaxKind expected)
        {
            Debug.Assert(SyntaxFacts.IsAnyToken(expected));

            var ct = this.CurrentToken;

            if (ct.Kind == expected)
            {
                MoveToNextToken();
                return(ct);
            }

            var replacement = CreateMissingToken(expected, this.CurrentToken.Kind, reportError: true);

            return(AddTrailingSkippedSyntax(replacement, this.EatToken()));
        }
Exemple #13
0
        protected SyntaxToken EatToken(SyntaxKind kind, bool reportError)
        {
            if (reportError)
            {
                return(EatToken(kind));
            }

            Debug.Assert(SyntaxFacts.IsAnyToken(kind));
            if (this.CurrentToken.Kind != kind)
            {
                // should we eat the current ParseToken's leading trivia?
                return(SyntaxFactory.MissingToken(kind));
            }
            else
            {
                return(this.EatToken());
            }
        }
Exemple #14
0
            public Cursor MoveToFirstToken()
            {
                var cursor = this;

                if (!cursor.IsFinished)
                {
                    for (
                        var node = cursor.CurrentNodeOrToken;
                        node.Kind() != SyntaxKind.None && !SyntaxFacts.IsAnyToken(node.Kind());
                        node = cursor.CurrentNodeOrToken
                        )
                    {
                        cursor = cursor.MoveToFirstChild();
                    }
                }

                return(cursor);
            }
Exemple #15
0
 public void TestIsAnyToken(SyntaxKind kind)
 {
     Assert.True(SyntaxFacts.IsAnyToken(kind));
 }