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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::ast::kw;
use wast::parser::{Parse, Parser, Result};
#[derive(Clone)]
pub struct Type<'a> {
pub name: Option<wast::Id<'a>>,
pub params: Vec<(Option<wast::Id<'a>>, ValType)>,
pub results: Vec<ValType>,
}
fn finish_parse<'a>(
parser: Parser<'a>,
params: &mut Vec<(Option<wast::Id<'a>>, ValType)>,
results: &mut Vec<ValType>,
) -> Result<()> {
while parser.peek2::<kw::param>() {
params.push(parser.parens(|p| {
p.parse::<kw::param>()?;
let id = p.parse()?;
let ty = p.parse()?;
Ok((id, ty))
})?);
}
while parser.peek2::<kw::result>() {
results.push(parser.parens(|p| {
p.parse::<kw::result>()?;
p.parse()
})?);
}
Ok(())
}
impl<'a> Parse<'a> for Type<'a> {
fn parse(parser: Parser<'a>) -> Result<Type<'a>> {
parser.parse::<kw::r#type>()?;
let name = parser.parse()?;
let mut params = Vec::new();
let mut results = Vec::new();
parser.parens(|p| {
p.parse::<kw::func>()?;
finish_parse(parser, &mut params, &mut results)
})?;
Ok(Type {
name,
params,
results,
})
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum ValType {
String,
Anyref,
S8,
S16,
S32,
S64,
I32,
I64,
U8,
U16,
U32,
U64,
F32,
F64,
}
impl<'a> Parse<'a> for ValType {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::s8>() {
parser.parse::<kw::s8>()?;
return Ok(ValType::S8);
}
if l.peek::<kw::s16>() {
parser.parse::<kw::s16>()?;
return Ok(ValType::S16);
}
if l.peek::<kw::s32>() {
parser.parse::<kw::s32>()?;
return Ok(ValType::S32);
}
if l.peek::<kw::s64>() {
parser.parse::<kw::s64>()?;
return Ok(ValType::S64);
}
if l.peek::<kw::u8>() {
parser.parse::<kw::u8>()?;
return Ok(ValType::U8);
}
if l.peek::<kw::u16>() {
parser.parse::<kw::u16>()?;
return Ok(ValType::U16);
}
if l.peek::<kw::u32>() {
parser.parse::<kw::u32>()?;
return Ok(ValType::U32);
}
if l.peek::<kw::u64>() {
parser.parse::<kw::u64>()?;
return Ok(ValType::U64);
}
if l.peek::<kw::f32>() {
parser.parse::<kw::f32>()?;
return Ok(ValType::F32);
}
if l.peek::<kw::f64>() {
parser.parse::<kw::f64>()?;
return Ok(ValType::F64);
}
if l.peek::<kw::i32>() {
parser.parse::<kw::i32>()?;
return Ok(ValType::I32);
}
if l.peek::<kw::i64>() {
parser.parse::<kw::i64>()?;
return Ok(ValType::I64);
}
if l.peek::<kw::anyref>() {
parser.parse::<kw::anyref>()?;
return Ok(ValType::Anyref);
}
if l.peek::<kw::string>() {
parser.parse::<kw::string>()?;
return Ok(ValType::String);
}
Err(l.error())
}
}
#[derive(Clone)]
pub struct TypeUse<'a> {
pub index_span: Option<wast::Span>,
pub index: Option<wast::Index<'a>>,
pub ty: Type<'a>,
}
impl<'a> Parse<'a> for TypeUse<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let index = if parser.peek2::<kw::r#type>() {
Some(parser.parens(|parser| {
parser.parse::<kw::r#type>()?;
Ok((parser.cur_span(), parser.parse()?))
})?)
} else {
None
};
let (index_span, index) = match index {
Some((a, b)) => (Some(a), Some(b)),
None => (None, None),
};
let mut params = Vec::new();
let mut results = Vec::new();
finish_parse(parser, &mut params, &mut results)?;
Ok(TypeUse {
index,
index_span,
ty: Type {
name: None,
params,
results,
},
})
}
}