Beispiel #1
0
        public void Parser_BinaryExpression_HonorsPrecedences(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precedence = SyntaxInfo.GetBinaryOperatorPrecedence(op1);
            var op2Precedence = SyntaxInfo.GetBinaryOperatorPrecedence(op2);
            var op1Text       = SyntaxInfo.GetText(op1);
            var op2Text       = SyntaxInfo.GetText(op2);

            Debug.Assert(op1Text != null);
            Debug.Assert(op2Text != null);

            var text       = $"a {op1Text} b {op2Text} c";
            var expression = ParseExpression(text);

            using var e = new AssertingEnumerator(expression);

            if (op1Precedence >= op2Precedence)
            {
                //     op2
                //    /   \
                //   op1   c
                //  /   \
                // a     b


                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "a");
                e.AssertToken(op1, op1Text);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "b");
                e.AssertToken(op2, op2Text);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "c");
            }
            else
            {
                //   op1
                //  /   \
                // a    op2
                //     /   \
                //    b     c

                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "a");
                e.AssertToken(op1, op1Text);
                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "b");
                e.AssertToken(op2, op2Text);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "c");
            }
        }
Beispiel #2
0
        public void HonorPrecedences_GivenBinaryExpression(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precedence    = op1.GetBinaryOperatorPrecedence();
            var op2Precedence    = op2.GetBinaryOperatorPrecedence();
            var op1Text          = op1.GetText();
            var op2Text          = op2.GetText();
            var text             = $"a {op1Text} b {op2Text} c";
            var expressionSyntax = ParseExpression(text);

            if (op1Precedence >= op2Precedence)
            {
                //      op2
                //     /   \
                //   op1    c
                //  /   \
                // a     b
                using (var asserter = new AssertingEnumerator(expressionSyntax))
                {
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "a");
                    asserter.AssertToken(op1, op1Text);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "b");
                    asserter.AssertToken(op2, op2Text);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "c");
                }
            }
            else
            {
                //   op1
                //  /   \
                // a    op2
                //     /   \
                //    b     c

                using (var asserter = new AssertingEnumerator(expressionSyntax))
                {
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "a");
                    asserter.AssertToken(op1, op1Text);
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "b");
                    asserter.AssertToken(op2, op2Text);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "c");
                }
            }
        }
Beispiel #3
0
        public void HonorPrecedences_GivenUnaryExpression(SyntaxKind unaaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecedence  = unaaryKind.GetUnaryOperatorPrecedence();
            var binaryPrecedence = binaryKind.GetBinaryOperatorPrecedence();
            var unaryText        = unaaryKind.GetText();
            var binaryText       = binaryKind.GetText();
            var text             = $"{unaryText} a {binaryText} b";
            var expressionSyntax = ParseExpression(text);

            if (unaryPrecedence >= binaryPrecedence)
            {
                //   op2
                //  /   \
                // op1   b
                //  |
                //  a

                using (var asserter = new AssertingEnumerator(expressionSyntax))
                {
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertNode(SyntaxKind.UnaryExpression);
                    asserter.AssertToken(unaaryKind, unaryText);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "a");
                    asserter.AssertToken(binaryKind, binaryText);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "b");
                }
            }
            else
            {
                //   op2
                //    |
                //   op1
                //  /   \
                // a     b

                using (var asserter = new AssertingEnumerator(expressionSyntax))
                {
                    asserter.AssertNode(SyntaxKind.UnaryExpression);
                    asserter.AssertNode(SyntaxKind.BinaryExpression);
                    asserter.AssertToken(unaaryKind, unaryText);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "a");
                    asserter.AssertToken(binaryKind, binaryText);
                    asserter.AssertNode(SyntaxKind.NameExpression);
                    asserter.AssertToken(SyntaxKind.IdentifierToken, "b");
                }
            }
        }
Beispiel #4
0
    public void ParseNestedNonUnitBlockExpression()
    {
        var text = AnnotatedText.Parse(
            @"
                         {
                            val x = 5
                            5
                         }"
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        //└──ExpressionStatement
        //    ├──BlockExpression
        //    │   ├──OpenBraceToken
        //    │   ├──BlockExpression
        //    │   │   ├──OpenBraceToken
        //    │   │   ├──UnitExpression
        //    │   │   │   ├──CloseBraceToken
        //    │   │   │   └──CloseBraceToken
        //    │   │   └──CloseBraceToken
        //    │   └──CloseBraceToken
        //    └──NewLineToken

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertNode(SyntaxKind.VariableDeclarationStatement);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "   ");
        e.AssertToken(SyntaxKind.ValKeyword, "val");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.Initializer);
        e.AssertToken(SyntaxKind.EqualsToken, "=");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.NumberToken, "5");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);

        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "   ");
        e.AssertToken(SyntaxKind.NumberToken, "5");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #5
0
    public void ParseZeroString()
    {
        var text = AnnotatedText.Parse("\"\0\"");
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.StringToken, "\"\0\"");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #6
0
    public void ParseForExpressionWithLineBreaks()
    {
        var text = AnnotatedText.Parse(
            @"
                for (x <- 0 to 5)
                    x
            "
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        //└──CompilationUnit
        //    ├──ExpressionStatement
        //    │   ├──ForExpression
        //    │   │   ├──ForKeyword
        //    │   │   ├──OpenParenToken
        //    │   │   ├──NameExpression
        //    │   │   │   └──IdentifierToken
        //    │   │   ├──LessThanDashToken
        //    │   │   ├──LiteralExpression
        //    │   │   │   └──NumberToken 0
        //    │   │   ├──ToKeyword
        //    │   │   ├──LiteralExpression
        //    │   │   │   └──NumberToken 5
        //    │   │   ├──CloseParenToken
        //    │   │   └──NameExpression
        //    │   │       └──IdentifierToken
        //    │   └──NewLineToken
        //    └──EndOfInputToken
        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.ForExpression);
        e.AssertToken(SyntaxKind.ForKeyword, "for");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.OpenParenToken, "(");
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.LessThanDashToken, "<-");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.NumberToken, "0");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.ToKeyword, "to");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.NumberToken, "5");
        e.AssertToken(SyntaxKind.CloseParenToken, ")");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "    ");
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #7
0
    public void ParseBinaryExpressionWithLineBreakInsideGroup()
    {
        var text = AnnotatedText.Parse(
            @"
                (false
                    || (false
                           || true))
            "
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.GroupExpression);
        e.AssertToken(SyntaxKind.OpenParenToken, "(");
        e.AssertNode(SyntaxKind.BinaryExpression);
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.FalseKeyword, "false");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "    ");
        e.AssertToken(SyntaxKind.PipePipeToken, "||");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.GroupExpression);
        e.AssertToken(SyntaxKind.OpenParenToken, "(");
        e.AssertNode(SyntaxKind.BinaryExpression);
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.FalseKeyword, "false");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "           ");
        e.AssertToken(SyntaxKind.PipePipeToken, "||");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.TrueKeyword, "true");
        e.AssertToken(SyntaxKind.CloseParenToken, ")");
        e.AssertToken(SyntaxKind.CloseParenToken, ")");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #8
0
    public void ParseUnaryExpressionAfterLineBreak()
    {
        var text = AnnotatedText.Parse(
            @"
                {
                    val x = 3
                    -x
                }
            "
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertNode(SyntaxKind.VariableDeclarationStatement);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "    ");
        e.AssertToken(SyntaxKind.ValKeyword, "val");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.Initializer);
        e.AssertToken(SyntaxKind.EqualsToken, "=");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.NumberToken, "3");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);

        e.AssertNode(SyntaxKind.UnaryExpression);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "    ");
        e.AssertToken(SyntaxKind.DashToken, "-");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #9
0
    public void ParseNestedBlockExpression()
    {
        var text       = "{{}}";
        var expression = SyntaxTree.Parse(text).Root;

        using var e = new AssertingEnumerator(expression);

        //└──ExpressionStatement
        //    ├──BlockExpression
        //    │   ├──OpenBraceToken
        //    │   ├──BlockExpression
        //    │   │   ├──OpenBraceToken
        //    │   │   ├──UnitExpression
        //    │   │   │   ├──CloseBraceToken
        //    │   │   │   └──CloseBraceToken
        //    │   │   └──CloseBraceToken
        //    │   └──CloseBraceToken
        //    └──NewLineToken

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertNode(SyntaxKind.UnitExpression);
        e.AssertToken(SyntaxKind.OpenParenToken, "(");
        e.AssertToken(SyntaxKind.CloseParenToken, ")");
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #10
0
        public void Parser_UnaryExpression_HonorsPrecedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecedence  = SyntaxInfo.GetUnaryOperatorPrecedence(unaryKind);
            var binaryPrecedence = SyntaxInfo.GetBinaryOperatorPrecedence(binaryKind);
            var unaryText        = SyntaxInfo.GetText(unaryKind);
            var binaryText       = SyntaxInfo.GetText(binaryKind);

            Debug.Assert(unaryText != null);
            Debug.Assert(binaryText != null);

            var text       = $"{unaryText} a {binaryText} b";
            var expression = ParseExpression(text);

            using var e = new AssertingEnumerator(expression);

            if (unaryPrecedence >= binaryPrecedence)
            {
                //   binary
                //   /    \
                // unary   b
                //   |
                //   a

                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.UnaryExpression);
                e.AssertToken(unaryKind, unaryText);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "a");
                e.AssertToken(binaryKind, binaryText);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "b");
            }
            else
            {
                //  unary
                //    |
                //  binary
                //  /   \
                // a     b

                e.AssertNode(SyntaxKind.UnaryExpression);
                e.AssertToken(unaryKind, unaryText);
                e.AssertNode(SyntaxKind.BinaryExpression);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "a");
                e.AssertToken(binaryKind, binaryText);
                e.AssertNode(SyntaxKind.NameExpression);
                e.AssertToken(SyntaxKind.Identifier, "b");
            }
        }
Beispiel #11
0
    public void ParseSingleLineBlockExpression()
    {
        var text = @"{ x }";
        var tree = SyntaxTree.Parse(text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "x");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #12
0
    public void ParseLineCommentOnly()
    {
        var text = @"// taco";
        var tree = SyntaxTree.Parse(text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertTrivia(SyntaxKind.LineCommentTrivia, "// taco");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #13
0
    public void UnaryExpressionHonorsPrecedences(
        UnaryOperatorSyntaxKind op1,
        BinaryOperatorSyntaxKind op2
        )
    {
        var unaryText  = SyntaxFacts.GetText(op1.Kind);
        var binaryText = SyntaxFacts.GetText(op2.Kind);

        Debug.Assert(unaryText != null);
        Debug.Assert(binaryText != null);

        var text       = $"{unaryText} a {binaryText} b";
        var expression = SyntaxTree.Parse(text).Root;

        using var e = new AssertingEnumerator(expression);

        // └──BinaryExpression
        //     ├──UnaryExpression
        //     │   ├──MinusToken
        //     │   └──NameExpression
        //     │       └──IdentifierToken
        //     ├──PlusToken
        //     └──NameExpression
        //         └──IdentifierToken

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BinaryExpression);
        e.AssertNode(SyntaxKind.UnaryExpression);
        e.AssertToken(op1.Kind, unaryText);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "a");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(op2.Kind, binaryText);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "b");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #14
0
    public void ParseWhileExpression()
    {
        var text = AnnotatedText.Parse(
            @"
                          {
                            while ( true ) 1
                          }"
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);
        e.AssertNode(SyntaxKind.GlobalStatement);
        e.AssertNode(SyntaxKind.ExpressionStatement);
        e.AssertNode(SyntaxKind.BlockExpression);
        e.AssertToken(SyntaxKind.OpenBraceToken, "{");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertNode(SyntaxKind.WhileExpression);
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, "  ");
        e.AssertToken(SyntaxKind.WhileKeyword, "while");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.OpenParenToken, "(");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.TrueKeyword, "true");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertToken(SyntaxKind.CloseParenToken, ")");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
        e.AssertNode(SyntaxKind.LiteralExpression);
        e.AssertToken(SyntaxKind.NumberToken, "1");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);
        e.AssertToken(SyntaxKind.CloseBraceToken, "}");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #15
0
    public void ParseUsingDirectives()
    {
        var text = AnnotatedText.Parse(
            @"
                using System
                using System.Collections.Immutable
                using System.Diagnostics
            "
            );
        var tree = SyntaxTree.Parse(text.Text);

        Assert.Empty(tree.Diagnostics);

        var expression = tree.Root;

        using var e = new AssertingEnumerator(expression);

        e.AssertNode(SyntaxKind.CompilationUnit);

        e.AssertNode(SyntaxKind.UsingDirective);
        e.AssertToken(SyntaxKind.UsingKeyword, "using");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia);
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "System");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);

        e.AssertNode(SyntaxKind.UsingDirective);
        e.AssertToken(SyntaxKind.UsingKeyword, "using");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia);
        e.AssertNode(SyntaxKind.QualifiedName);
        e.AssertNode(SyntaxKind.QualifiedName);
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "System");
        e.AssertToken(SyntaxKind.DotToken, ".");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "Collections");
        e.AssertToken(SyntaxKind.DotToken, ".");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "Immutable");
        e.AssertTrivia(SyntaxKind.EndOfLineTrivia);

        e.AssertNode(SyntaxKind.UsingDirective);
        e.AssertToken(SyntaxKind.UsingKeyword, "using");
        e.AssertTrivia(SyntaxKind.WhitespaceTrivia);
        e.AssertNode(SyntaxKind.QualifiedName);
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "System");
        e.AssertToken(SyntaxKind.DotToken, ".");
        e.AssertNode(SyntaxKind.IdentifierName);
        e.AssertToken(SyntaxKind.IdentifierToken, "Diagnostics");
        e.AssertToken(SyntaxKind.EndOfInputToken, "");
    }
Beispiel #16
0
    public void BinaryExpressionHonorsPrecedences(
        BinaryOperatorSyntaxKind op1,
        BinaryOperatorSyntaxKind op2
        )
    {
        var op1Precedence = op1.Kind.GetBinaryOperatorPrecedence();
        var op2Precedence = op2.Kind.GetBinaryOperatorPrecedence();

        var op1Text = SyntaxFacts.GetText(op1.Kind) ?? throw new Exception("Invalid operator");
        var op2Text = SyntaxFacts.GetText(op2.Kind) ?? throw new Exception("Invalid operator");

        var text       = $"a {op1Text} b {op2Text} c";
        var expression = SyntaxTree.Parse(text).Root;

        if (op1Precedence >= op2Precedence)
        {
            using var e = new AssertingEnumerator(expression);
            // └──BinaryExpression
            //    ├──BinaryExpression
            //    │   ├──NameExpression
            //    │   │   └──IdentifierToken
            //    │   ├──PlusToken
            //    │   └──NameExpression
            //    │       └──IdentifierToken
            //    ├──PlusToken
            //    └──NameExpression
            //        └──IdentifierToken
            e.AssertNode(SyntaxKind.CompilationUnit);
            e.AssertNode(SyntaxKind.GlobalStatement);
            e.AssertNode(SyntaxKind.ExpressionStatement);
            e.AssertNode(SyntaxKind.BinaryExpression);
            e.AssertNode(SyntaxKind.BinaryExpression);
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "a");
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertToken(op1.Kind, op1Text);
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "b");
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertToken(op2.Kind, op2Text);
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "c");
            e.AssertToken(SyntaxKind.EndOfInputToken, "");
        }
        else
        {
            using var e = new AssertingEnumerator(expression);
            // └──BinaryExpression
            //    ├──NameExpression
            //    │   └──IdentifierToken
            //    ├──PlusToken
            //    └──BinaryExpression
            //       ├──NameExpression
            //       │   └──IdentifierToken
            //       ├──StarToken
            //       └──NameExpression
            //          └──IdentifierToken

            e.AssertNode(SyntaxKind.CompilationUnit);
            e.AssertNode(SyntaxKind.GlobalStatement);
            e.AssertNode(SyntaxKind.ExpressionStatement);
            e.AssertNode(SyntaxKind.BinaryExpression);
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "a");
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertToken(op1.Kind, op1Text);
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertNode(SyntaxKind.BinaryExpression);
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "b");
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertToken(op2.Kind, op2Text);
            e.AssertTrivia(SyntaxKind.WhitespaceTrivia, " ");
            e.AssertNode(SyntaxKind.IdentifierName);
            e.AssertToken(SyntaxKind.IdentifierToken, "c");
            e.AssertToken(SyntaxKind.EndOfInputToken, "");
        }
    }