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
use crate::ast::{self, kw};
use crate::parser::{Parse, Parser, Result};
#[derive(Debug)]
pub struct Import<'a> {
pub span: ast::Span,
pub module: &'a str,
pub field: &'a str,
pub id: Option<ast::Id<'a>>,
pub name: Option<ast::NameAnnotation<'a>>,
pub kind: ImportKind<'a>,
}
#[derive(Debug)]
#[allow(missing_docs)]
pub enum ImportKind<'a> {
Func(ast::TypeUse<'a>),
Table(ast::TableType),
Memory(ast::MemoryType),
Global(ast::GlobalType<'a>),
Event(ast::EventType<'a>),
}
impl<'a> Parse<'a> for Import<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let span = parser.parse::<kw::import>()?.0;
let module = parser.parse()?;
let field = parser.parse()?;
let (id, name, kind) = parser.parens(|parser| {
let mut l = parser.lookahead1();
if l.peek::<kw::func>() {
parser.parse::<kw::func>()?;
Ok((
parser.parse()?,
parser.parse()?,
ImportKind::Func(parser.parse()?),
))
} else if l.peek::<kw::table>() {
parser.parse::<kw::table>()?;
Ok((parser.parse()?, None, ImportKind::Table(parser.parse()?)))
} else if l.peek::<kw::memory>() {
parser.parse::<kw::memory>()?;
Ok((parser.parse()?, None, ImportKind::Memory(parser.parse()?)))
} else if l.peek::<kw::global>() {
parser.parse::<kw::global>()?;
Ok((parser.parse()?, None, ImportKind::Global(parser.parse()?)))
} else if l.peek::<kw::event>() {
parser.parse::<kw::event>()?;
Ok((parser.parse()?, None, ImportKind::Event(parser.parse()?)))
} else {
Err(l.error())
}
})?;
Ok(Import {
span,
module,
field,
id,
kind,
name,
})
}
}