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
use chumsky::{select, Parser};

use crate::{
    lexer::{Kind, TagType},
    parser::{impl_parse, Prefix, See},
};

use super::Usage;

#[derive(Debug, Clone)]
pub struct Param {
    pub name: String,
    pub ty: String,
    pub desc: Vec<String>,
}

impl_parse!(Param, {
    select! { TagType::Param { name, ty, desc } => (name, ty, desc) }
        .then(select! { TagType::Comment(x) => x }.repeated())
        .map(|((name, ty, desc), extra)| {
            let desc = match desc {
                Some(d) => Vec::from([d])
                    .into_iter()
                    .chain(extra.into_iter())
                    .collect(),
                None => extra,
            };

            Self { name, ty, desc }
        })
});

#[derive(Debug, Clone)]
pub struct Return {
    pub ty: String,
    pub name: Option<String>,
    pub desc: Vec<String>,
}

impl_parse!(Return, {
    select! {
        TagType::Return { ty, name, desc } => (ty, name, desc)
    }
    .then(select! { TagType::Comment(x) => x }.repeated())
    .map(|((ty, name, desc), extra)| {
        let desc = match desc {
            Some(d) => Vec::from([d])
                .into_iter()
                .chain(extra.into_iter())
                .collect(),
            None => extra,
        };

        Self { name, ty, desc }
    })
});

#[derive(Debug, Clone)]
pub struct Func {
    pub name: String,
    pub kind: Kind,
    pub prefix: Prefix,
    pub desc: Vec<String>,
    pub params: Vec<Param>,
    pub returns: Vec<Return>,
    pub see: See,
    pub usage: Option<Usage>,
}

impl_parse!(Func, {
    select! {
        TagType::Comment(x) => x,
    }
    .repeated()
    .then(Param::parse().repeated())
    .then(Return::parse().repeated())
    .then(See::parse())
    .then(Usage::parse().or_not())
    .then(select! { TagType::Func { prefix, name, kind } => (prefix, name, kind) })
    .map(
        |(((((desc, params), returns), see), usage), (prefix, name, kind))| Self {
            name,
            kind,
            prefix: Prefix {
                left: prefix.clone(),
                right: prefix,
            },
            desc,
            params,
            returns,
            see,
            usage,
        },
    )
});

impl Func {
    pub fn rename_tag(&mut self, tag: String) {
        self.prefix.right = Some(tag);
    }

    pub fn is_public(&self, export: &str) -> bool {
        self.kind != Kind::Local && self.prefix.left.as_deref() == Some(export)
    }
}