Compile-time extensions in C# - c#

One of the possibilities demoed when the C# dev team was talking about porting the C# compiler to C# was the option to extend the compiler phase with custom plugins, written in C#.
Was that option included in the Visual Studio 2015 release, and if so, where can I find the MSDN page for it?

Related

How to add a custom language REPL to Visual Studio

Visual Studio 2017 (and 2015) now features a REPL for interactive C#, F#, R, Python and possibly others. View -> Other Windows -> and whatever is installed should be visible.
Suppose I wanted to integrate my own (existing domain-specific) language into Visual Studio such that its REPL would be started by clicking on a menu item in Other Windows. I am only interested in using Visual Studio's built-in REPL services to conduct a dialogue with an existing language - not to design a language nor to use any of the DSM facilities provided by .Net or Microsoft. At least today.
Where would I begin? Would the Interactive R implementation be a suitable starting point? (The R interpreter would resemble my DSM more closely than say C#)
The appeal here is Visual Studio integration and the ability to reuse existing components.
Based on limited information on your DSL it is necessary to make assumption which equally well may be treated as requirements:
There is a compiler for DSL which is capable to produce partial syntax trees and compiles them to partial binaries, 'method bodies' or there is a compiler based expression evaluator.
There is a runtime on which DSL can be executed even as partial binaries (if the result of a compilation is an intermediate language then you would need virtual machine otherwise system specific runtime library).
There is a lot of work to do the plumbing between input/output interactive window, runtime, compiler between themselves and Visual Studio.
There are several examples which can be analyzed for prior art:
Roslyn Compiler
Scripting https://github.com/dotnet/roslyn/tree/master/src/Scripting
Interactive Services https://github.com/dotnet/roslyn/tree/master/src/VisualStudio/InteractiveServices
One which could be of direct use in REPL project is:
ReplHTML project https://github.com/TiarkRompf/replhtml
There are many toolkits available for fast development of a DSL or in principle for development of any programming language. Check some papers by Tiark Rompf as he was and is working on cross language virtual machines and efficient DSL/programming language development based on Graal VM. He is currently at Purdue University but before that he was working with Martin Odersky at EPFL in Switzeralnd on Scala, programming language development frameworks and their integration with virtual machines.
I would discourage you in using anything R as this is one of the examples from which one should not learn how to work with or develop programming languages.
Final warning: this is a big project which may require much more than one engineer year to complete successfully.

Know Which compiler is used in visual studio 2015? [duplicate]

I've a bit of confusion about roslyn.
What I have done:
I've installed vs 2015 community edition and download in extensibilty > download compiler platform sdk.
So I created a simple console application: hello world example.
Well now I'm expect to choise the c# compiler between the vs2015 default one and roslyn..., but I've not found such option.
So my first question is: how to select version of c# compiler?
Second I've downloaded master-roslyn and I build, then I found csc.exe, well the odd things is that if I lauch the exe
I get c# compiler version 42.42.42.42. ???? Right?
Then I've follow some tutorials, but all purpose me:
to load a source from text file or string vars and analyze or change syntax tree, then compile to var.
Well at this point I'm confused... So:
What is roslyn exactly? A meta compiler? This mean that I can change my code at runtime just like Reflection?
Second: how can compile with vs2015 with default csc or choose roslyn?
third: If I build a custom version of roslyn How can I compile my source using Vs2015 ?
Which know if csc.exe is roslyn? No help or command line print the codename.
Thanks
So it looks like you've got a few questions:
What is Roslyn?
Roslyn is the new default compiler inside of Visual Studio 2015. If you're building and running applications within Visual Studio 2015, they're being compiled with the Roslyn compiler. You'll get to take advantage of all the new C# 6 features that are available only within the new compiler.
If you're using VS2015, Roslyn has replaced the old compiler entirely and as far as I know you can't use the old compiler within VS 2015.
Roslyn is also a platform that allows you to build programs that can modify, interpret and understand other programs. It's not really meant to let you write code that modifies itself (although that's probably possible to a degree).
The common use cases for Roslyn are:
Building Code Analyzers that provide errors and warnings within Visual Studio.
Building extensions for Visual Studio that understand source code.
Building other tools that understand or run source code. Example: ScriptCS - Scripting with C# code.
In order to use Roslyn for these purposes, you pull down the Microsoft.CodeAnalysis packages from NuGet. You can use these packages to parse code, analyze syntax trees, analyze symbols or compile code and emit IL.
If you're interested in learning more about Roslyn, I've started a series called Learn Roslyn Now that you might be interested in.
Can I replace the compiler?
Yes you can, but I'm not convinced this is a great idea outside of testing changes you want to contribute back to Roslyn. You can pull down Roslyn from GitHub and follow these instructions to build and run Roslyn from within Visual Studio.
If you follow those instructions, you'll be able to run the Roslyn project with F5. It will start a new instance of Visual Studio that's using your customized compiler. This is how people outside of Microsoft will contribute features to the compiler from now on. (Previously you couldn't deploy your custom compiler to Visual Studio but they fixed that in Visual Studio Update 1).
Roslyn is two things:
An API that lets you see "compiler things" like syntax trees and symbols.
A new csc.exe that is implemented atop #1.
If you want to make changes to the compiler and use that to build, take a look at these instructions if you haven't already. There's a few different ways you can make your own version of csc.exe and then use that to build something. But there's no "choice" dialog like you're looking for.
Roslyn is the default compiler of Visual Studio 2015. So, if you install VS2015 you´re already using Roslyn.
Roslyn is a codename for .NET Compiler Platform, and it provides open-source C# and Visual Basic compilers. The project is available on github.

Is there any way to use the 2005 C# compiler in Visual Studio 2008?

I'm using visual studio 2008 / C# on a project. We're using .NET 2.0, and whatever version of C# was released in 2.0. I can set my project to use the .NET 2.0 framework in VS 2008, but I can't figure out where to select my C# compiler. Generally this would be a problem but about 90% of my day is spent in JS, so I keep accidentally leaving in a var statement when declaring variables in C#. This compiles fine on my machine and VS reports no errors, then later it breaks the build. Is there any way to use the older C# compiler from VS 2005 (sorry I don't know what version # that is) in VS 2008?
You can't tell it to use the C# 2 compiler, but you can tell it to restrict itself to the C# 2 language.
In your project properties, go to the Build tab, click on the "Advanced..." button at the bottom right, and there's a Language Version option there... you want ISO-2.
Note that this isn't perfect - it stops you using most non-C#-2 features, but some changes may still get through. For example, I believe it will still use the more powerful generic type inference available in C# 3 than the C# 2 version. It's worth being careful about that. Here's an example:
using System;
class Test
{
static void Main()
{
Foo("hello", new object());
}
static void Foo<T>(T first, T second)
{
}
}
The C# 2 compiler can't decide what to use for T in the call to Foo, because each argument is used entirely separately to determine type inference, and any contradictory results give a compile-time error. C# 3 uses each argument to add bounds on what T can be, and only gives an error if the bounds conflict (or if a type parameter can't be determined). The C# 3 compiler when invoked in C# 2 mode still uses the C# 3 behaviour.
Is there any reason you can't switch your actual build to use the .NET 3.5 (C# 3) compiler though? There's no need to shackle yourself to C# 2 just because you're using .NET 2. Heck, with LINQBridge you can even use LINQ to Objects :)
In the project properties, go to the "Build" tab, then hit "Advanced", and change the Language Version to ISO-2 instead of C# 3. This still uses the new 2008 compiler, but prevents you from using C# 3.0 features, which should accomplish your goals here.
Are different team members use different versions of Visual Studio? The ideal strategy is for everyone to use the same version of Visual Studio, even if this means you are stuck with VS2005.
I think a VS2008 MSDN license includes permission to run VS2005 for development, so you could run the two VS versions concurrently if you need VS2008 for other reasons.

How to create a new language for use in Visual Studio

I want to write a new templating language, and I want Visual Studio to "support" it. What I need to know is:
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.
How do I create a new file type for Visual Studio?
How do I get syntax highlighting?
Can I use the same parser I created in step 1, or is this something entirely different?
How do I get Intellisense?
I'd prefer to write my parser in 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.
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
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
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.
There is a sample in the VS SDK that shows most of the features you are looking for.
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.
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.
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".

How can I get Visual Studio to error check my code (show the squiggles) without explicitly compiling?

I use Visual Studio (C# Express 2008) occasionally to work with some vendor supplied C# code. I am looking to make my experience with VS more like my experience with Eclipse. I have become accustomed to the way Eclipse handles underlining errors (in java source). When I make an error in my code in Eclipse, it will be underlined right away, and if I fix it, the underline will disappear almost immediately, or at worst, when I save the file. In Visual Studio however, the underline remains until I next build the project.
Is there a setting somewhere I can change so that VS will build every time I save, or even as I am typing? Is this hard to do with C# because it is more complex in some way than Java? Do I need to find someone to buy me the full (non express) version? Also, what is the squiggly underline feature called? I fear this question may have been asked before but I don't know the magic word to search for.
Jason's answer is fine, but a couple additional points:
is this hard to do with C# because it is more complex in some way than Java?
Yes, but that's not relevant. Yes, it is a hard problem to do on-the-fly analysis of any language in the 100 milliseconds between keystrokes. Doing it for C# is probably harder than Java, being that it supports so many more language features than Java.
But our IDE team is a bunch of buff coders who are awesome, so they can handle doing it for C#.
The real issue was that the compiler architecture of C# was not originally designed to do this sort of analysis in realtime; the VB compiler was. And therefore it took rather longer to fix up the semantic analysis engine of the C# compiler to make this feature feasible.
We're continuing to do research into how to rearchitect the compiler to expose more and more of these on-the-fly analysis services in a rich, extensible and compelling way, but this will take some time. It's a big compiler.
what is the squiggly underline feature called?
On the compiler team we call it "the squiggly red underline feature", or "squiggles" for short.
I don't know if marketing has a name for it or not. If they do, it's probably something like "Microsoft SquiggleSense .NET For The Microsoft Visual Studio Suite System 2008"; they seem to like these long names that have "Microsoft" in them twice.
Do you have Visual C# 2008 Express Edition with SP1? SP1 added exactly this feature to Visual Studio 2008 Professional and Visual C# 2008 Express Edition.
From the release notes:
This service pack adds a new Visual C# IDE feature that provides a richer set of error information about your code. Specifically, this feature presents the expression-level errors that occur in open files to you according to your code. These expression-level errors were previously reported only after a build operation.
From Scott Guthrie's release notes:
The C# code editor now identifies and displays red squiggle errors for many semantic code issues that previously required an explicit compilation to identify. For example, if you try to declare and use an unknown type in the C# code-editor today you won't see a compile error until you do a build. Now with SP1 you'll see live red squiggle errors immediately (no explicit compile required):
To turn this option on in VS 2010 use Tools > Options > Text Editor > C# > Advanced > Show live semantic errors
and
VS 2010 use Tools > Options > Text Editor > C# > Advanced >Enter outlining mode when files open
To turn this option on in VS 2010 use Tools > Options > Text Editor > C# > Advanced > Show live semantic errors

Categories