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)]
20#[non_exhaustive]
21pub enum CommentKind {
22 /// `// …` — single-line slash comment
23 Line,
24 /// `# …` — single-line hash comment
25 Hash,
26 /// `/* … */` — block comment
27 Block,
28 /// `/** … */` — doc-block comment (first non-whitespace char after `/*` is `*`)
29 Doc,
30}
31
32/// The root AST node representing a complete PHP file.
33#[derive(Debug, Serialize)]
34pub struct Program<'arena, 'src> {
35 /// Top-level statements.
36 pub stmts: ArenaVec<'arena, Stmt<'arena, 'src>>,
37 /// Source range of this node.
38 pub span: Span,
39}
40
41/// A call argument, including named, spread and by-reference forms.
42#[derive(Debug, Serialize)]
43pub struct Arg<'arena, 'src> {
44 /// Argument name in a named argument (PHP 8.0+).
45 pub name: Option<Name<'arena, 'src>>,
46 /// `None` is a PHP 8.6 partial-application placeholder (`?`, or `...` when `unpack` is set).
47 pub value: Option<Expr<'arena, 'src>>,
48 /// `true` for spread (`...$args`).
49 pub unpack: bool,
50 /// `true` when `&` precedes the argument.
51 pub by_ref: bool,
52 /// Source range of this node.
53 pub span: Span,
54}
55
56/// A single attribute: `#[Name(args)]`.
57#[derive(Debug, Serialize)]
58pub struct Attribute<'arena, 'src> {
59 /// Attribute class name.
60 pub name: Name<'arena, 'src>,
61 /// Attribute arguments.
62 pub args: ArenaVec<'arena, Arg<'arena, 'src>>,
63 /// Source range of this node.
64 pub span: Span,
65}