Skip to content

Commit 05c9dad

Browse files
committed
Add ConstructorInterfaceMember
1 parent f93999a commit 05c9dad

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/interface.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ ast_types! {
3737
identifier: Identifier<'a>,
3838
semi_colon: term!(;),
3939
}),
40+
/// Parses `[attributes]? constructor(( args ));`
41+
///
42+
/// (( )) means ( ) chars
43+
Constructor(struct ConstructorInterfaceMember<'a> {
44+
attributes: Option<ExtendedAttributeList<'a>>,
45+
constructor: term!(constructor),
46+
args: Parenthesized<ArgumentList<'a>>,
47+
semi_colon: term!(;),
48+
}),
4049
/// Parses `[attributes]? (stringifier|static)? special? returntype identifier? (( args ));`
4150
///
4251
/// (( )) means ( ) chars
@@ -169,6 +178,12 @@ mod test {
169178
attributes.is_none();
170179
});
171180

181+
test!(should_parse_constructor_interface_member { "constructor(long a);" =>
182+
"";
183+
ConstructorInterfaceMember;
184+
attributes.is_none();
185+
});
186+
172187
test!(should_parse_operation_interface_member { "void readString(long a, long b);" =>
173188
"";
174189
OperationInterfaceMember;

src/term.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ generate_terms_for_names! {
300300

301301
/// Represents the terminal symbol `legacycaller`
302302
LegacyCaller => "legacycaller",
303+
304+
/// Represents the terminal symbol `constructor`
305+
Constructor => "constructor",
303306
}
304307

305308
#[macro_export]
@@ -550,6 +553,9 @@ macro_rules! term {
550553
(legacycaller) => {
551554
$crate::term::LegacyCaller
552555
};
556+
(constructor) => {
557+
$crate::term::Constructor
558+
};
553559
}
554560

555561
#[cfg(test)]
@@ -680,5 +686,6 @@ mod test {
680686
error, Error, "Error";
681687
implements, Implements, "implements";
682688
legacycaller, LegacyCaller, "legacycaller";
689+
constructor, Constructor, "constructor";
683690
];
684691
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface InterfaceWithConstructor {
2+
[Throws]
3+
constructor(long a);
4+
};

tests/webidl.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ extern crate weedle;
33
use std::fs;
44
use std::io::Read;
55

6+
use weedle::*;
7+
68
fn read_file(path: &str) -> String {
79
let mut file = fs::File::open(path).unwrap();
810
let mut file_content = String::new();
@@ -33,3 +35,65 @@ fn should_parse_mediacapture_streams_webidl() {
3335

3436
assert_eq!(parsed.len(), 37);
3537
}
38+
39+
#[test]
40+
fn interface_constructor() {
41+
let content = read_file("./tests/defs/interface-constructor.webidl");
42+
let mut parsed = weedle::parse(&content).unwrap();
43+
44+
assert_eq!(parsed.len(), 1);
45+
46+
let definition = parsed.pop().unwrap();
47+
48+
match definition {
49+
Definition::Interface(mut interface) => {
50+
assert!(interface.attributes.is_none());
51+
assert_eq!(interface.interface, term!(interface));
52+
assert_eq!(interface.identifier.0, "InterfaceWithConstructor");
53+
assert_eq!(interface.inheritance, None);
54+
55+
assert_eq!(interface.members.body.len(), 1);
56+
57+
let body = interface.members.body.pop().unwrap();
58+
59+
match body {
60+
interface::InterfaceMember::Constructor(constructor) => {
61+
let mut attributes = constructor.attributes.unwrap().body.list;
62+
assert_eq!(attributes.len(), 1);
63+
let attribute = attributes.pop().unwrap();
64+
65+
match attribute {
66+
attribute::ExtendedAttribute::NoArgs(attribute) => {
67+
assert_eq!((attribute.0).0, "Throws");
68+
}
69+
_ => unreachable!(),
70+
}
71+
72+
let mut args = constructor.args.body.list;
73+
assert_eq!(args.len(), 1);
74+
let arg = args.pop().unwrap();
75+
76+
match arg {
77+
argument::Argument::Single(arg) => {
78+
assert!(arg.attributes.is_none());
79+
assert!(arg.optional.is_none());
80+
assert!(arg.type_.attributes.is_none());
81+
82+
match arg.type_.type_ {
83+
types::Type::Single(types::SingleType::NonAny(
84+
types::NonAnyType::Integer(_),
85+
)) => {}
86+
_ => unreachable!(),
87+
}
88+
}
89+
_ => unreachable!(),
90+
};
91+
92+
assert_eq!(constructor.constructor, term::Constructor);
93+
}
94+
_ => unreachable!(),
95+
}
96+
}
97+
_ => unreachable!(),
98+
}
99+
}

0 commit comments

Comments
 (0)