Compiler plugin capabillity


#1

Hi,

I am working on building document templating lib similar to jade/slim. I see that there is the ability to write a compiler plugin and I wondered if one can or in the future will be able to write a plugin that targets certain types of files and process them before they get passed to the compiler.

For example, rust will compile all .rs files. I would like a way that a plugin can tell the compiler to include files with extension .xyz and these files get passed through the plugin so that it can generate the valid rust code to be compiled.

This would allow me to have compile time checked templates (that are written in HTML, XML, etc…) and would prevent a bunch of serialization that I have to do at runtime for dynamic data access.

I hope that made some sense, if not let me know I will try to improve it.


#2

A compiler plugin is embedded within the compiler; the code it checks must be valid rust. So no HTML, XML, etc.

However, you can do it the other way round: Wrap rustc in your code and use it to compile whatever you give it. Look at compiletest_rs for an example on how to do this.


#3

Thanks for the link, I hadn’t thought of doing it that way around. Another option I have looked at is syntax extensions similar to how this lib does it.

I have the code written to do most of the transpiling so was looking to reuse that as much as possible.


#4

I think what you want is using a Cargo build script, to transpile from your format to valid Rust sources. http://doc.crates.io/build-script.html


#5

@olivren I have only been able to have a quick scan through that but a build script looks like the way forwards. I will hopefully get a better look through this evening, thanks!