wit

WIT parser and resolver in MoonBit

wit
wasm
component
moon add mizchi/wit@0.3.2
Download zip
Author
Version
0.3.2
License
Apache-2.0
Last updated
17 hours ago
Downloads
63K

Dependencies

README

#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)
} derive(Eq,
Debug
)

impl Show for ParseError

#
Field

pub struct Field {
name : String
ty : TypeExpr
} derive(Eq,
Debug
)

impl Show for Field

#
Function

pub struct Function {
name : String
kind : FunctionKind
params : Array[Param]
result : TypeExpr?
docs : String?
} derive(Eq,
Debug
)

impl Show for Function

#
FunctionKind

pub enum FunctionKind {
Freestanding
Constructor(String)
Method(String)
Static(String)
} derive(Eq,
Debug
)

#
IncludeDecl

pub struct IncludeDecl {
path : UsePath
renames : Array[IncludeRename]
} derive(Eq,
Debug
)

impl Show for IncludeDecl

#
IncludeRename

pub struct IncludeRename {
from : String
to : String
} derive(Eq,
Debug
)

#
InlineInterface

pub struct InlineInterface {
name : String
docs : String?
items : Array[InterfaceItem]
} derive(Eq,
Debug
)

#
Interface

pub struct Interface {
name : String
docs : String?
items : Array[InterfaceItem]
} derive(Eq,
Debug
)

impl Show for Interface

#
InterfaceItem

pub enum InterfaceItem {
Use(UseDecl)
TypeDef(TypeDef)
Function(Function)
} derive(Eq,
Debug
)

#
PackageName

pub struct PackageName {
ns : String
name : String
version : String?
} derive(Eq,
Debug
)

impl Show for PackageName

#
Param

pub struct Param {
name : String
ty : TypeExpr
} derive(Eq,
Debug
)

impl Show for Param

#
RFunction

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

Function definition
impl Show for RFunction
impl ToJson for RFunction

#
RFunctionKind

pub enum RFunctionKind {
Freestanding
Constructor(Int)
Method(Int)
Static(Int)
} derive(Eq,
Debug
)

Function kind

#
RHandleKind

pub enum RHandleKind {
Own(Int)
Borrow(Int)
} derive(Eq,
Debug
)

impl Show for RHandleKind

#
RInterface

pub struct RInterface {
name : String?
docs : String?
functions : Map[String, RFunction]
types : Map[String, Int]
pkg : Int?
} derive(Eq,
Debug
)

impl Show for RInterface

#
RInterfaceRef

pub struct RInterfaceRef {
id : Int
} derive(Eq,
Debug
)

#
RPackage

pub struct RPackage {
name : RPackageName
interfaces : Map[String, Int]
worlds : Map[String, Int]
} derive(Eq,
Debug
)

impl Show for RPackage
impl ToJson for RPackage

#
RPackageName

pub struct RPackageName {
ns : String
name : String
version : String?
} derive(Eq,
Debug
)

#
RRecordField

pub struct RRecordField {
name : String
field_type : RType
} derive(Eq,
Debug
)

#
RType

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

Resolve type reference
impl Show for RType
impl ToJson for RType

#
RTypeDef

pub struct RTypeDef {
name : String?
kind : RTypeKind
owner : RTypeOwner
docs : String?
} derive(Eq,
Debug
)

impl Show for RTypeDef
impl ToJson for RTypeDef

#
RTypeKind

pub enum RTypeKind {
Record(Array[RRecordField])
Variant(Array[RVariantCase])
List(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
)

impl Show for RTypeKind
impl ToJson for RTypeKind

#
RTypeOwner

pub enum RTypeOwner {
Interface(Int)
World(Int)
None
} derive(Eq,
Debug
)

impl Show for RTypeOwner

#
RVariantCase

pub struct RVariantCase {
name : String
case_type : RType?
} derive(Eq,
Debug
)

#
RWorld

pub struct RWorld {
name : String
docs : String?
imports : Map[String, RWorldItem]
exports : Map[String, RWorldItem]
pkg : Int?
} derive(Eq,
Debug
)

impl Show for RWorld
impl ToJson for RWorld

#
RWorldItem

pub enum RWorldItem {
Interface(RInterfaceRef)
Function(RFunction)
Type(Int)
} derive(Eq,
Debug
)

World item
impl Show for RWorldItem

#
Resolve

pub struct Resolve {
worlds : Array[RWorld]
interfaces : Array[RInterface]
types : Array[RTypeDef]
packages : Array[RPackage]
} derive(Eq,
Debug
)

impl Show for Resolve
impl ToJson for Resolve

#
ResolveInput

pub struct ResolveInput {
resolve : Resolve
world_id : Int
} derive(Eq,
Debug
)

#
ResolvedInterfaceInfo

pub struct ResolvedInterfaceInfo {
interface_id : Int
name : String?
docs : String?
package_name : RPackageName?
functions : Map[String, RFunction]
types : Map[String, ResolvedTypeInfo]
} derive(Eq,
Debug
)

#
ResolvedTypeInfo

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

#
Resource

pub struct Resource {
funcs : Array[Function]
} derive(Eq,
Debug
)

impl Show for Resource

#
Token

pub struct Token {
token_type : TokenType
offset : Int
} derive(Eq,
Debug
)

impl Show for Token

#
TokenType

pub enum TokenType {
Ident(String)
Keyword(String)
Symbol(String)
StringLit(String)
Number(String)
Doc(String)
Eof
} derive(Eq,
Debug
)

impl Show for TokenType

#
TypeDef

pub struct TypeDef {
name : String
kind : TypeDefKind
docs : String?
} derive(Eq,
Debug
)

impl Show for TypeDef

#
TypeDefKind

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

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)
Future(TypeExpr)
Stream(TypeExpr)
Result(TypeExpr, TypeExpr?)
Tuple(Array[TypeExpr])
Own(String)
Borrow(String)
} derive(Eq,
Debug
)

impl Show for TypeExpr

#
UseDecl

pub struct UseDecl {
from : UsePath
names : Array[String]
} derive(Eq,
Debug
)

impl Show for UseDecl

#
UsePath

pub struct UsePath {
pkg : PackageName?
interface : String
} derive(Eq,
Debug
)

impl Show for UsePath

#
VariantCase

pub struct VariantCase {
name : String
ty : TypeExpr?
} derive(Eq,
Debug
)

impl Show for VariantCase

#
WitFile

pub struct WitFile {
pkg : PackageName?
interfaces : Array[Interface]
worlds : Array[World]
} derive(Eq,
Debug
)

impl Show for WitFile

#
World

pub struct World {
name : String
docs : String?
types : Array[TypeDef]
imports : Array[WorldItem]
exports : Array[WorldItem]
} derive(Eq,
Debug
)

impl Show for World

#
WorldItem

pub enum WorldItem {
Interface(UsePath)
Function(Function)
InlineInterface(InlineInterface)
Include(IncludeDecl)
} derive(Eq,
Debug
)

impl Show for WorldItem

#
interface_infos

fn interface_infos(resolve : Resolve) -> Array[ResolvedInterfaceInfo]

Build resolved interface infos from Resolve.

#
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_interface_infos

fn resolve_interface_infos(source : String) -> Result[Array[ResolvedInterfaceInfo], ParseError]

Resolve WIT source and return resolved interface infos.

#
resolve_multi

fn resolve_multi(asts : Array[WitFile]) -> Result[Resolve, ParseError]

Resolve multiple WIT ASTs into a single Resolve.

#
resolve_path

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

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

#
resolve_path_interface_infos

fn resolve_path_interface_infos(path : String, world? : String) -> Result[Array[ResolvedInterfaceInfo], ParseError]

Resolve WIT path and return resolved interface infos.

#
tokenize

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