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

use sway_ast::{
    attribute::Annotated,
    keywords::{ColonToken, InToken},
    Braces, CommaToken, Expr, ItemStorage, Punctuated, StorageEntry, StorageField,
};
use sway_types::BaseIdent;

impl Parse for StorageEntry {
    fn parse(parser: &mut Parser) -> ParseResult<StorageEntry> {
        let name: BaseIdent = parser.parse()?;
        let mut field = None;
        let mut namespace = None;
        if parser.peek::<ColonToken>().is_some() || parser.peek::<InToken>().is_some() {
            let mut f: StorageField = parser.parse()?;
            f.name = name.clone();
            field = Some(f);
        } else {
            let n: Braces<Punctuated<Annotated<Box<StorageEntry>>, CommaToken>> = parser.parse()?;
            namespace = Some(n);
        }
        Ok(StorageEntry {
            name,
            namespace,
            field,
        })
    }
}

impl Parse for StorageField {
    fn parse(parser: &mut Parser) -> ParseResult<StorageField> {
        let name = BaseIdent::dummy(); // Name will be overridden in StorageEntry parse.
        let in_token: Option<InToken> = parser.take();
        let mut key_opt: Option<Expr> = None;
        if in_token.is_some() {
            key_opt = Some(parser.parse()?);
        }
        let colon_token = parser.parse()?;
        let ty = parser.parse()?;
        let eq_token = parser.parse()?;
        let initializer = parser.parse()?;
        Ok(StorageField {
            name,
            in_token,
            key_expr: key_opt,
            colon_token,
            ty,
            eq_token,
            initializer,
        })
    }
}

impl Parse for ItemStorage {
    fn parse(parser: &mut Parser) -> ParseResult<ItemStorage> {
        let storage_token = parser.parse()?;
        let entries = parser.parse()?;
        Ok(ItemStorage {
            storage_token,
            entries,
        })
    }
}