php_parser_rs/parser/ast/
try_block.rs1use 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::Block;
9
10use super::variables::SimpleVariable;
11
12#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
13#[serde(tag = "type", content = "value")]
14pub enum CatchType {
15 Identifier { identifier: SimpleIdentifier },
16 Union { identifiers: Vec<SimpleIdentifier> },
17}
18
19impl Node for CatchType {
20 fn children(&mut self) -> Vec<&mut dyn Node> {
21 match self {
22 CatchType::Identifier { identifier } => vec![identifier],
23 CatchType::Union { identifiers } => {
24 identifiers.iter_mut().map(|i| i as &mut dyn Node).collect()
25 }
26 }
27 }
28}
29
30#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
31
32pub struct TryStatement {
33 pub start: Span,
34 pub end: Span,
35 pub body: Block,
36 pub catches: Vec<CatchBlock>,
37 pub finally: Option<FinallyBlock>,
38}
39
40impl Node for TryStatement {
41 fn children(&mut self) -> Vec<&mut dyn Node> {
42 let mut children: Vec<&mut dyn Node> = vec![&mut self.body];
43 for catch in &mut self.catches {
44 children.push(catch);
45 }
46 if let Some(finally) = &mut self.finally {
47 children.push(finally);
48 }
49 children
50 }
51}
52
53#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
54
55pub struct CatchBlock {
56 pub start: Span,
57 pub end: Span,
58 pub types: CatchType,
59 pub var: Option<SimpleVariable>,
60 pub body: Block,
61}
62
63impl Node for CatchBlock {
64 fn children(&mut self) -> Vec<&mut dyn Node> {
65 let mut children = vec![&mut self.types as &mut dyn Node];
66 if let Some(var) = &mut self.var {
67 children.push(var as &mut dyn Node);
68 }
69 children.push(&mut self.body as &mut dyn Node);
70 children
71 }
72}
73
74#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
75
76pub struct FinallyBlock {
77 pub start: Span,
78 pub end: Span,
79 pub body: Block,
80}
81
82impl Node for FinallyBlock {
83 fn children(&mut self) -> Vec<&mut dyn Node> {
84 vec![&mut self.body as &mut dyn Node]
85 }
86}