php_parser_rs/parser/ast/
declares.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5use crate::lexer::token::Span;
6use crate::node::Node;
7use crate::parser::ast::identifiers::SimpleIdentifier;
8use crate::parser::ast::literals::Literal;
9use crate::parser::ast::Expression;
10use crate::parser::ast::Statement;
11
12#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
13
14pub struct DeclareEntry {
15    pub key: SimpleIdentifier, // `strict_types`
16    pub equals: Span,          // `=`
17    pub value: Literal,        // `1`
18}
19
20impl Node for DeclareEntry {
21    fn children(&mut self) -> Vec<&mut dyn Node> {
22        vec![&mut self.key, &mut self.value]
23    }
24}
25
26#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
27
28pub struct DeclareEntryGroup {
29    pub left_parenthesis: Span,     // `(`
30    pub right_parenthesis: Span,    // `)`
31    pub entries: Vec<DeclareEntry>, // `strict_types = 1`
32}
33
34impl Node for DeclareEntryGroup {
35    fn children(&mut self) -> Vec<&mut dyn Node> {
36        self.entries
37            .iter_mut()
38            .map(|e| e as &mut dyn Node)
39            .collect()
40    }
41}
42
43#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
44#[serde(tag = "type", content = "value")]
45pub enum DeclareBody {
46    // declaration is terminated with `;`
47    Noop {
48        semicolon: Span, // `;`
49    },
50    // declaration is followed by a `{` and terminated with `}` after multiple statements.
51    Braced {
52        left_brace: Span,           // `{`
53        statements: Vec<Statement>, // `*statements*`
54        right_brace: Span,          // `}`
55    },
56    // declaration is terminated with `;` after a single expression.
57    Expression {
58        expression: Expression, // `*expression*`
59        semicolon: Span,        // `;`
60    },
61    // declaration is followed by a `:` and terminated with `enddeclare` and `;` after multiple statements.
62    Block {
63        colon: Span,                // `:`
64        statements: Vec<Statement>, // `*statements*`
65        end: (Span, Span),          // `enddeclare` + `;`
66    },
67}
68
69impl Node for DeclareBody {
70    fn children(&mut self) -> Vec<&mut dyn Node> {
71        match self {
72            DeclareBody::Noop { .. } => vec![],
73            DeclareBody::Braced { statements, .. } => {
74                statements.iter_mut().map(|s| s as &mut dyn Node).collect()
75            }
76            DeclareBody::Expression { expression, .. } => vec![expression],
77            DeclareBody::Block { statements, .. } => {
78                statements.iter_mut().map(|s| s as &mut dyn Node).collect()
79            }
80        }
81    }
82}
83
84#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
85
86pub struct DeclareStatement {
87    pub declare: Span,              // `declare`
88    pub entries: DeclareEntryGroup, // `(strict_types = 1)`
89    pub body: DeclareBody,          // `;`
90}
91
92impl Node for DeclareStatement {
93    fn children(&mut self) -> Vec<&mut dyn Node> {
94        vec![&mut self.entries, &mut self.body]
95    }
96}