1 | use crate::component::*; |
2 | use crate::core; |
3 | use crate::kw; |
4 | use crate::parser::{Parse, Parser, Result}; |
5 | use crate::token::{Id, NameAnnotation, Span}; |
6 | |
7 | /// A core WebAssembly module to be created as part of a component. |
8 | /// |
9 | /// This is a member of the core module section. |
10 | #[derive (Debug)] |
11 | pub struct CoreModule<'a> { |
12 | /// Where this `core module` was defined. |
13 | pub span: Span, |
14 | /// An identifier that this module is resolved with (optionally) for name |
15 | /// resolution. |
16 | pub id: Option<Id<'a>>, |
17 | /// An optional name for this module stored in the custom `name` section. |
18 | pub name: Option<NameAnnotation<'a>>, |
19 | /// If present, inline export annotations which indicate names this |
20 | /// definition should be exported under. |
21 | pub exports: InlineExport<'a>, |
22 | /// What kind of module this is, be it an inline-defined or imported one. |
23 | pub kind: CoreModuleKind<'a>, |
24 | } |
25 | |
26 | /// Possible ways to define a core module in the text format. |
27 | #[derive (Debug)] |
28 | pub enum CoreModuleKind<'a> { |
29 | /// A core module which is actually defined as an import |
30 | Import { |
31 | /// Where this core module is imported from |
32 | import: InlineImport<'a>, |
33 | /// The type that this core module will have. |
34 | ty: CoreTypeUse<'a, ModuleType<'a>>, |
35 | }, |
36 | |
37 | /// Modules that are defined inline. |
38 | Inline { |
39 | /// Fields in the core module. |
40 | fields: Vec<core::ModuleField<'a>>, |
41 | }, |
42 | } |
43 | |
44 | impl<'a> Parse<'a> for CoreModule<'a> { |
45 | fn parse(parser: Parser<'a>) -> Result<Self> { |
46 | parser.depth_check()?; |
47 | |
48 | let span = parser.parse::<kw::core>()?.0; |
49 | parser.parse::<kw::module>()?; |
50 | let id = parser.parse()?; |
51 | let name = parser.parse()?; |
52 | let exports = parser.parse()?; |
53 | |
54 | let kind = if let Some(import) = parser.parse()? { |
55 | CoreModuleKind::Import { |
56 | import, |
57 | ty: parser.parse()?, |
58 | } |
59 | } else { |
60 | let mut fields = Vec::new(); |
61 | while !parser.is_empty() { |
62 | fields.push(parser.parens(|p| p.parse())?); |
63 | } |
64 | CoreModuleKind::Inline { fields } |
65 | }; |
66 | |
67 | Ok(Self { |
68 | span, |
69 | id, |
70 | name, |
71 | exports, |
72 | kind, |
73 | }) |
74 | } |
75 | } |
76 | |