API Stability Notice
Macroforge is under active development. The API is not yet stable and may change between versions. Some documentation sections may be outdated.
Declarative Macros
Declarative macros let you define compile-time code transformations entirely in TypeScript, using pattern-matching syntax inspired by Rust’s macro_rules!. No Rust toolchain is involved — the macro lives in the same .ts file that uses it.
import { macroRules } from "macroforge/rules";
const $double = macroRules`
($x:Expr) => ($x) * 2
`;
const four = $double(2); // → (2) * 2They run as a pre-pass, before attribute and derive macros, so a declarative macro can expand into code that later macros then process.
Full documentation
This page is a conceptual overview. The complete reference lives in its own section:
- Declarative Macros overview — what they are and when to use them
- Defining Macros — tag form, object form, scoping
- Patterns & Fragments — all 11 fragment specifiers
- Repetitions & Backtracking — variable-length patterns
- Expansion & Hygiene — contexts, IIFE wrapping, composition
- Type-Position Macros —
$Macro<T> - Reverse Monomorphization — shared runtime helpers
- Cross-File Macros & Diagnostics — imports and error reference