sway_parse/item/
item_impl.rs

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
use crate::{Parse, ParseResult, Parser};

use sway_ast::attribute::Annotated;
use sway_ast::keywords::{
    ConstToken, FnToken, OpenAngleBracketToken, SemicolonToken, TypeToken, WhereToken,
};
use sway_ast::{Braces, ItemImpl, ItemImplItem, PubToken, Ty};
use sway_error::parser_error::ParseErrorKind;

impl Parse for ItemImplItem {
    fn parse(parser: &mut Parser) -> ParseResult<ItemImplItem> {
        if parser.peek::<PubToken>().is_some() || parser.peek::<FnToken>().is_some() {
            let fn_decl = parser.parse()?;
            Ok(ItemImplItem::Fn(fn_decl))
        } else if let Some(_const_keyword) = parser.peek::<ConstToken>() {
            let const_decl = parser.parse()?;
            parser.parse::<SemicolonToken>()?;
            Ok(ItemImplItem::Const(const_decl))
        } else if let Some(_type_keyword) = parser.peek::<TypeToken>() {
            let type_decl = parser.parse()?;
            parser.parse::<SemicolonToken>()?;
            Ok(ItemImplItem::Type(type_decl))
        } else {
            Err(parser.emit_error(ParseErrorKind::ExpectedAnItem))
        }
    }
}

impl Parse for ItemImpl {
    fn parse(parser: &mut Parser) -> ParseResult<ItemImpl> {
        let impl_token = parser.parse()?;
        let generic_params_opt = parser.guarded_parse::<OpenAngleBracketToken, _>()?;
        let ty = parser.parse()?;
        let (trait_opt, ty) = match parser.take() {
            Some(for_token) => match ty {
                Ty::Path(path_type) => (Some((path_type, for_token)), parser.parse()?),
                _ => {
                    return Err(parser.emit_error(ParseErrorKind::ExpectedPathType));
                }
            },
            None => (None, ty),
        };
        let where_clause_opt = parser.guarded_parse::<WhereToken, _>()?;
        let contents: Braces<Vec<Annotated<ItemImplItem>>> = parser.parse()?;
        if trait_opt.is_some() {
            for annotated in contents.get().iter() {
                if let ItemImplItem::Fn(item_fn) = &annotated.value {
                    parser.ban_visibility_qualifier(&item_fn.fn_signature.visibility)?;
                }
            }
        }
        Ok(ItemImpl {
            impl_token,
            generic_params_opt,
            trait_opt,
            ty,
            where_clause_opt,
            contents,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::parse;
    use assert_matches::*;

    #[test]
    fn parse_impl_ptr() {
        let item = parse::<ItemImpl>(
            r#"
            impl __ptr[T] {}
            "#,
        );
        assert_matches!(item.ty, Ty::Ptr { .. });
    }

    #[test]
    fn parse_impl_for_ptr() {
        let item = parse::<ItemImpl>(
            r#"
            impl Foo for __ptr[T] {}
            "#,
        );
        assert_matches!(item.ty, Ty::Ptr { .. });
    }

    #[test]
    fn parse_impl_slice() {
        // deprecated syntax
        let item = parse::<ItemImpl>("impl __slice[T] {}");
        assert_matches!(
            item.ty,
            Ty::Slice {
                slice_token: Some(..),
                ty: _
            }
        );

        // "new" syntax
        let item = parse::<ItemImpl>("impl [T] {}");
        assert_matches!(
            item.ty,
            Ty::Slice {
                slice_token: None,
                ty: _
            }
        );

        let item = parse::<ItemImpl>("impl &[T] {}");
        assert_matches!(item.ty, Ty::Ref { ty, .. } if matches!(&*ty, Ty::Slice { .. }));
    }

    #[test]
    fn parse_impl_for_slice() {
        let item = parse::<ItemImpl>(
            r#"
            impl Foo for __slice[T] {}
            "#,
        );
        assert_matches!(item.ty, Ty::Slice { .. });
    }

    #[test]
    fn parse_impl_ref() {
        let item = parse::<ItemImpl>(
            r#"
            impl &T {}
            "#,
        );
        assert_matches!(
            item.ty,
            Ty::Ref {
                mut_token: None,
                ..
            }
        );
    }

    #[test]
    fn parse_impl_for_ref() {
        let item = parse::<ItemImpl>(
            r#"
            impl Foo for &T {}
            "#,
        );
        assert_matches!(
            item.ty,
            Ty::Ref {
                mut_token: None,
                ..
            }
        );
    }

    #[test]
    fn parse_impl_mut_ref() {
        let item = parse::<ItemImpl>(
            r#"
            impl &mut T {}
            "#,
        );
        assert_matches!(
            item.ty,
            Ty::Ref {
                mut_token: Some(_),
                ..
            }
        );
    }

    #[test]
    fn parse_impl_for_mut_ref() {
        let item = parse::<ItemImpl>(
            r#"
            impl Foo for &mut T {}
            "#,
        );
        assert_matches!(
            item.ty,
            Ty::Ref {
                mut_token: Some(_),
                ..
            }
        );
    }
}