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
use crate::core::ExportKind;
use crate::kw;
use crate::parser::{Parse, Parser, Result};
use crate::token::{Id, Index, NameAnnotation, Span};

/// A inline alias for component exported items.
///
/// Handles both `core export` and `export` aliases
#[derive(Debug)]
pub struct InlineExportAlias<'a, const CORE: bool> {
    /// The instance to alias the export from.
    pub instance: Index<'a>,
    /// The name of the export to alias.
    pub name: &'a str,
}

impl<'a, const CORE: bool> Parse<'a> for InlineExportAlias<'a, CORE> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        parser.parse::<kw::alias>()?;
        if CORE {
            parser.parse::<kw::core>()?;
        }
        parser.parse::<kw::export>()?;
        let instance = parser.parse()?;
        let name = parser.parse()?;
        Ok(Self { instance, name })
    }
}

/// An alias to a component item.
#[derive(Debug)]
pub struct Alias<'a> {
    /// Where this `alias` was defined.
    pub span: Span,
    /// An identifier that this alias is resolved with (optionally) for name
    /// resolution.
    pub id: Option<Id<'a>>,
    /// An optional name for this alias stored in the custom `name` section.
    pub name: Option<NameAnnotation<'a>>,
    /// The target of this alias.
    pub target: AliasTarget<'a>,
}

impl<'a> Alias<'a> {
    /// Parses only an outer type alias.
    pub fn parse_outer_core_type_alias(parser: Parser<'a>) -> 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 => {
                    return Err(parser.error("expected type for outer alias"))
                }
                ComponentOuterAliasKind::Type => {
                    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,
        })
    }
}

/// Represents the kind of instance export alias.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComponentExportAliasKind {
    /// The alias is to a core module export.
    CoreModule,
    /// The alias is to a function export.
    Func,
    /// The alias is to a value export.
    Value,
    /// The alias is to a type export.
    Type,
    /// The alias is to a component export.
    Component,
    /// The alias is to an instance export.
    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())
        }
    }
}

/// Represents the kind of outer alias.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComponentOuterAliasKind {
    /// The alias is to an outer core module.
    CoreModule,
    /// The alias is to an outer core type.
    CoreType,
    /// The alias is to an outer type.
    Type,
    /// The alias is to an outer component.
    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())
        }
    }
}

/// The target of a component alias.
#[derive(Debug)]
pub enum AliasTarget<'a> {
    /// The alias is to an export of a component instance.
    Export {
        /// The component instance exporting the item.
        instance: Index<'a>,
        /// The name of the exported item to alias.
        name: &'a str,
        /// The export kind of the alias.
        kind: ComponentExportAliasKind,
    },
    /// The alias is to an export of a module instance.
    CoreExport {
        /// The module instance exporting the item.
        instance: Index<'a>,
        /// The name of the exported item to alias.
        name: &'a str,
        /// The export kind of the alias.
        kind: ExportKind,
    },
    /// The alias is to an item from an outer component.
    Outer {
        /// The number of enclosing components to skip.
        outer: Index<'a>,
        /// The index of the item being aliased.
        index: Index<'a>,
        /// The outer alias kind.
        kind: ComponentOuterAliasKind,
    },
}