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.
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.
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.
Let us create a compilation unit called Animal. We will pass this class to create syntax tree and do the evaluation.
You can pass class Animal as string to SyntaxTree class
Have you noticed two times double quotes before Animal in print statement
If you execute below statement you will find returned syntax tree is the compilation unit.
Variable tree is the instance of SyntaxTree class.
Expected Output
Typecast returned root to CompilationUnitSyntax and put a breakpoint to analyze more what Roslyn syntax API returns .
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.
To get the First member you need to fetch it as you are fetching usual array with the index value.
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
Since kind of member is ClassDeclaration so typecast it to ClassDeclaration to evaluate it more.
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.
When you print kind of FirstMethod as below,
Expected Output
Put breakpoint on FirstMethod variable, you will find number of parameters count to 1. In Animal class , Display method has one input parameter.
To print input parameter you need to traverse the syntax tree as below
Expected output
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,
I hope this post is useful to you. Thanks for readingÂ
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.
Leave a Reply