sqlexpr-congo-rust 1.0.0

Parser for SqlExprParser - Generated by CongoCC
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Arena allocator for AST nodes. Generated by CongoCC Parser Generator. Do not edit.

use crate::tokens::Token;
use std::fmt;

/// Type-safe index for nodes in the arena
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeId(pub usize);
impl fmt::Display for NodeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "NodeId({})", self.0)
    }
}

/// Type-safe index for tokens in the arena
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TokenId(pub usize);

/// Arena that owns all AST nodes and tokens
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Arena {
    /// All AST nodes
    nodes: Vec<AstNode>,
    /// All tokens
    tokens: Vec<Token>,
}

impl Arena {
    /// Create a new empty arena
    pub fn new() -> Self {
        Arena {
            nodes: Vec::new(),
            tokens: Vec::new(),
        }
    }

    /// Allocate a new node in the arena
    pub fn alloc_node(&mut self, node: AstNode) -> NodeId {
        let id = NodeId(self.nodes.len());
        self.nodes.push(node);
        id
    }

    /// Get a reference to a node
    pub fn get_node(&self, id: NodeId) -> &AstNode {
        &self.nodes[id.0]
    }

    /// Get a mutable reference to a node
    pub fn get_node_mut(&mut self, id: NodeId) -> &mut AstNode {
        &mut self.nodes[id.0]
    }

    /// Allocate a new token in the arena
    pub fn alloc_token(&mut self, token: Token) -> TokenId {
        let id = TokenId(self.tokens.len());
        self.tokens.push(token);
        id
    }

    /// Get a reference to a token
    pub fn get_token(&self, id: TokenId) -> &Token {
        &self.tokens[id.0]
    }

    /// Pretty print the AST starting from the given node
    pub fn pretty_print(&self, root: NodeId, indent: usize, input: &str) -> String {
        let mut result = format!("AST: \"{}\"\n", input);
        self.pretty_print_impl(root, indent + 1, &mut result);
        result
    }

    /// Check if a node is a pass-through (single child, no semantic value)
    fn is_passthrough(&self, node_id: NodeId) -> bool {
        match self.get_node(node_id) {
            AstNode::JmsSelector(n) => n.children.len() == 1,
            AstNode::OrExpression(n) => n.children.len() == 1,
            AstNode::AndExpression(n) => n.children.len() == 1,
            AstNode::EqualityExpression(n) => n.children.len() == 1 && n.operators.is_empty(),
            AstNode::ComparisonExpression(n) => n.children.len() == 1 && n.operators.is_empty(),
            AstNode::AddExpression(n) => n.children.len() == 1 && n.operators.is_empty(),
            AstNode::MultExpr(n) => n.children.len() == 1 && n.operators.is_empty(),
            AstNode::UnaryExpr(n) => n.children.len() == 1 && n.operator.is_none(),
            AstNode::PrimaryExpr(_) => false,
            AstNode::Literal(_) => false,
            AstNode::StringLiteral(n) => n.children.len() == 1,
            AstNode::Variable(n) => n.children.len() == 1,
        }
    }

    /// Get the first child of a node (if any)
    fn first_child(&self, node_id: NodeId) -> Option<NodeId> {
        match self.get_node(node_id) {
            AstNode::JmsSelector(n) => n.children.first().copied(),
            AstNode::OrExpression(n) => n.children.first().copied(),
            AstNode::AndExpression(n) => n.children.first().copied(),
            AstNode::EqualityExpression(n) => n.children.first().copied(),
            AstNode::ComparisonExpression(n) => n.children.first().copied(),
            AstNode::AddExpression(n) => n.children.first().copied(),
            AstNode::MultExpr(n) => n.children.first().copied(),
            AstNode::UnaryExpr(n) => n.children.first().copied(),
            AstNode::PrimaryExpr(n) => n.children.first().copied(),
            AstNode::Literal(n) => n.children.first().copied(),
            AstNode::StringLiteral(n) => n.children.first().copied(),
            AstNode::Variable(n) => n.children.first().copied(),
        }
    }

    fn pretty_print_impl(&self, node_id: NodeId, indent: usize, result: &mut String) {
        // Skip pass-through nodes
        if self.is_passthrough(node_id) {
            if let Some(child) = self.first_child(node_id) {
                self.pretty_print_impl(child, indent, result);
            }
            return;
        }

        let indent_str = "  ".repeat(indent);
        match self.get_node(node_id) {
            AstNode::JmsSelector(node) => {
                if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}JmsSelector(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}JmsSelector\n", indent_str));
                    for child in &node.children {
                        self.pretty_print_impl(*child, indent + 1, result);
                    }
                }
            }
            AstNode::OrExpression(node) => {
                if node.children.len() > 1 {
                    result.push_str(&format!("{}OrExpression [OR x{}]\n", indent_str, node.children.len() - 1));
                } else {
                    result.push_str(&format!("{}OrExpression\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::AndExpression(node) => {
                if node.children.len() > 1 {
                    result.push_str(&format!("{}AndExpression [AND x{}]\n", indent_str, node.children.len() - 1));
                } else {
                    result.push_str(&format!("{}AndExpression\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::EqualityExpression(node) => {
                if !node.operators.is_empty() {
                    let ops: Vec<&str> = node.operators.iter()
                        .map(|op| match op {
                            EqualityOp::Equal => "=",
                            EqualityOp::NotEqual => "<>",
                            EqualityOp::IsNull => "IS NULL",
                            EqualityOp::IsNotNull => "IS NOT NULL",
                        })
                        .collect();
                    result.push_str(&format!("{}EqualityExpression [{}]\n", indent_str, ops.join(", ")));
                } else if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}EqualityExpression(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}EqualityExpression\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::ComparisonExpression(node) => {
                if !node.operators.is_empty() {
                    let ops: Vec<&str> = node.operators.iter()
                        .map(|op| match op {
                            ComparisonOp::GreaterThan => ">",
                            ComparisonOp::GreaterThanEqual => ">=",
                            ComparisonOp::LessThan => "<",
                            ComparisonOp::LessThanEqual => "<=",
                            ComparisonOp::Like => "LIKE",
                            ComparisonOp::NotLike => "NOT LIKE",
                            ComparisonOp::LikeEscape => "LIKE ESCAPE",
                            ComparisonOp::NotLikeEscape => "NOT LIKE ESCAPE",
                            ComparisonOp::Between => "BETWEEN",
                            ComparisonOp::NotBetween => "NOT BETWEEN",
                            ComparisonOp::In => "IN",
                            ComparisonOp::NotIn => "NOT IN",
                        })
                        .collect();
                    result.push_str(&format!("{}ComparisonExpression [{}]\n", indent_str, ops.join(", ")));
                } else if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}ComparisonExpression(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}ComparisonExpression\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::AddExpression(node) => {
                if !node.operators.is_empty() {
                    let ops: Vec<&str> = node.operators.iter()
                        .map(|op| match op {
                            AddOp::Plus => "+",
                            AddOp::Minus => "-",
                        })
                        .collect();
                    result.push_str(&format!("{}AddExpression [{}]\n", indent_str, ops.join(", ")));
                } else {
                    result.push_str(&format!("{}AddExpression\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::MultExpr(node) => {
                if !node.operators.is_empty() {
                    let ops: Vec<&str> = node.operators.iter()
                        .map(|op| match op {
                            MultExprOp::Star => "*",
                            MultExprOp::Slash => "/",
                            MultExprOp::Percent => "%",
                        })
                        .collect();
                    result.push_str(&format!("{}MultExpr [{}]\n", indent_str, ops.join(", ")));
                } else {
                    result.push_str(&format!("{}MultExpr\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::UnaryExpr(node) => {
                if let Some(op) = &node.operator {
                    let op_str = match op {
                        UnaryOp::Plus => "+",
                        UnaryOp::Negate => "-",
                        UnaryOp::Not => "NOT",
                    };
                    result.push_str(&format!("{}UnaryExpr [{}]\n", indent_str, op_str));
                } else if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}UnaryExpr(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}UnaryExpr\n", indent_str));
                }
                for child in &node.children {
                    self.pretty_print_impl(*child, indent + 1, result);
                }
            }
            AstNode::PrimaryExpr(node) => {
                if node.children.is_empty() {
                    let token = self.get_token(node.begin_token);
                    result.push_str(&format!("{}PrimaryExpr(\"{}\")\n", indent_str, token.image));
                } else {
                    result.push_str(&format!("{}PrimaryExpr\n", indent_str));
                    for child in &node.children {
                        self.pretty_print_impl(*child, indent + 1, result);
                    }
                }
            }
            AstNode::Literal(node) => {
                if node.children.is_empty() {
                    let token = self.get_token(node.begin_token);
                    result.push_str(&format!("{}Literal(\"{}\")\n", indent_str, token.image));
                } else {
                    result.push_str(&format!("{}Literal\n", indent_str));
                    for child in &node.children {
                        self.pretty_print_impl(*child, indent + 1, result);
                    }
                }
            }
            AstNode::StringLiteral(node) => {
                if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}StringLiteral(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}StringLiteral\n", indent_str));
                    for child in &node.children {
                        self.pretty_print_impl(*child, indent + 1, result);
                    }
                }
            }
            AstNode::Variable(node) => {
                if node.children.is_empty() {
                    let value = &self.get_token(node.begin_token).image;
                    result.push_str(&format!("{}Variable(\"{}\")\n", indent_str, value));
                } else {
                    result.push_str(&format!("{}Variable\n", indent_str));
                    for child in &node.children {
                        self.pretty_print_impl(*child, indent + 1, result);
                    }
                }
            }
        }
    }
}

impl Default for Arena {
    fn default() -> Self {
        Self::new()
    }
}

/// Enum containing all AST node types
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AstNode {
    /// AST node: JmsSelector
    JmsSelector(JmsSelectorNode),
    /// AST node: orExpression
    OrExpression(OrExpressionNode),
    /// AST node: andExpression
    AndExpression(AndExpressionNode),
    /// AST node: equalityExpression
    EqualityExpression(EqualityExpressionNode),
    /// AST node: comparisonExpression
    ComparisonExpression(ComparisonExpressionNode),
    /// AST node: addExpression
    AddExpression(AddExpressionNode),
    /// AST node: multExpr
    MultExpr(MultExprNode),
    /// AST node: unaryExpr
    UnaryExpr(UnaryExprNode),
    /// AST node: primaryExpr
    PrimaryExpr(PrimaryExprNode),
    /// AST node: literal
    Literal(LiteralNode),
    /// AST node: stringLiteral
    StringLiteral(StringLiteralNode),
    /// AST node: variable
    Variable(VariableNode),
}

/// Operator for addExpression
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AddOp {
    /// +
    Plus,
    /// -
    Minus,
}

/// Operator for multExpr
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MultExprOp {
    /// *
    Star,
    /// /
    Slash,
    /// %
    Percent,
}

/// Operator for equalityExpression
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EqualityOp {
    /// =
    Equal,
    /// <>
    NotEqual,
    /// IS NULL
    IsNull,
    /// IS NOT NULL
    IsNotNull,
}

/// Operator for comparisonExpression
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ComparisonOp {
    /// >
    GreaterThan,
    /// >=
    GreaterThanEqual,
    /// <
    LessThan,
    /// <=
    LessThanEqual,
    /// LIKE
    Like,
    /// NOT LIKE
    NotLike,
    /// LIKE with ESCAPE clause
    LikeEscape,
    /// NOT LIKE with ESCAPE clause
    NotLikeEscape,
    /// BETWEEN
    Between,
    /// NOT BETWEEN
    NotBetween,
    /// IN
    In,
    /// NOT IN
    NotIn,
}

/// Operator for unaryExpr
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UnaryOp {
    /// +
    Plus,
    /// -
    Negate,
    /// NOT
    Not,
}

/// AST node for JmsSelector production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JmsSelectorNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl JmsSelectorNode {
    /// Create a new JmsSelector node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        JmsSelectorNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for orExpression production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OrExpressionNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl OrExpressionNode {
    /// Create a new orExpression node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        OrExpressionNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for andExpression production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AndExpressionNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl AndExpressionNode {
    /// Create a new andExpression node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        AndExpressionNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for equalityExpression production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EqualityExpressionNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// Operators applied in this expression
    pub operators: Vec<EqualityOp>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl EqualityExpressionNode {
    /// Create a new equalityExpression node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        EqualityExpressionNode {
            parent: None,
            children: Vec::new(),
            operators: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for comparisonExpression production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ComparisonExpressionNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// Operators applied in this expression
    pub operators: Vec<ComparisonOp>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl ComparisonExpressionNode {
    /// Create a new comparisonExpression node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        ComparisonExpressionNode {
            parent: None,
            children: Vec::new(),
            operators: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for addExpression production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AddExpressionNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// Operators between children: operators[i] is between children[i] and children[i+1]
    pub operators: Vec<AddOp>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl AddExpressionNode {
    /// Create a new addExpression node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        AddExpressionNode {
            parent: None,
            children: Vec::new(),
            operators: Vec::new(),
            begin_token,
            end_token,
        }
    }

    /// Get the left operand (first child)
    pub fn left<'a>(&self, arena: &'a Arena) -> &'a AstNode {
        arena.get_node(self.children[0])
    }

    /// Get the right operand (second child for binary case)
    pub fn right<'a>(&self, arena: &'a Arena) -> Option<&'a AstNode> {
        self.children.get(1).map(|id| arena.get_node(*id))
    }

    /// Get operator at index (between children[i] and children[i+1])
    pub fn op(&self, index: usize) -> Option<AddOp> {
        self.operators.get(index).copied()
    }

    /// Get first operator (for binary expressions)
    pub fn first_op(&self) -> Option<AddOp> {
        self.operators.first().copied()
    }

    /// Iterator over (operator, operand) pairs after the first operand
    pub fn op_operand_pairs(&self) -> impl Iterator<Item = (AddOp, NodeId)> + '_ {
        self.operators.iter().copied().zip(self.children.iter().skip(1).copied())
    }
}

/// AST node for multExpr production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MultExprNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// Operators between children: operators[i] is between children[i] and children[i+1]
    pub operators: Vec<MultExprOp>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl MultExprNode {
    /// Create a new multExpr node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        MultExprNode {
            parent: None,
            children: Vec::new(),
            operators: Vec::new(),
            begin_token,
            end_token,
        }
    }

    /// Get the left operand (first child)
    pub fn left<'a>(&self, arena: &'a Arena) -> &'a AstNode {
        arena.get_node(self.children[0])
    }

    /// Get the right operand (second child for binary case)
    pub fn right<'a>(&self, arena: &'a Arena) -> Option<&'a AstNode> {
        self.children.get(1).map(|id| arena.get_node(*id))
    }

    /// Get operator at index (between children[i] and children[i+1])
    pub fn op(&self, index: usize) -> Option<MultExprOp> {
        self.operators.get(index).copied()
    }

    /// Get first operator (for binary expressions)
    pub fn first_op(&self) -> Option<MultExprOp> {
        self.operators.first().copied()
    }

    /// Iterator over (operator, operand) pairs after the first operand
    pub fn op_operand_pairs(&self) -> impl Iterator<Item = (MultExprOp, NodeId)> + '_ {
        self.operators.iter().copied().zip(self.children.iter().skip(1).copied())
    }
}

/// AST node for unaryExpr production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UnaryExprNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// Prefix unary operator (if any)
    pub operator: Option<UnaryOp>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl UnaryExprNode {
    /// Create a new unaryExpr node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        UnaryExprNode {
            parent: None,
            children: Vec::new(),
            operator: None,
            begin_token,
            end_token,
        }
    }

}

/// AST node for primaryExpr production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PrimaryExprNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl PrimaryExprNode {
    /// Create a new primaryExpr node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        PrimaryExprNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

    /// Get the token image (the actual value as string)
    pub fn value<'a>(&self, arena: &'a Arena) -> &'a str {
        &arena.get_token(self.begin_token).image
    }
}

/// AST node for literal production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LiteralNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl LiteralNode {
    /// Create a new literal node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        LiteralNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

    /// Get the token image (the actual value as string)
    pub fn value<'a>(&self, arena: &'a Arena) -> &'a str {
        &arena.get_token(self.begin_token).image
    }
}

/// AST node for stringLiteral production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StringLiteralNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl StringLiteralNode {
    /// Create a new stringLiteral node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        StringLiteralNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

}

/// AST node for variable production
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VariableNode {
    /// Parent node (if any)
    pub parent: Option<NodeId>,
    /// Child nodes
    pub children: Vec<NodeId>,
    /// First token of this node
    pub begin_token: TokenId,
    /// Last token of this node
    pub end_token: TokenId,
}

impl VariableNode {
    /// Create a new variable node
    pub fn new(begin_token: TokenId, end_token: TokenId) -> Self {
        VariableNode {
            parent: None,
            children: Vec::new(),
            begin_token,
            end_token,
        }
    }

}