How to create a new language for use in Visual Studio

C#Visual StudioParsingLanguageservice

C# Problem Overview


I want to write a new templating language, and I want Visual Studio to "support" it. What I need to know is:

  1. How do I parse my new language?
    Given some code in my new template language, how do I translate it into HTML? Right now I'm using regular expressions to parse it token by token, but I don't think this is going to scale very well as the language gets more complicated, and there's no error checking. I've heard of ANTLR but never used it. Would that be the right tool for this job, or is there perhaps something simpler? Ideally I'd like to send any syntax errors to the error window with as much information as possible (line #, type of error) like other languages do.
  2. How do I create a new file type for Visual Studio?
  3. How do I get syntax highlighting?
    Can I use the same parser I created in step 1, or is this something entirely different?
  4. How do I get Intellisense?

I'd prefer to write my parser in C#.

C# Solutions


Solution 1 - C#

I would take a look at another language that has already done the legwork of integrating with Visual Studio. A great example is Boo. The language and Visual Studio integration are open source. So you can take a look at exactly what they had to do.

The Boo Syntax Highlighting for VS2010 includes some recommended links on its homepage, which I'll copy for easy reference:

Solution 2 - C#

Regarding the Visual Studio aspects, what you need is a "language service", which is the entity that handles colorizing, intellisense, etc. for a given file extension/type.

For an intro, see this article
And for a code sample see here

Regarding parsing, there are lots of technologies, and I won't offer an opinion/advice.

Beware, there is a fair amount of work involved, although in my opinion it is much more straightforward in VS2010 than in previous versions of Visual Studio to provide this kind of extension.

See also

https://stackoverflow.com/questions/2604197/visual-studio-2010-extensibility-mpf-and-language-services

Solution 3 - C#

I wrote a VS Language Service using this article as my basis: http://www.codeproject.com/KB/recipes/VSLanguageService.aspx

It wasn't too bad if you have a basic handle on Grammars.

Solution 4 - C#

There is a sample in the VS SDK that shows most of the features you are looking for.

Solution 5 - C#

I found this very useful collection of recent samples for Visual Studio 2013 SDK: http://blogs.msdn.com/b/vsx/archive/2014/05/30/vs-2013-sdk-samples-released.aspx

It also contains the recent version of the OokLanguage which sounds promising.

We used ANTLR 4 to parse our language which works like a charm and allows direct interaction with C# code. Can totally recommend it.

Solution 6 - C#

I was using VS with own language and desperately needed a syntax highlight. I built mine based on this tutorial: https://mattduffield.wordpress.com/2012/07/31/writing-a-brightscript-syntax-highlight-extension-for-visual-studio-2010/

I know the tutorial is in VS2010. I made mine in VS2012 with no or very small hiccups. (also worked in VS2013) Recently I changed to VS2015 and the solution can be edited, built with no problem.

Solution 7 - C#

As mentioned in other answers, the most interesting code sample is the Ook language extension for the latest version of Visual Studio (2017 at the time of writing).

For VS 2015 see the sample in the VS2015 branch.

In order to install the SDK for 2015 or later, you need to rerun the VS setup. In 2015 it's called "Visual Studio Extensibility Tools Update 3".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - C#James KovacsView Answer on Stackoverflow
Solution 2 - C#BrianView Answer on Stackoverflow
Solution 3 - C#ColinCrenView Answer on Stackoverflow
Solution 4 - C#leppieView Answer on Stackoverflow
Solution 5 - C#Alexander PachaView Answer on Stackoverflow
Solution 6 - C#DDanView Answer on Stackoverflow
Solution 7 - C#Andreas HaferburgView Answer on Stackoverflow