Skip to main content

oak_powershell/ast/
mod.rs

1use serde::{Deserialize, Serialize};
2
3/// PowerShell AST 根节点
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub struct PowerShellRoot {
6    pub items: Vec<PowerShellItem>,
7}
8
9/// PowerShell 顶级项目
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum PowerShellItem {
12    Statement(PowerShellStatement),
13    Function(PowerShellFunction),
14    Class(PowerShellClass),
15    Workflow(PowerShellWorkflow),
16}
17
18/// PowerShell 语句
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub enum PowerShellStatement {
21    Expression(Box<PowerShellExpression>),
22    Assignment(PowerShellAssignment),
23    If(PowerShellIf),
24    While(PowerShellWhile),
25    For(PowerShellFor),
26    ForEach(PowerShellForEach),
27    Switch(PowerShellSwitch),
28    Try(PowerShellTry),
29    Return(PowerShellReturn),
30    Break(PowerShellBreak),
31    Continue(PowerShellContinue),
32    Exit(PowerShellExit),
33    Throw(PowerShellThrow),
34    Block(PowerShellBlock),
35}
36
37/// PowerShell 表达式
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
39pub enum PowerShellExpression {
40    Literal(PowerShellLiteral),
41    Variable(PowerShellVariable),
42    Command(PowerShellCommand),
43    Pipeline(PowerShellPipeline),
44    Binary(PowerShellBinaryOp),
45    Unary(PowerShellUnaryOp),
46    Member(PowerShellMemberAccess),
47    Index(PowerShellIndexAccess),
48    Subexpression(Box<PowerShellExpression>),
49    Array(PowerShellArray),
50    Hashtable(PowerShellHashtable),
51    ScriptBlock(PowerShellScriptBlock),
52}
53
54/// PowerShell 字面量
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub enum PowerShellLiteral {
57    String(String),
58    Number(String),
59    Boolean(bool),
60    Null,
61}
62
63/// PowerShell 变量
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct PowerShellVariable {
66    pub name: String,
67    pub scope: Option<String>,
68}
69
70/// PowerShell 命令
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72pub struct PowerShellCommand {
73    pub name: String,
74    pub arguments: Vec<PowerShellArgument>,
75}
76
77/// PowerShell 参数
78#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
79pub enum PowerShellArgument {
80    Positional(Box<PowerShellExpression>),
81    Named(String, Box<PowerShellExpression>),
82    Switch(String),
83}
84
85/// PowerShell 管道
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub struct PowerShellPipeline {
88    pub commands: Vec<PowerShellCommand>,
89}
90
91/// PowerShell 二元操作
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93pub struct PowerShellBinaryOp {
94    pub left: Box<PowerShellExpression>,
95    pub operator: PowerShellBinaryOperator,
96    pub right: Box<PowerShellExpression>,
97}
98
99/// PowerShell 二元操作符
100#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
101pub enum PowerShellBinaryOperator {
102    Add,
103    Subtract,
104    Multiply,
105    Divide,
106    Modulo,
107    Equal,
108    NotEqual,
109    Less,
110    LessEqual,
111    Greater,
112    GreaterEqual,
113    Like,
114    NotLike,
115    Match,
116    NotMatch,
117    Contains,
118    NotContains,
119    In,
120    NotIn,
121    And,
122    Or,
123    Xor,
124    BitwiseAnd,
125    BitwiseOr,
126    BitwiseXor,
127}
128
129/// PowerShell 一元操作
130#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
131pub struct PowerShellUnaryOp {
132    pub operator: PowerShellUnaryOperator,
133    pub operand: Box<PowerShellExpression>,
134}
135
136/// PowerShell 一元操作符
137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138pub enum PowerShellUnaryOperator {
139    Plus,
140    Minus,
141    Not,
142    BitwiseNot,
143    Increment,
144    Decrement,
145}
146
147/// PowerShell 成员访问
148#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
149pub struct PowerShellMemberAccess {
150    pub object: Box<PowerShellExpression>,
151    pub member: String,
152}
153
154/// PowerShell 索引访问
155#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
156pub struct PowerShellIndexAccess {
157    pub object: Box<PowerShellExpression>,
158    pub index: Box<PowerShellExpression>,
159}
160
161/// PowerShell 数组
162#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
163pub struct PowerShellArray {
164    pub elements: Vec<PowerShellExpression>,
165}
166
167/// PowerShell 哈希表
168#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
169pub struct PowerShellHashtable {
170    pub entries: Vec<PowerShellHashtableEntry>,
171}
172
173/// PowerShell 哈希表条目
174#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
175pub struct PowerShellHashtableEntry {
176    pub key: Box<PowerShellExpression>,
177    pub value: Box<PowerShellExpression>,
178}
179
180/// PowerShell 脚本块
181#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
182pub struct PowerShellScriptBlock {
183    pub statements: Vec<PowerShellStatement>,
184}
185
186/// PowerShell 赋值
187#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
188pub struct PowerShellAssignment {
189    pub target: Box<PowerShellExpression>,
190    pub operator: PowerShellAssignmentOperator,
191    pub value: Box<PowerShellExpression>,
192}
193
194/// PowerShell 赋值操作符
195#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
196pub enum PowerShellAssignmentOperator {
197    Assign,
198    PlusAssign,
199    MinusAssign,
200    MultiplyAssign,
201    DivideAssign,
202    ModuloAssign,
203}
204
205/// PowerShell if 语句
206#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
207pub struct PowerShellIf {
208    pub condition: Box<PowerShellExpression>,
209    pub then_block: PowerShellScriptBlock,
210    pub elseif_blocks: Vec<PowerShellElseIf>,
211    pub else_block: Option<PowerShellScriptBlock>,
212}
213
214/// PowerShell elseif 语句
215#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
216pub struct PowerShellElseIf {
217    pub condition: Box<PowerShellExpression>,
218    pub block: PowerShellScriptBlock,
219}
220
221/// PowerShell while 语句
222#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
223pub struct PowerShellWhile {
224    pub condition: Box<PowerShellExpression>,
225    pub block: PowerShellScriptBlock,
226}
227
228/// PowerShell for 语句
229#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
230pub struct PowerShellFor {
231    pub init: Option<Box<PowerShellExpression>>,
232    pub condition: Option<Box<PowerShellExpression>>,
233    pub update: Option<Box<PowerShellExpression>>,
234    pub block: PowerShellScriptBlock,
235}
236
237/// PowerShell foreach 语句
238#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
239pub struct PowerShellForEach {
240    pub variable: PowerShellVariable,
241    pub collection: Box<PowerShellExpression>,
242    pub block: PowerShellScriptBlock,
243}
244
245/// PowerShell switch 语句
246#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
247pub struct PowerShellSwitch {
248    pub expression: Box<PowerShellExpression>,
249    pub cases: Vec<PowerShellSwitchCase>,
250    pub default: Option<PowerShellScriptBlock>,
251}
252
253/// PowerShell switch case
254#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
255pub struct PowerShellSwitchCase {
256    pub pattern: Box<PowerShellExpression>,
257    pub block: PowerShellScriptBlock,
258}
259
260/// PowerShell try 语句
261#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
262pub struct PowerShellTry {
263    pub block: PowerShellScriptBlock,
264    pub catch_blocks: Vec<PowerShellCatch>,
265    pub finally_block: Option<PowerShellScriptBlock>,
266}
267
268/// PowerShell catch 语句
269#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
270pub struct PowerShellCatch {
271    pub exception_type: Option<String>,
272    pub block: PowerShellScriptBlock,
273}
274
275/// PowerShell return 语句
276#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
277pub struct PowerShellReturn {
278    pub value: Option<Box<PowerShellExpression>>,
279}
280
281/// PowerShell break 语句
282#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
283pub struct PowerShellBreak {
284    pub label: Option<String>,
285}
286
287/// PowerShell continue 语句
288#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
289pub struct PowerShellContinue {
290    pub label: Option<String>,
291}
292
293/// PowerShell exit 语句
294#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
295pub struct PowerShellExit {
296    pub code: Option<Box<PowerShellExpression>>,
297}
298
299/// PowerShell throw 语句
300#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
301pub struct PowerShellThrow {
302    pub exception: Option<Box<PowerShellExpression>>,
303}
304
305/// PowerShell 代码块
306#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
307pub struct PowerShellBlock {
308    pub statements: Vec<PowerShellStatement>,
309}
310
311/// PowerShell 函数
312#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
313pub struct PowerShellFunction {
314    pub name: String,
315    pub parameters: Vec<PowerShellParameter>,
316    pub body: PowerShellScriptBlock,
317    pub attributes: Vec<PowerShellAttribute>,
318}
319
320/// PowerShell 参数定义
321#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
322pub struct PowerShellParameter {
323    pub name: String,
324    pub param_type: Option<String>,
325    pub default_value: Option<Box<PowerShellExpression>>,
326    pub attributes: Vec<PowerShellAttribute>,
327}
328
329/// PowerShell 属性
330#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
331pub struct PowerShellAttribute {
332    pub name: String,
333    pub arguments: Vec<PowerShellExpression>,
334}
335
336/// PowerShell 类
337#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
338pub struct PowerShellClass {
339    pub name: String,
340    pub base_class: Option<String>,
341    pub members: Vec<PowerShellClassMember>,
342}
343
344/// PowerShell 类成员
345#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
346pub enum PowerShellClassMember {
347    Property(PowerShellProperty),
348    Method(PowerShellMethod),
349    Constructor(PowerShellConstructor),
350}
351
352/// PowerShell 属性
353#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
354pub struct PowerShellProperty {
355    pub name: String,
356    pub property_type: Option<String>,
357    pub default_value: Option<Box<PowerShellExpression>>,
358    pub attributes: Vec<PowerShellAttribute>,
359}
360
361/// PowerShell 方法
362#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
363pub struct PowerShellMethod {
364    pub name: String,
365    pub parameters: Vec<PowerShellParameter>,
366    pub return_type: Option<String>,
367    pub body: PowerShellScriptBlock,
368    pub attributes: Vec<PowerShellAttribute>,
369}
370
371/// PowerShell 构造函数
372#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
373pub struct PowerShellConstructor {
374    pub parameters: Vec<PowerShellParameter>,
375    pub body: PowerShellScriptBlock,
376}
377
378/// PowerShell 工作流
379#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
380pub struct PowerShellWorkflow {
381    pub name: String,
382    pub parameters: Vec<PowerShellParameter>,
383    pub body: PowerShellScriptBlock,
384}