weedle/
namespace.rs

1use crate::argument::ArgumentList;
2use crate::attribute::ExtendedAttributeList;
3use crate::common::{Identifier, Parenthesized};
4use crate::literal::ConstValue;
5use crate::types::{AttributedType, ConstType, ReturnType};
6
7/// Parses namespace members declaration
8pub type NamespaceMembers<'a> = Vec<NamespaceMember<'a>>;
9
10ast_types! {
11    /// Parses namespace member declaration
12    enum NamespaceMember<'a> {
13        /// Parses a const interface member `[attributes]? const type identifier = value;`
14        Const(struct ConstNamespaceMember<'a> {
15            attributes: Option<ExtendedAttributeList<'a>>,
16            const_: term!(const),
17            const_type: ConstType<'a>,
18            identifier: Identifier<'a>,
19            assign: term!(=),
20            const_value: ConstValue<'a>,
21            semi_colon: term!(;),
22        }),
23        /// Parses `[attributes]? returntype identifier? (( args ));`
24        ///
25        /// (( )) means ( ) chars
26        Operation(struct OperationNamespaceMember<'a> {
27            attributes: Option<ExtendedAttributeList<'a>>,
28            return_type: ReturnType<'a>,
29            identifier: Option<Identifier<'a>>,
30            args: Parenthesized<ArgumentList<'a>>,
31            semi_colon: term!(;),
32        }),
33        /// Parses `[attribute]? readonly attributetype type identifier;`
34        Attribute(struct AttributeNamespaceMember<'a> {
35            attributes: Option<ExtendedAttributeList<'a>>,
36            readonly: term!(readonly),
37            attribute: term!(attribute),
38            type_: AttributedType<'a>,
39            identifier: Identifier<'a>,
40            semi_colon: term!(;),
41        }),
42    }
43}
44
45#[cfg(test)]
46mod test {
47    use super::*;
48    use crate::Parse;
49
50    test!(should_parse_const_member { "const long name = 5;" =>
51        "";
52        ConstNamespaceMember;
53        attributes.is_none();
54        identifier.0 == "name";
55    });
56
57    test!(should_parse_attribute_namespace_member { "readonly attribute short name;" =>
58        "";
59        AttributeNamespaceMember;
60        attributes.is_none();
61        identifier.0 == "name";
62    });
63
64    test!(should_parse_operation_namespace_member { "short (long a, long b);" =>
65        "";
66        OperationNamespaceMember;
67        attributes.is_none();
68        identifier.is_none();
69    });
70}