Rule based macros? That's sweet.js!

by on under technical
1 minute read

Imagine you want to insert your own syntax into a Javascript project without the pain of writing and entire compiler to go from one syntax to another (Like Coffeescript). Sweet.js gives you the ability to define a name with the macro keywords, define a few patterns and then whenever you invoke the name you created in your project the pattern will be matched and expanded. This allows for some really powerful stuff, with very simple syntax.

Just to give a basic idea for what one of these might look like, here is a simple identity macro:

macro myFirstMacro {
  rule { $x } => { $x }
}

All this says is that when you use the keyword myFirstMacro that you just defined, the sweet.js compiler will look for a pattern following that keyword to match $x which is the way sweet.js says to just expand that pattern to itself. I will show an invocation followed by what the compiler would output. Invocation:

myFirstMacro "Hello, world!";

Output:

"Hello, world!";

Now this may not seem very useful from this simple example but let me tweak the example just a little bit to see how this can be used.

macro addTwo {
  rule { $x } => { $x + 2 }
}

Now if we are to invoke this macro here is the invocation and output:

//Invocation
addTwo 5;

//Expanded output
5 + 2;

This article was meant to just be a short and sweet (hah!) intro to macros from sweet.js. If you want to dive a bit deeper take a look at how sweet.js uses hygienic macros or how you can have recursive macros, case based macros and even override operators and keywords that Javascript already uses natively.

sweet, sweet.js, sweetjs, macros, javascript
comments powered by Disqus