Skip to main content

php_ast/ast/
misc.rs

1#[cfg(feature = "serde")]
2use serde::Serialize;
3
4use crate::Span;
5
6use super::{ArenaVec, Expr, Name, Stmt};
7
8/// A comment found in the source file.
9#[derive(Debug, Clone, Copy)]
10#[cfg_attr(feature = "serde", derive(Serialize))]
11pub struct Comment<'src> {
12    /// Comment syntax form.
13    pub kind: CommentKind,
14    /// Raw text of the comment including its delimiters (e.g. `// foo`, `/* bar */`, `/** baz */`).
15    pub text: &'src str,
16    /// Source range of the comment.
17    pub span: Span,
18}
19
20/// Distinguishes the four syntactic forms of PHP comment.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[cfg_attr(feature = "serde", derive(Serialize))]
23#[non_exhaustive]
24pub enum CommentKind {
25    /// `// …` — single-line slash comment
26    Line,
27    /// `# …` — single-line hash comment
28    Hash,
29    /// `/* … */` — block comment
30    Block,
31    /// `/** … */` — doc-block comment (first non-whitespace char after `/*` is `*`)
32    Doc,
33}
34
35/// The root AST node representing a complete PHP file.
36#[derive(Debug)]
37#[cfg_attr(feature = "serde", derive(Serialize))]
38pub struct Program<'arena, 'src> {
39    /// Top-level statements.
40    pub stmts: ArenaVec<'arena, Stmt<'arena, 'src>>,
41    /// Source range of this node.
42    pub span: Span,
43}
44
45/// A call argument, including named, spread and by-reference forms.
46#[derive(Debug)]
47#[cfg_attr(feature = "serde", derive(Serialize))]
48pub struct Arg<'arena, 'src> {
49    /// Argument name in a named argument (PHP 8.0+).
50    pub name: Option<Name<'arena, 'src>>,
51    /// `None` is a PHP 8.6 partial-application placeholder (`?`, or `...` when `unpack` is set).
52    pub value: Option<Expr<'arena, 'src>>,
53    /// `true` for spread (`...$args`).
54    pub unpack: bool,
55    /// `true` when `&` precedes the argument.
56    pub by_ref: bool,
57    /// Source range of this node.
58    pub span: Span,
59}
60
61/// A single attribute: `#[Name(args)]`.
62#[derive(Debug)]
63#[cfg_attr(feature = "serde", derive(Serialize))]
64pub struct Attribute<'arena, 'src> {
65    /// Attribute class name.
66    pub name: Name<'arena, 'src>,
67    /// Attribute arguments.
68    pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
69    /// Source range of this node.
70    pub span: Span,
71}