Roslyn CTP–A Walk Through The Syntax Tree–Part I

Posted: November 20, 2011 in .NET, C#
Tags: , ,

The Roslyn project aims to expose the functionalities of C#/VB complier as API/Services. The Roslyn CTP was released this October.One of the important components of Roslyn is the Compiler API. This exposes an object model which provides access to information generated at the different stages of compilation.The first phase of any compilation process involves parsing and generating the syntax tree. In this post we will discuss about the basics of the Syntax Tree API.

The major components of a syntax tree are shown below:

roslyn

A sample node in a code fragment is shown in the figure below:

roslyn1

In this post we will develop a small sample syntax tree walker to start with. But before that let’s take a quick look how the Roslyn Syntax API is structured for SyntaxNodes.

RoslynUML

We will develop a  syntax walker which will traverse the different declaration syntax nodes and print their text contents.

static void Main(string[] args) 
{ 
     SyntaxTree tree = SyntaxTree.ParseCompilationUnit( 
                 @" 
                 using System; 
                 using System.Collections; 
                 using System.Linq; 
                 using System.Text; 
  
                 namespace HelloWorld 
                 { 
                     class Program 
                     { 
                         
                         static void Main(string[] args) 
                         { 
                             var a = 10; 
                             Console.WriteLine(""Hello, World!""); 
                         } 
                         public int i {get;set;} 
                         var i = 11; 
                     } 
                 }"); 
    
     var root = (CompilationUnitSyntax)tree.Root; 
     foreach (var n in root.Members) 
     { 
         if (n is NamespaceDeclarationSyntax) 
         { 
             foreach (var t in (n as NamespaceDeclarationSyntax).Members) 
             { 
                 if (t is ClassDeclarationSyntax) 
                 { 
                     foreach (var m in (t as ClassDeclarationSyntax).Members) 
                     { 
                         if (m is PropertyDeclarationSyntax) 
                         { 
                             Console.WriteLine("Property::" + m.GetText()); 
                         } 
                         if (m is MethodDeclarationSyntax) 
                         { 
                             Console.WriteLine("Method::" + m.GetText()); 
                         } 
                         if (m is FieldDeclarationSyntax) 
                         { 
                             Console.WriteLine("Variable::" + m.GetText()); 
                         }  
                     } 
                 } 
             } 
         } 
    } 
     Console.Read();

} 


The programs does the following:

  • Invokes SyntaxTree.ParseCompilationUnit method with source code as input to generate SyntaxTree.
  • Loops through all the members of the root node
  • Checks the type of each member to determine the type of declaration syntax node and prints the contained text of that node.

The output is as shown below:

image

 

Now we will take a look into the different tokens of the method declaration. Every method has modifiers, return type, identifier and parameter list as shown below:

roslyn

The code below shown how Roslyn Syntax API can be used to explore and walkthrough the same:

if (m is MethodDeclarationSyntax) 
{ 
    var meth = (m as MethodDeclarationSyntax); 
    foreach (var mt in (m as MethodDeclarationSyntax).Modifiers) 
    { 
        Console.WriteLine("Modifier::" + mt.GetText()); 
    } 
    Console.WriteLine("Identifier::" + meth.ReturnType); 
    Console.WriteLine("Identifier::" + meth.Identifier); 
    foreach (var p in meth.ParameterList.Parameters) 
    { 
        Console.WriteLine("Parameter Type::" + p.TypeOpt.PlainName + "::" + p.TypeOpt.Kind ); 
        Console.WriteLine("Parameter Identifier::" + p.Identifier); 
    }

} 

The output now changes to:

image

In the next post we will how we can use Syntax API in source code analysis and checking.

About these ads
Comments
  1. Roslyn CTP–A Walk Through The Syntax Tree–Part I « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. [...] the last post related to Roslyn we developed a very basic syntax walker. In this post we will see how we can use [...]

  3. [...] The Roslyn project aims to expose the functionalities of C#/VB complier as API/Services. The Roslyn CTP was released this October.One of the important components of Roslyn is the Compiler API. This exposes an object model which provides access to information generated at the different stages of compilation.The first phase of any compilation process involves parsing and generating the syntax tree. In this post we will discuss about the basics of the Syntax Tree API. The major components of a syntax tree …    .NET, C# Read the original post on DotNetShoutout… [...]

  4. [...] Roslyn CTP – A Walk Through The Syntax Tree – Part I & Part II – Sankarsan takes a look at the Roslyn CTP release, exploring the compiler API and exploring the use of syntax trees to allow you to explore code programmatically, looking at an example to show how you can use it for code analysis by looking for uses of singletons in your code. [...]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s