Reusable MoonBit code-generation library: naming, a backend-neutral source model, AST building, formatting and manifest emission.
moon add vigoo/moonbit_codegen| Package | Purpose |
|---|---|
| name | Word splitting, case conversion (to_pascal/to_snake/…), keyword escaping, scoped unique-name allocation. |
| doc | Backend-neutral model: SourceFile, Item, declarations, doc comments, attributes, TypeExpr. |
| ast | doc model → moonbitlang/parser syntax nodes (including raw-body parsing). |
| fmt | Rendering: one canonical formatter pass; raw-text route; is_formatted. |
| project | ImportSet (alias-collision detection) and canonical manifest emission. |
///|
test "naming" {
inspect(@name.to_snake("Idempotency-Key"), content="idempotency_key")
inspect(@name.to_pascal("OAuth2Provider"), content="OAuth2Provider")
inspect(@name.ident_snake("type"), content="type_")
inspect(
@name.split_words("r#CreateParams").join(","),
content="Create,Params",
)
let namer = @name.Namer::new()
inspect(namer.unique("field"), content="field")
inspect(namer.unique("field"), content="field2")
}///|
test "render a struct" {
let file = @doc.SourceFile::new()
file.push(
@doc.struct_item(@doc.DocComment::new(["A point in 2D space."]), {
name: "Point",
type_params: [],
vis: @doc.PubAll,
fields: [
{
name: "x",
ty: @doc.Name("Int32"),
mut_: false,
vis: @doc.Private,
doc: @doc.DocComment::empty(),
attrs: [],
},
{
name: "y",
ty: @doc.Name("Int32"),
mut_: false,
vis: @doc.Private,
doc: @doc.DocComment::empty(),
attrs: [],
},
],
derives: ["Debug", "Eq"],
}),
)
inspect(
@fmt.render_file(file),
content=(
#|///|
#|/// A point in 2D space.
#|#derive(Debug, Eq)
#|pub(all) struct Point {
#| x : Int32
#| y : Int32
#|}
#|
),
)
}///|
test "function with a raw body" {
let file = @doc.SourceFile::new()
file.push(
@doc.item(
@doc.DocComment::empty(),
@doc.Decl::Fn({
self_type: None,
name: "twice",
type_params: [],
params: [@doc.FnParam::positional("value", @doc.Name("Int32"))],
return_type: Some(@doc.Name("Int32")),
is_async: false,
error: @doc.NoRaise,
vis: @doc.Pub,
body: "value * 2",
}),
),
)
inspect(
@fmt.render_file(file),
content=(
#|///|
#|pub fn twice(value : Int32) -> Int32 {
#| value * 2
#|}
#|
),
)
}///|
test "emit a moon.mod" {
let deps = @project.ImportSet::new()
deps.add_dependency("moonbitlang/parser@0.4.9")
let moon_mod : @project.MoonMod = {
header: [],
name: "vigoo/sample",
version: "0.1.0",
readme: None,
repository: None,
license: Some("Apache-2.0"),
keywords: [],
description: None,
preferred_target: Some("native"),
is_private: false,
deps,
}
inspect(
moon_mod.emit(),
content=(
#|name = "vigoo/sample"
#|
#|version = "0.1.0"
#|
#|license = "Apache-2.0"
#|
#|keywords = []
#|
#|preferred_target = "native"
#|
#|import {
#| "moonbitlang/parser@0.4.9",
#|}
#|
),
)
}///|
test "emit moon.pkg and moon.work" {
let pkg = @project.MoonPkg::new()
pkg.imports.add("moonbitlang/core/json", as_="cjson")
inspect(
pkg.emit(),
content=(
#|import {
#| "moonbitlang/core/json" @cjson,
#|}
#|
),
)
let work = @project.MoonWork::new(["./runtime", "./client"])
inspect(
work.emit(),
content=(
#|members = [
#| "./runtime",
#| "./client",
#|]
#|
),
)
}moon check --target native # this module is pure — any target works
moon test --target native
./scripts/golden_check.sh # from repo root: golden + binary moonfmt gateInstall
Download zipReusable MoonBit code-generation library: naming, a backend-neutral source model, AST building, formatting and manifest emission.