Example #1
0
        public void Parser_BinaryExpression_honors_precedences(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precendence = SyntaxFacts.GetBinaryOperatorPrecedence(op1);
            var op2Precendence = SyntaxFacts.GetBinaryOperatorPrecedence(op2);

            var op1Text = SyntaxFacts.GetText(op1);
            var op2Text = SyntaxFacts.GetText(op2);
            var text    = $"a {op1Text} b {op2Text} c";

            var expression = ParseExpression(text);

            if (op1Precendence >= op2Precendence)
            {
                //      op2
                //     /  \
                //   op1   c
                //  /   \
                // a     b
                using (var e = new AssertingEnumerator(expression))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(op1, op1Text);

                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                    e.AssertToken(op2, op2Text);

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

                using (var e = new AssertingEnumerator(expression))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(op1, op1Text);

                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                    e.AssertToken(op2, op2Text);

                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "c");
                }
            }
        }
Example #2
0
        public void Parse_UnaryExpression_HonorsPrecedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecedence  = unaryKind.GetUnaryOperatorPrecedence();
            var binaryPrecedence = binaryKind.GetBinaryOperatorPrecedence();
            var unaryText        = SyntaxFacts.GetText(unaryKind);
            var binaryText       = SyntaxFacts.GetText(binaryKind);
            var text             = $"{unaryText} a {binaryText} b";
            var expression       = ParseExpression(text);

            if (unaryPrecedence >= binaryPrecedence)
            {
                //     binary
                //     /   \ 
                //  unary   b
                //    |
                //    a
                using (var enumerator = new AssertingEnumerator(expression))
                {
                    enumerator.AssertNode(SyntaxKind.BinaryExpression);

                    enumerator.AssertNode(SyntaxKind.UnaryExpression);
                    enumerator.AssertToken(unaryKind, unaryText);

                    enumerator.AssertNode(SyntaxKind.NameExpression);
                    enumerator.AssertToken(SyntaxKind.Identifier, "a");

                    enumerator.AssertToken(binaryKind, binaryText);

                    enumerator.AssertNode(SyntaxKind.NameExpression);
                    enumerator.AssertToken(SyntaxKind.Identifier, "b");
                }
            }
            else
            {
                //  unary
                //    |
                //  binary
                //  /   \
                // a     b
                using (var enumerator = new AssertingEnumerator(expression))
                {
                    enumerator.AssertNode(SyntaxKind.UnaryExpression);
                    enumerator.AssertToken(unaryKind, unaryText);

                    enumerator.AssertNode(SyntaxKind.BinaryExpression);

                    enumerator.AssertNode(SyntaxKind.NameExpression);
                    enumerator.AssertToken(SyntaxKind.Identifier, "a");

                    enumerator.AssertToken(binaryKind, binaryText);

                    enumerator.AssertNode(SyntaxKind.NameExpression);
                    enumerator.AssertToken(SyntaxKind.Identifier, "b");
                }
            }
        }
Example #3
0
        public void Parser_BinaryExpression_HonorsPrecedences(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precedence = SyntaxFacts.GetBinaryOperatorPrecedence(op1);
            var op2Precedence = SyntaxFacts.GetBinaryOperatorPrecedence(op2);
            var op1Text       = SyntaxFacts.GetText(op1);
            var op2Text       = SyntaxFacts.GetText(op2);


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

            if (op1Precedence >= op2Precedence)
            {
                using (var e = new AssertingEnumerator(expression))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(op1, op1Text);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                    e.AssertToken(op2, op2Text);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "c");
                }
            }
            else
            {
                using (var e = new AssertingEnumerator(expression))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(op1, op1Text);
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                    e.AssertToken(op2, op2Text);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "c");
                }
            }
        }
Example #4
0
        public void Parser_UnaryExpression_HonorsPrecedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecedence  = SyntaxFacts.GetUnaryOperatorPrecedence(unaryKind);
            var binaryPrecedence = SyntaxFacts.GetBinaryOperatorPrecedence(binaryKind);
            var unaryText        = SyntaxFacts.GetText(unaryKind);
            var binaryText       = SyntaxFacts.GetText(binaryKind);
            var text             = $"{unaryText} a {binaryText} b";
            var expr             = ParseExpression(text);

            if (unaryPrecedence >= binaryPrecedence)
            {
                using (var e = new AssertingEnumerator(expr))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.UnaryExpression);
                    e.AssertToken(unaryKind, unaryText);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(binaryKind, binaryText);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                }
            }
            else
            {
                using (var e = new AssertingEnumerator(expr))
                {
                    e.AssertNode(SyntaxKind.UnaryExpression);
                    e.AssertToken(unaryKind, unaryText);
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "a");
                    e.AssertToken(binaryKind, binaryText);

                    e.AssertNode(SyntaxKind.NameExpression);
                    e.AssertToken(SyntaxKind.IdentifierToken, "b");
                }
            }
        }