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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use crate::ast::{self, kw};
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum ValType {
I32,
I64,
F32,
F64,
Anyref,
Funcref,
V128,
Nullref,
}
impl<'a> Parse<'a> for ValType {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::i32>() {
parser.parse::<kw::i32>()?;
Ok(ValType::I32)
} else if l.peek::<kw::i64>() {
parser.parse::<kw::i64>()?;
Ok(ValType::I64)
} else if l.peek::<kw::f32>() {
parser.parse::<kw::f32>()?;
Ok(ValType::F32)
} else if l.peek::<kw::f64>() {
parser.parse::<kw::f64>()?;
Ok(ValType::F64)
} else if l.peek::<kw::anyref>() {
parser.parse::<kw::anyref>()?;
Ok(ValType::Anyref)
} else if l.peek::<kw::funcref>() {
parser.parse::<kw::funcref>()?;
Ok(ValType::Funcref)
} else if l.peek::<kw::anyfunc>() {
parser.parse::<kw::anyfunc>()?;
Ok(ValType::Funcref)
} else if l.peek::<kw::nullref>() {
parser.parse::<kw::nullref>()?;
Ok(ValType::Nullref)
} else if l.peek::<kw::v128>() {
parser.parse::<kw::v128>()?;
Ok(ValType::V128)
} else {
Err(l.error())
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct GlobalType {
pub ty: ValType,
pub mutable: bool,
}
impl<'a> Parse<'a> for GlobalType {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.peek2::<kw::r#mut>() {
parser.parens(|p| {
p.parse::<kw::r#mut>()?;
Ok(GlobalType {
ty: parser.parse()?,
mutable: true,
})
})
} else {
Ok(GlobalType {
ty: parser.parse()?,
mutable: false,
})
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum TableElemType {
Funcref,
Anyref,
Nullref,
}
impl<'a> Parse<'a> for TableElemType {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.peek::<kw::anyfunc>() {
parser.parse::<kw::anyfunc>()?;
return Ok(TableElemType::Funcref);
}
let mut l = parser.lookahead1();
if l.peek::<kw::funcref>() {
parser.parse::<kw::funcref>()?;
Ok(TableElemType::Funcref)
} else if l.peek::<kw::anyref>() {
parser.parse::<kw::anyref>()?;
Ok(TableElemType::Anyref)
} else if l.peek::<kw::nullref>() {
parser.parse::<kw::nullref>()?;
Ok(TableElemType::Nullref)
} else {
Err(l.error())
}
}
}
impl Peek for TableElemType {
fn peek(cursor: Cursor<'_>) -> bool {
kw::funcref::peek(cursor)
|| kw::anyref::peek(cursor)
|| kw::anyfunc::peek(cursor)
}
fn display() -> &'static str {
"table element type"
}
}
#[derive(Copy, Clone, Debug)]
pub struct Limits {
pub min: u32,
pub max: Option<u32>,
}
impl<'a> Parse<'a> for Limits {
fn parse(parser: Parser<'a>) -> Result<Self> {
let min = parser.parse()?;
let max = if parser.peek::<u32>() {
Some(parser.parse()?)
} else {
None
};
Ok(Limits { min, max })
}
}
#[derive(Copy, Clone, Debug)]
pub struct TableType {
pub limits: Limits,
pub elem: TableElemType,
}
impl<'a> Parse<'a> for TableType {
fn parse(parser: Parser<'a>) -> Result<Self> {
Ok(TableType {
limits: parser.parse()?,
elem: parser.parse()?,
})
}
}
#[derive(Copy, Clone, Debug)]
pub struct MemoryType {
pub limits: Limits,
pub shared: bool,
}
impl<'a> Parse<'a> for MemoryType {
fn parse(parser: Parser<'a>) -> Result<Self> {
let limits: Limits = parser.parse()?;
let shared = parser.parse::<Option<kw::shared>>()?.is_some();
Ok(MemoryType { limits, shared })
}
}
#[derive(Clone, Debug)]
pub struct FunctionType<'a> {
pub params: Vec<(Option<ast::Id<'a>>, ValType)>,
pub results: Vec<ValType>,
}
impl<'a> FunctionType<'a> {
fn finish_parse(&mut self, allow_names: bool, parser: Parser<'a>) -> Result<()> {
while parser.peek2::<kw::param>() || parser.peek2::<kw::result>() {
parser.parens(|p| {
let mut l = p.lookahead1();
if l.peek::<kw::param>() {
if self.results.len() > 0 {
return Err(p.error(
"result before parameter (or unexpected token): \
cannot list params after results",
));
}
p.parse::<kw::param>()?;
if p.is_empty() {
return Ok(());
}
let id = if allow_names {
p.parse::<Option<_>>()?
} else {
None
};
let parse_more = id.is_none();
let ty = p.parse()?;
self.params.push((id, ty));
while parse_more && !p.is_empty() {
self.params.push((None, p.parse()?));
}
} else if l.peek::<kw::result>() {
p.parse::<kw::result>()?;
while !p.is_empty() {
self.results.push(p.parse()?);
}
} else {
return Err(l.error());
}
Ok(())
})?;
}
Ok(())
}
}
impl<'a> Parse<'a> for FunctionType<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::func>()?;
let mut ret = FunctionType {
params: Vec::new(),
results: Vec::new(),
};
ret.finish_parse(true, parser)?;
Ok(ret)
}
}
#[derive(Debug)]
pub struct Type<'a> {
pub name: Option<ast::Id<'a>>,
pub func: FunctionType<'a>,
}
impl<'a> Parse<'a> for Type<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::r#type>()?;
let name = parser.parse()?;
let func = parser.parens(FunctionType::parse)?;
Ok(Type { name, func })
}
}
#[derive(Clone, Debug)]
pub struct TypeUse<'a> {
pub index_span: Option<ast::Span>,
pub index: Option<ast::Index<'a>>,
pub ty: ast::FunctionType<'a>,
}
impl<'a> TypeUse<'a> {
pub fn parse_no_names(parser: Parser<'a>) -> Result<Self> {
TypeUse::parse_allow_names(parser, false)
}
fn parse_allow_names(parser: Parser<'a>, allow_names: bool) -> 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 ty = FunctionType {
params: Vec::new(),
results: Vec::new(),
};
if parser.peek2::<kw::param>() || parser.peek2::<kw::result>() {
ty.finish_parse(allow_names, parser)?;
}
Ok(TypeUse {
index,
index_span,
ty,
})
}
}
impl<'a> Parse<'a> for TypeUse<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
TypeUse::parse_allow_names(parser, true)
}
}