xdl-parser 0.1.0

XDL/IDL language parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! XDL Abstract Syntax Tree definitions

use xdl_core::XdlValue;
/// Source location information
#[derive(Debug, Clone, PartialEq)]
pub struct Location {
    pub line: usize,
    pub column: usize,
    pub filename: Option<String>,
}

/// XDL Program (top-level AST node)
#[derive(Debug, Clone, PartialEq)]
pub struct Program {
    pub statements: Vec<Statement>,
    pub location: Location,
}

/// XDL Statements
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
    Assignment {
        target: Expression,
        value: Expression,
        location: Location,
    },
    Expression {
        expr: Expression,
        location: Location,
    },
    If {
        condition: Expression,
        then_block: Vec<Statement>,
        else_block: Option<Vec<Statement>>,
        location: Location,
    },
    For {
        variable: String,
        start: Expression,
        end: Expression,
        step: Option<Expression>,
        body: Vec<Statement>,
        location: Location,
    },
    Foreach {
        variable: String,
        iterable: Expression,
        index_var: Option<String>,
        body: Vec<Statement>,
        location: Location,
    },
    While {
        condition: Expression,
        body: Vec<Statement>,
        location: Location,
    },
    Repeat {
        body: Vec<Statement>,
        condition: Expression,
        location: Location,
    },
    Break {
        location: Location,
    },
    Continue {
        location: Location,
    },
    Return {
        value: Option<Expression>,
        location: Location,
    },
    ProcedureCall {
        name: String,
        args: Vec<Expression>,
        keywords: Vec<Keyword>,
        location: Location,
    },
    Common {
        name: String,
        variables: Vec<String>,
        location: Location,
    },
    CompileOpt {
        options: Vec<String>,
        location: Location,
    },
    FunctionDef {
        name: String,
        params: Vec<Parameter>,
        keywords: Vec<KeywordDecl>,
        body: Vec<Statement>,
        location: Location,
    },
    ProcedureDef {
        name: String,
        params: Vec<Parameter>,
        keywords: Vec<KeywordDecl>,
        body: Vec<Statement>,
        location: Location,
    },
    Label {
        name: String,
        location: Location,
    },
    Goto {
        label: String,
        location: Location,
    },
    Case {
        expr: Expression,
        branches: Vec<CaseBranch>,
        else_block: Option<Vec<Statement>>,
        location: Location,
    },
    Switch {
        expr: Expression,
        branches: Vec<CaseBranch>,
        else_block: Option<Vec<Statement>>,
        location: Location,
    },
    // Object-oriented programming
    ClassDefinition {
        name: String,
        body: Vec<Statement>,
        location: Location,
    },
    MethodDefinition {
        class_name: String,
        method_name: String,
        is_function: bool, // true for FUNCTION, false for PRO
        params: Vec<Parameter>,
        keywords: Vec<KeywordDecl>,
        body: Vec<Statement>,
        location: Location,
    },
    ObjectDestroy {
        objects: Vec<Expression>,
        location: Location,
    },
}

/// XDL Expressions
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    Literal {
        value: XdlValue,
        location: Location,
    },
    Variable {
        name: String,
        location: Location,
    },
    SystemVariable {
        name: String,
        location: Location,
    },
    ArrayRef {
        array: Box<Expression>,
        indices: Vec<ArrayIndex>,
        location: Location,
    },
    StructRef {
        object: Box<Expression>,
        field: String,
        location: Location,
    },
    MethodCall {
        object: Box<Expression>,
        method: String,
        args: Vec<Expression>,
        keywords: Vec<Keyword>,
        location: Location,
    },
    FunctionCall {
        name: String,
        args: Vec<Expression>,
        keywords: Vec<Keyword>,
        location: Location,
    },
    Binary {
        op: BinaryOp,
        left: Box<Expression>,
        right: Box<Expression>,
        location: Location,
    },
    Unary {
        op: UnaryOp,
        expr: Box<Expression>,
        location: Location,
    },
    Ternary {
        condition: Box<Expression>,
        if_true: Box<Expression>,
        if_false: Box<Expression>,
        location: Location,
    },
    ArrayDef {
        elements: Vec<Expression>,
        location: Location,
    },
    StructDef {
        name: Option<String>,
        fields: Vec<StructField>,
        location: Location,
    },
    Pointer {
        expr: Box<Expression>,
        location: Location,
    },
    Deref {
        expr: Box<Expression>,
        location: Location,
    },
    PostIncrement {
        expr: Box<Expression>,
        location: Location,
    },
    PostDecrement {
        expr: Box<Expression>,
        location: Location,
    },
    PreIncrement {
        expr: Box<Expression>,
        location: Location,
    },
    PreDecrement {
        expr: Box<Expression>,
        location: Location,
    },
    // Object-oriented programming
    ObjectNew {
        class_name: String,
        args: Vec<Expression>,
        keywords: Vec<Keyword>,
        location: Location,
    },
}

/// Array indexing expressions
#[derive(Debug, Clone, PartialEq)]
pub enum ArrayIndex {
    Single(Box<Expression>),
    Range {
        start: Option<Box<Expression>>,
        end: Option<Box<Expression>>,
        step: Option<Box<Expression>>,
    },
    All, // *
}

/// Binary operators
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    // Arithmetic
    Add,
    Subtract,
    Multiply,
    Divide,
    Modulo,
    Power,
    MatrixMultiply,

    // Logical
    And,
    Or,
    Xor,

    // Bitwise
    BitwiseAnd,
    BitwiseOr,
    BitwiseXor,
    LeftShift,
    RightShift,

    // Comparison
    Equal,
    NotEqual,
    Less,
    LessEqual,
    Greater,
    GreaterEqual,

    // String
    Concatenate,

    // Assignment
    Assign,
    PlusAssign,
    MinusAssign,
    MultiplyAssign,
    DivideAssign,
    ModuloAssign,
    PowerAssign,
    AndAssign,
    OrAssign,
    XorAssign,
}

/// Unary operators
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
    Plus,
    Minus,
    Not,
    BitwiseNot,
}

/// Function/procedure parameters
#[derive(Debug, Clone, PartialEq)]
pub struct Parameter {
    pub name: String,
    pub by_reference: bool,
    pub optional: bool,
    pub location: Location,
}

/// Keyword declarations in function/procedure definitions
#[derive(Debug, Clone, PartialEq)]
pub struct KeywordDecl {
    pub name: String,
    pub by_reference: bool,
    pub location: Location,
}

/// Keyword arguments in function/procedure calls
#[derive(Debug, Clone, PartialEq)]
pub struct Keyword {
    pub name: String,
    pub value: Option<Expression>,
    pub location: Location,
}

/// CASE/SWITCH branch
#[derive(Debug, Clone, PartialEq)]
pub struct CaseBranch {
    pub values: Vec<Expression>, // List of values to match (can be ranges or single values)
    pub body: Vec<Statement>,
    pub location: Location,
}

/// Structure field definition
#[derive(Debug, Clone, PartialEq)]
pub struct StructField {
    pub name: String,
    pub value: Expression,
    pub location: Location,
}

impl Location {
    pub fn new(line: usize, column: usize) -> Self {
        Self {
            line,
            column,
            filename: None,
        }
    }

    pub fn with_file(line: usize, column: usize, filename: String) -> Self {
        Self {
            line,
            column,
            filename: Some(filename),
        }
    }

    pub fn unknown() -> Self {
        Self {
            line: 0,
            column: 0,
            filename: None,
        }
    }
}

impl Statement {
    /// Get the location of this statement
    pub fn location(&self) -> &Location {
        match self {
            Statement::Assignment { location, .. } => location,
            Statement::Expression { location, .. } => location,
            Statement::If { location, .. } => location,
            Statement::For { location, .. } => location,
            Statement::Foreach { location, .. } => location,
            Statement::While { location, .. } => location,
            Statement::Repeat { location, .. } => location,
            Statement::Break { location } => location,
            Statement::Continue { location } => location,
            Statement::Return { location, .. } => location,
            Statement::ProcedureCall { location, .. } => location,
            Statement::Common { location, .. } => location,
            Statement::CompileOpt { location, .. } => location,
            Statement::FunctionDef { location, .. } => location,
            Statement::ProcedureDef { location, .. } => location,
            Statement::Label { location, .. } => location,
            Statement::Goto { location, .. } => location,
            Statement::Case { location, .. } => location,
            Statement::Switch { location, .. } => location,
            Statement::ClassDefinition { location, .. } => location,
            Statement::MethodDefinition { location, .. } => location,
            Statement::ObjectDestroy { location, .. } => location,
        }
    }
}

impl Expression {
    /// Get the location of this expression
    pub fn location(&self) -> &Location {
        match self {
            Expression::Literal { location, .. } => location,
            Expression::Variable { location, .. } => location,
            Expression::SystemVariable { location, .. } => location,
            Expression::ArrayRef { location, .. } => location,
            Expression::StructRef { location, .. } => location,
            Expression::MethodCall { location, .. } => location,
            Expression::FunctionCall { location, .. } => location,
            Expression::Binary { location, .. } => location,
            Expression::Unary { location, .. } => location,
            Expression::Ternary { location, .. } => location,
            Expression::ArrayDef { location, .. } => location,
            Expression::StructDef { location, .. } => location,
            Expression::Pointer { location, .. } => location,
            Expression::Deref { location, .. } => location,
            Expression::PostIncrement { location, .. } => location,
            Expression::PostDecrement { location, .. } => location,
            Expression::PreIncrement { location, .. } => location,
            Expression::PreDecrement { location, .. } => location,
            Expression::ObjectNew { location, .. } => location,
        }
    }

    /// Check if this expression is a constant literal
    pub fn is_constant(&self) -> bool {
        matches!(self, Expression::Literal { .. })
    }

    /// Check if this expression is a simple variable reference
    pub fn is_variable(&self) -> bool {
        matches!(self, Expression::Variable { .. })
    }
}