Arash's Personal blog

NRefactory : Front-end parser for C#

September 22, 2013 | 1 Minute Read

NRefactroy is an open source library for parsing C# source codes, or more precisely a front-end parser for C#. It can parse your C# codes and provide you with an abstract syntax tree. It can also resolve this parse tree and give you semantic of nodes in the syntax tree.

What makes it more interesting is that the syntax tree produced by this framework is mutable, which means you can change it’s node’s contents; unlike other products, such as not-yet-stable Microsoft® “Roslyn” CTP.

You can use this library to change your source code, analyze it, or reformat it (pretty print);NRefactory project is actually a part of bigger project, SharpDevelop : A free IDE for C#. SharpDevelop takes advantage of this library for IntelliSense implementation and some other stuff.

At the beginning, it may take a while till you become comfortable with representations, APIs and how to interpret and manipulate data structures. To start, take a look this article, wrote by one the authors of NRefactory. In this prologue you can find all preliminary information you need to know in order to dive in. After that IMHO best practice is to fool around huge unit tests that exist in project itself. It’ll be your best guide through all difficulties and misunderstandings ( there’s about 3000 of theme! ).

I also wrote the simplest possible program with this library that you can download from here. The purpose of this program is only to show you how to glue all the things together and make it work. Enjoy Parsing.