Skip to main content

Bending Your Code Like Anders With C# Roslyn APIs - More About Syntax Trees

imageIn my previous post Introduction to C# Roslyn CTP, I gave a quick introduction about C# Roslyn CTP, and briefed how you could use C# as a Scripting Language using Roslyn. I also gave a quick introduction regarding parsing code using Roslyn APIs. Recommend you to read the same before starting this.

On a side note, you could also read few of my related C# vNext posts here.

In this post, we’ll re-look at few Roslyn features, based on the June 2012 CTP release – so that you can start leveraging Roslyn to write your own build tasks and pre processors. Also, note that you can compile the Syntax Debugger Visualizer sample that comes with the Roslyn CTP – Find it in the Shared folder in the CTP installation. Open the SyntaxDebuggerVisualizer project and compile the libraries, and place it in your Documents\<VisualStudioFolder>\Visualizers so that you can visualize syntax trees during debugging.

Update: Roslyn CTP September 2012 now has got released, and there are some breaking changes. I suggest you read this post about the new September 2012 CTP. 

Parsing Syntax Trees

So, let us start by parsing some code. Create a new Roslyn CTP Console project in Visual Studio, and try parsing this code. Note that in the new June 2012 CTP, Roslyn has added support for Query expressions, anonymous types, iterators, Indexers, switch statements etc. A Full list of features added since Oct 2011 CTP  is available here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using AForge.Genetic;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;

namespace Roslyn.Console
    class Program
        static void Main(string[] args)

            string code=@"class SimpleClass { 
                                public void SimpleMethod()
                                        var list = new List<string>();
                                        var result = from item in list
                                                     where item == ""first""
                                                     select item;

            var tree = SyntaxTree.ParseCompilationUnit(code);

Now, put a break point near the closing bracket of our Main method, and try bringing up the Syntax Visualizer, assuming have the Visualizer libraries copied as above.


And check out the Syntax Tree formed by Roslyn. The most interesting part is how the LINQ Expressions are parsed. So, this should provide a useful way to load Query Expressions dynamically (for implementing dynamic filters etc), once full version is available.




Walking The Syntax Tree

Now, let us quickly explore how you can walk the syntax tree. You can simply inherit your own SyntaxWalker in the Roslyn API, that internally implements the Visitor pattern for visiting all nodes in the tree. Let us write a quick ConsoleDumpWalker that can dump the above syntax tree to a console, and a quick extension method so that we can use the walker to dump any syntax tree to console.

public static class SyntaxTreeExtensions
        public static void Dump(this SyntaxTree tree)
            var writer = new ConsoleDumpWalker();

        class ConsoleDumpWalker : SyntaxWalker
            public override void Visit(SyntaxNode node)
                int padding = node.Ancestors().Count();
                //To identify leaf nodes vs nodes with children
                string prepend = node.ChildNodes().Count() > 0 ? "[-]" : "[.]";
                //Get the type of the node
                string line = new String(' ', padding) + prepend +
                                        " " + node.GetType().ToString();
                //Write the line

And now, you can try dumping your syntax tree with the above Dump extension method. For example, dumping the above code we parsed in step 1 should show the syntax tree we found earlier in the Debug syntax visualizer.
  var tree = SyntaxTree.ParseCompilationUnit(code);



Modifying the Syntax Tree

Modifying Syntax Trees are equally easy. You can implement your own syntax re-writers, by inheriting from the SyntaxRewriter class.  Here is a pretty na├»ve SyntaxRewriter, that appends an ‘I’ to the beginning of all interfaces that doesn’t start with an I in their name. In an actual scenario, you should also change the related implementations, but this is just for an example. So, here is our InterfaceRenameRewriter

//Our rewriter
public class InterfaceRenameRewriter : SyntaxRewriter

        public override SyntaxToken VisitToken(SyntaxToken token)
            //If the token is an identifier name, and it's 
           //parent is an interface declaration

            if (token.Kind == SyntaxKind.IdentifierToken && 
              //If the name doesn't start with I - bluntly fix it. 
               if (!token.GetText().StartsWith("I"))
                        return Syntax.Identifier("I" + token.GetText());

            return base.VisitToken(token);

And you can test our InterfaceRenameRewriter on some sample code. 
            string code = @"interface SomeInterface { 
                                //Some Simple Method
                                public void SomeMethod();

            var root = SyntaxTree.ParseCompilationUnit(code).GetRoot();
            var commentRewriter = new InterfaceRenameRewriter();
            var newRoot = commentRewriter.Visit(root);


And if you inspect newRoot, you’ll find that the interface name got renamed as intended. Here we go.


You can experiment with more overrides available in the SyntaxRewriter, as you understood how to modify syntax trees. Another way to modify syntax trees is directly replacing an old node in the root with a new node, but that is quite simple.

So, in this post we explored a bit about Roslyn Syntax Trees, and we saw how to start bending your code to write your own build tasks and pre-processors- Oh yes, all of us will take a little bit of time and practice to really bend it like Anders and Eric – but you just kicked the ball. Happy coding. Checkout my other C# posts as well Winking smile

Popular posts from this blog

MVVM - Binding Multiple Radio Buttons To a single Enum Property in WPF

I had a property in my View Model, of an Enum type, and wanted to bind multiple radio buttons to this.

Firstly, I wrote a simple Enum to Bool converter, like this.

public class EnumToBoolConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (parameter.Equals(value)) return true; else return false; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return parameter; } #endregion }

And my enumeration is like

public enum CompanyTypes { Type1Comp, Type2Comp, Type3Comp } Now, in my XAML, I provided the enumeration as the ConverterParameter, of the Converter we wrote earlier, like

Creating a quick Todo listing app on Windows using IIS7, Node.js and Mongodb

As I mentioned in my last post, more and more organizations are leaning towards Web Oriented Architecture (WOA) which are highly scalable. If you were exploring cool, scalable options to build highly performing web applications, you know what Node.js is for.After following the recent post from Scott Hanselman, I was up and running quickly with Node.js. In this post, I’ll explain step by step how I’ve setup Node.js and Mongodb to create a simple Todo listing application.Setting up Node.jsThis is what I’ve done.1 – Goto, scroll down and download node.exe for Windows, and place it in your c:\node folder2 – Goto IIS Node project in Git at, download the correct ‘retail’ link of IIS Node zip file (I downloaded the already built retail package, otherwise you can download and build from the source).3 – Extract the zip file some where, and run the install.bat or install_iisexpress.bat depending on your IIS Version. If you don’t have IIS in…

Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

Some time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain. So, here we go, the listing is not in any specific order.1 – IDesign C# Coding StandardsIDesign C# coding stand…