How to create a new language for use in Visual Studio

戏子无情 提交于 2019-12-17 06:22:04

问题


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#.


回答1:


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.

  • Boo Language: https://github.com/boo/boo-lang
  • Boo Syntax Highlighting for VS2010 (VSX add-in): http://vs2010boo.codeplex.com/
  • Boo Language Studio (syntax highlighting for VS2008): http://boolangstudio.codeplex.com/

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

  • Nice article about "classification" (syntax highligting) in VS 2010: http://dotneteers.net/blogs/divedeeper/archive/2008/11/04/LearnVSXNowPart38.aspx
  • Examples for VSX add-ins: http://blogs.msdn.com/vsxteam/archive/2009/06/17/new-editor-samples-for-visual-studio-2010-beta-1.aspx



回答2:


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

Visual Studio 2010 Extensibility, MPF and language services




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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".



来源:https://stackoverflow.com/questions/4283072/how-to-create-a-new-language-for-use-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!