Exemplo n.º 1
0
        private static IEnumerable <(SyntaxKind kind, string text)> GetTokens()
        {
            var fixedTokens = Enum.GetValues(typeof(SyntaxKind)).Cast <SyntaxKind>()
                              .Select(k => (kind: k, text: SyntaxInfo.GetText(k)))
                              .Where(t => t.text != null)
                              .Cast <(SyntaxKind, string)>();


            var dynamicTokens = new[]
Exemplo n.º 2
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");
            }
        }
Exemplo n.º 3
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");
            }
        }
Exemplo n.º 4
0
        public void SyntaxFact_GetText_RoundTrips(SyntaxKind kind)
        {
            var text = SyntaxInfo.GetText(kind);

            if (text == null)
            {
                return;
            }

            var tokens = SyntaxTree.ParseTokens(text);
            var token  = Assert.Single(tokens);

            Assert.Equal(kind, token.Kind);
            Assert.Equal(text, token.Text);
        }