示例#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");
            }
        }
示例#2
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");
            }
        }