Question: Implementing custom languge syntax #3246
-
One feature I love of Babel, a modern js compiler that compiles down to "works everywhere" javascript is it's extensibility and how it allows any project to implement custom language features via plugins. I was wondering if there is any way to do somthign similar in roslyn/C# in general. I want to be able to build custom syntax (perhapse a C# future proposed for the future) and use it in my own regular C# projects today! Is this somthing which is already possible? My only currently solution is to create a custom C# AST program that takes my custom 'cs' somthing files and code-gens out to standard C# |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
That sounds like you already have a solution, which makes it clearly possible. What more are you after? |
Beta Was this translation helpful? Give feedback.
-
Using code-gen to solve this does solve it, however it feels less correct than defining rules around how syntax should work. With my solution the only way I can get code linting/roslyn refactoring would be via custom VS and jetbrains plugins instead of just writing custom roslyn based tools that work at the compiler level. Implementing new features early via the compiler is just a better solution. Other benefits include being able to migrate feature proposals prior to approval from the plugin directly into the compiler without any re-implementations. |
Beta Was this translation helpful? Give feedback.
-
@fgfmichael As a result making the syntax extensible from inside Roslyn is practically impossible. Instead you will have to parse it yourself, and convert it to roslyns syntax model. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt so to re-define my question: Would it be possible to somhow hook into the roslyn build process and pass in costom "roslyn syntax model"s for specific regions of code Or potentially entire files. What I want to be able to do is write my HTML inline of my c# methods and convert the HTML tags into method calls, much how React has JSX. Edit: |
Beta Was this translation helpful? Give feedback.
-
@fgfmichael You can easily build a Roslyn syntax tree and compile it from C# without needing to generate an interim C# document. |
Beta Was this translation helpful? Give feedback.
-
You cannot do so for regions of code very easily, since Roslyn cannot meaningfully parse just part of a file. What you could do is expand a region of invalid C# into valid C# and insert that into the rest of the file before passing it to Roslyn. |
Beta Was this translation helpful? Give feedback.
-
Thanks for using some good keywords in your responses! https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis is starting to look promising! My todo list now:
If I can achieve that I believe that It starting to look allot more plausable. I will check back in later with any learnings |
Beta Was this translation helpful? Give feedback.
-
Good luck! If you need any help, check out the Roslyn gitter channel. |
Beta Was this translation helpful? Give feedback.
-
I wouldn't say "easily", to be honest. |
Beta Was this translation helpful? Give feedback.
@fgfmichael You can easily build a Roslyn syntax tree and compile it from C# without needing to generate an interim C# document.
The necessary packages are all available on nuget.