Skip to main content

oak_php/ast/
mod.rs

1use serde::{Deserialize, Serialize};
2
3/// PHP AST 根节点
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub struct PhpRoot {
6    pub items: Vec<PhpItem>,
7}
8
9/// PHP 顶级项目
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum PhpItem {
12    OpenTag,
13    CloseTag,
14    Statement(PhpStatement),
15    Function(PhpFunction),
16    Class(PhpClass),
17    Interface(PhpInterface),
18    Trait(PhpTrait),
19    Namespace(PhpNamespace),
20    Use(PhpUse),
21}
22
23/// PHP 语句
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
25pub enum PhpStatement {
26    Expression(PhpExpression),
27    If(PhpIf),
28    While(PhpWhile),
29    For(PhpFor),
30    Foreach(PhpForeach),
31    Switch(PhpSwitch),
32    Try(PhpTry),
33    Return(Option<PhpExpression>),
34    Break(Option<PhpExpression>),
35    Continue(Option<PhpExpression>),
36    Echo(Vec<PhpExpression>),
37    Print(PhpExpression),
38    Global(Vec<String>),
39    Static(Vec<PhpVariable>),
40    Unset(Vec<PhpExpression>),
41    Declare(Vec<PhpDeclareItem>),
42    Block(Vec<PhpStatement>),
43}
44
45/// PHP 表达式
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47pub enum PhpExpression {
48    Literal(PhpLiteral),
49    Variable(PhpVariable),
50    Array(Vec<PhpArrayElement>),
51    FunctionCall(PhpFunctionCall),
52    MethodCall(PhpMethodCall),
53    PropertyAccess(PhpPropertyAccess),
54    ArrayAccess(PhpArrayAccess),
55    Assignment(PhpAssignment),
56    BinaryOp(PhpBinaryOp),
57    UnaryOp(PhpUnaryOp),
58    TernaryOp(PhpTernaryOp),
59    Cast(PhpCast),
60    New(PhpNew),
61    Clone(Box<PhpExpression>),
62    Instanceof(PhpInstanceof),
63    Include(PhpInclude),
64    Require(PhpRequire),
65    Eval(Box<PhpExpression>),
66    Exit(Option<Box<PhpExpression>>),
67    Empty(Box<PhpExpression>),
68    Isset(Vec<PhpExpression>),
69    List(Vec<Option<PhpExpression>>),
70    Yield(PhpYield),
71}
72
73/// PHP 字面量
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub enum PhpLiteral {
76    String(String),
77    Number(String),
78    Boolean(bool),
79    Null,
80}
81
82/// PHP 变量
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub struct PhpVariable {
85    pub name: String,
86}
87
88/// PHP 数组元素
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct PhpArrayElement {
91    pub key: Option<PhpExpression>,
92    pub value: PhpExpression,
93}
94
95/// PHP 函数调用
96#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
97pub struct PhpFunctionCall {
98    pub name: Box<PhpExpression>,
99    pub arguments: Vec<PhpExpression>,
100}
101
102/// PHP 方法调用
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct PhpMethodCall {
105    pub object: Box<PhpExpression>,
106    pub method: String,
107    pub arguments: Vec<PhpExpression>,
108}
109
110/// PHP 属性访问
111#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
112pub struct PhpPropertyAccess {
113    pub object: Box<PhpExpression>,
114    pub property: String,
115}
116
117/// PHP 数组访问
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119pub struct PhpArrayAccess {
120    pub array: Box<PhpExpression>,
121    pub index: Box<PhpExpression>,
122}
123
124/// PHP 赋值
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
126pub struct PhpAssignment {
127    pub left: Box<PhpExpression>,
128    pub operator: PhpAssignmentOp,
129    pub right: Box<PhpExpression>,
130}
131
132/// PHP 赋值操作符
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub enum PhpAssignmentOp {
135    Assign,
136    PlusAssign,
137    MinusAssign,
138    MultiplyAssign,
139    DivideAssign,
140    ModuloAssign,
141    PowerAssign,
142    ConcatAssign,
143    BitwiseAndAssign,
144    BitwiseOrAssign,
145    BitwiseXorAssign,
146    LeftShiftAssign,
147    RightShiftAssign,
148    NullCoalesceAssign,
149}
150
151/// PHP 二元操作
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
153pub struct PhpBinaryOp {
154    pub left: Box<PhpExpression>,
155    pub operator: PhpBinaryOperator,
156    pub right: Box<PhpExpression>,
157}
158
159/// PHP 二元操作符
160#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161pub enum PhpBinaryOperator {
162    Plus,
163    Minus,
164    Multiply,
165    Divide,
166    Modulo,
167    Power,
168    Concat,
169    Equal,
170    NotEqual,
171    Identical,
172    NotIdentical,
173    Less,
174    LessEqual,
175    Greater,
176    GreaterEqual,
177    Spaceship,
178    LogicalAnd,
179    LogicalOr,
180    LogicalXor,
181    BitwiseAnd,
182    BitwiseOr,
183    BitwiseXor,
184    LeftShift,
185    RightShift,
186    NullCoalesce,
187}
188
189/// PHP 一元操作
190#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
191pub struct PhpUnaryOp {
192    pub operator: PhpUnaryOperator,
193    pub operand: Box<PhpExpression>,
194}
195
196/// PHP 一元操作符
197#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
198pub enum PhpUnaryOperator {
199    Plus,
200    Minus,
201    LogicalNot,
202    BitwiseNot,
203    PreIncrement,
204    PostIncrement,
205    PreDecrement,
206    PostDecrement,
207    ErrorSuppression,
208}
209
210/// PHP 三元操作
211#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
212pub struct PhpTernaryOp {
213    pub condition: Box<PhpExpression>,
214    pub true_expr: Option<Box<PhpExpression>>,
215    pub false_expr: Box<PhpExpression>,
216}
217
218/// PHP 类型转换
219#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
220pub struct PhpCast {
221    pub cast_type: PhpCastType,
222    pub expression: Box<PhpExpression>,
223}
224
225/// PHP 类型转换类型
226#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
227pub enum PhpCastType {
228    Int,
229    Float,
230    String,
231    Bool,
232    Array,
233    Object,
234    Unset,
235}
236
237/// PHP new 表达式
238#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
239pub struct PhpNew {
240    pub class: Box<PhpExpression>,
241    pub arguments: Vec<PhpExpression>,
242}
243
244/// PHP instanceof 表达式
245#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
246pub struct PhpInstanceof {
247    pub expression: Box<PhpExpression>,
248    pub class: Box<PhpExpression>,
249}
250
251/// PHP include 表达式
252#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
253pub struct PhpInclude {
254    pub once: bool,
255    pub path: Box<PhpExpression>,
256}
257
258/// PHP require 表达式
259#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
260pub struct PhpRequire {
261    pub once: bool,
262    pub path: Box<PhpExpression>,
263}
264
265/// PHP yield 表达式
266#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
267pub struct PhpYield {
268    pub key: Option<Box<PhpExpression>>,
269    pub value: Option<Box<PhpExpression>>,
270    pub from: bool,
271}
272
273/// PHP if 语句
274#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
275pub struct PhpIf {
276    pub condition: Box<PhpExpression>,
277    pub then_stmt: Box<PhpStatement>,
278    pub elseif_stmts: Vec<PhpElseif>,
279    pub else_stmt: Option<Box<PhpStatement>>,
280}
281
282/// PHP elseif 语句
283#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
284pub struct PhpElseif {
285    pub condition: Box<PhpExpression>,
286    pub statement: Box<PhpStatement>,
287}
288
289/// PHP while 语句
290#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
291pub struct PhpWhile {
292    pub condition: Box<PhpExpression>,
293    pub statement: Box<PhpStatement>,
294}
295
296/// PHP for 语句
297#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
298pub struct PhpFor {
299    pub init: Vec<PhpExpression>,
300    pub condition: Vec<PhpExpression>,
301    pub update: Vec<PhpExpression>,
302    pub statement: Box<PhpStatement>,
303}
304
305/// PHP foreach 语句
306#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
307pub struct PhpForeach {
308    pub iterable: Box<PhpExpression>,
309    pub key: Option<Box<PhpExpression>>,
310    pub value: Box<PhpExpression>,
311    pub statement: Box<PhpStatement>,
312}
313
314/// PHP switch 语句
315#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
316pub struct PhpSwitch {
317    pub expression: Box<PhpExpression>,
318    pub cases: Vec<PhpCase>,
319}
320
321/// PHP case 语句
322#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
323pub struct PhpCase {
324    pub value: Option<Box<PhpExpression>>, // None for default case
325    pub statements: Vec<PhpStatement>,
326}
327
328/// PHP try 语句
329#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
330pub struct PhpTry {
331    pub statement: Box<PhpStatement>,
332    pub catches: Vec<PhpCatch>,
333    pub finally_stmt: Option<Box<PhpStatement>>,
334}
335
336/// PHP catch 语句
337#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
338pub struct PhpCatch {
339    pub types: Vec<String>,
340    pub variable: PhpVariable,
341    pub statement: Box<PhpStatement>,
342}
343
344/// PHP declare 项目
345#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
346pub struct PhpDeclareItem {
347    pub name: String,
348    pub value: Box<PhpExpression>,
349}
350
351/// PHP 函数
352#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
353pub struct PhpFunction {
354    pub name: String,
355    pub parameters: Vec<PhpParameter>,
356    pub return_type: Option<PhpType>,
357    pub body: Box<PhpStatement>,
358    pub by_ref: bool,
359}
360
361/// PHP 参数
362#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
363pub struct PhpParameter {
364    pub name: String,
365    pub param_type: Option<PhpType>,
366    pub default: Option<Box<PhpExpression>>,
367    pub by_ref: bool,
368    pub variadic: bool,
369}
370
371/// PHP 类型
372#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
373pub enum PhpType {
374    Named(String),
375    Nullable(Box<PhpType>),
376    Union(Vec<PhpType>),
377}
378
379/// PHP 类
380#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
381pub struct PhpClass {
382    pub name: String,
383    pub extends: Option<String>,
384    pub implements: Vec<String>,
385    pub members: Vec<PhpClassMember>,
386    pub modifiers: Vec<PhpModifier>,
387}
388
389/// PHP 类成员
390#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
391pub enum PhpClassMember {
392    Property(PhpProperty),
393    Method(PhpMethod),
394    Constant(PhpConstant),
395    Use(PhpTraitUse),
396}
397
398/// PHP 属性
399#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
400pub struct PhpProperty {
401    pub name: String,
402    pub property_type: Option<PhpType>,
403    pub default: Option<Box<PhpExpression>>,
404    pub modifiers: Vec<PhpModifier>,
405}
406
407/// PHP 方法
408#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
409pub struct PhpMethod {
410    pub name: String,
411    pub parameters: Vec<PhpParameter>,
412    pub return_type: Option<PhpType>,
413    pub body: Option<Box<PhpStatement>>,
414    pub modifiers: Vec<PhpModifier>,
415    pub by_ref: bool,
416}
417
418/// PHP 常量
419#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
420pub struct PhpConstant {
421    pub name: String,
422    pub value: Box<PhpExpression>,
423    pub modifiers: Vec<PhpModifier>,
424}
425
426/// PHP trait use
427#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
428pub struct PhpTraitUse {
429    pub traits: Vec<String>,
430    pub adaptations: Vec<PhpTraitAdaptation>,
431}
432
433/// PHP trait 适配
434#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
435pub enum PhpTraitAdaptation {
436    Precedence { method: String, trait_name: String, insteadof: Vec<String> },
437    Alias { method: String, trait_name: Option<String>, alias: String, modifier: Option<PhpModifier> },
438}
439
440/// PHP 修饰符
441#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
442pub enum PhpModifier {
443    Public,
444    Protected,
445    Private,
446    Static,
447    Abstract,
448    Final,
449}
450
451/// PHP 接口
452#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
453pub struct PhpInterface {
454    pub name: String,
455    pub extends: Vec<String>,
456    pub members: Vec<PhpInterfaceMember>,
457}
458
459/// PHP 接口成员
460#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
461pub enum PhpInterfaceMember {
462    Method(PhpMethod),
463    Constant(PhpConstant),
464}
465
466/// PHP trait
467#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
468pub struct PhpTrait {
469    pub name: String,
470    pub members: Vec<PhpClassMember>,
471}
472
473/// PHP 命名空间
474#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
475pub struct PhpNamespace {
476    pub name: Option<String>,
477    pub items: Vec<PhpItem>,
478}
479
480/// PHP use 语句
481#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
482pub struct PhpUse {
483    pub uses: Vec<PhpUseItem>,
484    pub use_type: PhpUseType,
485}
486
487/// PHP use 项目
488#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
489pub struct PhpUseItem {
490    pub name: String,
491    pub alias: Option<String>,
492}
493
494/// PHP use 类型
495#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
496pub enum PhpUseType {
497    Normal,
498    Function,
499    Const,
500}