Skip to main content

php_ast/ast/
misc.rs

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