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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::ast::{self, kw};
use wast::parser::{Parse, Parser, Result};
pub struct Wit<'a> {
pub module: Module<'a>,
}
impl<'a> Parse<'a> for Wit<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let module = parser.parens(|parser| parser.parse())?;
Ok(Wit { module })
}
}
pub struct Module<'a> {
pub core: wast::Module<'a>,
pub adapters: Vec<Adapter<'a>>,
}
impl Module<'_> {
pub fn encode(&mut self) -> std::result::Result<Vec<u8>, wast::Error> {
let names = self.core.resolve()?;
let core = match &self.core.kind {
wast::ModuleKind::Text(list) => &list[..],
wast::ModuleKind::Binary(_) => &[],
};
crate::resolve::resolve(core, &mut self.adapters, &names)?;
let mut core = self.core.encode()?;
crate::binary::append(&self.adapters, &mut core);
Ok(core)
}
}
impl<'a> Parse<'a> for Module<'a> {
fn parse(parser: Parser<'a>) -> Result<Module<'a>> {
let span = parser.parse::<kw::module>()?.0;
let name = parser.parse()?;
let mut fields = Vec::new();
let mut adapters = Vec::new();
while !parser.is_empty() {
parser.parens(|parser| {
if parser.peek::<ast::AtInterface>() {
adapters.push(parser.parse()?);
} else {
fields.push(parser.parse()?);
}
Ok(())
})?;
}
Ok(Module {
core: wast::Module {
span,
name,
kind: wast::ModuleKind::Text(fields),
},
adapters,
})
}
}
pub enum Adapter<'a> {
Type(ast::Type<'a>),
Import(ast::Import<'a>),
Export(ast::Export<'a>),
Func(ast::Func<'a>),
Implement(ast::Implement<'a>),
}
impl<'a> Parse<'a> for Adapter<'a> {
fn parse(parser: Parser<'a>) -> Result<Adapter<'a>> {
parser.parse::<ast::AtInterface>()?;
let mut l = parser.lookahead1();
if l.peek::<kw::r#type>() {
return Ok(Adapter::Type(parser.parse()?));
}
if l.peek::<kw::import>() {
return Ok(Adapter::Import(parser.parse()?));
}
if l.peek::<kw::export>() {
return Ok(Adapter::Export(parser.parse()?));
}
if l.peek::<kw::func>() {
return Ok(Adapter::Func(parser.parse()?));
}
if l.peek::<kw::implement>() {
return Ok(Adapter::Implement(parser.parse()?));
}
Err(l.error())
}
}