Question

I need to have a textbox on a ASP.NET page in which a user would paste code and select one of the preset languages (C#, VB.NET, Python, Ruby etc) and I need to verify if the code compiles successfully. If it doesn't, then I need to show the errors and warnings with line numbers.

How do I go about doing this?

Was it helpful?

Solution

You'll need to host all sorts of compilers you'd like to support and use their APIs. If a compiler doesn't expose an API, you'd need to use System.Diagnostics.Process to launch it and grab the output for parsing.

This is far from a trivial task though.
If you're confident you need this, ask yourself several questions.

  • Which languages, platforms and languages do you plan to support? Python and Ruby, do you mean .NET versions of them or do you want to use their standard compilers? Which versions?
  • Do these compilers have any public APIs? Will you need to run them as executables?
  • If the site is going to be used concurrently by many users, how do you plan to distribute the load? Compiling is an intensive process and having a lot of people doing it will overload your server. Also note that compilation always takes time so you'll need to think how to present it to user. Do you want it to start immediately or do you want to put users in a queue?
  • What is the user coding scope? Is user input limited to one method contents, or does the user have to write the whole file? Is any code added by system by default? How do you resolve the references e.g. for C# code? Are some libraries referenced by default?

And this is just the tip of the iceberg.

Answering your comment, yes, there is a compiler API for C# and VB .NET which is called CodeDOM.
There's plenty of information about it on the net. You may want to check out this question as well.

OTHER TIPS

You may want to take a look at the Roslyn project (formerly Compiler as a Service) at http://msdn.microsoft.com/en-us/roslyn which was released yesterday. This is the new compiler APIs coming for C# and VB. It won't help for Python or Ruby but should do what you need for C# and VB once it ships (post Dev 11).

For C# and VB, using the Roslyn CTP to create a Compilation object, then call the GetDiagnostics() method to determine the errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top