Skip to main content

phpdoc_parser/
ast.rs

1use crate::Span;
2#[cfg(feature = "serde")]
3use serde::Serialize;
4
5// =============================================================================
6// Document-structure AST
7// =============================================================================
8
9/// An inline `{@tagname body}` tag embedded in text.
10#[derive(Debug, Clone)]
11#[cfg_attr(feature = "serde", derive(Serialize))]
12#[non_exhaustive]
13pub struct InlineTag {
14    /// Tag name without braces or `@`.
15    pub name: String,
16    /// Text after the name, if any.
17    pub body: Option<String>,
18    /// Source range of the whole tag.
19    pub span: Span,
20}
21
22/// A segment of prose text — either plain text or an inline tag.
23#[derive(Debug, Clone)]
24#[cfg_attr(feature = "serde", derive(Serialize))]
25#[non_exhaustive]
26pub enum TextSegment {
27    /// Literal prose.
28    Text(String),
29    /// Embedded `{@tag}`.
30    InlineTag(InlineTag),
31}
32
33/// A prose run (summary, description, or tag body) that may contain inline tags.
34#[derive(Debug, Clone)]
35#[cfg_attr(feature = "serde", derive(Serialize))]
36#[non_exhaustive]
37pub struct PhpDocText {
38    /// Segments in source order.
39    pub segments: Vec<TextSegment>,
40    /// Source range of the text.
41    pub span: Span,
42}
43
44/// A block-level `@tag` — generic, no semantic interpretation.
45#[derive(Debug, Clone)]
46#[cfg_attr(feature = "serde", derive(Serialize))]
47#[non_exhaustive]
48pub struct PhpDocTag {
49    /// Raw tag name, e.g. `"param"`, `"psalm-type"`, `"return"`.
50    pub name: String,
51    /// Tag body, if any.
52    pub body: Option<PhpDocText>,
53    /// Source range of the tag.
54    pub span: Span,
55}
56
57// =============================================================================
58// Top-level document
59// =============================================================================
60
61/// A parsed docblock.
62#[derive(Debug, Clone)]
63#[cfg_attr(feature = "serde", derive(Serialize))]
64#[non_exhaustive]
65pub struct PhpDoc {
66    /// Leading summary text.
67    pub summary: Option<PhpDocText>,
68    /// Description following the summary.
69    pub description: Option<PhpDocText>,
70    /// Block-level tags in source order.
71    pub tags: Vec<PhpDocTag>,
72    /// Always `Span::new(0, text.len() as u32)`.
73    pub span: Span,
74}