Rockstar feat. Source Code Generators
I recently watched a really interesting video from Dylan Beattie on the NDC called The Art of Code, where he talks about code that has no practical reason to exist except because it is fun, or beautiful, to write it. If you haven’t watched it, I seriously recommend it — it’s interesting and fun!

https://shadowmountain.org/hub/h-v-a1.html

https://shadowmountain.org/hub/h-v-a2.html

https://shadowmountain.org/hub/h-v-a3.html

https://shadowmountain.org/hub/h-v-a4.html

https://shadowmountain.org/hub/h-v-t5.html

https://shadowmountain.org/hub/h-v-t6.html

https://shadowmountain.org/hub/h-v-t7.html

https://shadowmountain.org/hub/h-v-t8.html

https://www.agwest.com/voc/h-v-a1.html

https://www.agwest.com/voc/h-v-a2.html

https://www.agwest.com/voc/h-v-a3.html

https://www.agwest.com/voc/h-v-a4.html

https://www.agwest.com/voc/h-v-t5.html

https://www.agwest.com/voc/h-v-t6.html

https://www.agwest.com/voc/h-v-t7.html

https://www.agwest.com/voc/h-v-t8.html

https://www.agwest.com/voc/video-bvb-v-zenit-match-liv-de1.html

https://www.agwest.com/voc/video-bvb-v-zenit-match-liv-de2.html

https://www.agwest.com/voc/video-bvb-v-zenit-match-liv-de3.html

https://www.agwest.com/voc/video-bvb-v-zenit-match-liv-de4.html

https://www.agwest.com/voc/video-bvb-v-zenit-match-liv-de5.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk1.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk2.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk3.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk4.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk5.html

https://www.agwest.com/voc/video-lazio-v-brugge-match-liv-uk6.html

https://shadowmountain.org/hub/video-bvb-v-zenit-match-liv-de1.html

https://shadowmountain.org/hub/video-bvb-v-zenit-match-liv-de2.html

https://shadowmountain.org/hub/video-bvb-v-zenit-match-liv-de3.html

https://shadowmountain.org/hub/video-bvb-v-zenit-match-liv-de4.html

https://shadowmountain.org/hub/video-bvb-v-zenit-match-liv-de5.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk1.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk2.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk3.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk4.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk5.html

https://shadowmountain.org/hub/video-lazio-v-brugge-match-liv-uk6.html

The Art of Code — Dylan Beattie — YouTube

Towards the end of his talk, he introduces us to his own programming language. He created it in a bar and it’s since become famous worldwide: Rockstar.

Introducing, ROCKSTAR

While watching the talk, I remembered that I’ve been wanting to get started with source-code generators since the release of NET5.0 for another side project of mine, and what better than using Rockstar as a way to get started?

Introducing, the Rockstar Source Code Generator

Source-code generators are a new C# compiler feature recently introduced by Microsoft that lets developers inspect user code as part of the code compilation, and generate new C# source files that are added to the compilation. This is done via a new kind of code component that is called a Source Generator, that is added as an analyzer to any existing project. You use a generator in your project by either referencing a generator project or by adding it as a NuGet package. With that in mind, let’s start building our Rockstar to C# generator!

99 bottles of beer in Rockstar

I wanted the generator to allow me to embed Rockstar code in any existing C# project, and allow me to call it from normal C# code. So a natural place to “hide” such code is within comments. This way we can hide something as innocents as this comment on the left in our normal C# code-base.

As we don’t want to parse every single comment in our source-code as Rockstar code, let’s start by defining a suitable header format for identifying our code and giving a name to the class that will hold the generated code. Something appropriate as Let’s rock with (class name), to keep with the spirits.

Now to implementing the code generator. We start by importing a few NuGet packages as suggested on the official samples, and proceed to implement the ISourceGenerator interface and tag your generator with the [Generator] attribute:

The next step is to capture all comments from the input source-code syntax tree. A few google searches and I end up in a familiar blog — familiar because Dan is a colleague of mine at Curiosity. A few laughs after, and a bit of reading his post, code and playing with the Roslyn API, I’m happy to see that the API now provides an easy to use enumerator over all trivia in the parsed syntax tree, with the method DescendantTrivia() — this makes it easy to achieve what I need with a simple for loop:

We can use a simple regex to detect the right comment markers and extract both the class name and the source-code within the comment, and we are ready to go to the next step: transpiling Rockstar to C#, so we can inject it back into the compilation process using our generator.

Regex to the rescue (source: XKCD)
['Hasan Mahmud']
['Rockstar', 'Dotnet Core', 'Dotnet', 'C Sharp Programming', 'Csharp']