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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
//! Weedle - A WebIDL Parser
//!
//! Parses valid WebIDL definitions & produces a data structure starting from
//! [`Definitions`](struct.Definitions.html).
//!
//! ### Example
//!
//! ```
//! extern crate weedle;
//!
//! let parsed = weedle::parse("
//! interface Window {
//! readonly attribute Storage sessionStorage;
//! };
//! ").unwrap();
//! println!("{:?}", parsed);
//! ```
//!
//! Note:
//! This parser follows the grammar given at [WebIDL](https://heycam.github.io/webidl).
//!
//! If any flaws found when parsing string with a valid grammar, create an issue.
use self::argument::ArgumentList;
use self::attribute::ExtendedAttributeList;
use self::common::{Braced, Identifier, Parenthesized, PunctuatedNonEmpty};
use self::dictionary::DictionaryMembers;
use self::interface::{Inheritance, InterfaceMembers};
use self::literal::StringLit;
use self::mixin::MixinMembers;
use self::namespace::NamespaceMembers;
use self::types::{AttributedType, ReturnType};
pub use nom::{error::Error, Err, IResult};
#[macro_use]
mod macros;
#[macro_use]
mod whitespace;
#[macro_use]
pub mod term;
pub mod argument;
pub mod attribute;
pub mod common;
pub mod dictionary;
pub mod interface;
pub mod literal;
pub mod mixin;
pub mod namespace;
pub mod types;
/// A convenient parse function
///
/// ### Example
///
/// ```
/// extern crate weedle;
///
/// let parsed = weedle::parse("
/// interface Window {
/// readonly attribute Storage sessionStorage;
/// };
/// ").unwrap();
///
/// println!("{:?}", parsed);
/// ```
pub fn parse(raw: &str) -> Result<Definitions<'_>, Err<Error<&str>>> {
let (remaining, parsed) = Definitions::parse(raw)?;
assert!(
remaining.is_empty(),
"There is redundant raw data after parsing"
);
Ok(parsed)
}
pub trait Parse<'a>: Sized {
fn parse(input: &'a str) -> IResult<&'a str, Self>;
}
/// Parses WebIDL definitions. It is the root struct for a complete WebIDL definition.
///
/// ### Example
/// ```
/// use weedle::{Definitions, Parse};
///
/// let (_, parsed) = Definitions::parse("
/// interface Window {
/// readonly attribute Storage sessionStorage;
/// };
/// ").unwrap();
///
/// println!("{:?}", parsed);
/// ```
///
/// It is recommended to use [`parse`](fn.parse.html) instead.
pub type Definitions<'a> = Vec<Definition<'a>>;
ast_types! {
/// Parses a definition
enum Definition<'a> {
/// Parses `[attributes]? callback identifier = type ( (arg1, arg2, ..., argN)? );`
Callback(struct CallbackDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
callback: term!(callback),
identifier: Identifier<'a>,
assign: term!(=),
return_type: ReturnType<'a>,
arguments: Parenthesized<ArgumentList<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? callback interface identifier ( : inheritance )? { members };`
CallbackInterface(struct CallbackInterfaceDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
callback: term!(callback),
interface: term!(interface),
identifier: Identifier<'a>,
inheritance: Option<Inheritance<'a>>,
members: Braced<InterfaceMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? interface identifier ( : inheritance )? { members };`
Interface(struct InterfaceDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
interface: term!(interface),
identifier: Identifier<'a>,
inheritance: Option<Inheritance<'a>>,
members: Braced<InterfaceMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? interface mixin identifier { members };`
InterfaceMixin(struct InterfaceMixinDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
interface: term!(interface),
mixin: term!(mixin),
identifier: Identifier<'a>,
members: Braced<MixinMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? namespace identifier { members };`
Namespace(struct NamespaceDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
namespace: term!(namespace),
identifier: Identifier<'a>,
members: Braced<NamespaceMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? dictionary identifier ( : inheritance )? { members };`
Dictionary(struct DictionaryDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
dictionary: term!(dictionary),
identifier: Identifier<'a>,
inheritance: Option<Inheritance<'a>>,
members: Braced<DictionaryMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? partial interface identifier { members };`
PartialInterface(struct PartialInterfaceDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
partial: term!(partial),
interface: term!(interface),
identifier: Identifier<'a>,
members: Braced<InterfaceMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? partial interface mixin identifier { members };`
PartialInterfaceMixin(struct PartialInterfaceMixinDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
partial: term!(partial),
interface: term!(interface),
mixin: term!(mixin),
identifier: Identifier<'a>,
members: Braced<MixinMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? partial dictionary identifier { members };`
PartialDictionary(struct PartialDictionaryDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
partial: term!(partial),
dictionary: term!(dictionary),
identifier: Identifier<'a>,
members: Braced<DictionaryMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? partial namespace identifier { members };`
PartialNamespace(struct PartialNamespaceDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
partial: term!(partial),
namespace: term!(namespace),
identifier: Identifier<'a>,
members: Braced<NamespaceMembers<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? enum identifier { values };`
Enum(struct EnumDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
enum_: term!(enum),
identifier: Identifier<'a>,
values: Braced<EnumValueList<'a>>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? typedef attributedtype identifier;`
Typedef(struct TypedefDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
typedef: term!(typedef),
type_: AttributedType<'a>,
identifier: Identifier<'a>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? identifier includes identifier;`
IncludesStatement(struct IncludesStatementDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
lhs_identifier: Identifier<'a>,
includes: term!(includes),
rhs_identifier: Identifier<'a>,
semi_colon: term!(;),
}),
/// Parses `[attributes]? identifier implements identifier;`
Implements(struct ImplementsDefinition<'a> {
attributes: Option<ExtendedAttributeList<'a>>,
lhs_identifier: Identifier<'a>,
includes: term!(implements),
rhs_identifier: Identifier<'a>,
semi_colon: term!(;),
}),
}
}
/// Parses a non-empty enum value list
pub type EnumValueList<'a> = PunctuatedNonEmpty<StringLit<'a>, term!(,)>;
#[cfg(test)]
mod test {
use super::*;
test!(should_parse_includes_statement { "first includes second;" =>
"";
IncludesStatementDefinition;
attributes.is_none();
lhs_identifier.0 == "first";
rhs_identifier.0 == "second";
});
test!(should_parse_typedef { "typedef short Short;" =>
"";
TypedefDefinition;
attributes.is_none();
identifier.0 == "Short";
});
test!(should_parse_enum { r#"enum name { "first", "second" }; "# =>
"";
EnumDefinition;
attributes.is_none();
identifier.0 == "name";
values.body.list.len() == 2;
});
test!(should_parse_dictionary { "dictionary A { long c; long g; };" =>
"";
DictionaryDefinition;
attributes.is_none();
identifier.0 == "A";
inheritance.is_none();
members.body.len() == 2;
});
test!(should_parse_dictionary_inherited { "dictionary C : B { long e; long f; };" =>
"";
DictionaryDefinition;
attributes.is_none();
identifier.0 == "C";
inheritance.is_some();
members.body.len() == 2;
});
test!(should_parse_partial_namespace { "
partial namespace VectorUtils {
readonly attribute Vector unit;
double dotProduct(Vector x, Vector y);
Vector crossProduct(Vector x, Vector y);
};
" =>
"";
PartialNamespaceDefinition;
attributes.is_none();
identifier.0 == "VectorUtils";
members.body.len() == 3;
});
test!(should_parse_partial_dictionary { "partial dictionary C { long e; long f; };" =>
"";
PartialDictionaryDefinition;
attributes.is_none();
identifier.0 == "C";
members.body.len() == 2;
});
test!(should_parse_partial_interface_mixin { "
partial interface mixin WindowSessionStorage {
readonly attribute Storage sessionStorage;
};
" =>
"";
PartialInterfaceMixinDefinition;
attributes.is_none();
identifier.0 == "WindowSessionStorage";
members.body.len() == 1;
});
test!(should_parse_partial_interface { "
partial interface Window {
readonly attribute Storage sessionStorage;
};
" =>
"";
PartialInterfaceDefinition;
attributes.is_none();
identifier.0 == "Window";
members.body.len() == 1;
});
test!(should_parse_namespace { "
namespace VectorUtils {
readonly attribute Vector unit;
double dotProduct(Vector x, Vector y);
Vector crossProduct(Vector x, Vector y);
};
" =>
"";
NamespaceDefinition;
attributes.is_none();
identifier.0 == "VectorUtils";
members.body.len() == 3;
});
test!(should_parse_interface_mixin { "
interface mixin WindowSessionStorage {
readonly attribute Storage sessionStorage;
};
" =>
"";
InterfaceMixinDefinition;
attributes.is_none();
identifier.0 == "WindowSessionStorage";
members.body.len() == 1;
});
test!(should_parse_interface { "
interface Window {
readonly attribute Storage sessionStorage;
};
" =>
"";
InterfaceDefinition;
attributes.is_none();
identifier.0 == "Window";
members.body.len() == 1;
});
test!(should_parse_callback_interface {"
callback interface Options {
attribute DOMString? option1;
attribute DOMString? option2;
attribute long? option3;
};
" =>
"";
CallbackInterfaceDefinition;
attributes.is_none();
identifier.0 == "Options";
members.body.len() == 3;
});
test!(should_parse_callback { "callback AsyncOperationCallback = undefined (DOMString status);" =>
"";
CallbackDefinition;
attributes.is_none();
identifier.0 == "AsyncOperationCallback";
arguments.body.list.len() == 1;
});
test!(should_parse_with_line_comments { "
// This is a comment
callback AsyncOperationCallback = undefined (DOMString status);
" =>
"";
CallbackDefinition;
});
test!(should_parse_with_block_comments { "
/* This is a comment */
callback AsyncOperationCallback = undefined (DOMString status);
" =>
"";
CallbackDefinition;
});
test!(should_parse_with_multiple_comments { "
// This is a comment
// This is a comment
// This is a comment
// This is a comment
callback AsyncOperationCallback = undefined (DOMString status);
" =>
"";
CallbackDefinition;
});
}
|