mago-docblock 1.22.0

Analyzes PHP docblocks to extract annotations, tags, and documentation comments, aiding tools that rely on inline documentation.
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use bumpalo::Bump;
use bumpalo::collections::Vec;

use mago_span::Span;

use crate::document::Code;
use crate::document::Document;
use crate::document::Element;
use crate::document::Tag;
use crate::document::Text;
use crate::document::TextSegment;
use crate::error::ParseError;

use super::token::Token;

pub fn parse_document<'arena>(
    span: Span,
    tokens: &[Token<'arena>],
    arena: &'arena Bump,
) -> Result<Document<'arena>, ParseError> {
    let mut elements = Vec::new_in(arena);
    let mut i = 0;

    while i < tokens.len() {
        match &tokens[i] {
            Token::Line { content, .. } => {
                if content.starts_with('@') {
                    let (tag, new_i) = parse_tag(tokens, i, arena)?;
                    elements.push(Element::Tag(tag));
                    i = new_i;
                } else if content.starts_with("```") {
                    let (code, new_i) = parse_code_block(tokens, i, arena)?;
                    elements.push(Element::Code(code));
                    i = new_i;
                } else if is_indented_line(content) {
                    let (code, new_i) = parse_indented_code(tokens, i, arena)?;
                    elements.push(Element::Code(code));
                    i = new_i;
                } else {
                    let (text, new_i) = parse_text(tokens, i, arena)?;
                    elements.push(Element::Text(text));
                    i = new_i;
                }
            }
            Token::EmptyLine { span } => {
                elements.push(Element::Line(*span));
                i += 1;
            }
        }
    }

    Ok(Document { span, elements })
}

fn is_indented_line(content: &str) -> bool {
    content.starts_with(' ') || content.starts_with('\t')
}

fn parse_tag<'arena>(
    tokens: &[Token<'arena>],
    start_index: usize,
    arena: &'arena Bump,
) -> Result<(Tag<'arena>, usize), ParseError> {
    let mut i = start_index;
    let Token::Line { content, span } = &tokens[i] else {
        return Err(ParseError::ExpectedLine(tokens[i].span()));
    };

    let mut name_end = None;
    for (byte_pos, ch) in content[1..].char_indices() {
        if ch.is_whitespace() || ch == '(' {
            name_end = Some(1 + byte_pos);
            break;
        }
    }

    let tag_name = if let Some(end) = name_end { &content[1..end] } else { &content[1..] };

    if tag_name.is_empty()
        || !tag_name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-' || b == b':' || b == b'\\' || b >= 0x80)
    {
        return Err(ParseError::InvalidTagName(span.subspan(0, tag_name.len() as u32 + 1)));
    }

    let after_name = &content[1 + tag_name.len()..];

    let mut metadata: Option<&'arena str> = None;
    let mut after_metadata = after_name;
    if after_name.starts_with('(') {
        let mut depth: usize = 0;
        let mut close_pos = None;
        for (pos, ch) in after_name.char_indices() {
            match ch {
                '(' => depth += 1,
                ')' => {
                    depth -= 1;
                    if depth == 0 {
                        close_pos = Some(pos);
                        break;
                    }
                }
                _ => {}
            }
        }

        if let Some(close) = close_pos {
            metadata = Some(arena.alloc_str(&after_name[..=close]));
            after_metadata = &after_name[close + 1..];
        }
    }

    let description_part;
    let description_start;
    let trimmed = after_metadata.trim_start();
    if trimmed.is_empty() {
        description_part = "";
        description_start = span.start.forward(content.len() as u32);
    } else {
        let offset = content.len() - trimmed.len();
        description_part = trimmed;
        description_start = span.start.forward(offset as u32);
    }

    let mut description = String::from(description_part);
    let mut end_span = *span;

    i += 1;
    while i < tokens.len() {
        match &tokens[i] {
            Token::Line { content, span } => {
                if content.is_empty()
                    || content.trim().is_empty()
                    || content.starts_with('@')
                    || content.starts_with("```")
                {
                    break;
                }
                description.push('\n');
                description.push_str(content);
                end_span = *span;
                i += 1;
            }
            Token::EmptyLine { .. } => {
                break;
            }
        }
    }

    let kind = tag_name.into();

    let tag_span = Span::new(span.file_id, span.start, end_span.end);

    let tag = Tag {
        span: tag_span,
        name: tag_name,
        kind,
        metadata,
        description: arena.alloc_str(&description),
        description_span: Span::new(span.file_id, description_start, end_span.end),
    };

    Ok((tag, i))
}

fn parse_code_block<'arena>(
    tokens: &[Token<'arena>],
    start_index: usize,
    arena: &'arena Bump,
) -> Result<(Code<'arena>, usize), ParseError> {
    let mut i = start_index;
    let Token::Line { content, span } = &tokens[i] else {
        return Err(ParseError::ExpectedLine(tokens[i].span()));
    };

    let mut directives = Vec::new_in(arena);
    let rest = &content[3..].trim();
    if !rest.is_empty() {
        directives = Vec::from_iter_in(rest.split(',').map(str::trim), arena);
    }

    let mut code_content = String::new();
    let mut end_span = *span;
    i += 1;

    let mut found_closing = false;
    while i < tokens.len() {
        match &tokens[i] {
            Token::Line { content, span } => {
                if content.starts_with("```") {
                    found_closing = true;
                    end_span = *span;
                    i += 1;
                    break;
                }
                if !code_content.is_empty() {
                    code_content.push('\n');
                }
                code_content.push_str(content);
                end_span = *span;
                i += 1;
            }
            Token::EmptyLine { span } => {
                code_content.push('\n');
                end_span = *span;
                i += 1;
            }
        }
    }

    let code_span = Span::new(span.file_id, span.start, end_span.end);
    if !found_closing {
        return Err(ParseError::UnclosedCodeBlock(code_span));
    }

    Ok((Code { span: code_span, directives, content: arena.alloc_str(&code_content) }, i))
}

fn parse_indented_code<'arena>(
    tokens: &[Token<'arena>],
    start_index: usize,
    arena: &'arena Bump,
) -> Result<(Code<'arena>, usize), ParseError> {
    let mut i = start_index;
    let Token::Line { content, span } = &tokens[i] else {
        return Err(ParseError::ExpectedLine(tokens[i].span()));
    };

    let indent_len = content.chars().take_while(|c| c.is_whitespace()).count();

    let mut code_content = String::new();
    let mut end_span = *span;

    while i < tokens.len() {
        match &tokens[i] {
            Token::Line { content, span } => {
                if content.starts_with('@') || content.starts_with("```") {
                    break;
                }
                let current_indent_len = content.chars().take_while(|c| c.is_whitespace()).count();
                if current_indent_len < indent_len {
                    break;
                }

                // Calculate byte offset from original indent character count
                let current_indent_bytes: usize = content.chars().take(indent_len).map(|c| c.len_utf8()).sum();
                let line_content = &content[current_indent_bytes..];
                if !code_content.is_empty() {
                    code_content.push('\n');
                }
                code_content.push_str(line_content);
                end_span = *span;
                i += 1;
            }
            Token::EmptyLine { span } => {
                code_content.push('\n');
                end_span = *span;
                i += 1;
            }
        }
    }

    Ok((
        Code {
            span: Span::new(span.file_id, span.start, end_span.end),
            directives: Vec::new_in(arena),
            content: arena.alloc_str(&code_content),
        },
        i,
    ))
}

fn parse_text<'arena>(
    tokens: &[Token<'arena>],
    start_index: usize,
    arena: &'arena Bump,
) -> Result<(Text<'arena>, usize), ParseError> {
    let mut i = start_index;
    let mut text_content = String::new();
    let start_span = tokens[start_index].span();

    let mut end_span = start_span;
    let mut open_braces: usize = 0;

    while i < tokens.len() {
        match &tokens[i] {
            Token::Line { content, span } => {
                if open_braces == 0
                    && (content.is_empty()
                        || content.trim().is_empty()
                        || content.starts_with('@')
                        || content.starts_with("```")
                        || is_indented_line(content))
                {
                    break;
                }

                if !text_content.is_empty() {
                    text_content.push('\n');
                }

                text_content.push_str(content);
                end_span = *span;
                i += 1;

                for ch in content.chars() {
                    match ch {
                        '{' => open_braces += 1,
                        '}' => open_braces = open_braces.saturating_sub(1),
                        _ => {}
                    }
                }
            }
            Token::EmptyLine { .. } => {
                if open_braces > 0 {
                    if !text_content.is_empty() {
                        text_content.push('\n');
                    }
                    end_span = tokens[i].span();
                    i += 1;
                } else {
                    break;
                }
            }
        }
    }

    // Now parse text_content into TextSegments
    let text_span = Span::new(start_span.file_id, start_span.start, end_span.end);
    let segments = parse_text_segments(arena.alloc_str(&text_content), text_span, arena)?;

    let text = Text { span: text_span, segments };

    Ok((text, i))
}

fn parse_text_segments<'arena>(
    text_content: &'arena str,
    base_span: Span,
    arena: &'arena Bump,
) -> Result<Vec<'arena, TextSegment<'arena>>, ParseError> {
    let mut segments = Vec::new_in(arena);
    let mut char_indices = text_content.char_indices().peekable();

    while let Some((start_pos, ch)) = char_indices.peek().copied() {
        if ch == '`' {
            let is_start = start_pos == 0;
            let is_prev_whitespace = if start_pos > 0 {
                text_content[..start_pos].chars().next_back().is_some_and(|c| c.is_ascii_whitespace())
            } else {
                false
            };

            if is_start || is_prev_whitespace {
                let mut backtick_count = 0;
                let mut end_pos = start_pos;

                while let Some((idx, ch)) = char_indices.peek() {
                    if *ch == '`' {
                        backtick_count += 1;
                        end_pos = *idx + ch.len_utf8();
                        char_indices.next();
                    } else {
                        break;
                    }
                }

                let backticks = "`".repeat(backtick_count);
                let code_start_pos = end_pos;

                let mut code_end_pos = None;
                while let Some((idx, _)) = char_indices.peek() {
                    if text_content[*idx..].starts_with(&backticks) {
                        code_end_pos = Some(*idx);
                        for _ in 0..backtick_count {
                            char_indices.next();
                        }
                        break;
                    }
                    char_indices.next();
                }

                if let Some(code_end_pos) = code_end_pos {
                    let code_content = &text_content[code_start_pos..code_end_pos];
                    let code_span = base_span.subspan(start_pos as u32, code_end_pos as u32 + backtick_count as u32);

                    let code = Code { span: code_span, directives: Vec::new_in(arena), content: code_content };

                    segments.push(TextSegment::InlineCode(code));
                } else {
                    return Err(ParseError::UnclosedInlineCode(
                        base_span.subspan(start_pos as u32, base_span.length()),
                    ));
                }
                continue;
            }
        }

        if text_content[start_pos..].starts_with("{@") {
            let is_start = start_pos == 0;
            let is_prev_whitespace = if start_pos > 0 {
                text_content[..start_pos].chars().next_back().is_some_and(|c| c.is_ascii_whitespace())
            } else {
                false
            };

            if is_start || is_prev_whitespace {
                let tag_start_pos = start_pos;
                char_indices.next(); // Skip '{'
                char_indices.next(); // Skip '@'

                let tag_content_start = tag_start_pos + 2;
                let mut tag_end_pos = None;
                for (idx, ch) in char_indices.by_ref() {
                    if ch == '}' {
                        tag_end_pos = Some(idx);
                        break;
                    }
                }

                if let Some(tag_end_pos) = tag_end_pos {
                    let tag_content = &text_content[tag_content_start..tag_end_pos];
                    let tag_span = base_span.subspan(tag_start_pos as u32, tag_end_pos as u32 + 1);
                    let tag = parse_inline_tag(tag_content, tag_span);
                    segments.push(TextSegment::InlineTag(tag));
                } else {
                    // Unclosed inline tag
                    return Err(ParseError::UnclosedInlineTag(base_span.subspan(start_pos as u32, base_span.length())));
                }
                continue;
            }
        }

        let paragraph_start_pos = start_pos;
        let mut paragraph_end_pos = start_pos;

        while let Some((idx, ch)) = char_indices.peek().copied() {
            let is_code_start = ch == '`' && {
                let is_start = idx == 0;
                let is_prev_whitespace = if idx > 0 {
                    text_content[..idx].chars().next_back().is_some_and(|c| c.is_ascii_whitespace())
                } else {
                    false
                };

                is_start || is_prev_whitespace
            };

            let is_tag_start = text_content[idx..].starts_with("{@") && {
                let is_start = idx == 0;
                let is_prev_whitespace = if idx > 0 {
                    text_content[..idx].chars().next_back().is_some_and(|c| c.is_ascii_whitespace())
                } else {
                    false
                };

                is_start || is_prev_whitespace
            };

            if is_code_start || is_tag_start {
                break;
            }
            char_indices.next();
            paragraph_end_pos = idx + ch.len_utf8();
        }

        let paragraph_content = &text_content[paragraph_start_pos..paragraph_end_pos];

        segments.push(TextSegment::Paragraph { span: base_span, content: paragraph_content });
    }

    Ok(segments)
}

fn parse_inline_tag(tag_content: &str, span: Span) -> Tag<'_> {
    let mut parts = tag_content.trim().splitn(2, char::is_whitespace);
    let name = parts.next().unwrap_or("");
    let description = parts.next().unwrap_or("");

    Tag {
        span,
        name,
        kind: name.into(),
        metadata: None,
        description,
        description_span: Span::new(span.file_id, span.start.forward(name.len() as u32 + 1), span.end),
    }
}