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
use crate::core::ExportKind;
use crate::kw;
use crate::parser::{Parse, Parser, Result};
use crate::token::{Id, Index, NameAnnotation, Span};
#[derive(Debug)]
pub struct InlineExportAlias<'a> {
pub instance: Index<'a>,
pub name: &'a str,
}
impl<'a> Parse<'a> for InlineExportAlias<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::alias>()?;
parser.parse::<kw::export>()?;
let instance = parser.parse()?;
let name = parser.parse()?;
Ok(Self { instance, name })
}
}
#[derive(Debug)]
pub struct Alias<'a> {
pub span: Span,
pub id: Option<Id<'a>>,
pub name: Option<NameAnnotation<'a>>,
pub target: AliasTarget<'a>,
}
impl<'a> Alias<'a> {
pub fn parse_outer_type_alias(parser: Parser<'a>, core_prefix_assumed: bool) -> Result<Self> {
let span = parser.parse::<kw::alias>()?.0;
parser.parse::<kw::outer>()?;
let outer = parser.parse()?;
let index = parser.parse()?;
let (kind, id, name) = parser.parens(|parser| {
let mut kind: ComponentOuterAliasKind = parser.parse()?;
match kind {
ComponentOuterAliasKind::CoreType | ComponentOuterAliasKind::Type => {
if core_prefix_assumed {
if kind == ComponentOuterAliasKind::CoreType {
return Err(parser.error("expected type for outer alias"));
}
kind = ComponentOuterAliasKind::CoreType;
}
}
_ => return Err(parser.error("expected core type or type for outer alias")),
}
Ok((kind, parser.parse()?, parser.parse()?))
})?;
Ok(Self {
span,
target: AliasTarget::Outer { outer, index, kind },
id,
name,
})
}
}
impl<'a> Parse<'a> for Alias<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let span = parser.parse::<kw::alias>()?.0;
let mut l = parser.lookahead1();
let (target, id, name) = if l.peek::<kw::outer>() {
parser.parse::<kw::outer>()?;
let outer = parser.parse()?;
let index = parser.parse()?;
let (kind, id, name) =
parser.parens(|parser| Ok((parser.parse()?, parser.parse()?, parser.parse()?)))?;
(AliasTarget::Outer { outer, index, kind }, id, name)
} else if l.peek::<kw::export>() {
parser.parse::<kw::export>()?;
let instance = parser.parse()?;
let export_name = parser.parse()?;
let (kind, id, name) =
parser.parens(|parser| Ok((parser.parse()?, parser.parse()?, parser.parse()?)))?;
(
AliasTarget::Export {
instance,
name: export_name,
kind,
},
id,
name,
)
} else if l.peek::<kw::core>() {
parser.parse::<kw::core>()?;
parser.parse::<kw::export>()?;
let instance = parser.parse()?;
let export_name = parser.parse()?;
let (kind, id, name) = parser.parens(|parser| {
parser.parse::<kw::core>()?;
Ok((parser.parse()?, parser.parse()?, parser.parse()?))
})?;
(
AliasTarget::CoreExport {
instance,
name: export_name,
kind,
},
id,
name,
)
} else {
return Err(l.error());
};
Ok(Self {
span,
target,
id,
name,
})
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComponentExportAliasKind {
CoreModule,
Func,
Value,
Type,
Component,
Instance,
}
impl<'a> Parse<'a> for ComponentExportAliasKind {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::core>() {
parser.parse::<kw::core>()?;
let mut l = parser.lookahead1();
if l.peek::<kw::module>() {
parser.parse::<kw::module>()?;
Ok(Self::CoreModule)
} else {
Err(l.error())
}
} else if l.peek::<kw::func>() {
parser.parse::<kw::func>()?;
Ok(Self::Func)
} else if l.peek::<kw::value>() {
parser.parse::<kw::value>()?;
Ok(Self::Value)
} else if l.peek::<kw::r#type>() {
parser.parse::<kw::r#type>()?;
Ok(Self::Type)
} else if l.peek::<kw::component>() {
parser.parse::<kw::component>()?;
Ok(Self::Component)
} else if l.peek::<kw::instance>() {
parser.parse::<kw::instance>()?;
Ok(Self::Instance)
} else {
Err(l.error())
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComponentOuterAliasKind {
CoreModule,
CoreType,
Type,
Component,
}
impl<'a> Parse<'a> for ComponentOuterAliasKind {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::core>() {
parser.parse::<kw::core>()?;
let mut l = parser.lookahead1();
if l.peek::<kw::module>() {
parser.parse::<kw::module>()?;
Ok(Self::CoreModule)
} else if l.peek::<kw::r#type>() {
parser.parse::<kw::r#type>()?;
Ok(Self::CoreType)
} else {
Err(l.error())
}
} else if l.peek::<kw::r#type>() {
parser.parse::<kw::r#type>()?;
Ok(Self::Type)
} else if l.peek::<kw::component>() {
parser.parse::<kw::component>()?;
Ok(Self::Component)
} else {
Err(l.error())
}
}
}
#[derive(Debug)]
pub enum AliasTarget<'a> {
Export {
instance: Index<'a>,
name: &'a str,
kind: ComponentExportAliasKind,
},
CoreExport {
instance: Index<'a>,
name: &'a str,
kind: ExportKind,
},
Outer {
outer: Index<'a>,
index: Index<'a>,
kind: ComponentOuterAliasKind,
},
}