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
use proc_macro2::Span;
use proc_macro_error::abort;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
token, Ident, LitBool, LitStr, Token,
};
pub enum WasmerAttr {
Export {
identifier: Option<LitStr>,
optional: bool,
aliases: Vec<LitStr>,
span: Span,
},
}
#[derive(Debug)]
struct ExportExpr {
name: Option<LitStr>,
optional: bool,
aliases: Vec<LitStr>,
}
#[derive(Debug)]
struct ExportOptions {
name: Option<LitStr>,
optional: bool,
aliases: Vec<LitStr>,
}
impl Parse for ExportOptions {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let mut name = None;
let mut optional: bool = false;
let mut aliases: Vec<LitStr> = vec![];
loop {
let ident = input.parse::<Ident>()?;
let _ = input.parse::<Token![=]>()?;
let ident_str = ident.to_string();
match ident_str.as_str() {
"name" => {
name = Some(input.parse::<LitStr>()?);
}
"optional" => {
optional = input.parse::<LitBool>()?.value;
}
"alias" => {
let alias = input.parse::<LitStr>()?;
aliases.push(alias);
}
otherwise => {
abort!(
ident,
"Unrecognized argument in export options: expected `name = \"string\"`, `optional = bool`, or `alias = \"string\"` found `{}`",
otherwise
);
}
}
match input.parse::<Token![,]>() {
Ok(_) => continue,
Err(_) => break,
}
}
Ok(ExportOptions {
name,
optional,
aliases,
})
}
}
impl Parse for ExportExpr {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let name;
let optional;
let aliases;
if input.peek(Ident) {
let options = input.parse::<ExportOptions>()?;
name = options.name;
optional = options.optional;
aliases = options.aliases;
} else {
name = None;
optional = false;
aliases = vec![];
}
Ok(Self {
name,
optional,
aliases,
})
}
}
struct WasmerAttrInner(WasmerAttr);
impl Parse for WasmerAttrInner {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let ident: Ident = input.parse()?;
let ident_str = ident.to_string();
let span = ident.span();
let out = match ident_str.as_str() {
"export" => {
let export_expr;
let (name, optional, aliases) = if input.peek(token::Paren) {
let _: token::Paren = parenthesized!(export_expr in input);
let expr = export_expr.parse::<ExportExpr>()?;
(expr.name, expr.optional, expr.aliases)
} else {
(None, false, vec![])
};
WasmerAttr::Export {
identifier: name,
optional,
aliases,
span,
}
}
otherwise => abort!(
ident,
"Unexpected identifier `{}`. Expected `export`.",
otherwise
),
};
Ok(WasmerAttrInner(out))
}
}
impl Parse for WasmerAttr {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let attr_inner;
parenthesized!(attr_inner in input);
Ok(attr_inner.parse::<WasmerAttrInner>()?.0)
}
}