Exemple #1
0
        private static void WriteUnaryExpression(BoundUnaryExpression node, IndentedTextWriter writer)
        {
            var precedence = SyntaxFacts.GetUnaryOperatorPrecedence(node.Op.SyntaxKind);

            writer.WritePunctuation(node.Op.SyntaxKind);
            writer.WriteNestedExpression(precedence, node.Operand);
        }
Exemple #2
0
        private ExpressionSyntax ParseUnaryExpression(SyntaxKind unaryExpression)
        {
            var operatorToken      = NextToken();
            var operatorPrecedence = SyntaxFacts.GetUnaryOperatorPrecedence(unaryExpression);
            var expression         = ParseSubExpression(null, operatorPrecedence);

            return(new UnaryExpressionSyntax(_syntaxTree, operatorToken, expression));
        }
Exemple #3
0
        public void Parse_UnaryExpression_HonorsPrecedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            //Arrange
            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 expression = ParseExpression(text);

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

                using (var e = new AssertingEnumerator(expression))
                {
                    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
            {
                /*
                 *     unary
                 |
                 |    binary
                 |    /   \
                 |   a     b
                 */
                using (var e = new AssertingEnumerator(expression))
                {
                    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");
                }
            }
        }
        public void Parser_UnaryExpression_honors_precedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecendence  = SyntaxFacts.GetUnaryOperatorPrecedence(unaryKind);
            var binaryPrecendence = SyntaxFacts.GetBinaryOperatorPrecedence(binaryKind);

            var unaryText  = SyntaxFacts.GetText(unaryKind);
            var binaryText = SyntaxFacts.GetText(binaryKind);
            var text       = $"{unaryText} a {binaryText} b";

            var expression = ParseExpression(text);

            if (unaryPrecendence >= binaryPrecendence)
            {
                //    binary
                //    /    \ 
                //  unary   b
                //    |
                //    a
                using (var e = new AssertingEnumerator(expression))
                {
                    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
            {
                //  unary
                //    |
                //  binary
                //  /   \
                // a     b

                using (var e = new AssertingEnumerator(expression))
                {
                    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");
                }
            }
        }
 private static void WriteNestedExpression(this IndentedTextWriter writer, int parentPrecedence, BoundExpression expression)
 {
     if (expression is BoundUnaryExpression unary)
     {
         writer.WriteNestedExpression(parentPrecedence, SyntaxFacts.GetUnaryOperatorPrecedence(unary.Operator.SyntaxKind), unary);
     }
     else if (expression is BoundBinaryExpression binary)
     {
         writer.WriteNestedExpression(parentPrecedence, SyntaxFacts.GetBinaryOperatorPrecedence(binary.Operator.SyntaxKind), binary);
     }
     else
     {
         expression.WriteTo(writer);
     }
 }
Exemple #6
0
        public void Parser_UnaryExpression_HonorsPrecedences(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precedence = SyntaxFacts.GetUnaryOperatorPrecedence(op1);
            var op2Precedence = SyntaxFacts.GetBinaryOperatorPrecedence(op2);
            var op1Text       = SyntaxFacts.GetText(op1);
            var op2Text       = SyntaxFacts.GetText(op2);
            var text          = $"{op1Text} b {op2Text} c";
            var expression    = ParseExpression(text);

            if (op1Precedence >= op2Precedence)
            {
                /*
                 * └───BinaryExpression
                 *  ├───UnaryExpression
                 *  │   ├───MinusToken
                 *  │   └───NameExpression
                 *  │       └───IndentifierToken
                 *  ├───StarToken
                 *  └───NameExpression
                 *      └───IndentifierToken
                 */
                using (var e = new AssertingEnumerator(expression))
                {
                    e.AssertNode(SyntaxKind.BinaryExpression);
                    e.AssertNode(SyntaxKind.UnaryExpression);
                    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.UnaryExpression);
                    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");
                }
            }
        }
        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 expression       = SyntaxTree.Parse(text).Root;



            if (unaryPrecedence >= binaryPrecedence)
            {
                using (var e = new AssertingEnumerator(expression))
                {
                    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(expression))
                {
                    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");
                }
            }
        }
Exemple #8
0
        public void Parser_UnaryExpression_HonoursPrecedences(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 _expression       = ParseExpression(_text);

            if (_unaryPrecedence >= _binaryPrecedence)
            {
                using (var _e = new AssertingEnumerator(_expression))
                {
                    _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(_expression))
                {
                    _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");
                }
            }
        }
Exemple #9
0
        public void ParserUnaryExpressionHonornsPrecedences(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            int              unaryPrecedence  = SyntaxFacts.GetUnaryOperatorPrecedence(unaryKind);
            int              binaryPrecedence = SyntaxFacts.GetBinaryOperatorPrecedence(binaryKind);
            string           unaryText        = SyntaxFacts.GetText(unaryKind);
            string           binaryText       = SyntaxFacts.GetText(binaryKind);
            string           text             = $"{unaryText} a {binaryText} b";
            ExpressionSyntax expression       = ParseExpression(text);

            if (unaryPrecedence >= binaryPrecedence)
            {
                using (var e = new AssertingEnumerator(expression))
                {
                    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(expression))
                {
                    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");
                }
            }
        }
Exemple #10
0
        protected override void RenderLine(string text)
        {
            var tokens = SyntaxTree.ParseTokens(text);

            foreach (var token in tokens)
            {
                var isKeyword    = token.Kind.ToString().EndsWith("Keyword");
                var isOperator   = SyntaxFacts.GetBinaryOperatorPrecedence(token.Kind) > 0 || SyntaxFacts.GetUnaryOperatorPrecedence(token.Kind) > 0;
                var isIdentifier = token.Kind == SyntaxKind.IdentifierToken;
                var isNumber     = token.Kind == SyntaxKind.NumberToken;
                var isString     = token.Kind == SyntaxKind.StringToken;

                if (isKeyword)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                }
                else if (isIdentifier)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                }
                else if (isOperator)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                }
                else if (isNumber)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                }
                else if (isString)
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                }
                Console.Write(token.Text);

                Console.ResetColor();
            }
        }