php_parser_rs/parser/ast/
constant.rs

1use std::slice::Iter;
2
3use schemars::JsonSchema;
4use serde::Deserialize;
5use serde::Serialize;
6
7use crate::lexer::token::Span;
8use crate::node::Node;
9use crate::parser::ast::attributes::AttributeGroup;
10use crate::parser::ast::comments::CommentGroup;
11use crate::parser::ast::identifiers::SimpleIdentifier;
12use crate::parser::ast::modifiers::ConstantModifierGroup;
13use crate::parser::ast::Expression;
14
15#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
16
17pub struct ConstantEntry {
18    pub name: SimpleIdentifier, // `FOO`
19    pub equals: Span,           // `=`
20    pub value: Expression,      // `123`
21}
22
23impl Node for ConstantEntry {
24    fn children(&mut self) -> Vec<&mut dyn Node> {
25        vec![&mut self.name, &mut self.value]
26    }
27}
28
29#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
30
31pub struct ConstantStatement {
32    pub comments: CommentGroup,
33    pub r#const: Span,               // `const`
34    pub entries: Vec<ConstantEntry>, // `FOO = 123`
35    pub semicolon: Span,             // `;`
36}
37
38impl ConstantStatement {
39    pub fn iter(&self) -> Iter<'_, ConstantEntry> {
40        self.entries.iter()
41    }
42}
43
44impl IntoIterator for ConstantStatement {
45    type Item = ConstantEntry;
46    type IntoIter = std::vec::IntoIter<Self::Item>;
47
48    fn into_iter(self) -> Self::IntoIter {
49        self.entries.into_iter()
50    }
51}
52
53impl Node for ConstantStatement {
54    fn children(&mut self) -> Vec<&mut dyn Node> {
55        self.entries
56            .iter_mut()
57            .map(|e| e as &mut dyn Node)
58            .collect()
59    }
60}
61
62#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
63
64pub struct ClassishConstant {
65    pub comments: CommentGroup,
66    pub attributes: Vec<AttributeGroup>,  // `#[Foo]`
67    pub modifiers: ConstantModifierGroup, // `public`
68    pub r#const: Span,                    // `const`
69    #[serde(flatten)]
70    pub entries: Vec<ConstantEntry>, // `FOO = 123`
71    pub semicolon: Span,                  // `;`
72}
73
74impl ClassishConstant {
75    pub fn iter(&self) -> Iter<'_, ConstantEntry> {
76        self.entries.iter()
77    }
78}
79
80impl IntoIterator for ClassishConstant {
81    type Item = ConstantEntry;
82    type IntoIter = std::vec::IntoIter<Self::Item>;
83
84    fn into_iter(self) -> Self::IntoIter {
85        self.entries.into_iter()
86    }
87}
88
89impl Node for ClassishConstant {
90    fn children(&mut self) -> Vec<&mut dyn Node> {
91        self.entries
92            .iter_mut()
93            .map(|e| e as &mut dyn Node)
94            .collect()
95    }
96}