WIT parser and resolver in MoonBit
moon add mizchi/witlet source = "package local:hello;\n\ninterface greet {\n greet: func(name: string) -> string;\n}\n\nworld hello {\n export greet;\n}\n"
inspect(parse(source))let source = "package local:hello;\n\ninterface greet {\n greet: func(name: string) -> string;\n}\n\nworld hello {\n export greet;\n}\n"
inspect(resolve(source))| Function | Description |
|---|---|
| parse(String) -> Result[WitFile, ParseError] | Parse a WIT source string into an AST |
| parse_path(String) -> Result[WitFile, ParseError] | Parse a WIT file from a file path |
| resolve(String) -> Result[Resolve, ParseError] | Parse and resolve a WIT source string |
| resolve_path(String, world? : String) -> Result[ResolveInput, ParseError] | Resolve WIT from a directory path with optional world selection |
| tokenize(String) -> Result[Array[Token], ParseError] | Tokenize a WIT source string |
pub suberror ParseError {
Message(String, Int)
}pub struct Function {
name : String
kind : FunctionKind
params : Array[Param]
result : TypeExpr?
docs : String?
}pub enum FunctionKind {
Freestanding
Constructor(String)
Method(String)
Static(String)
}pub struct PackageName {
ns : String
name : String
version : String?
}pub struct RFunction {
name : String
kind : RFunctionKind
params : Array[(String, RType)]
result : RType?
docs : String?
}pub enum RFunctionKind {
Freestanding
Constructor(Int)
Method(Int)
Static(Int)
}impl Eq for RFunctionKindimpl Show for RFunctionKindimpl ToJson for RFunctionKindpub enum RHandleKind {
Own(Int)
Borrow(Int)
}impl Eq for RHandleKindimpl Show for RHandleKindimpl ToJson for RHandleKindimpl Eq for RInterfaceimpl Show for RInterfaceimpl ToJson for RInterfacepub struct RInterfaceRef {
id : Int
}impl Eq for RInterfaceRefimpl Show for RInterfaceRefimpl ToJson for RInterfaceRefpub struct RPackageName {
ns : String
name : String
version : String?
}impl Eq for RPackageNameimpl Show for RPackageNameimpl ToJson for RPackageNameimpl Eq for RRecordFieldimpl Show for RRecordFieldimpl ToJson for RRecordFieldpub enum RType {
Bool
U8
U16
U32
U64
S8
S16
S32
S64
F32
F64
Char
String_
Id(Int)
}pub enum RTypeKind {
Record(Array[RRecordField])
Variant(Array[RVariantCase])
List(RType)
Option(RType)
Result(RType?, RType?)
Tuple(Array[RType])
Flags(Array[String])
Enum(Array[String])
Alias(RType)
Resource
Handle(RHandleKind)
}pub enum RTypeOwner {
Interface(Int)
World(Int)
None
}impl Eq for RTypeOwnerimpl Show for RTypeOwnerimpl ToJson for RTypeOwnerimpl Eq for RVariantCaseimpl Show for RVariantCaseimpl ToJson for RVariantCasepub struct RWorld {
name : String
docs : String?
imports : Map[String, RWorldItem]
exports : Map[String, RWorldItem]
pkg : Int?
}impl Eq for RWorldItemimpl Show for RWorldItemimpl ToJson for RWorldItemimpl Eq for ResolveInputimpl Show for ResolveInputimpl ToJson for ResolveInputpub enum TokenType {
Ident(String)
Keyword(String)
Symbol(String)
StringLit(String)
Number(String)
Doc(String)
Eof
}