Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wit-smith): add wit-smith for include with syntax #1057

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/wit-component/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ impl<'a> ComponentInfo<'a> {
imports: Default::default(),
exports: Default::default(),
package: None,
includes: Default::default(),
include_names: Default::default(),
});
let mut decoder = WitPackageDecoder {
resolve,
Expand Down Expand Up @@ -752,6 +754,8 @@ impl WitPackageDecoder<'_> {
docs: Default::default(),
imports: Default::default(),
exports: Default::default(),
includes: Default::default(),
include_names: Default::default(),
package: None,
};

Expand Down
2 changes: 2 additions & 0 deletions crates/wit-component/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl Default for Bindgen {
docs: Default::default(),
imports: Default::default(),
exports: Default::default(),
includes: Default::default(),
include_names: Default::default(),
package: Some(package),
});
resolve.packages[package]
Expand Down
22 changes: 11 additions & 11 deletions crates/wit-component/tests/interfaces/diamond.wat
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,10 @@
(export (;1;) "the-enum" (type (eq 0)))
)
)
(import "foo" (instance (;1;) (type 2)))
(type (;3;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "the-enum" (type (eq 0)))
)
)
(import "bar" (instance (;2;) (type 3)))
(export (;1;) "bar" (instance (type 2)))
)
)
(export (;0;) (interface "foo:foo/w1") (component (type 1)))
(export (;0;) (interface "foo:foo/w3") (component (type 1)))
(type (;2;)
(component
(type (;0;)
Expand Down Expand Up @@ -78,10 +71,17 @@
(export (;1;) "the-enum" (type (eq 0)))
)
)
(export (;1;) "bar" (instance (type 2)))
(import "foo" (instance (;1;) (type 2)))
(type (;3;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "the-enum" (type (eq 0)))
)
)
(import "bar" (instance (;2;) (type 3)))
)
)
(export (;2;) (interface "foo:foo/w3") (component (type 3)))
(export (;2;) (interface "foo:foo/w1") (component (type 3)))
)
)
(@producers
Expand Down
14 changes: 7 additions & 7 deletions crates/wit-component/tests/interfaces/diamond.wit.print
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ interface shared-items {

}

world w1 {
world w3 {
import shared-items
import foo: interface {
use shared-items.{the-enum}
}
import bar: interface {
export bar: interface {
use shared-items.{the-enum}
}
}
Expand All @@ -25,9 +22,12 @@ world w2 {
use shared-items.{the-enum}
}
}
world w3 {
world w1 {
import shared-items
export bar: interface {
import foo: interface {
use shared-items.{the-enum}
}
import bar: interface {
use shared-items.{the-enum}
}
}
30 changes: 15 additions & 15 deletions crates/wit-component/tests/interfaces/world-top-level.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
(type (;0;)
(component
(type (;0;)
(component
(type (;0;) (func))
(import "foo" (func (;0;) (type 0)))
)
)
(export (;0;) (interface "foo:foo/just-import") (component (type 0)))
(type (;1;)
(component
(type (;0;) (func))
(export (;0;) "foo" (func (type 0)))
)
)
(export (;1;) (interface "foo:foo/just-export") (component (type 1)))
(type (;2;)
(component
(type (;0;)
(instance)
Expand All @@ -20,21 +34,7 @@
(export (;3;) "bar2" (func (type 4)))
)
)
(export (;0;) (interface "foo:foo/foo") (component (type 0)))
(type (;1;)
(component
(type (;0;) (func))
(import "foo" (func (;0;) (type 0)))
)
)
(export (;1;) (interface "foo:foo/just-import") (component (type 1)))
(type (;2;)
(component
(type (;0;) (func))
(export (;0;) "foo" (func (type 0)))
)
)
(export (;2;) (interface "foo:foo/just-export") (component (type 2)))
(export (;2;) (interface "foo:foo/foo") (component (type 2)))
)
)
(@producers
Expand Down
12 changes: 6 additions & 6 deletions crates/wit-component/tests/interfaces/world-top-level.wit.print
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package foo:foo

world just-import {
import foo: func()
}
world just-export {
export foo: func()
}
world foo {
import some-interface: interface {
}
Expand All @@ -10,9 +16,3 @@ world foo {
export foo2: func()
export bar2: func() -> u32
}
world just-import {
import foo: func()
}
world just-export {
export foo: func()
}
24 changes: 12 additions & 12 deletions crates/wit-component/tests/interfaces/worlds-with-types.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@
)
(export (;0;) (interface "foo:foo/import-me") (instance (type 0)))
(type (;1;)
(component
(type (;0;) (record))
(import "foo" (type (;1;) (eq 0)))
(import "bar" (type (;2;) (eq 1)))
(type (;3;) (func (param "a" 1) (result 2)))
(import "a" (func (;0;) (type 3)))
(export (;1;) "b" (func (type 3)))
)
)
(export (;0;) (interface "foo:foo/simple") (component (type 1)))
(type (;2;)
(component
(type (;0;)
(instance
Expand All @@ -35,7 +24,18 @@
(export (;1;) "b" (func (type 3)))
)
)
(export (;1;) (interface "foo:foo/with-imports") (component (type 2)))
(export (;0;) (interface "foo:foo/with-imports") (component (type 1)))
(type (;2;)
(component
(type (;0;) (record))
(import "foo" (type (;1;) (eq 0)))
(import "bar" (type (;2;) (eq 1)))
(type (;3;) (func (param "a" 1) (result 2)))
(import "a" (func (;0;) (type 3)))
(export (;1;) "b" (func (type 3)))
)
)
(export (;1;) (interface "foo:foo/simple") (component (type 2)))
)
)
(@producers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ interface import-me {

}

world with-imports {
import import-me
import a: func(a: foo)
use import-me.{foo}
export b: func(a: foo)
}
world simple {
import a: func(a: foo) -> bar
record foo {
Expand All @@ -14,9 +20,3 @@ world simple {

export b: func(a: foo) -> bar
}
world with-imports {
import import-me
import a: func(a: foo)
use import-me.{foo}
export b: func(a: foo)
}
84 changes: 77 additions & 7 deletions crates/wit-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ impl<'a> Ast<'a> {

fn for_each_path<'b>(
&'b self,
mut f: impl FnMut(Option<&'b Id<'a>>, &'b UsePath<'a>, Option<&'b [UseName<'a>]>) -> Result<()>,
mut f: impl FnMut(
Option<&'b Id<'a>>,
&'b UsePath<'a>,
Option<&'b [UseName<'a>]>,
WorldOrInterface,
) -> Result<()>,
) -> Result<()> {
for item in self.items.iter() {
match item {
Expand All @@ -49,7 +54,12 @@ impl<'a> Ast<'a> {
let mut exports = Vec::new();
for item in world.items.iter() {
match item {
WorldItem::Use(u) => f(None, &u.from, Some(&u.names))?,
WorldItem::Use(u) => {
f(None, &u.from, Some(&u.names), WorldOrInterface::Interface)?
}
WorldItem::Include(i) => {
f(Some(&world.name), &i.from, None, WorldOrInterface::World)?
}
WorldItem::Type(_) => {}
WorldItem::Import(Import { kind, .. }) => imports.push(kind),
WorldItem::Export(Export { kind, .. }) => exports.push(kind),
Expand All @@ -60,13 +70,18 @@ impl<'a> Ast<'a> {
ExternKind::Interface(_, items) => {
for item in items {
match item {
InterfaceItem::Use(u) => f(None, &u.from, Some(&u.names))?,
InterfaceItem::Use(u) => f(
None,
&u.from,
Some(&u.names),
WorldOrInterface::Interface,
)?,
_ => {}
}
}
Ok(())
}
ExternKind::Path(path) => f(None, path, None),
ExternKind::Path(path) => f(None, path, None, WorldOrInterface::Interface),
ExternKind::Func(..) => Ok(()),
};

Expand All @@ -80,13 +95,20 @@ impl<'a> Ast<'a> {
AstItem::Interface(i) => {
for item in i.items.iter() {
match item {
InterfaceItem::Use(u) => f(Some(&i.name), &u.from, Some(&u.names))?,
InterfaceItem::Use(u) => f(
Some(&i.name),
&u.from,
Some(&u.names),
WorldOrInterface::Interface,
)?,
_ => {}
}
}
}
AstItem::Use(u) => {
f(None, &u.item, None)?;
// At the top-level, we don't know if this is a world or an interface
// It is up to the resolver to decides how to handle this ambiguity.
f(None, &u.item, None, WorldOrInterface::Unknown)?;
}
}
}
Expand Down Expand Up @@ -199,6 +221,7 @@ enum WorldItem<'a> {
Export(Export<'a>),
Use(Use<'a>),
Type(TypeDef<'a>),
Include(Include<'a>),
}

impl<'a> WorldItem<'a> {
Expand All @@ -220,9 +243,10 @@ impl<'a> WorldItem<'a> {
}
Some((_span, Token::Union)) => TypeDef::parse_union(tokens, docs).map(WorldItem::Type),
Some((_span, Token::Enum)) => TypeDef::parse_enum(tokens, docs).map(WorldItem::Type),
Some((_span, Token::Include)) => Include::parse(tokens).map(WorldItem::Include),
other => Err(err_expected(
tokens,
"`import`, `export`, `use`, or type definition",
"`import`, `export`, `include`, `use`, or type definition",
other,
)
.into()),
Expand Down Expand Up @@ -332,6 +356,13 @@ impl<'a> Interface<'a> {
}
}

#[derive(Debug)]
pub enum WorldOrInterface {
World,
Interface,
Unknown,
}

enum InterfaceItem<'a> {
TypeDef(TypeDef<'a>),
Value(Value<'a>),
Expand Down Expand Up @@ -416,6 +447,45 @@ impl<'a> Use<'a> {
}
}

struct Include<'a> {
from: UsePath<'a>,
names: Vec<IncludeName<'a>>,
}

struct IncludeName<'a> {
name: Id<'a>,
as_: Id<'a>,
}

impl<'a> Include<'a> {
fn parse(tokens: &mut Tokenizer<'a>) -> Result<Self> {
tokens.expect(Token::Include)?;
let from = UsePath::parse(tokens)?;

let mut names = Vec::new();

match tokens.clone().next()? {
Some((_span, Token::With)) => {
tokens.expect(Token::With)?;
tokens.expect(Token::LeftBrace)?;
while !tokens.eat(Token::RightBrace)? {
let name = parse_id(tokens)?;
tokens.eat(Token::As)?;
let as_ = parse_id(tokens)?;
let name = IncludeName { name, as_ };
names.push(name);
if !tokens.eat(Token::Comma)? {
tokens.expect(Token::RightBrace)?;
break;
}
}
}
_ => {}
}
Ok(Include { from, names })
}
}

#[derive(Debug, Clone)]
pub struct Id<'a> {
name: &'a str,
Expand Down
Loading