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

            var dynamicTokens = new []
Example #2
0
        public void Parser_BinaryExpression_HonorsPrecedence(SyntaxKind op1, SyntaxKind op2)
        {
            var op1Precedence = SyntaxRules.GetBinaryOperatorPrecedence(op1);
            var op2Precedence = SyntaxRules.GetBinaryOperatorPrecedence(op2);
            var op1Text       = SyntaxRules.GetText(op1);
            var op2Text       = SyntaxRules.GetText(op2);
            var text          = $"a {op1Text} b {op2Text} c";

            var expression = ParseExpression(text);

            if (op1Precedence >= op2Precedence)
            {
                //    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 #3
0
        public void Parser_UnaryExpression_HonorsPrecedence(SyntaxKind unaryKind, SyntaxKind binaryKind)
        {
            var unaryPrecedence  = SyntaxRules.GetUnaryOperatorPrecedence(unaryKind);
            var binaryPrecedence = SyntaxRules.GetBinaryOperatorPrecedence(binaryKind);
            var unaryText        = SyntaxRules.GetText(unaryKind);
            var binaryText       = SyntaxRules.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");
                }
            }
        }
Example #4
0
        public void SyntaxRule_GetText_RoundTrips(SyntaxKind kind)
        {
            var text = SyntaxRules.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);
        }
Example #5
0
        public void GetSyntaxRuleText(SyntaxKind kind)
        {
            foreach (var val in GetSyntaxKindData())
            {
                var text = SyntaxRules.GetText(kind);

                if (text == null)
                {
                    return;
                }

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

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