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

use sway_ast::ItemConst;

impl Parse for ItemConst {
    fn parse(parser: &mut Parser) -> ParseResult<ItemConst> {
        let visibility = parser.take();
        let const_token = parser.parse()?;
        let name = parser.parse()?;
        let ty_opt = match parser.take() {
            Some(colon_token) => {
                let ty = parser.parse()?;
                Some((colon_token, ty))
            }
            None => None,
        };
        let eq_token_opt = parser.take();
        let expr_opt = match &eq_token_opt {
            Some(_eq) => Some(parser.parse()?),
            None => None,
        };
        // Use the default here since the braces parsing is expecting
        // a semicolon, that allows us to re-use the same parsing code
        // between associated consts and module-level consts.
        let semicolon_token = parser.peek().unwrap_or_default();
        Ok(ItemConst {
            visibility,
            const_token,
            name,
            ty_opt,
            eq_token_opt,
            expr_opt,
            semicolon_token,
        })
    }
}