Skip to main content

oak_vbnet/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use core::range::Range;
4
5/// Root node of the VB.NET AST.
6#[derive(Debug, Clone, PartialEq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct VbNetRoot {
9    /// Items in the compilation unit.
10    pub items: Vec<Item>,
11}
12
13/// Top-level items in a VB.NET program.
14#[derive(Debug, Clone, PartialEq)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub enum Item {
17    /// Namespace declaration.
18    Namespace(NamespaceDeclaration),
19    /// Imports directive.
20    Imports(ImportsDirective),
21    /// Class declaration.
22    Class(ClassDeclaration),
23    /// Interface declaration.
24    Interface(InterfaceDeclaration),
25    /// Structure declaration.
26    Structure(StructureDeclaration),
27    /// Enum declaration.
28    Enum(EnumDeclaration),
29    /// Module declaration.
30    Module(ModuleDeclaration),
31    /// Delegate declaration.
32    Delegate(DelegateDeclaration),
33    /// Event declaration.
34    Event(EventDeclaration),
35    /// Function declaration.
36    Function(FunctionDeclaration),
37    /// Subroutine declaration.
38    Sub(SubDeclaration),
39    /// Property declaration.
40    Property(PropertyDeclaration),
41    /// Variable declaration.
42    Variable(VariableDeclaration),
43}
44
45/// Namespace declaration.
46#[derive(Debug, Clone, PartialEq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub struct NamespaceDeclaration {
49    /// The fully qualified name of the namespace.
50    pub name: String,
51    /// Items defined within this namespace.
52    pub items: Vec<Item>,
53    /// Source location of the entire namespace declaration.
54    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
55    pub span: Range<usize>,
56}
57
58/// Imports directive.
59#[derive(Debug, Clone, PartialEq)]
60#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
61pub struct ImportsDirective {
62    /// The namespace or type path being imported.
63    pub path: String,
64    /// An optional alias for the namespace or type.
65    pub alias: Option<String>,
66    /// Source location of the imports directive.
67    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
68    pub span: Range<usize>,
69}
70
71/// Class declaration.
72#[derive(Debug, Clone, PartialEq)]
73#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
74pub struct ClassDeclaration {
75    /// The name of the class.
76    pub name: String,
77    /// Attributes applied to the class.
78    pub attributes: Vec<Attribute>,
79    /// Modifiers like `Public`, `Private`, `Shared`, `MustInherit`, `NotInheritable`.
80    pub modifiers: Vec<String>,
81    /// The base class and any implemented interfaces.
82    pub base_types: Vec<String>,
83    /// Members of the class.
84    pub members: Vec<Member>,
85    /// Source location of the class declaration.
86    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
87    pub span: Range<usize>,
88}
89
90/// Interface declaration.
91#[derive(Debug, Clone, PartialEq)]
92#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
93pub struct InterfaceDeclaration {
94    /// The name of the interface.
95    pub name: String,
96    /// Attributes applied to the interface.
97    pub attributes: Vec<Attribute>,
98    /// Modifiers like `Public`, `Friend`.
99    pub modifiers: Vec<String>,
100    /// Members defined in the interface.
101    pub members: Vec<Member>,
102    /// Source location of the interface declaration.
103    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
104    pub span: Range<usize>,
105}
106
107/// Structure declaration.
108#[derive(Debug, Clone, PartialEq)]
109#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
110pub struct StructureDeclaration {
111    /// The name of the structure.
112    pub name: String,
113    /// Attributes applied to the structure.
114    pub attributes: Vec<Attribute>,
115    /// Modifiers like `Public`, `Private`.
116    pub modifiers: Vec<String>,
117    /// Members of the structure.
118    pub members: Vec<Member>,
119    /// Source location of the structure declaration.
120    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
121    pub span: Range<usize>,
122}
123
124/// Enum declaration.
125#[derive(Debug, Clone, PartialEq)]
126#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
127pub struct EnumDeclaration {
128    /// The name of the enum.
129    pub name: String,
130    /// Attributes applied to the enum.
131    pub attributes: Vec<Attribute>,
132    /// Modifiers like `Public`, `Friend`.
133    pub modifiers: Vec<String>,
134    /// The individual members (constants) of the enum.
135    pub members: Vec<EnumMember>,
136    /// Source location of the enum declaration.
137    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
138    pub span: Range<usize>,
139}
140
141/// Enum member.
142#[derive(Debug, Clone, PartialEq)]
143#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
144pub struct EnumMember {
145    /// Member name.
146    pub name: String,
147    /// Attributes.
148    pub attributes: Vec<Attribute>,
149    /// Member value.
150    pub value: Option<Expression>,
151}
152
153/// Module declaration.
154#[derive(Debug, Clone, PartialEq)]
155#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
156pub struct ModuleDeclaration {
157    /// The name of the module.
158    pub name: String,
159    /// Attributes applied to the module.
160    pub attributes: Vec<Attribute>,
161    /// Modifiers like `Public`, `Friend`.
162    pub modifiers: Vec<String>,
163    /// Members of the module.
164    pub members: Vec<Member>,
165    /// Source location of the module declaration.
166    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
167    pub span: Range<usize>,
168}
169
170/// Delegate declaration.
171#[derive(Debug, Clone, PartialEq)]
172#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
173pub struct DelegateDeclaration {
174    /// The name of the delegate.
175    pub name: String,
176    /// Attributes applied to the delegate.
177    pub attributes: Vec<Attribute>,
178    /// Modifiers like `Public`, `Friend`.
179    pub modifiers: Vec<String>,
180    /// The return type of the method signature.
181    pub return_type: String,
182    /// The parameters of the delegate method signature.
183    pub parameters: Vec<Parameter>,
184    /// Source location of the delegate declaration.
185    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
186    pub span: Range<usize>,
187}
188
189/// Event declaration.
190#[derive(Debug, Clone, PartialEq)]
191#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
192pub struct EventDeclaration {
193    /// Event name.
194    pub name: String,
195    /// Attributes.
196    pub attributes: Vec<Attribute>,
197    /// Event type.
198    pub event_type: String,
199    /// Modifiers.
200    pub modifiers: Vec<String>,
201    /// Source location.
202    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
203    pub span: Range<usize>,
204}
205
206/// Function declaration.
207#[derive(Debug, Clone, PartialEq)]
208#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
209pub struct FunctionDeclaration {
210    /// Function name.
211    pub name: String,
212    /// Attributes.
213    pub attributes: Vec<Attribute>,
214    /// Modifiers.
215    pub modifiers: Vec<String>,
216    /// Return type.
217    pub return_type: String,
218    /// Parameters.
219    pub parameters: Vec<Parameter>,
220    /// Function body.
221    pub body: Option<Vec<Statement>>,
222    /// Source location.
223    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
224    pub span: Range<usize>,
225}
226
227/// Subroutine declaration.
228#[derive(Debug, Clone, PartialEq)]
229#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
230pub struct SubDeclaration {
231    /// Subroutine name.
232    pub name: String,
233    /// Attributes.
234    pub attributes: Vec<Attribute>,
235    /// Modifiers.
236    pub modifiers: Vec<String>,
237    /// Parameters.
238    pub parameters: Vec<Parameter>,
239    /// Subroutine body.
240    pub body: Option<Vec<Statement>>,
241    /// Source location.
242    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
243    pub span: Range<usize>,
244}
245
246/// Property declaration.
247#[derive(Debug, Clone, PartialEq)]
248#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
249pub struct PropertyDeclaration {
250    /// Property name.
251    pub name: String,
252    /// Attributes.
253    pub attributes: Vec<Attribute>,
254    /// Property type.
255    pub property_type: String,
256    /// Modifiers.
257    pub modifiers: Vec<String>,
258    /// Get accessor.
259    pub get_accessor: Option<Accessor>,
260    /// Set accessor.
261    pub set_accessor: Option<Accessor>,
262    /// Source location.
263    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
264    pub span: Range<usize>,
265}
266
267/// Variable declaration.
268#[derive(Debug, Clone, PartialEq)]
269#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
270pub struct VariableDeclaration {
271    /// Variable name.
272    pub name: String,
273    /// Attributes.
274    pub attributes: Vec<Attribute>,
275    /// Variable type.
276    pub variable_type: String,
277    /// Modifiers.
278    pub modifiers: Vec<String>,
279    /// Initializer.
280    pub initializer: Option<Expression>,
281    /// Source location.
282    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
283    pub span: Range<usize>,
284}
285
286/// Member declaration.
287#[derive(Debug, Clone, PartialEq)]
288#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
289pub enum Member {
290    /// Event declaration.
291    Event(EventDeclaration),
292    /// Function declaration.
293    Function(FunctionDeclaration),
294    /// Subroutine declaration.
295    Sub(SubDeclaration),
296    /// Property declaration.
297    Property(PropertyDeclaration),
298    /// Variable declaration.
299    Variable(VariableDeclaration),
300}
301
302/// Accessor (Get/Set).
303#[derive(Debug, Clone, PartialEq)]
304#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
305pub struct Accessor {
306    /// Attributes.
307    pub attributes: Vec<Attribute>,
308    /// Accessor body.
309    pub body: Option<Vec<Statement>>,
310    /// Modifiers.
311    pub modifiers: Vec<String>,
312}
313
314/// Parameter.
315#[derive(Debug, Clone, PartialEq)]
316#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
317pub struct Parameter {
318    /// Attributes.
319    pub attributes: Vec<Attribute>,
320    /// Parameter name.
321    pub name: String,
322    /// Parameter type.
323    pub parameter_type: String,
324    /// Modifiers (ByVal, ByRef, Optional).
325    pub modifiers: Vec<String>,
326    /// Default value (for Optional parameters).
327    pub default_value: Option<Expression>,
328}
329
330/// Attribute.
331#[derive(Debug, Clone, PartialEq)]
332#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
333pub struct Attribute {
334    /// Attribute name.
335    pub name: String,
336    /// Argument list.
337    pub arguments: Vec<Expression>,
338    /// Source location.
339    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
340    pub span: Range<usize>,
341}
342
343/// Statement.
344#[derive(Debug, Clone, PartialEq)]
345#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
346pub enum Statement {
347    /// Expression statement.
348    Expression(Expression),
349    /// Return statement.
350    Return(Option<Expression>),
351    /// If statement.
352    If {
353        /// The condition expression.
354        condition: Expression,
355        /// The then branch statement.
356        then_branch: Box<Statement>,
357        /// The optional else branch statement.
358        else_branch: Option<Box<Statement>>,
359    },
360    /// For loop.
361    For {
362        /// The loop variable.
363        variable: String,
364        /// The start expression.
365        start: Expression,
366        /// The end expression.
367        end: Expression,
368        /// The optional step expression.
369        step: Option<Expression>,
370        /// The loop body.
371        body: Box<Statement>,
372    },
373    /// For Each loop.
374    ForEach {
375        /// The item variable.
376        variable: String,
377        /// The iterable expression.
378        iterable: Expression,
379        /// The loop body.
380        body: Box<Statement>,
381    },
382    /// While loop.
383    While {
384        /// The condition expression.
385        condition: Expression,
386        /// The loop body.
387        body: Box<Statement>,
388    },
389    /// Do While loop.
390    DoWhile {
391        /// The condition expression.
392        condition: Expression,
393        /// The loop body.
394        body: Box<Statement>,
395        /// Whether the condition is checked at the end.
396        check_at_end: bool,
397    },
398    /// Select Case statement.
399    SelectCase {
400        /// The expression to evaluate.
401        expression: Expression,
402        /// The case clauses.
403        cases: Vec<CaseClause>,
404        /// The optional default case.
405        default_case: Option<Box<Statement>>,
406    },
407    /// With statement.
408    With {
409        /// The target expression.
410        target: Expression,
411        /// The statements within the With block.
412        statements: Vec<Statement>,
413    },
414    /// Try statement.
415    Try {
416        /// The try block statements.
417        try_block: Vec<Statement>,
418        /// The catch clauses.
419        catch_clauses: Vec<CatchClause>,
420        /// The optional finally block.
421        finally_block: Option<Vec<Statement>>,
422    },
423    /// Dim statement (variable declaration).
424    Dim(VariableDeclaration),
425    /// Const statement (constant declaration).
426    Const {
427        /// Constant name.
428        name: String,
429        /// Constant type.
430        constant_type: String,
431        /// Constant value.
432        value: Expression,
433    },
434    /// Exit statement.
435    Exit(String),
436    /// Continue statement.
437    Continue(String),
438    /// Block statement (multiple statements).
439    Block(Vec<Statement>),
440}
441
442/// Case clause in Select Case statement.
443#[derive(Debug, Clone, PartialEq)]
444#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
445pub struct CaseClause {
446    /// The case expressions.
447    expressions: Vec<Expression>,
448    /// The case body.
449    body: Box<Statement>,
450}
451
452/// Catch clause in Try statement.
453#[derive(Debug, Clone, PartialEq)]
454#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
455pub struct CatchClause {
456    /// The exception variable (optional).
457    variable: Option<String>,
458    /// The exception type (optional).
459    exception_type: Option<String>,
460    /// The catch block statements.
461    body: Vec<Statement>,
462}
463
464/// Expression.
465#[derive(Debug, Clone, PartialEq)]
466#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
467pub enum Expression {
468    /// Literal.
469    Literal(Literal),
470    /// Identifier.
471    Identifier(String),
472    /// Method call.
473    MethodCall(MethodCall),
474    /// Member access.
475    MemberAccess(MemberAccess),
476    /// Element access.
477    ElementAccess(ElementAccess),
478    /// New expression.
479    New(NewExpression),
480    /// Me expression.
481    Me,
482    /// MyBase expression.
483    MyBase,
484    /// MyClass expression.
485    MyClass,
486    /// Binary expression.
487    Binary {
488        /// The left operand.
489        left: Box<Expression>,
490        /// The operator string.
491        op: String,
492        /// The right operand.
493        right: Box<Expression>,
494    },
495    /// Unary expression.
496    Unary {
497        /// The operator string.
498        op: String,
499        /// The operand expression.
500        expression: Box<Expression>,
501    },
502    /// Assignment expression.
503    Assignment {
504        /// The left-hand side expression.
505        left: Box<Expression>,
506        /// The operator string.
507        op: String,
508        /// The right-hand side expression.
509        right: Box<Expression>,
510    },
511    /// Array expression.
512    Array(Vec<Expression>),
513    /// Tuple expression.
514    Tuple(Vec<Expression>),
515    /// Parenthesized expression.
516    Parenthesized(Box<Expression>),
517    /// TypeOf expression.
518    TypeOf(Box<Expression>, String),
519    /// Is expression.
520    Is(Box<Expression>, Box<Expression>),
521    /// Like expression.
522    Like(Box<Expression>, Box<Expression>),
523    /// If expression.
524    If {
525        /// The condition expression.
526        condition: Box<Expression>,
527        /// The true expression.
528        true_expression: Box<Expression>,
529        /// The false expression.
530        false_expression: Box<Expression>,
531    },
532}
533
534/// Literal.
535#[derive(Debug, Clone, PartialEq)]
536#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
537pub enum Literal {
538    /// Integer.
539    Integer(i64),
540    /// String.
541    String(String),
542    /// Boolean.
543    Boolean(bool),
544    /// Double.
545    Double(f64),
546    /// Date.
547    Date(String),
548    /// Nothing.
549    Nothing,
550}
551
552/// Member access.
553#[derive(Debug, Clone, PartialEq)]
554#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
555pub struct MemberAccess {
556    /// Target expression.
557    pub target: Box<Expression>,
558    /// Member name.
559    pub name: String,
560}
561
562/// Method call.
563#[derive(Debug, Clone, PartialEq)]
564#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
565pub struct MethodCall {
566    /// Target expression.
567    pub target: Option<Box<Expression>>,
568    /// Method name.
569    pub name: String,
570    /// Argument list.
571    pub arguments: Vec<Expression>,
572}
573
574/// Element access (indexer).
575#[derive(Debug, Clone, PartialEq)]
576#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
577pub struct ElementAccess {
578    /// Target expression.
579    pub target: Box<Expression>,
580    /// Argument list.
581    pub arguments: Vec<Expression>,
582}
583
584/// New expression.
585#[derive(Debug, Clone, PartialEq)]
586#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
587pub struct NewExpression {
588    /// Type name.
589    pub r#type: String,
590    /// Argument list.
591    pub arguments: Vec<Expression>,
592}