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
use crate::ast::{self, kw};
use wast::parser::{Parse, Parser, Result};
pub struct Func<'a> {
pub span: wast::Span,
pub name: Option<wast::Id<'a>>,
pub export: Option<&'a str>,
pub ty: ast::TypeUse<'a>,
pub kind: FuncKind<'a>,
}
pub enum FuncKind<'a> {
Import {
module: &'a str,
name: &'a str,
},
#[allow(missing_docs)]
Inline { instrs: ast::Instructions<'a> },
}
impl<'a> Parse<'a> for Func<'a> {
fn parse(parser: Parser<'a>) -> Result<Func<'a>> {
let span = parser.parse::<kw::func>()?.0;
let name = parser.parse()?;
let export = if parser.peek2::<kw::export>() {
Some(parser.parens(|p| {
p.parse::<kw::export>()?;
p.parse()
})?)
} else {
None
};
let (ty, kind) = if parser.peek2::<kw::import>() {
let (module, name) = parser.parens(|p| {
p.parse::<kw::import>()?;
Ok((p.parse()?, p.parse()?))
})?;
(parser.parse()?, FuncKind::Import { module, name })
} else {
let ty = parser.parse()?;
let instrs = parser.parse()?;
(ty, FuncKind::Inline { instrs })
};
Ok(Func {
span,
name,
export,
ty,
kind,
})
}
}
#[allow(missing_docs)]
pub struct Instructions<'a> {
pub instrs: Vec<ast::Instruction<'a>>,
}
impl<'a> Parse<'a> for Instructions<'a> {
fn parse(parser: Parser<'a>) -> Result<Instructions<'a>> {
let mut instrs = Vec::new();
while !parser.is_empty() {
instrs.push(parser.parse()?);
}
Ok(Instructions { instrs })
}
}