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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use crate::parser::{NestedString, Value};
use crate::utils::NotationMatching;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug)]
pub struct ParsedDoxygen {
pub title: Option<NestedString>,
pub brief: Option<NestedString>,
pub description: Option<Vec<NestedString>>,
pub warnings: Option<Vec<Warning>>,
pub notes: Option<Vec<Note>>,
pub params: Option<Vec<Param>>,
pub deprecated: Option<Deprecated>,
pub todos: Option<Vec<NestedString>>,
pub returns: Option<Vec<Return>>,
pub return_values: Option<Vec<ReturnValue>>,
}
#[derive(Clone, Debug)]
pub struct Param {
pub arg_name: String,
pub direction: Option<Direction>,
pub description: Option<NestedString>,
}
#[derive(Clone, Debug)]
pub struct Deprecated {
pub is_deprecated: bool,
pub message: Option<NestedString>,
}
#[derive(Clone, Debug)]
pub struct Note(pub NestedString);
#[derive(Clone, Debug)]
pub struct Warning(pub NestedString);
#[derive(Clone, Debug)]
pub struct Return(pub NestedString);
#[derive(Clone, Debug)]
pub struct ReturnValue(pub NestedString);
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum Direction {
In,
Out,
InOut,
}
impl Display for Direction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Direction::In => f.write_str("In"),
Direction::Out => f.write_str("In, Out"),
Direction::InOut => f.write_str("Out"),
}
}
}
impl TryFrom<&str> for Direction {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value == "in" {
Ok(Direction::In)
} else if value == "out" {
Ok(Direction::Out)
} else if value == "in,out" || value == "out,in" {
Ok(Direction::InOut)
} else {
Err(())
}
}
}
#[derive(Debug, Clone)]
enum MoveBufferTo {
Description,
ToDo,
}
pub fn generate_ast(input: Vec<Value>) -> ParsedDoxygen {
let mut title = None;
let mut brief = None;
let mut deprecated = None;
let mut return_values = vec![];
let mut returns = vec![];
let mut warnings = vec![];
let mut notes = vec![];
let mut todos = vec![];
let mut params = vec![];
let mut description = vec![];
let mut currently_saving_paragraph = false;
let mut paragraph_buffer = vec![];
let mut move_buffer_to = None;
for value in input {
match value {
Value::Notation(notation, mut content) => {
if notation.starts_with_notation("brief") {
brief = Some(content);
} else if notation.starts_with_notation("deprecated") {
deprecated = Some(Deprecated {
is_deprecated: true,
message: if content.top.is_empty() {
None
} else {
Some(content)
},
});
} else if notation.starts_with_notation("details") {
currently_saving_paragraph = true;
move_buffer_to = Some(MoveBufferTo::Description);
paragraph_buffer.push(content);
} else if notation.starts_with_notation("todo") {
currently_saving_paragraph = true;
move_buffer_to = Some(MoveBufferTo::ToDo);
paragraph_buffer.push(content);
} else if notation.starts_with_notation("param") {
let direction = {
let raw_direction = notation.remove_notation("param");
if raw_direction.is_empty() || raw_direction.starts_with("[]") {
None
} else if raw_direction.starts_with("[in]") {
Some(Direction::In)
} else if raw_direction.starts_with("[out]") {
Some(Direction::Out)
} else if raw_direction.starts_with("[in,out]")
|| raw_direction.starts_with("[out,in]")
{
Some(Direction::InOut)
} else {
None
}
};
let mut split = content.top.splitn(2, char::is_whitespace);
let arg_name = split
.next()
.expect("content.top should be non-empty")
.to_owned();
let description = split.next().map(String::from);
let description = description.map(|desc| {
content.top = desc;
content
});
params.push(Param {
arg_name,
direction,
description,
})
} else if notation.starts_with_notation("return")
|| notation.starts_with_notation("returns")
{
returns.push(Return(content));
} else if notation.starts_with_notation("name") {
title = Some(content)
} else if notation.starts_with_notation("note")
|| notation.starts_with_notation("remark")
|| notation.starts_with_notation("remarks")
{
notes.push(Note(content))
} else if notation.starts_with_notation("warning") {
warnings.push(Warning(content))
} else if notation.starts_with_notation("retval") {
return_values.push(ReturnValue(content))
}
}
Value::Text(content) => {
if currently_saving_paragraph {
paragraph_buffer.push(content);
} else if content.to_string().trim() != "*"
&& content.to_string().trim() != "*/"
&& content.to_string().trim() != "**"
{
description.push(content);
}
}
Value::Separator => {
if currently_saving_paragraph {
if let Some(move_buffer_to) = move_buffer_to.clone() {
match move_buffer_to {
MoveBufferTo::Description => {
description.append(&mut paragraph_buffer.clone())
}
MoveBufferTo::ToDo => todos.append(&mut paragraph_buffer.clone()),
}
}
currently_saving_paragraph = false;
paragraph_buffer = vec![];
move_buffer_to = None;
}
}
Value::Unknown => {}
Value::Continuation(_, _, _) => unreachable!(), }
}
let description = (!description.is_empty()).then_some(description);
let returns = (!returns.is_empty()).then_some(returns);
let todos = (!todos.is_empty()).then_some(todos);
let warnings = (!warnings.is_empty()).then_some(warnings);
let notes = (!notes.is_empty()).then_some(notes);
let params = (!params.is_empty()).then_some(params);
let return_values = (!return_values.is_empty()).then_some(return_values);
ParsedDoxygen {
title,
brief,
description,
warnings,
notes,
params,
deprecated,
todos,
returns,
return_values,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser;
#[test]
fn parses_param() {
let doxygen = generate_ast(parser::parse_comment(
"@param random Random thing lmao\n@param[in] goes_in This goes in lmao",
));
let first_param = doxygen.params.as_ref().unwrap();
let first_param = first_param.get(0).unwrap();
assert_eq!(first_param.arg_name, "random");
assert_eq!(
first_param.description,
Some(NestedString::new("Random thing lmao".to_string()))
);
assert_eq!(first_param.direction, None);
let second_param = doxygen.params.as_ref().unwrap();
let second_param = second_param.get(1).unwrap();
assert_eq!(second_param.arg_name, "goes_in");
assert_eq!(
second_param.description,
Some(NestedString::new("This goes in lmao".to_string()))
);
assert_eq!(second_param.direction, Some(Direction::In));
}
#[test]
fn parses_brief() {
let doxygen = generate_ast(parser::parse_comment("@brief This function does things"));
assert_eq!(
doxygen.brief,
Some(NestedString::new("This function does things".to_string()))
);
}
#[test]
fn parses_description() {
let doxygen = generate_ast(parser::parse_comment("@brief This is a function\n\nThis is the description of the thing.\nYou should do things with this function.\nOr not, I don't really care."));
assert_eq!(
doxygen.description,
Some(vec![
NestedString::new("This is the description of the thing.".to_string()),
NestedString::new("You should do things with this function.".to_string()),
NestedString::new("Or not, I don't really care.".to_string())
])
)
}
#[test]
fn parses_multiline_simple() {
let doxygen = generate_ast(parser::parse_comment("@brief This is a function\n\nThis is the description of the thing.\n You should do things with this function.\n Or not, I don't really care."));
assert_eq!(doxygen.description, Some(vec![NestedString::new("This is the description of the thing. You should do things with this function. Or not, I don't really care.".to_string())]))
}
#[test]
fn parses_multiline_sublist() {
let doxygen = generate_ast(parser::parse_comment("@brief This is a function\n\nThis is the description of the thing.\n - You should do things with this function.\n - Or not, I don't really care."));
assert_eq!(
doxygen.description,
Some(vec![NestedString {
top: "This is the description of the thing.".to_string(),
sub: vec![
NestedString::new("You should do things with this function.".to_string()),
NestedString::new("Or not, I don't really care.".to_string())
]
}])
)
}
#[test]
fn parses_multiline_sublist_complex() {
let doxygen = generate_ast(parser::parse_comment("@brief This is a function\n\nThis is the description of the thing.\n - You should do things with:\n - this function.\n - that function.\n (with long description)\n - Or not, I don't really care."));
assert_eq!(
doxygen.description,
Some(vec![NestedString {
top: "This is the description of the thing.".to_string(),
sub: vec![
NestedString {
top: "You should do things with:".to_string(),
sub: vec![
NestedString::new("this function.".to_string()),
NestedString::new("that function. (with long description)".to_string())
]
},
NestedString::new("Or not, I don't really care.".to_string())
]
}])
);
}
#[test]
fn parses_deprecated() {
let doxygen = generate_ast(parser::parse_comment(
"@deprecated This function is pure spaghetti lmao\n\n@brief Creates a single spaghetti",
));
let deprecated = doxygen.deprecated.unwrap();
assert_eq!(deprecated.is_deprecated, true);
assert_eq!(
deprecated.message,
Some(NestedString::new(
"This function is pure spaghetti lmao".to_string()
))
);
}
#[test]
fn parses_details() {
let doxygen = generate_ast(parser::parse_comment("@brief This does things\n\n@details This does _advanced_ things\nAnd the _advanced_ things are not easy"));
let description = doxygen.description.unwrap();
assert_eq!(
description,
vec![
NestedString::new("This does _advanced_ things".to_string()),
NestedString::new("And the _advanced_ things are not easy".to_string())
]
);
}
#[test]
fn parses_todo() {
let doxygen = generate_ast(parser::parse_comment(
"@brief This is WIP\n\n@todo Fix the bug where the C: drive is deleted",
));
let todos = doxygen.todos.unwrap();
assert_eq!(
todos.get(0).unwrap().clone(),
NestedString::new("Fix the bug where the C: drive is deleted".to_string())
);
}
#[test]
fn parses_advanced_doxygen() {
let doxygen = generate_ast(parser::parse_comment("@brief Creates a new dog.\n\nCreates a new Dog named `_name` with half of its maximum energy.\n\n@param _name The dog's name."));
let first_param = doxygen.params.unwrap();
let first_param = first_param.first().unwrap();
assert_eq!(
doxygen.brief,
Some(NestedString::new("Creates a new dog.".to_string()))
);
assert_eq!(
doxygen.description,
Some(vec![NestedString::new(
"Creates a new Dog named `_name` with half of its maximum energy.".to_string()
)])
);
assert_eq!(first_param.arg_name, "_name".to_string());
assert_eq!(
first_param.description,
Some(NestedString::new("The dog's name.".to_string()))
);
assert_eq!(first_param.direction, None);
}
#[test]
fn parses_params_and_returns_missing_descriptions() {
let doxygen = generate_ast(parser::parse_comment(" @brief Search for needle in string from position start.\n @param string\n @param needle\n @param start\n @return size_t"));
assert_eq!(
doxygen.brief,
Some(NestedString::new(
"Search for needle in string from position start.".to_string()
)),
);
let params = doxygen.params.unwrap();
assert_eq!(params.len(), 3);
let param_string = ¶ms[0];
assert_eq!(param_string.arg_name, "string".to_string());
assert_eq!(param_string.description, None);
assert_eq!(param_string.direction, None);
let param_needle = ¶ms[1];
assert_eq!(param_needle.arg_name, "needle".to_string());
assert_eq!(param_needle.description, None);
assert_eq!(param_needle.direction, None);
let param_start = ¶ms[2];
assert_eq!(param_start.arg_name, "start".to_string());
assert_eq!(param_start.description, None);
assert_eq!(param_start.direction, None);
let returns = doxygen.returns.unwrap();
assert_eq!(returns.len(), 1);
assert_eq!(returns[0].0, NestedString::new("size_t".to_string()));
assert!(doxygen.return_values.is_none());
}
}