1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::ast::{self, kw};
use crate::parser::{Parse, Parser, Result};
#[derive(Debug)]
pub struct Instance<'a> {
pub span: ast::Span,
pub id: Option<ast::Id<'a>>,
pub name: Option<ast::NameAnnotation<'a>>,
pub exports: ast::InlineExport<'a>,
pub kind: InstanceKind<'a>,
}
#[derive(Debug)]
pub enum InstanceKind<'a> {
Import {
import: ast::InlineImport<'a>,
ty: ast::TypeUse<'a, ast::InstanceType<'a>>,
},
Inline {
module: ast::ItemRef<'a, kw::module>,
args: Vec<InstanceArg<'a>>,
},
}
#[derive(Debug)]
#[allow(missing_docs)]
pub struct InstanceArg<'a> {
pub name: &'a str,
pub index: ast::ItemRef<'a, ast::ExportKind>,
}
impl<'a> Parse<'a> for Instance<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let span = parser.parse::<kw::instance>()?.0;
let id = parser.parse()?;
let name = parser.parse()?;
let exports = parser.parse()?;
let kind = if let Some(import) = parser.parse()? {
InstanceKind::Import {
import,
ty: parser.parse()?,
}
} else {
parser.parens(|p| {
p.parse::<kw::instantiate>()?;
let module = p.parse::<ast::IndexOrRef<_>>()?.0;
let mut args = Vec::new();
while !p.is_empty() {
args.push(p.parens(|p| p.parse())?);
}
Ok(InstanceKind::Inline { module, args })
})?
};
Ok(Instance {
span,
id,
name,
exports,
kind,
})
}
}
impl<'a> Parse<'a> for InstanceArg<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::import>()?;
Ok(InstanceArg {
name: parser.parse()?,
index: parser.parse()?,
})
}
}