Skip to content
Merged
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
1 change: 1 addition & 0 deletions crates/gen-wasmtime-py/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ mod tests {
let resource_id = iface.resources.alloc(Resource {
docs: Docs::default(),
name: "foo".into(),
supertype: None,
foreign_module: None,
});
iface.resource_lookup.insert("foo".into(), resource_id);
Expand Down
13 changes: 12 additions & 1 deletion crates/parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct UseName<'a> {
pub struct Resource<'a> {
docs: Docs<'a>,
name: Id<'a>,
supertype: Option<Id<'a>>,
values: Vec<(bool, Value<'a>)>,
}

Expand Down Expand Up @@ -377,6 +378,11 @@ impl<'a> Resource<'a> {
fn parse(tokens: &mut Tokenizer<'a>, docs: Docs<'a>) -> Result<Self> {
tokens.expect(Token::Resource)?;
let name = parse_id(tokens)?;
let supertype = if tokens.eat(Token::Implements)? {
Some(parse_id(tokens)?)
} else {
None
};
let mut values = Vec::new();
if tokens.eat(Token::LeftBrace)? {
loop {
Expand All @@ -388,7 +394,12 @@ impl<'a> Resource<'a> {
values.push((statik, Value::parse(tokens, docs)?));
}
}
Ok(Resource { docs, name, values })
Ok(Resource {
docs,
name,
supertype,
values,
})
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/parser/src/ast/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum Token {
Tuple,
Async,
Unit,
Implements,

Id,
ExplicitId,
Expand Down Expand Up @@ -272,6 +273,7 @@ impl<'a> Tokenizer<'a> {
"tuple" => Tuple,
"async" => Async,
"unit" => Unit,
"implements" => Implements,
_ => Id,
}
}
Expand Down Expand Up @@ -540,6 +542,7 @@ impl Token {
Tuple => "keyword `tuple`",
Async => "keyword `async`",
Unit => "keyword `unit`",
Implements => "keyword `implements`",
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl Resolver {
let resource = Resource {
docs: r.docs.clone(),
name: r.name.clone(),
supertype: r.supertype.clone(),
foreign_module: Some(
r.foreign_module
.clone()
Expand Down Expand Up @@ -279,6 +280,10 @@ impl Resolver {
let id = self.resources.alloc(Resource {
docs,
name: r.name.name.to_string(),
supertype: r
.supertype
.as_ref()
.map(|supertype| supertype.name.to_string()),
foreign_module: None,
});
self.define_resource(&r.name.name, r.name.span, id)?;
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ pub struct Docs {
pub struct Resource {
pub docs: Docs,
pub name: String,
pub supertype: Option<String>,
/// `None` if this resource is defined within the containing instance,
/// otherwise `Some` if it's defined in an instance named here.
pub foreign_module: Option<String>,
Expand Down
3 changes: 3 additions & 0 deletions crates/parser/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ fn to_json(i: &Interface) -> String {
struct Resource {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
supertype: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
foreign_module: Option<String>,
}

Expand Down Expand Up @@ -233,6 +235,7 @@ fn to_json(i: &Interface) -> String {
.iter()
.map(|(_, r)| Resource {
name: r.name.clone(),
supertype: r.supertype.as_ref().map(|supertype| supertype.clone()),
foreign_module: r.foreign_module.clone(),
})
.collect::<Vec<_>>();
Expand Down
5 changes: 5 additions & 0 deletions crates/parser/tests/ui/resource.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ resource f {
x: func()
y: func()
}
resource g implements a
resource h implements b {}
resource i implements e {
z: func()
}
31 changes: 31 additions & 0 deletions crates/parser/tests/ui/resource.wit.result
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
},
{
"name": "f"
},
{
"name": "g",
"supertype": "a"
},
{
"name": "h",
"supertype": "b"
},
{
"name": "i",
"supertype": "e"
}
],
"types": [
Expand All @@ -43,6 +55,18 @@
{
"idx": 5,
"primitive": "handle-5"
},
{
"idx": 6,
"primitive": "handle-6"
},
{
"idx": 7,
"primitive": "handle-7"
},
{
"idx": 8,
"primitive": "handle-8"
}
],
"functions": [
Expand All @@ -66,6 +90,13 @@
"handle-5"
],
"result": "unit"
},
{
"name": "i::z",
"params": [
"handle-8"
],
"result": "unit"
}
]
}