Roslyn Syntax API: First Look

When BUILD was going on, I blogged ROSLYN: Complier as Service?

Recently Microsoft released Roslyn CTP and you can download it from here. You can learn more here

In this post, I will focus on Roslyn Syntax API.

Syntax Tree is used by the compiler to understand the language constructs.

image

Syntax Tree exposed by Roslyn Syntax API is immutable. So to use it as a developer you can consume it on multiple threads without much thinking about concurrency measures and all.

Essentially; Roslyn Syntax API exposes below classes.

image

Let us now explore each of classes and structure exposed by Roslyn Syntax API one by one. Create a Roslyn Console Application. Once you install Roslyn CTP, you will get Roslyn project template installed. Choose Console Application from installed template.

image

Let us create a compilation unit called Animal. We will pass this class to create syntax tree and do the evaluation.

clip_image002

You can pass class Animal as string to SyntaxTree class

clip_image004

Have you noticed two times double quotes before Animal in print statement Smile

If you execute below statement you will find returned syntax tree is the compilation unit.

clip_image005

Variable tree is the instance of SyntaxTree class.

Expected Output

clip_image006

Typecast returned root to CompilationUnitSyntax and put a breakpoint to analyze more what Roslyn syntax API returns .

clip_image008

You will notice there are 0 counts in using directive since we are not having any using directive in the input text (Animal class) to the SyntaxTree ParseCompilation. In count of Member you will find 1.

clip_image010

To get the First member you need to fetch it as you are fetching usual array with the index value.

clip_image011

In our case we have only one member and that is Animal class. So first print statement will print the class and second statement will print the type that is Class Declaration.

Expected Output

clip_image012

Since kind of member is ClassDeclaration so typecast it to ClassDeclaration to evaluate it more.

clip_image014

And if you put breakpoint and execute it you will find there are Members, Keywords, and Identifier etc. for ClassDeclarationSynatx also. Count value for Members is 1. This value is 1 because there is one function in Animal class.

clip_image016

When you print kind of FirstMethod as below,

clip_image017

Expected Output

clip_image018

Put breakpoint on FirstMethod variable, you will find number of parameters count to 1. In Animal class , Display method has one input parameter.

clip_image020

To print input parameter you need to traverse the syntax tree as below

clip_image022

Expected output

clip_image023

In this way you can harness all the capability and features of Roslyn Syntax API to do reverse engineering on your code. For your reference all the source code discussed in this article is given below.


using System;
using Roslyn.Compilers.CSharp;

namespace ConsoleApplication30
{
class Program
{
static void Main(string[] args)
{

SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
@"class Animal
{
void Display(string animalName)
{
Console.WriteLine(""Animal name is "" + animalName);
}
}");
var root = (CompilationUnitSyntax) tree.Root;
var FirstMember = root.Members[0];
Console.WriteLine(FirstMember.ToString());
Console.WriteLine(FirstMember.Kind.ToString());
var FirstMemberAsClass = (ClassDeclarationSyntax)FirstMember;
var FirstMethod = (MethodDeclarationSyntax) FirstMemberAsClass.Members[0];
Console.WriteLine(FirstMethod.Kind.ToString());
var FirstMethodParameter = FirstMethod.ParameterList.Parameters[0];
Console.WriteLine(FirstMethodParameter.ToString());
Console.ReadKey(true);
}
}
}

On running the above code you will get consolidated output as below,

clip_image002[6]

I hope this post is useful to you. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

3 responses to “Roslyn Syntax API: First Look”

  1. […] previous post I discussed about Roslyn Syntax API: First Look […]

  2. Hey,
    Nice article 🙂
    But why you did not continue this post with the execution part? Getting the input from the console application, and executing the method of that class with this runtime parameter?

    Rajesh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com