Generate MoonBit code from annotated declarations: templates over the official syntax tree, typed attribute arguments, and the scaffolding a dev_build generator needs. Built on moonbitlang/parser; not a parser itself.
Dependencies
let source =
#|fn $name() -> $T {
#| $value
#|}
let code = @quote.items(source)
.set("name", @quote.ident("answer"))
.set("T", @quote.ident("Int"))
.set("value", @quote.int(42))
.render()
println(code) // fn answer() -> Int { 42 }, laid out the way `moon fmt` lays it out| Package | What it holds |
|---|---|
| moonast/quote | Templates: real MoonBit with $holes, parsed by moonbitlang/parser, filled by replacing nodes, and printed by its formatter, so quoting, escaping and layout are the official ones |
| moonast/attr | Annotations as typed arguments: find #lib.name(...) on a declaration and bind its arguments against a spec, with the source position in every refusal |
| moonast/gen | Declarations read into plain views (Struct, Enum, Func, with types as written), the Generator trait, and render, which gives the file a generator writes |
| moonast/cli | run, the whole main of a generator binary: [-C <dir>] <inputs...> -o <output> [--check]; native and wasm |
| Hole | Takes | Example |
|---|---|---|
| $T (upper case) | a type or a constructor | fn f(x : $T) |
| $name (lower case) | a value, an expression, or a name | self.$name, $value + 1 |
| $name* | a list, spliced among statements, array elements or positional arguments | [$items*] |
| $name inside a string literal | a name or a string, as text | "field $key" |
| $$ | a literal $ | "$$5" |
async fn main {
@cli.run(Describe::{}, @env.args()[1:])
}rule(name: "describe", command: "moonx <module>@<version> -C \"$mod_dir\" \"$input\" -o \"$output\"")dev_build(rule: "describe", input: "models.mbt", output: "describe.g.mbt")
formatter(ignore: [ "describe.g.mbt" ])Install
Download zipGenerate MoonBit code from annotated declarations: templates over the official syntax tree, typed attribute arguments, and the scaffolding a dev_build generator needs. Built on moonbitlang/parser; not a parser itself.
Dependencies