@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

October 21, 2011 comment , ,

Build Online C# Syntax Checker with Roslyn

On 19th Oct, Microsoft released first Community Technology Preview of the Roslyn Project. With the release of Roslyn Project, First time Microsoft is opening up the C# and Visual Basic compilers through Rich Roslyn API. More detail about Core API and others feature of October CTP released can be found here.

As far as the web development concerns, Possibilities are lot increased with Roslyn project. In future we might see web application for online syntax checker-formatter, online code compiler (Upload code and download dll / assembly!!!), and might even whole online IDE based on SaaS model!!!

Possibilities are endless. Here we will see how we can create online C# syntax checker with the help of Roslyn.Compilers.CSharp.DiagnosticList class.

NOTE: Please note that Roslyn Project is a still in technology preview and there are known issues and only a subset of the VB and C# languages has been implemented at this time.

Create new web application with Roslyn (See my post on how to setup .NET project with Roslyn).

In default.aspx add one textbox, button, and label as below. And on button click event write following code.

protected void btnCheck_Click(object sender, EventArgs e)
    string strDetail = "";
    Diagnostic obj;
    var tree = SyntaxTree.ParseCompilationUnit(txtSyntax.Text);
    if (tree.GetDiagnostics().Count == 0)
        strDetail += "Diagnostic test passed!!!";
    for (int i = 0; i < tree.GetDiagnostics().Count; i++)
        obj = tree.GetDiagnostics()[i];
        strDetail += "<b>" + (i + 1).ToString() + ". Info: </b>" + obj.Info.ToString();
        strDetail += " <b>Warning Level: </b>" + obj.Info.WarningLevel.ToString();
        strDetail += " <b>Severity Level: </b>" + obj.Info.Severity.ToString() + "<br/>";
        strDetail += " <b>Location: </b>" + obj.Location.Kind.ToString();
        strDetail += " <b>Character at: </b>" + obj.Location.GetLineSpan(true).StartLinePosition.Character.ToString();
        strDetail += " <b>On Line: </b>" + obj.Location.GetLineSpan(true).StartLinePosition.Line.ToString();
        strDetail += "</br>";                
    lblResult.Text = strDetail;

Code Explanation

DiagnosticList is a collection or list of Diagnostic class gives information which can be helpful to diagnosis any error if there is. Diagnostic.Info property gives various information which is helpful in diagnosis. For e.g. Error code, Warning Level, Severity Level, Message, etc. whereas Diagnostic.Location property give information which can be helpful to identify error location. For e.g. line no, character position, source file or metadata error and lot more.

Sample Output

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading