rune/ast/
item_fn.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
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    use crate::parse;
    use crate::SourceId;

    rt::<ast::ItemFn>("async fn hello() {}");
    assert!(
        parse::parse_all::<ast::ItemFn>("fn async hello() {}", SourceId::EMPTY, false).is_err()
    );

    let item = rt::<ast::ItemFn>("fn hello() {}");
    assert_eq!(item.args.len(), 0);

    let item = rt::<ast::ItemFn>("fn hello(foo, bar) {}");
    assert_eq!(item.args.len(), 2);

    rt::<ast::ItemFn>("pub fn hello(foo, bar) {}");
    rt::<ast::ItemFn>("pub async fn hello(foo, bar) {}");
    rt::<ast::ItemFn>("#[inline] fn hello(foo, bar) {}");

    let item = rt::<ast::ItemFn>("#[inline] pub async fn hello(foo, bar) {}");
    assert!(matches!(item.visibility, ast::Visibility::Public(..)));

    assert_eq!(item.args.len(), 2);
    assert_eq!(item.attributes.len(), 1);
    assert!(item.async_token.is_some());
    assert!(item.const_token.is_none());

    let item = rt::<ast::ItemFn>("#[inline] pub const fn hello(foo, bar) {}");
    assert!(matches!(item.visibility, ast::Visibility::Public(..)));

    assert_eq!(item.args.len(), 2);
    assert_eq!(item.attributes.len(), 1);
    assert!(item.async_token.is_none());
    assert!(item.const_token.is_some());
}

/// A function item.
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
#[non_exhaustive]
pub struct ItemFn {
    /// The attributes for the fn
    #[rune(iter, meta)]
    pub attributes: Vec<ast::Attribute>,
    /// The visibility of the `fn` item
    #[rune(option, meta)]
    pub visibility: ast::Visibility,
    /// The optional `const` keyword.
    #[rune(iter, meta)]
    pub const_token: Option<T![const]>,
    /// The optional `async` keyword.
    #[rune(iter, meta)]
    pub async_token: Option<T![async]>,
    /// The `fn` token.
    pub fn_token: T![fn],
    /// The name of the function.
    pub name: ast::Ident,
    /// The arguments of the function.
    pub args: ast::Parenthesized<ast::FnArg, T![,]>,
    /// The body of the function.
    pub body: ast::Block,
    /// Opaque identifier for fn item.
    #[rune(skip)]
    pub(crate) id: ItemId,
}

impl ItemFn {
    /// Get the descriptive span of this item, e.g. `pub fn foo()` instead of
    /// the span for the whole function declaration, body included.
    pub(crate) fn descriptive_span(&self) -> Span {
        if let Some(async_token) = &self.async_token {
            async_token.span().join(self.args.span())
        } else {
            self.fn_token.span().join(self.args.span())
        }
    }

    /// Test if function is an instance fn.
    pub(crate) fn is_instance(&self) -> bool {
        matches!(self.args.first(), Some((ast::FnArg::SelfValue(..), _)))
    }
}

item_parse!(Fn, ItemFn, "function item");

impl Peek for ItemFn {
    fn peek(p: &mut Peeker<'_>) -> bool {
        match (p.nth(0), p.nth(1)) {
            (K![fn], _) => true,
            (K![async], K![fn]) => true,
            (K![const], K![fn]) => true,
            _ => false,
        }
    }
}