sway_parse/item/
item_trait.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
use crate::{Parse, ParseBracket, ParseResult, Parser};

use sway_ast::attribute::Annotated;
use sway_ast::keywords::{ConstToken, FnToken, OpenAngleBracketToken, TypeToken, WhereToken};
use sway_ast::{Braces, ItemFn, ItemTrait, ItemTraitItem, PubToken, Traits};
use sway_error::parser_error::ParseErrorKind;

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

    fn error(
        spans: Box<[sway_types::Span]>,
        error: sway_error::handler::ErrorEmitted,
    ) -> Option<Self>
    where
        Self: Sized,
    {
        Some(ItemTraitItem::Error(spans, error))
    }
}

impl Parse for ItemTrait {
    fn parse(parser: &mut Parser) -> ParseResult<ItemTrait> {
        let visibility = parser.take();
        let trait_token = parser.parse()?;
        let name = parser.parse()?;
        let generics = parser.guarded_parse::<OpenAngleBracketToken, _>()?;
        let super_traits = match parser.take() {
            Some(colon_token) => {
                let traits = parser.parse()?;
                Some((colon_token, traits))
            }
            None => None,
        };
        let where_clause_opt = parser.guarded_parse::<WhereToken, _>()?;

        let trait_items: Braces<Vec<Annotated<ItemTraitItem>>> = parser.parse()?;
        for item in trait_items.get().iter() {
            if let ItemTraitItem::Fn(fn_sig, _) = &item.value {
                parser.ban_visibility_qualifier(&fn_sig.visibility)?;
            }
        }

        let trait_defs_opt: Option<Braces<Vec<Annotated<ItemFn>>>> = Braces::try_parse(parser)?;
        if let Some(trait_defs) = &trait_defs_opt {
            for item in trait_defs.get().iter() {
                parser.ban_visibility_qualifier(&item.value.fn_signature.visibility)?;
            }
        }

        Ok(ItemTrait {
            visibility,
            trait_token,
            name,
            generics,
            where_clause_opt,
            super_traits,
            trait_items,
            trait_defs_opt,
        })
    }
}

impl Parse for Traits {
    fn parse(parser: &mut Parser) -> ParseResult<Traits> {
        let prefix = parser.parse()?;
        let mut suffixes = Vec::new();
        while let Some(add_token) = parser.take() {
            let suffix = parser.parse()?;
            suffixes.push((add_token, suffix));
        }
        let traits = Traits { prefix, suffixes };
        Ok(traits)
    }
}