cp-ast-core 0.1.3

Core AST types for competitive programming problem specification DSL
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
use super::engine::AstEngine;
use super::error::OperationError;
use super::fill_hole::var_type_to_expected_from_fill;
use super::result::ApplyResult;
use super::types::FillContent;
use crate::constraint::{Constraint, ExpectedType};
use crate::structure::{Literal, NodeId, NodeKind, Reference};

fn constraint_is_compatible_with_replacement(
    constraint: &Constraint,
    expected_type: Option<&ExpectedType>,
) -> bool {
    match constraint {
        Constraint::TypeDecl { .. } => false,
        Constraint::Range { .. } => matches!(expected_type, Some(ExpectedType::Int) | None),
        Constraint::CharSet { .. } => {
            matches!(expected_type, Some(ExpectedType::Char | ExpectedType::Str))
        }
        Constraint::StringLength { .. } => matches!(expected_type, Some(ExpectedType::Str)),
        _ => true,
    }
}

impl AstEngine {
    /// Replace an existing node with new content.
    ///
    /// # Errors
    /// Returns `OperationError` if:
    /// - Target node doesn't exist
    /// - Target node is a Hole (use `FillHole` instead)
    /// - Constraints incompatible with the replacement are removed
    pub(crate) fn replace_node(
        &mut self,
        target: NodeId,
        replacement: &FillContent,
    ) -> Result<ApplyResult, OperationError> {
        // 1. Verify target exists
        let node = self
            .structure
            .get(target)
            .ok_or(OperationError::NodeNotFound { node: target })?;

        // 2. Verify it's NOT a Hole (replacing holes should use FillHole)
        if matches!(node.kind(), NodeKind::Hole { .. }) {
            return Err(OperationError::InvalidOperation {
                action: "ReplaceNode".to_owned(),
                reason: "Use FillHole for Hole nodes".to_owned(),
            });
        }

        // 3. Remove constraints that cannot survive the new node shape/type.
        let existing_constraints = self.constraints.for_node(target);
        let expected_type = var_type_to_expected_from_fill(replacement);
        let mut affected_constraints = Vec::new();
        for cid in &existing_constraints {
            let Some(constraint) = self.constraints.get(*cid) else {
                continue;
            };
            if !constraint_is_compatible_with_replacement(constraint, expected_type.as_ref()) {
                self.constraints.remove(*cid);
                affected_constraints.push(*cid);
            }
        }

        // 4. Expand replacement FillContent using existing helper
        let mut created_nodes = Vec::new();
        let new_kind = self.expand_fill_content(replacement, &mut created_nodes);

        // 5. Replace the node's kind
        if let Some(node) = self.structure.get_mut(target) {
            node.set_kind(new_kind);
        }

        // 5.5. Resolve Unresolved variable references in structure expressions
        self.resolve_structure_references(target);
        for &child_id in &created_nodes {
            self.resolve_structure_references(child_id);
        }

        let mut created_constraints = Vec::new();
        if let Some(expected) = expected_type {
            let cid = self.constraints.add(
                Some(target),
                Constraint::TypeDecl {
                    target: Reference::VariableRef(target),
                    expected,
                },
            );
            created_constraints.push(cid);
        }

        Ok(ApplyResult {
            created_nodes,
            removed_nodes: vec![],
            created_constraints,
            affected_constraints,
        })
    }

    /// Add an element to a parent node's slot.
    ///
    /// # Errors
    /// Returns `OperationError` if:
    /// - Parent node doesn't exist
    /// - Parent node doesn't have the specified slot
    #[allow(clippy::too_many_lines)]
    pub(crate) fn add_slot_element(
        &mut self,
        parent: NodeId,
        slot_name: &str,
        element: &FillContent,
    ) -> Result<ApplyResult, OperationError> {
        // 1. Verify parent exists
        if !self.structure.contains(parent) {
            return Err(OperationError::NodeNotFound { node: parent });
        }

        // 2. Expand element FillContent first (needs &mut self)
        let mut created_nodes = Vec::new();
        let new_kind = self.expand_fill_content(element, &mut created_nodes);

        // 3. Create the new node
        let new_node_id = self.structure.add_node(new_kind);
        created_nodes.push(new_node_id);

        // 3.5. Resolve Unresolved variable references in structure expressions
        self.resolve_structure_references(new_node_id);
        for &child_id in &created_nodes {
            self.resolve_structure_references(child_id);
        }

        // 4. Auto-add TypeDecl constraint if applicable
        let mut created_constraints = Vec::new();
        if let Some(expected_type) = var_type_to_expected_from_fill(element) {
            let cid = self.constraints.add(
                Some(new_node_id),
                Constraint::TypeDecl {
                    target: Reference::VariableRef(new_node_id),
                    expected: expected_type,
                },
            );
            created_constraints.push(cid);
        }

        // 5. Get parent mutably and add to the correct slot
        let parent_node = self
            .structure
            .get_mut(parent)
            .ok_or(OperationError::NodeNotFound { node: parent })?;

        match parent_node.kind() {
            NodeKind::Sequence { children: _ } => {
                if slot_name == "children" {
                    if let NodeKind::Sequence { children } = parent_node.kind() {
                        // Clone children to avoid borrow checker issues
                        let mut new_children = children.clone();
                        new_children.push(new_node_id);
                        parent_node.set_kind(NodeKind::Sequence {
                            children: new_children,
                        });
                    }
                } else {
                    return Err(OperationError::InvalidOperation {
                        action: "AddSlotElement".to_owned(),
                        reason: format!("Sequence node does not have slot '{slot_name}'"),
                    });
                }
            }
            NodeKind::Section { header, body } => {
                if slot_name == "body" {
                    let mut new_body = body.clone();
                    new_body.push(new_node_id);
                    parent_node.set_kind(NodeKind::Section {
                        header: *header,
                        body: new_body,
                    });
                } else {
                    return Err(OperationError::InvalidOperation {
                        action: "AddSlotElement".to_owned(),
                        reason: format!("Section node does not have slot '{slot_name}'"),
                    });
                }
            }
            NodeKind::Repeat {
                count,
                index_var,
                body,
            } => {
                if slot_name == "body" {
                    let mut new_body = body.clone();
                    new_body.push(new_node_id);
                    parent_node.set_kind(NodeKind::Repeat {
                        count: count.clone(),
                        index_var: index_var.clone(),
                        body: new_body,
                    });
                } else {
                    return Err(OperationError::InvalidOperation {
                        action: "AddSlotElement".to_owned(),
                        reason: format!("Repeat node does not have slot '{slot_name}'"),
                    });
                }
            }
            NodeKind::Tuple { elements } => {
                if slot_name == "elements" {
                    let mut new_elements = elements.clone();
                    new_elements.push(new_node_id);
                    parent_node.set_kind(NodeKind::Tuple {
                        elements: new_elements,
                    });
                } else {
                    return Err(OperationError::InvalidOperation {
                        action: "AddSlotElement".to_owned(),
                        reason: format!("Tuple node does not have slot '{slot_name}'"),
                    });
                }
            }
            _ => {
                return Err(OperationError::InvalidOperation {
                    action: "AddSlotElement".to_owned(),
                    reason: format!("Node does not have slot '{slot_name}'"),
                });
            }
        }

        Ok(ApplyResult {
            created_nodes,
            removed_nodes: vec![],
            created_constraints,
            affected_constraints: vec![],
        })
    }

    /// Remove an element from a parent node's slot.
    ///
    /// # Errors
    /// Returns `OperationError` if:
    /// - Parent or child node doesn't exist
    /// - Parent node doesn't have the specified slot
    /// - Child is not in the specified slot
    #[allow(clippy::too_many_lines)]
    pub(crate) fn remove_slot_element(
        &mut self,
        parent: NodeId,
        slot_name: &str,
        child: NodeId,
    ) -> Result<ApplyResult, OperationError> {
        // 1. Verify parent and child both exist
        if !self.structure.contains(parent) {
            return Err(OperationError::NodeNotFound { node: parent });
        }
        if !self.structure.contains(child) {
            return Err(OperationError::NodeNotFound { node: child });
        }

        // 2. Get parent's NodeKind and find the slot
        let parent_node = self
            .structure
            .get(parent)
            .ok_or(OperationError::NodeNotFound { node: parent })?;

        let (slot_exists, child_in_slot) = match parent_node.kind() {
            NodeKind::Sequence { children } if slot_name == "children" => {
                (true, children.contains(&child))
            }
            NodeKind::Section { body, .. } | NodeKind::Repeat { body, .. }
                if slot_name == "body" =>
            {
                (true, body.contains(&child))
            }
            NodeKind::Tuple { elements } if slot_name == "elements" => {
                (true, elements.contains(&child))
            }
            _ => (false, false),
        };

        if !slot_exists {
            return Err(OperationError::InvalidOperation {
                action: "RemoveSlotElement".to_owned(),
                reason: format!("Node does not have slot '{slot_name}'"),
            });
        }

        if !child_in_slot {
            return Err(OperationError::InvalidOperation {
                action: "RemoveSlotElement".to_owned(),
                reason: format!("Child {child:?} not found in slot '{slot_name}'"),
            });
        }

        // 3. Remove child from the slot's Vec<NodeId>
        let parent_node = self
            .structure
            .get_mut(parent)
            .ok_or(OperationError::NodeNotFound { node: parent })?;

        match parent_node.kind() {
            NodeKind::Sequence { children } => {
                let mut new_children = children.clone();
                new_children.retain(|&id| id != child);
                parent_node.set_kind(NodeKind::Sequence {
                    children: new_children,
                });
            }
            NodeKind::Section { header, body } => {
                let mut new_body = body.clone();
                new_body.retain(|&id| id != child);
                parent_node.set_kind(NodeKind::Section {
                    header: *header,
                    body: new_body,
                });
            }
            NodeKind::Repeat {
                count,
                index_var,
                body,
            } => {
                let mut new_body = body.clone();
                new_body.retain(|&id| id != child);
                parent_node.set_kind(NodeKind::Repeat {
                    count: count.clone(),
                    index_var: index_var.clone(),
                    body: new_body,
                });
            }
            NodeKind::Tuple { elements } => {
                let mut new_elements = elements.clone();
                new_elements.retain(|&id| id != child);
                parent_node.set_kind(NodeKind::Tuple {
                    elements: new_elements,
                });
            }
            _ => unreachable!("We checked this above"),
        }

        // 4. Remove the child node from the arena
        self.structure.remove(child);

        // 5. Remove any constraints referencing the child node
        let child_constraints = self.constraints.for_node(child);
        for cid in &child_constraints {
            self.constraints.remove(*cid);
        }

        Ok(ApplyResult {
            created_nodes: vec![],
            removed_nodes: vec![child],
            created_constraints: vec![],
            affected_constraints: child_constraints,
        })
    }

    // ── AddSibling / AddChoiceVariant ──────────────────────────────────

    /// Add a sibling element next to a target node.
    ///
    /// If the target is inside a Tuple, the new element is appended to that Tuple.
    /// If the target is a direct child of a Sequence, Section body, or Repeat body,
    /// a new Tuple is created containing the target and the new element, and that
    /// Tuple replaces the target in the parent's slot.
    ///
    /// # Errors
    /// Returns `OperationError` if the target node doesn't exist or has no parent slot.
    pub(crate) fn add_sibling(
        &mut self,
        target: NodeId,
        element: &FillContent,
    ) -> Result<ApplyResult, OperationError> {
        if !self.structure.contains(target) {
            return Err(OperationError::NodeNotFound { node: target });
        }

        let (parent_id, slot) =
            self.find_parent(target)
                .ok_or(OperationError::InvalidOperation {
                    action: "AddSibling".to_owned(),
                    reason: "Target node has no parent slot".to_owned(),
                })?;

        // Expand the fill content into a new node
        let mut created_nodes = Vec::new();
        let new_kind = self.expand_fill_content(element, &mut created_nodes);
        let new_node_id = self.structure.add_node(new_kind);
        created_nodes.push(new_node_id);

        // Resolve Unresolved variable references in structure expressions
        self.resolve_structure_references(new_node_id);
        for &child_id in &created_nodes {
            self.resolve_structure_references(child_id);
        }

        // Auto-add TypeDecl constraint if applicable
        let mut created_constraints = Vec::new();
        if let Some(expected_type) = var_type_to_expected_from_fill(element) {
            let cid = self.constraints.add(
                Some(new_node_id),
                Constraint::TypeDecl {
                    target: Reference::VariableRef(new_node_id),
                    expected: expected_type,
                },
            );
            created_constraints.push(cid);
        }

        if let ParentSlot::Tuple = slot {
            // Target is already inside a Tuple — append new element
            let parent_node = self
                .structure
                .get(parent_id)
                .ok_or(OperationError::NodeNotFound { node: parent_id })?;
            if let NodeKind::Tuple { elements } = parent_node.kind() {
                let mut new_elements = elements.clone();
                new_elements.push(new_node_id);
                let parent_node = self
                    .structure
                    .get_mut(parent_id)
                    .ok_or(OperationError::NodeNotFound { node: parent_id })?;
                parent_node.set_kind(NodeKind::Tuple {
                    elements: new_elements,
                });
            } else {
                return Err(OperationError::InvalidOperation {
                    action: "AddSibling".to_owned(),
                    reason: "Parent slot/kind invariant violation".to_owned(),
                });
            }
        } else {
            // Target is a direct child of Sequence/Section/Repeat —
            // create a Tuple[target, new_element] and replace target in parent
            let tuple_id = self.structure.add_node(NodeKind::Tuple {
                elements: vec![target, new_node_id],
            });
            created_nodes.push(tuple_id);
            self.replace_child_in_slot(parent_id, &slot, target, tuple_id)?;
        }

        Ok(ApplyResult {
            created_nodes,
            removed_nodes: vec![],
            created_constraints,
            affected_constraints: vec![],
        })
    }

    /// Add a variant to a Choice node.
    ///
    /// # Errors
    /// Returns `OperationError` if the target node doesn't exist or is not a Choice.
    pub(crate) fn add_choice_variant(
        &mut self,
        choice: NodeId,
        tag_value: &Literal,
        first_element: &FillContent,
    ) -> Result<ApplyResult, OperationError> {
        if !self.structure.contains(choice) {
            return Err(OperationError::NodeNotFound { node: choice });
        }

        // Verify it's a Choice
        {
            let node = self
                .structure
                .get(choice)
                .ok_or(OperationError::NodeNotFound { node: choice })?;
            if !matches!(node.kind(), NodeKind::Choice { .. }) {
                return Err(OperationError::InvalidOperation {
                    action: "AddChoiceVariant".to_owned(),
                    reason: format!("Node {choice:?} is not a Choice"),
                });
            }
        }

        // Expand the fill content
        let mut created_nodes = Vec::new();
        let new_kind = self.expand_fill_content(first_element, &mut created_nodes);
        let new_node_id = self.structure.add_node(new_kind);
        created_nodes.push(new_node_id);

        // Resolve Unresolved variable references in structure expressions
        self.resolve_structure_references(new_node_id);
        for &child_id in &created_nodes {
            self.resolve_structure_references(child_id);
        }

        // Auto-add TypeDecl constraint if applicable
        let mut created_constraints = Vec::new();
        if let Some(expected_type) = var_type_to_expected_from_fill(first_element) {
            let cid = self.constraints.add(
                Some(new_node_id),
                Constraint::TypeDecl {
                    target: Reference::VariableRef(new_node_id),
                    expected: expected_type,
                },
            );
            created_constraints.push(cid);
        }

        // Append the variant
        // expand_fill_content only creates new nodes — it never mutates existing ones,
        // so the choice node kind is unchanged here.
        let node = self
            .structure
            .get(choice)
            .ok_or(OperationError::NodeNotFound { node: choice })?;
        if let NodeKind::Choice { tag, variants } = node.kind() {
            let tag_cloned = tag.clone();
            let mut new_variants = variants.clone();
            new_variants.push((tag_value.clone(), vec![new_node_id]));
            let node = self
                .structure
                .get_mut(choice)
                .ok_or(OperationError::NodeNotFound { node: choice })?;
            node.set_kind(NodeKind::Choice {
                tag: tag_cloned,
                variants: new_variants,
            });
        } else {
            return Err(OperationError::InvalidOperation {
                action: "AddChoiceVariant".to_owned(),
                reason: "Choice node kind invariant violation".to_owned(),
            });
        }

        Ok(ApplyResult {
            created_nodes,
            removed_nodes: vec![],
            created_constraints,
            affected_constraints: vec![],
        })
    }

    /// Find which node contains `target` in its children/body/elements slot.
    ///
    /// Note: does not search inside Choice variant bodies.
    fn find_parent(&self, target: NodeId) -> Option<(NodeId, ParentSlot)> {
        for node in self.structure.iter() {
            let parent_id = node.id();
            match node.kind() {
                NodeKind::Sequence { children } if children.contains(&target) => {
                    return Some((parent_id, ParentSlot::Sequence));
                }
                NodeKind::Section { body, .. } if body.contains(&target) => {
                    return Some((parent_id, ParentSlot::SectionBody));
                }
                NodeKind::Repeat { body, .. } if body.contains(&target) => {
                    return Some((parent_id, ParentSlot::RepeatBody));
                }
                NodeKind::Tuple { elements } if elements.contains(&target) => {
                    return Some((parent_id, ParentSlot::Tuple));
                }
                _ => {}
            }
        }
        None
    }

    /// Replace `old_child` with `new_child` in the specified slot of a parent node.
    fn replace_child_in_slot(
        &mut self,
        parent: NodeId,
        slot: &ParentSlot,
        old_child: NodeId,
        new_child: NodeId,
    ) -> Result<(), OperationError> {
        let parent_node = self
            .structure
            .get(parent)
            .ok_or(OperationError::NodeNotFound { node: parent })?;

        let new_kind = match (slot, parent_node.kind()) {
            (ParentSlot::Sequence, NodeKind::Sequence { children }) => {
                let new_children = children
                    .iter()
                    .map(|&id| if id == old_child { new_child } else { id })
                    .collect();
                NodeKind::Sequence {
                    children: new_children,
                }
            }
            (ParentSlot::SectionBody, NodeKind::Section { header, body }) => {
                let new_body = body
                    .iter()
                    .map(|&id| if id == old_child { new_child } else { id })
                    .collect();
                NodeKind::Section {
                    header: *header,
                    body: new_body,
                }
            }
            (
                ParentSlot::RepeatBody,
                NodeKind::Repeat {
                    count,
                    index_var,
                    body,
                },
            ) => {
                let new_body = body
                    .iter()
                    .map(|&id| if id == old_child { new_child } else { id })
                    .collect();
                NodeKind::Repeat {
                    count: count.clone(),
                    index_var: index_var.clone(),
                    body: new_body,
                }
            }
            _ => {
                return Err(OperationError::InvalidOperation {
                    action: "AddSibling".to_owned(),
                    reason: "Parent slot mismatch".to_owned(),
                });
            }
        };

        let parent_node = self
            .structure
            .get_mut(parent)
            .ok_or(OperationError::NodeNotFound { node: parent })?;
        parent_node.set_kind(new_kind);
        Ok(())
    }
}

/// Describes which slot of a parent node contains the target child.
enum ParentSlot {
    Sequence,
    SectionBody,
    RepeatBody,
    Tuple,
}