Macros
Rock has declarative token macros that generate declarations or expressions before ordinary declaration collection and type checking. Macro expansion is useful when the repeated source shape cannot be expressed as a function, trait, or generic. It also makes ownership and generated control flow less obvious, so a small conventional expansion is easier to review than a clever one.
Current status: Declarative macro parsing, direct captures, expression captures, and flat repetition are covered by compiler tests. Macro use remains experimental: nested repetition, indentation-sensitive arguments, diagnostics, and formatter support are incomplete. The examples below use parser- and expansion-tested syntax; inspect generated source with
rock expandbefore depending on it in a package.
A zero-argument declaration macro
The simplest matcher has no captures. The invocation is a top-level %name line, and the body emits an ordinary Rock declaration.
macro make_main
=>
main = !->
42.println!
%make_main
The expansion produces a normal unit-returning main function, which prints 42 and exits with status 0. The macro itself is not a runtime function and has no runtime ownership effect; only the generated declarations participate in type checking and cleanup. Keep generated names unique because two expansions that emit the same top-level name create the same conflict as handwritten declarations.
Capturing identifiers and expressions
The tested fragment categories are ident, expr, and ty. The example below uses identifier and expression captures; the next example demonstrates type-name captures.
The invocation syntax is token-based and uses spaces rather than a function-call comma list. In the example below, $name:ident captures answer and $value:expr captures the expression 6 * 7:
macro make_constant
$name:ident $value:expr =>
$name = -> $value
%make_constant answer 6 * 7
main = !->
answer! .println!
The generated answer returns 42, so this executable example prints 42. A macro invocation always starts with %, while an ordinary declaration would not be expanded.
Capturing a type
Use ty to capture a single uppercase type name. Rock distinguishes uppercase type-name tokens from ordinary identifiers, so both the new struct name Number and the field type I64 require ty captures. An ident capture would not match Number.
macro make_wrapper
$name:ty $inner:ty =>
struct $name
< value: $inner
%make_wrapper Number I64
main = !->
number = Number
value: 42
Here $name:ty captures Number and $inner:ty captures I64. The expansion declares a struct with an exported value: I64 field, and main constructs a value of that struct. Captures match syntax before type checking, so Number does not need to be declared before the invocation. Currently, ty captures consume only one type-name token, not a compound type such as a reference or a generic type application.
Repetition
A repetition starts with $( and ends with )*, in the matcher and in the template alike. A plain ) inside a repetition is an ordinary token, so a repetition may contain parentheses. Forgetting the * is a compile-time error that points at the unclosed $(. The following syntax is covered by the expansion tests and is deliberately flat.
macro define_values
$( $name:ident )* =>
$( $name = -> 9 )*
%define_values first second third
main = !->
first! .println!
second! .println!
third! .println!
The matcher captures three identifiers and the template emits three functions. The output is 9, 9, and 9. Repetition consumes the captured tokens into generated declarations; it does not clone a runtime value and does not create a runtime loop. Captures in one repeated group must have the same number of elements.
Where the repeated copies land depends on where the repetition sits in the template:
- A repetition written on one line, with nothing else on that line, repeats lines. Each copy becomes its own line at the same indentation, which is how
define_valuesemits three separate functions. - Any other repetition repeats inline. Its copies are placed one after another inside the surrounding line.
The next macro uses an inline repetition. The matcher takes a first expression followed by any number of , expression groups, and the template chains one .concat call per extra expression onto the same line:
macro make_greeting
$name:ident $first:expr $( , $rest:expr )* =>
$name = -> ($first).show! $( .concat (($rest).show!) )*
%make_greeting greeting "Hello ", 42, "!"
main = !->
greeting! .println!
The invocation expands to greeting = -> ("Hello ").show! .concat ((42).show!) .concat (("!").show!), so the program prints Hello 42!. With no extra expressions, as in %make_greeting alone "hi", the repetition produces nothing and the rest of the line is kept. An expr capture stops at a comma, except that a function call with arguments takes every comma-separated argument that follows it; wrap such a call in parentheses, as in "a", (add 1, 2), "b".
The compiler also tests a direct capture used inside a repeated template. This form is useful when several generated functions should return one already-declared value:
macro define_aliases
$source:ident $( $name:ident )* =>
$( $name = -> $source! )*
source = -> 7
%define_aliases source left right
main = !->
left! .println!
right! .println!
The output is 7 and 7. The source declaration appears before the invocation, so the generated functions have a real target and no placeholder value. Do not nest another repetition inside either matcher or template; nested repetition is a known unsupported area even though the token grammar accepts repetition groups.
Invocation matching and errors
Macro arms are tried in declaration order. A matcher that does not consume the invocation’s tokens produces a compile-time diagnostic; it does not become a runtime branch. This example has two complete arms and therefore demonstrates a deliberate fallback without relying on an undefined error type.
macro make_value
$name:ident $value:expr =>
$name = -> $value
$name:ident =>
$name = -> 0
%make_value explicit 8
%make_value fallback
main = !->
explicit! .println!
fallback! .println!
The output is 8 and 0. Put the more specific arm first; a broad arm can consume input before a later arm gets a chance. Invalid input is reported near the invocation, but expansion-origin labels and indentation diagnostics are still being improved.
Macros in expressions
A macro can also be called wherever an expression is expected: on the right of an assignment, as a statement, or as a call argument. Its template must then produce exactly one expression. The macro below joins any number of values into a String by calling show on each one:
macro join
$first:expr $( , $rest:expr )* =>
($first).show! $( .concat (($rest).show!) )*
main = !->
name = String::from "rock"
greeting = %join "Hello ", name, ", you are ", 42
%println greeting, "\n", name
The program prints Hello rock, you are 42 and then rock on the next line. show borrows its receiver, so name is still usable after the call.
In an expression, a macro call takes every token up to the end of its line, commas included. To use the result in a larger expression, wrap the call in parentheses: (%join "a", 1).concat (%join "b", 2) evaluates both calls and joins them into a1b2. The expansion itself is treated as parenthesized, so it never mixes with the operators around the call. A template that produces something other than one expression, such as an assignment, is reported at the call site.
A template may call other macros, including from an expression. Each expansion is expanded again until no macro call is left, up to a fixed depth that stops runaway recursion with a diagnostic.
Sharing macros between crates
A macro is private to the module that declares it. Prefix the declaration with < to export it from its crate, the same way < exports a function or a type:
< macro join
$first:expr $( , $rest:expr )* =>
($first).show! $( .concat (($rest).show!) )*
If this declaration lives in the module text of a library crate named mylib, a program that depends on mylib imports it by its full path and then calls it by name:
> mylib::text::join
main = !->
(%join "a", 1, true).println!
The program prints a1true. Without the import, %join is reported as an unknown macro. A macro declared without < cannot be imported from another crate.
An exported macro expands in the program that calls it, so the names in its template are looked up there, not in the library. The join template only calls the methods show and concat, which every program that uses the std prelude can see. A template that names other items, or calls another macro, needs those names to be visible where the macro is called. Glob imports such as > mylib::text::* do not bring macros into scope.
Expansion workflow and design rules
Use rock expand to inspect generated source before debugging type or ownership errors. A generated ~@ method still consumes its receiver, and a generated mutable method still requires a mutable binding; macros do not weaken the borrow checker. Prefer ordinary functions for behavior, generics for type reuse, and a macro only when the repeated declaration shape itself is the useful abstraction.
Rock does not yet expose a stable procedural-macro, derive, or attribute API for application authors. This chapter therefore documents only declarative %name macros.