Sign in

    wit

    WIT parser and resolver in MoonBit

    wit
    wasm
    component
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    8 months ago
    Downloads
    65K

    #mizchi/wit

    WIT (WebAssembly Interface Types) parser and resolver written in MoonBit.

    #Features

    • Lexer & Parser – Tokenize and parse WIT source into a typed AST
    • Resolver – Resolve parsed WIT into a flat, index-based representation suitable for code generation
    • Path resolver – Load WIT files from deps/ directory layouts produced by wkg

    #Install

    moon add mizchi/wit

    #Usage

    #Parse a WIT source string

    let 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))

    #Resolve to a flat model

    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))

    #API

    FunctionDescription
    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

    #License

    Apache-2.0

    ParseError

    pub suberror ParseError {
    Message(String, Int)
    }

    impl Eq for ParseError
    impl Show for ParseError

    Field

    pub struct Field {
    name : String
    ty : TypeExpr
    }

    impl Eq for Field
    impl Show for Field

    Function

    pub struct Function {
    name : String
    kind : FunctionKind
    params : Array[Param]
    result : TypeExpr?
    docs : String?
    }

    impl Eq for Function
    impl Show for Function

    FunctionKind

    pub enum FunctionKind {
    Freestanding
    Constructor(String)
    Method(String)
    Static(String)
    }

    impl Eq for FunctionKind

    InlineInterface

    pub struct InlineInterface {
    name : String
    docs : String?
    items : Array[InterfaceItem]
    }

    Interface

    pub struct Interface {
    name : String
    docs : String?
    items : Array[InterfaceItem]
    }

    impl Eq for Interface
    impl Show for Interface

    InterfaceItem

    pub enum InterfaceItem {
    Use(UseDecl)
    TypeDef(TypeDef)
    Function(Function)
    }

    impl Eq for InterfaceItem

    PackageName

    pub struct PackageName {
    ns : String
    name : String
    version : String?
    }

    impl Eq for PackageName
    impl Show for PackageName

    Param

    pub struct Param {
    name : String
    ty : TypeExpr
    }

    impl Eq for Param
    impl Show for Param

    RFunction

    pub struct RFunction {
    name : String
    kind : RFunctionKind
    params : Array[(String, RType)]
    result : RType?
    docs : String?
    }

    Function definition
    impl Eq for RFunction
    impl Show for RFunction
    impl ToJson for RFunction

    RFunctionKind

    pub enum RFunctionKind {
    Freestanding
    Constructor(Int)
    Method(Int)
    Static(Int)
    }

    Function kind
    impl Eq for RFunctionKind

    RHandleKind

    pub enum RHandleKind {
    Own(Int)
    Borrow(Int)
    }

    impl Eq for RHandleKind
    impl Show for RHandleKind

    RInterface

    pub struct RInterface {
    name : String?
    docs : String?
    functions : Map[String, RFunction]
    types : Map[String, Int]
    pkg : Int?
    }

    impl Eq for RInterface
    impl Show for RInterface

    RInterfaceRef

    pub struct RInterfaceRef {
    id : Int
    }

    impl Eq for RInterfaceRef

    RPackage

    pub struct RPackage {
    name : RPackageName
    interfaces : Map[String, Int]
    worlds : Map[String, Int]
    }

    impl Eq for RPackage
    impl Show for RPackage
    impl ToJson for RPackage

    RPackageName

    pub struct RPackageName {
    ns : String
    name : String
    version : String?
    }

    impl Eq for RPackageName

    RRecordField

    pub struct RRecordField {
    name : String
    field_type : RType
    }

    impl Eq for RRecordField

    RType

    pub enum RType {
    Bool
    U8
    U16
    U32
    U64
    S8
    S16
    S32
    S64
    F32
    F64
    Char
    String_
    Id(Int)
    }

    Resolve type reference
    impl Eq for RType
    impl Show for RType
    impl ToJson for RType

    RTypeDef

    pub struct RTypeDef {
    name : String?
    kind : RTypeKind
    owner : RTypeOwner
    docs : String?
    }

    impl Eq for RTypeDef
    impl Show for RTypeDef
    impl ToJson for RTypeDef

    RTypeKind

    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)
    }

    impl Eq for RTypeKind
    impl Show for RTypeKind
    impl ToJson for RTypeKind

    RTypeOwner

    pub enum RTypeOwner {
    Interface(Int)
    World(Int)
    None
    }

    impl Eq for RTypeOwner
    impl Show for RTypeOwner

    RVariantCase

    pub struct RVariantCase {
    name : String
    case_type : RType?
    }

    impl Eq for RVariantCase

    RWorld

    pub struct RWorld {
    name : String
    docs : String?
    imports : Map[String, RWorldItem]
    exports : Map[String, RWorldItem]
    pkg : Int?
    }

    impl Eq for RWorld
    impl Show for RWorld
    impl ToJson for RWorld

    RWorldItem

    pub enum RWorldItem {
    Interface(RInterfaceRef)
    Function(RFunction)
    Type(Int)
    }

    World item
    impl Eq for RWorldItem
    impl Show for RWorldItem

    Resolve

    pub struct Resolve {
    worlds : Array[RWorld]
    interfaces : Array[RInterface]
    types : Array[RTypeDef]
    packages : Array[RPackage]
    }

    impl Eq for Resolve
    impl Show for Resolve
    impl ToJson for Resolve

    ResolveInput

    pub struct ResolveInput {
    resolve : Resolve
    world_id : Int
    }

    impl Eq for ResolveInput

    Resource

    pub struct Resource {
    funcs : Array[Function]
    }

    impl Eq for Resource
    impl Show for Resource

    Token

    pub struct Token {
    token_type : TokenType
    offset : Int
    }

    impl Eq for Token
    impl Show for Token

    TokenType

    pub enum TokenType {
    Ident(String)
    Keyword(String)
    Symbol(String)
    StringLit(String)
    Number(String)
    Doc(String)
    Eof
    }

    impl Eq for TokenType
    impl Show for TokenType

    TypeDef

    pub struct TypeDef {
    name : String
    kind : TypeDefKind
    docs : String?
    }

    impl Eq for TypeDef
    impl Show for TypeDef

    TypeDefKind

    pub enum TypeDefKind {
    Record(Array[Field])
    Enum(Array[String])
    Flags(Array[String])
    Variant(Array[VariantCase])
    Alias(TypeExpr)
    Resource(Resource)
    }

    impl Eq for TypeDefKind
    impl Show for TypeDefKind

    TypeExpr

    pub enum TypeExpr {
    Bool
    U8
    U16
    U32
    U64
    S8
    S16
    S32
    S64
    F32
    F64
    Char
    String_
    Id(String)
    List(TypeExpr)
    Option(TypeExpr)
    Result(TypeExpr, TypeExpr?)
    Tuple(Array[TypeExpr])
    Own(String)
    Borrow(String)
    }

    impl Eq for TypeExpr
    impl Show for TypeExpr

    UseDecl

    pub struct UseDecl {
    from : String
    names : Array[String]
    }

    impl Eq for UseDecl
    impl Show for UseDecl

    UsePath

    pub struct UsePath {
    pkg : PackageName?
    interface : String
    }

    impl Eq for UsePath
    impl Show for UsePath

    VariantCase

    pub struct VariantCase {
    name : String
    ty : TypeExpr?
    }

    impl Eq for VariantCase
    impl Show for VariantCase

    WitFile

    pub struct WitFile {
    pkg : PackageName?
    interfaces : Array[Interface]
    worlds : Array[World]
    }

    impl Eq for WitFile
    impl Show for WitFile

    World

    pub struct World {
    name : String
    docs : String?
    types : Array[TypeDef]
    imports : Array[WorldItem]
    exports : Array[WorldItem]
    }

    impl Eq for World
    impl Show for World

    WorldItem

    pub enum WorldItem {
    Interface(UsePath)
    Function(Function)
    InlineInterface(InlineInterface)
    }

    impl Eq for WorldItem
    impl Show for WorldItem

    parse

    fn parse(source : String) -> Result[WitFile, ParseError]

    Parse a WIT source string into a WitFile.

    parse_path

    fn parse_path(path : String) -> Result[WitFile, ParseError]

    Parse a WIT file or directory into a WitFile.

    resolve

    fn resolve(source : String) -> Result[Resolve, ParseError]

    Resolve a WIT source string into a Resolve.

    resolve_path

    fn resolve_path(path : String, world? : String) -> Result[ResolveInput, ParseError]

    Resolve a WIT file or directory (with deps) into a ResolveInput.

    tokenize

    fn tokenize(source : String) -> Result[Array[Token], ParseError]