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 `[attributes]? returntype identifier? (( args ));`
14        ///
15        /// (( )) means ( ) chars
16        Operation(struct OperationNamespaceMember<'a> {
17            attributes: Option<ExtendedAttributeList<'a>>,
18            return_type: ReturnType<'a>,
19            identifier: Option<Identifier<'a>>,
20            args: Parenthesized<ArgumentList<'a>>,
21            semi_colon: term!(;),
22        }),
23        /// Parses `[attribute]? readonly attributetype type identifier;`
24        Attribute(struct AttributeNamespaceMember<'a> {
25            attributes: Option<ExtendedAttributeList<'a>>,
26            readonly: term!(readonly),
27            attribute: term!(attribute),
28            type_: AttributedType<'a>,
29            identifier: Identifier<'a>,
30            semi_colon: term!(;),
31        }),
32        /// Parses a const namespace member `[attributes]? const type identifier = value;`
33        Const(struct ConstNamespaceMember<'a> {
34            attributes: Option<ExtendedAttributeList<'a>>,
35            const_: term!(const),
36            const_type: ConstType<'a>,
37            identifier: Identifier<'a>,
38            assign: term!(=),
39            const_value: ConstValue<'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_attribute_namespace_member { "readonly attribute short name;" =>
51        "";
52        AttributeNamespaceMember;
53        attributes.is_none();
54        identifier.0 == "name";
55    });
56
57    test!(should_parse_operation_namespace_member { "short (long a, long b);" =>
58        "";
59        OperationNamespaceMember;
60        attributes.is_none();
61        identifier.is_none();
62    });
63
64    test!(should_parse_const_namespace_member { "const short name = 5;" =>
65        "";
66        ConstNamespaceMember;
67        attributes.is_none();
68        identifier.0 == "name";
69    });
70}