phpdoc-parser 0.26.0

Structural PHPDoc parser. Parses doc-comment blocks into a tag/prose AST with accurate spans; tag bodies exposed as raw text.
Documentation
use crate::Span;
#[cfg(feature = "serde")]
use serde::Serialize;

// =============================================================================
// Document-structure AST
// =============================================================================

/// An inline `{@tagname body}` tag embedded in text.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct InlineTag {
    /// Tag name without braces or `@`.
    pub name: String,
    /// Text after the name, if any.
    pub body: Option<String>,
    /// Source range of the whole tag.
    pub span: Span,
}

/// A segment of prose text — either plain text or an inline tag.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub enum TextSegment {
    /// Literal prose.
    Text(String),
    /// Embedded `{@tag}`.
    InlineTag(InlineTag),
}

/// A prose run (summary, description, or tag body) that may contain inline tags.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct PhpDocText {
    /// Segments in source order.
    pub segments: Vec<TextSegment>,
    /// Source range of the text.
    pub span: Span,
}

/// A block-level `@tag` — generic, no semantic interpretation.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct PhpDocTag {
    /// Raw tag name, e.g. `"param"`, `"psalm-type"`, `"return"`.
    pub name: String,
    /// Tag body, if any.
    pub body: Option<PhpDocText>,
    /// Source range of the tag.
    pub span: Span,
}

// =============================================================================
// Top-level document
// =============================================================================

/// A parsed docblock.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[non_exhaustive]
pub struct PhpDoc {
    /// Leading summary text.
    pub summary: Option<PhpDocText>,
    /// Description following the summary.
    pub description: Option<PhpDocText>,
    /// Block-level tags in source order.
    pub tags: Vec<PhpDocTag>,
    /// Always `Span::new(0, text.len() as u32)`.
    pub span: Span,
}