// Files and items, plus small construction helpers.
///|
/// One top-level item: doc comment, attributes and the declaration itself.
#derive(Debug, Eq)
pub struct Item {
doc : DocComment
priv attrs : Array[Attribute]
decl : Decl
}
///|
/// Construct a top-level `Item` with optional doc comment and attributes.
pub fn Item::new(
doc~ : DocComment = DocComment::empty(),
attrs~ : Array[Attribute] = [],
decl : Decl,
) -> Item {
{ doc, attrs: attrs.copy(), decl, }
}
///|
/// Return a defensive copy of the item's attributes.
pub fn Item::copy_attrs(self : Item) -> Array[Attribute] {
self.attrs.copy()
}
///|
/// A source file: leading header comments plus ordered items. Renderers turn
/// it into text; `fmt.render_file` is the MoonBit renderer.
#derive(Debug, Eq)
pub struct SourceFile {
priv header : Array[String]
priv items : Array[Item]
}
///|
/// Create an empty `SourceFile` with leading `//` header lines.
pub fn SourceFile::with_header(header : Array[String]) -> SourceFile {
{ header: header.copy(), items: [], }
}
///|
/// Create an empty `SourceFile` with no header comments.
pub fn SourceFile::new() -> SourceFile {
SourceFile::with_header([])
}
///|
/// Append a top-level item to the file.
pub fn SourceFile::push(self : SourceFile, item : Item) -> Unit {
self.items.push(item)
}
///|
/// Number of top-level items currently in the file.
pub fn SourceFile::length(self : SourceFile) -> Int32 {
self.items.length()
}
///|
/// Return a defensive copy of the leading header lines.
pub fn SourceFile::header_lines(self : SourceFile) -> Array[String] {
self.header.copy()
}
///|
/// Return a defensive copy of the top-level items in emission order.
pub fn SourceFile::copy_items(self : SourceFile) -> Array[Item] {
self.items.copy()
}
///|
/// Shorthand: struct item (`#derive` is synthesized by the renderer from
/// `decl.derives`).
pub fn struct_item(doc : DocComment, decl : StructDecl) -> Item {
{ doc, attrs: [], decl: Decl::Struct(decl), }
}
///|
/// Shorthand: enum item (`#derive` and `#non_exhaustive` are synthesized by
/// the renderer from `decl`).
pub fn enum_item(doc : DocComment, decl : EnumDecl) -> Item {
{ doc, attrs: [], decl: Decl::Enum(decl), }
}
///|
/// Shorthand: suberror item (`#derive` is synthesized by the renderer from
/// `decl.derives`).
pub fn suberror_item(doc : DocComment, decl : SuberrorDecl) -> Item {
{ doc, attrs: [], decl: Decl::Suberror(decl), }
}
///|
/// Shorthand: plain item with a doc comment and no extra attributes.
pub fn item(doc : DocComment, decl : Decl) -> Item {
{ doc, attrs: [], decl, }
}
///|
/// Shorthand: item with a doc comment and explicit attributes (`#deprecated`,
/// `#alias`, `#cfg`, `#warnings`, …).
pub fn item_with_attrs(
doc : DocComment,
attrs : Array[Attribute],
decl : Decl,
) -> Item {
{ doc, attrs: attrs.copy(), decl, }
}