// Core model types: doc comments, attributes, visibility, type expressions,
// error specifications, and type parameters.
///|
/// A doc comment as raw lines — no `///` prefixes, `""` for blank lines.
/// Renderers apply the MoonBit convention (`/// text`, blank `///`) and the
/// leading-space rule themselves.
#derive(Debug, Eq)
pub struct DocComment {
priv lines : Array[String]
}
///|
/// Construct a `DocComment` from raw lines (without `///` prefixes).
pub fn DocComment::new(lines : Array[String]) -> DocComment {
{ lines: lines.copy(), }
}
///|
/// An empty doc comment (renders nothing).
pub fn DocComment::empty() -> DocComment {
{ lines: [], }
}
///|
/// True when the doc comment has no lines.
pub fn DocComment::is_empty(self : DocComment) -> Bool {
self.lines.is_empty()
}
///|
/// Return a defensive copy of the raw doc comment lines.
pub fn DocComment::copy_lines(self : DocComment) -> Array[String] {
self.lines.copy()
}
///|
/// An attribute, e.g. `#derive(Debug, Eq)` or a bare `#deprecated`.
#derive(Debug, Eq)
pub struct Attribute {
name : String
payload : String?
}
///|
/// Bare attribute `#` with no payload.
pub fn Attribute::bare(name : String) -> Attribute {
{ name, payload: None, }
}
///|
/// Attribute with parenthesized payload `#()`.
pub fn Attribute::with_payload(name : String, payload : String) -> Attribute {
{ name, payload: Some(payload), }
}
///|
/// `#derive(...)` from trait names; `None` for an empty list.
pub fn Attribute::derive_from(traits : Array[String]) -> Attribute? {
if traits.is_empty() {
None
} else {
Some({ name: "derive", payload: Some(traits.join(", ")), })
}
}
///|
/// Declaration visibility.
/// - `Private`: default / implicit (no visibility modifier)
/// - `Priv`: explicit `priv`
/// - `Pub`: `pub`
/// - `PubAll`: `pub(all)`
/// - `PubOpen`: `pub(open)`
/// - `PubReadonly`: `pub(readonly)`
#derive(Debug, Eq)
pub(all) enum Visibility {
Private
Priv
Pub
PubAll
PubOpen
PubReadonly
}
///|
/// Error specification on a function, method, trait method, impl, or function
/// type.
#derive(Debug, Eq)
pub(all) enum ErrorSpec {
/// No error annotation (inferred / non-raising).
NoRaise
/// Bare `raise` (polymorphic `Error`).
DefaultRaise
/// Typed `raise SpecificError`.
TypedRaise(TypeExpr)
/// Explicit `noraise`.
ExplicitNoraise
}
///|
/// A quantified type parameter with optional trait constraints
/// (`T` or `T : TraitA + TraitB`).
#derive(Debug, Eq)
pub(all) struct TypeParam {
name : String
constraints : Array[TypeExpr]
}
///|
/// Unconstrained type parameter `T`.
pub fn TypeParam::plain(name : String) -> TypeParam {
{ name, constraints: [], }
}
///|
/// Type parameter constrained by one or more traits (`T : A + B`).
pub fn TypeParam::bounded(
name : String,
constraints : Array[TypeExpr],
) -> TypeParam {
{ name, constraints: constraints.copy(), }
}
///|
/// A small type-expression tree. `Raw` is the escape hatch for anything the
/// tree does not cover.
#derive(Debug, Eq)
pub(all) enum TypeExpr {
Name(String)
Qualified(pkg~ : String, name~ : String)
Applied(base~ : TypeExpr, args~ : Array[TypeExpr])
Option(TypeExpr)
Tuple(Array[TypeExpr])
/// Function type `(A, B) -> R raise E` / `async (A) -> R`.
Arrow(
args~ : Array[TypeExpr],
ret~ : TypeExpr,
error~ : ErrorSpec,
is_async~ : Bool,
)
/// Trait object type `&Trait` / `&@pkg.Trait`.
Object(TypeExpr)
Raw(String)
}
///|
/// `@pkg.Type` style reference.
pub fn TypeExpr::qualified(pkg : String, name : String) -> TypeExpr {
Qualified(pkg~, name~)
}
///|
/// `Array[T]`.
pub fn TypeExpr::array(inner : TypeExpr) -> TypeExpr {
Applied(base=Name("Array"), args=[inner])
}
///|
/// `Map[String, T]`.
pub fn TypeExpr::map(value : TypeExpr) -> TypeExpr {
Applied(base=Name("Map"), args=[Name("String"), value])
}
///|
/// `T?`.
pub fn TypeExpr::optional(inner : TypeExpr) -> TypeExpr {
Option(inner)
}
///|
/// `&Trait` trait object type.
pub fn TypeExpr::object(trait_ : TypeExpr) -> TypeExpr {
Object(trait_)
}
///|
/// Error raised by generation (e.g. a raw body that fails to parse).
/// Match the payload: `catch { CodegenError(message) => ... }`.
#derive(Debug, Eq)
pub(all) suberror CodegenError {
CodegenError(String)
}