php_parser_rs/parser/ast/
variables.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5use std::fmt::Display;
6
7use crate::lexer::byte_string::ByteString;
8use crate::lexer::token::Span;
9use crate::node::Node;
10use crate::parser::ast::Expression;
11
12#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
13#[serde(tag = "type", content = "value")]
14pub enum Variable {
15    SimpleVariable(SimpleVariable),
16    VariableVariable(VariableVariable),
17    BracedVariableVariable(BracedVariableVariable),
18}
19
20impl Node for Variable {
21    fn children(&mut self) -> Vec<&mut dyn Node> {
22        match self {
23            Variable::SimpleVariable(variable) => variable.children(),
24            Variable::VariableVariable(variable) => variable.children(),
25            Variable::BracedVariableVariable(variable) => variable.children(),
26        }
27    }
28}
29
30#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
31
32pub struct SimpleVariable {
33    pub span: Span,
34    pub name: ByteString,
35}
36
37impl Node for SimpleVariable {
38    //
39}
40
41#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
42
43pub struct VariableVariable {
44    pub span: Span,
45    pub variable: Box<Variable>,
46}
47
48impl Node for VariableVariable {
49    fn children(&mut self) -> Vec<&mut dyn Node> {
50        vec![self.variable.as_mut()]
51    }
52}
53
54#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
55
56pub struct BracedVariableVariable {
57    pub start: Span,
58    pub variable: Box<Expression>,
59    pub end: Span,
60}
61
62impl Node for BracedVariableVariable {
63    fn children(&mut self) -> Vec<&mut dyn Node> {
64        vec![self.variable.as_mut()]
65    }
66}
67
68impl Display for SimpleVariable {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{}", self.name)
71    }
72}