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 |
impl Show for ParseErrorpub struct Function {
name : String
kind : FunctionKind
params : Array[Param]
result : TypeExpr?
docs : String?
external_id : String?
attributes : Attributes
} derive(Eq, Debug)pub struct IncludeDecl {
path : UsePath
renames : Array[IncludeRename]
attributes : Attributes
} derive(Eq, Debug)impl Show for IncludeDeclpub struct InlineInterface {
name : String
docs : String?
items : Array[InterfaceItem]
} derive(Eq, Debug)pub struct Interface {
name : String
docs : String?
items : Array[InterfaceItem]
attributes : Attributes
} derive(Eq, Debug)impl Show for PackageNamepub struct RFunction {
name : String
kind : RFunctionKind
params : Array[(String, RType)]
result : RType?
docs : String?
external_id : String?
attributes : Attributes
} derive(Eq, Debug)pub struct RInterfaceRef {
id : Int
external_id : String?
attributes : Attributes
} derive(Eq, Debug)pub struct RPackage {
name : RPackageName
interfaces : Map[String, Int]
worlds : Map[String, Int]
} derive(Eq, Debug)pub struct RTypeDef {
name : String?
kind : RTypeKind
owner : RTypeOwner
docs : String?
external_id : String?
attributes : Attributes
} derive(Eq, Debug)pub enum RTypeKind {
Record(Array[RRecordField])
Variant(Array[RVariantCase])
List(RType)
FixedLengthList(RType, UInt)
Map(RType, RType)
Option(RType)
Future(RType?)
Stream(RType?)
Result(ok~ : RType?, err~ : RType?)
Tuple(Array[RType])
Flags(Array[String])
Enum(Array[String])
Alias(RType)
Resource
Handle(RHandleKind)
} derive(Eq, Debug)pub struct RWorld {
name : String
docs : String?
imports : Map[String, RWorldItem]
exports : Map[String, RWorldItem]
pkg : Int?
attributes : Attributes
} derive(Eq, Debug)fn ResolveOptions::new(features? : Array[String], all_features? : Bool, target_version? : String) -> ResolveOptionspub struct ResolvedInterfaceInfo {
interface_id : Int
name : String?
docs : String?
package_name : RPackageName?
functions : Map[String, RFunction]
types : Map[String, ResolvedTypeInfo]
} derive(Eq, Debug)impl Show for ResolvedInterfaceInfopub struct ResolvedTypeInfo {
local_name : String
type_id : Int
type_name : String?
type_kind : RTypeKind
owner : RTypeOwner
owner_package : RPackageName?
owner_interface : String?
owner_world : String?
} derive(Eq, Debug)pub struct TypeDef {
name : String
kind : TypeDefKind
docs : String?
external_id : String?
attributes : Attributes
} derive(Eq, Debug)pub enum TypeExpr {
Bool
U8
U16
U32
U64
S8
S16
S32
S64
F32
F64
Char
String_
ErrorContext
Id(String)
List(TypeExpr)
FixedLengthList(TypeExpr, UInt)
Map(TypeExpr, TypeExpr)
Option(TypeExpr)
Future(TypeExpr?)
Stream(TypeExpr?)
Result(TypeExpr?, TypeExpr?)
Tuple(Array[TypeExpr])
Own(String)
Borrow(String)
} derive(Eq, Debug)pub struct UseDecl {
from : UsePath
names : Array[String]
renames : Array[IncludeRename]
attributes : Attributes
} derive(Eq, Debug)impl Show for VariantCasepub enum WorldItem {
Interface(UsePath)
NamedInterface(String, UsePath)
External(String, WorldItem)
Attributed(Attributes, WorldItem)
Function(Function)
InlineInterface(InlineInterface)
Include(IncludeDecl)
Use(UseDecl)
} derive(Eq, Debug)fn resolve_interface_infos(source : String, options? : ResolveOptions) -> Result[Array[ResolvedInterfaceInfo], ParseError]fn resolve_path(path : String, world? : String, options? : ResolveOptions) -> Result[ResolveInput, ParseError]fn resolve_path_interface_infos(path : String, world? : String, options? : ResolveOptions) -> Result[Array[ResolvedInterfaceInfo], ParseError]