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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! This module contains functions to generate a [`Vec`] of [`Value`] from a given input
//!
//! **The functions and structs here should _not_ be considered stable**

use std::fmt::Display;

use crate::utils::NotationMatching;

mod preprocessor;
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Default)]
pub struct NestedString {
    pub top: String,
    pub sub: Vec<NestedString>,
}


impl NestedString {
    pub fn new(top: String) -> Self {
        Self { top, sub: vec![] }
    }

    pub fn modify_sub(mut self, nesting_depth: usize, sublist: bool, line: String) -> NestedString {
        let mut refs_vec = vec![self];
        for _ in 1..nesting_depth {
            let mut high = refs_vec.pop().unwrap(); // Unwrap won't panic, because `refs_vec` has initial len > 0
            let low = high.sub.pop();
            refs_vec.push(high);
            if let Some(low) = low {
                refs_vec.push(low);
            } else {
                refs_vec.push(NestedString::default());
            }
        }
        if sublist {
            refs_vec
                .last_mut()
                .unwrap()
                .sub
                .push(NestedString::new(line));
        } else {
            refs_vec.last_mut().unwrap().top.push(' ');
            refs_vec.last_mut().unwrap().top.push_str(line.as_str());
        }

        for _ in 1..nesting_depth {
            let last = refs_vec.pop().unwrap();
            refs_vec.last_mut().unwrap().sub.push(last);
        }
        self = refs_vec.swap_remove(0);
        self
    }
}
impl Display for NestedString {
    /// usable formating options:
    /// :{width} - NestedString.top intendation/sublist depth, NestedString.sub is one sublist level deeper
    /// :# - NestedString.top is at 0 sublist depth(no sublist at all), but NestedString.sub is on sublist level width+1
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut line_prefix = String::new();
        let width = formatter.width().unwrap_or(0);
        if !(width == 0 || formatter.alternate()) {
            line_prefix += format!("{:>width$}", "* ", width = 2 * width).as_str();
            // Set element's initial depth in list
        }

        {
            write!(formatter, "{}{}", line_prefix, self.top)?;
        }

        if !self.sub.is_empty() {
            for ns in self.sub.iter() {
                write!(formatter, "\n{:width$}", ns, width = width + 1)?;
            }
        }
        write!(formatter, "")
    }
}

/// The enum used to represent the distinct _raw_ values of a comment
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Value {
    /// The first [`String`] is the _notation_ found, and .top of [`NestedString`] are the _contents without the notation_; the .sub of [`NestedString`] is a vector that contains consecutive list (lines starting with "-")
    Notation(String, NestedString),
    /// Raw text, without notation
    Text(NestedString),
    /// Double new-line, or any other separator
    Separator,
    /// indented Text- probably continuation of previous line; [`String`]- line text stripped of leading whitespaces & sublist characters as '-','*'and '+'; [`usize`] - number of leading whitespaces; [`bool`]- sublist char present?
    Continuation(String, usize, bool),
    /// Unknown value
    Unknown,
}

fn parse_single_line(line: &str) -> Value {
    let mut line = preprocessor::preprocess_line(line);
    let mut leading_whitespaces = 0;
    let mut chars_to_skip = 0;
    let mut sublist = false;
    for ch in line.chars() {
        if ch.is_whitespace() {
            leading_whitespaces += 1;
        } else if ch == '-' || ch == '*' || ch == '+' {
            sublist = true;
            chars_to_skip = leading_whitespaces + 1;
            break;
        } else {
            chars_to_skip = leading_whitespaces;
            break;
        };
    }
    line.drain(..chars_to_skip); // Remove leading whitespaces and sublist mark
    line = line.trim_start().to_string(); // Remove leading whitespaces after sublist mark
    if let Some(notation) = line.contains_any_notation() {
        let split = line.split_whitespace().collect::<Vec<&str>>();
        Value::Notation(notation, NestedString::new(split[1..].to_vec().join(" ")))
    } else if line.is_empty() {
        Value::Separator
    } else if leading_whitespaces > 0 {
        Value::Continuation(line, leading_whitespaces, sublist)
    } else {
        Value::Text(NestedString::new(line))
    }
}

/// Generate a [`Vec`] of [`Value`] from a given [`&str`]
///
/// # Examples
/// ```
/// use doxygen_rs::parser::parse_comment;
///
/// let parsed = parse_comment("@brief Random function");
/// ```
pub fn parse_comment(input: &str) -> Vec<Value> {
    let lines = input
        .split('\n')
        .map(|v| v.to_string())
        .collect::<Vec<String>>();
    let mut values = vec![];
    let mut nesting_depth = 0;
    let mut nesting_spaces = vec![0];

    for line in lines {
        let value = if line.trim().starts_with("* ") {
            parse_single_line(line.replace("* ", "").as_str())
        } else {
            parse_single_line(line.as_str())
        };
        if let Value::Continuation(line, leading_whitespaces, sublist) = value {
            for nd in (0..=nesting_depth).rev() {
                // Assume that sublist level takes at least two whitespaces, and single whitespace is a typo
                if leading_whitespaces >= nesting_spaces[nd] + 2 {
                    // Sublist level has increased
                    nesting_depth += 1;
                    nesting_spaces.push(leading_whitespaces);
                    break;
                } else if leading_whitespaces + 2 <= nesting_spaces[nd] {
                    // Sublist level has decreased
                    nesting_depth -= 1;
                    nesting_spaces.pop();
                    continue;
                } else {
                    break;
                }
            }
            match values.pop() {
                Some(Value::Notation(notation, mut values_depths)) => {
                    values_depths = values_depths.modify_sub(nesting_depth, sublist, line);
                    values.push(Value::Notation(notation.clone(), values_depths));
                }
                Some(Value::Text(mut values_depths)) => {
                    values_depths = values_depths.modify_sub(nesting_depth, sublist, line);
                    values.push(Value::Text(values_depths));
                }
                None => {
                    values.push(Value::Text(NestedString::new(line)));
                    nesting_depth = 0;
                    nesting_spaces = vec![0];
                }
                Some(v) => {
                    values.push(v);
                    values.push(Value::Text(NestedString::new(line)));
                    nesting_depth = 0;
                    nesting_spaces = vec![0];
                }
            }
        } else {
            values.push(value);
            nesting_depth = 0;
            nesting_spaces = vec![0];
        }
    }
    values.push(Value::Separator);

    values
}

/// The enum used to represent values of a _raw_ bindgen file
#[derive(Clone, Debug)]
pub enum StringType {
    /// Parsed value
    Parsed(Vec<Value>),
    /// No-doc, value is given raw
    Raw(String),
}

/// Generate a [`Vec`] of [`StringType`] from a given [`&str`], assuming it's a _raw_ bindgen file
pub fn parse_bindgen(input: &str) -> Vec<StringType> {
    let lines: Vec<String> = input
        .split('\n')
        .map(|v| v.to_string())
        .collect::<Vec<String>>();
    let mut strings = vec![];

    let mut comment_buffer = vec![];
    for line in lines {
        if line.trim().starts_with("#[doc = \"") && line.trim().ends_with("\"]") {
            comment_buffer.push(line.replace("#[doc = \"", "").replace("\"]", ""));
        } else {
            if !comment_buffer.is_empty() {
                strings.push(StringType::Parsed(parse_comment(
                    comment_buffer.join("\n").as_str(),
                )));
                comment_buffer = vec![];
            }
            strings.push(StringType::Raw(line));
        }
    }

    strings
}

#[cfg(test)]
mod tests {
    use crate::parser::parse_comment;
    use crate::parser::NestedString;
    use crate::parser::Value::Notation;

    #[test]
    fn test() {
        let parsed = parse_comment("@param random Random thing lmao\n\n@block This is going to be\nA block of text\nThis is crazy right??\n\nHello this is not anotated\n");
        println!("{:?}", parsed);
    }

    #[test]
    fn italic_works() {
        let parsed = parse_comment("@brief \\a example \\\\e example 2 @em example 3");
        assert_eq!(
            parsed[0],
            Notation(
                "@brief".to_owned(),
                NestedString {
                    top: "*example* *example* 2 *example* 3".to_owned(),
                    sub: vec![]
                }
            )
        )
    }

    #[test]
    fn emojis_work() {
        let parsed = parse_comment("@brief @emoji :smirk: \\emoji smirk \\\\emoji smiley");
        assert_eq!(
            parsed[0],
            Notation(
                "@brief".to_owned(),
                NestedString {
                    top: "😏 😏 😃".to_owned(),
                    sub: vec![]
                }
            )
        )
    }
}