Exemplo n.º 1
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");
            }
        }