zerodds-idl-cst 1.0.0-rc.7

Lossless concrete syntax tree for OMG IDL 4.2: tokens with attached trivia, a green/red tree that reproduces its input byte for byte.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The green/red tree. Spec §2.
//!
//! Green nodes are immutable, carry only kind and width, and are shared
//! through `Arc` — two identical subtrees can be one allocation. Red
//! nodes are built on demand and add what green cannot have without
//! losing sharing: an absolute offset and a parent.
//!
//! Trivia lives in the tree as ordinary tokens. That is what makes
//! `text()` reproduce the input rather than approximate it, and it is
//! why a green token carries its text instead of a span into some
//! particular file — a span would tie the tree to one source and
//! make sharing impossible.

use crate::span::Span;
use crate::token::TokenKind;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;

/// What a syntax node is.
///
/// One flat enum over every form the grammar produces. Adding a form is
/// a variant, and every exhaustive match on it has to account for the
/// new one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum CstKind {
    /// The whole file.
    File,

    // -- declarations ----------------------------------------------------
    /// `module X { ... };`
    Module,
    /// `const T N = expr;`
    ConstDecl,
    /// `typedef T N;`
    TypedefDecl,
    /// `struct S { ... };`
    StructDecl,
    /// `union U switch (T) { ... };`
    UnionDecl,
    /// `enum E { ... };`
    EnumDecl,
    /// `bitmask B { ... };`
    BitmaskDecl,
    /// `bitset B { ... };`
    BitsetDecl,
    /// `native N;`
    NativeDecl,
    /// `exception E { ... };`
    ExceptionDecl,
    /// `interface I { ... };`
    InterfaceDecl,
    /// `valuetype V { ... };`
    ValuetypeDecl,
    /// `valuetype VB long;` — a boxed value. IDL 4.2 §7.4.7 rule (126).
    ///
    /// Its own kind and not a `ValuetypeDecl` with a type beside it: a
    /// value box has no state members, no initialisers and no bases, and
    /// a consumer matching on the kind should not have to look for the
    /// absence of a body to tell the two apart.
    ValueBoxDecl,
    /// A forward declaration: `struct S;`
    ForwardDecl,
    /// `@annotation A { ... };`
    AnnotationDecl,
    /// `typeid X "id";` or `typeprefix X "prefix";`
    RepositoryDirective,
    /// `import X;`
    ImportDecl,
    /// `keylist T (a, b);` — an RTI Connext directive.
    KeylistDecl,
    /// The `<...>` formal parameter list of a template module.
    /// `module M<long> N;` — a template module instantiation.
    /// IDL 4.2 §7.4.12 rule (190).
    TemplateModuleInst,
    /// The actual parameters of an instantiation, rule (191). Each child
    /// is a type specification or a constant expression, rule (192).
    ActualParameters,
    /// `alias M<T> R;` inside a template module body, rule (193).
    TemplateModuleRef,
    /// The formal parameters of a template module, rule (186).
    TemplateParameters,
    /// One formal parameter of a template module.
    TemplateParameter,
    /// One port of a component: `provides`, `uses`, `emits`,
    /// `publishes`, `consumes`, `port` or `mirrorport`.
    PortDecl,
    /// A home's `manages X primarykey K` clause.
    ManagesClause,

    // -- members and parts -----------------------------------------------
    /// One member of a struct, exception or valuetype.
    Member,
    /// One `case` of a union, with its labels.
    UnionCase,
    /// A `case <expr>:` or `default:` label.
    CaseLabel,
    /// One enumerator.
    Enumerator,
    /// One bitmask bit value.
    BitValue,
    /// One bitset field.
    BitField,
    /// An operation.
    Operation,
    /// One parameter of an operation.
    Parameter,
    /// An attribute.
    AttributeDecl,
    /// A `raises (...)` clause.
    RaisesClause,
    /// A `context (...)` clause.
    ContextClause,
    /// An inheritance clause.
    InheritanceClause,

    // -- types -----------------------------------------------------------
    /// A named type reference.
    NamedType,
    /// A primitive keyword used as a type.
    PrimitiveType,
    /// `sequence<T>` or `sequence<T, N>`.
    SequenceType,
    /// `string` or `string<N>`.
    StringType,
    /// `wstring` or `wstring<N>`.
    WStringType,
    /// `map<K, V>` or `map<K, V, N>`.
    MapType,
    /// `fixed<d, s>`.
    FixedType,
    /// The `<...>` argument list of a parameterised type.
    TypeArguments,
    /// A declarator, simple or array.
    Declarator,
    /// One `[expr]` of an array declarator.
    ArrayDimension,

    // -- names and expressions -------------------------------------------
    /// A scoped name, possibly rooted at `::`.
    ScopedName,
    /// A literal used as an expression.
    LiteralExpr,
    /// A scoped name used as an expression.
    NameExpr,
    /// A unary application.
    UnaryExpr,
    /// A binary application.
    BinaryExpr,
    /// A parenthesised expression.
    ParenExpr,

    // -- annotations -----------------------------------------------------
    /// `@ann`, `@ann(v)` or `@ann(k = v, ...)`.
    Annotation,
    /// The parenthesised part of an annotation.
    AnnotationArguments,
    /// One `k = v` inside an annotation.
    AnnotationArgument,

    // -- recovery --------------------------------------------------------
    /// A range the parser could not make sense of.
    ///
    /// An ordinary node, not a separate channel: a file that fails to
    /// parse still round-trips because its bad ranges are in the tree
    /// rather than dropped. Spec §2.2, §3.2.
    Error,
}

impl CstKind {
    /// Whether this node is one the parser produced while recovering.
    #[must_use]
    pub const fn is_error(self) -> bool {
        matches!(self, Self::Error)
    }
}

/// A token in the green tree.
///
/// Carries its own text rather than a span, so an identical token in two
/// files is one value and identical subtrees can share an allocation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GreenToken {
    kind: TokenKind,
    text: Arc<str>,
}

impl GreenToken {
    /// A token of `kind` spelling `text`.
    #[must_use]
    pub fn new(kind: TokenKind, text: &str) -> Self {
        Self {
            kind,
            text: Arc::from(text),
        }
    }

    /// What it is.
    #[must_use]
    pub const fn kind(&self) -> TokenKind {
        self.kind
    }

    /// How it is spelled.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// How many bytes it occupies.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn width(&self) -> u32 {
        self.text.len() as u32
    }
}

/// A child of a green node.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GreenChild {
    /// A nested node.
    Node(Arc<GreenNode>),
    /// A token, which may be trivia.
    Token(GreenToken),
}

impl GreenChild {
    /// How many bytes it occupies.
    #[must_use]
    pub fn width(&self) -> u32 {
        match self {
            Self::Node(node) => node.width(),
            Self::Token(token) => token.width(),
        }
    }
}

/// An immutable node.
///
/// Knows its kind, its children and its total width — never its
/// position, because a node that knew where it was could not be shared
/// between two places.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GreenNode {
    kind: CstKind,
    width: u32,
    children: Vec<GreenChild>,
}

impl GreenNode {
    /// A node of `kind` over `children`, with the width they sum to.
    #[must_use]
    pub fn new(kind: CstKind, children: Vec<GreenChild>) -> Self {
        let width = children
            .iter()
            .fold(0u32, |sum, child| sum.saturating_add(child.width()));
        Self {
            kind,
            width,
            children,
        }
    }

    /// What it is.
    #[must_use]
    pub const fn kind(&self) -> CstKind {
        self.kind
    }

    /// Total bytes, trivia included.
    #[must_use]
    pub const fn width(&self) -> u32 {
        self.width
    }

    /// Its children in order.
    #[must_use]
    pub fn children(&self) -> &[GreenChild] {
        &self.children
    }

    /// Reconstruct the source this node covers.
    ///
    /// The property the whole tree exists for: for the root, this equals
    /// the input byte for byte. Spec §2.2.
    ///
    /// Iterative rather than recursive. A deeply nested type — the
    /// grammar puts no bound on how far `sequence<sequence<...>>` can
    /// go — must not make reconstruction depend on stack depth, and this
    /// crate forbids panics, which a stack overflow would not respect.
    #[must_use]
    pub fn text(&self) -> String {
        let mut out = String::with_capacity(self.width as usize);
        let mut stack: Vec<&GreenChild> = self.children.iter().rev().collect();
        while let Some(child) = stack.pop() {
            match child {
                GreenChild::Token(token) => out.push_str(token.text()),
                GreenChild::Node(node) => stack.extend(node.children.iter().rev()),
            }
        }
        out
    }
}

/// Dropping a deeply nested tree must not depend on stack depth either.
///
/// `text()` is iterative, but the derived drop glue is not: releasing a
/// chain of fifty thousand nested `Arc<GreenNode>` recurses once per
/// level and aborts the process. Found by the depth test, which built
/// the tree successfully and then died tearing it down.
///
/// This unnests the children into a work list and releases them there,
/// so depth costs heap rather than stack. `Arc::into_inner` yields the
/// node only when this was the last reference, which is what keeps
/// shared subtrees alive.
impl Drop for GreenNode {
    fn drop(&mut self) {
        let mut stack: Vec<GreenChild> = core::mem::take(&mut self.children);
        while let Some(child) = stack.pop() {
            if let GreenChild::Node(node) = child {
                if let Some(mut inner) = Arc::into_inner(node) {
                    stack.append(&mut inner.children);
                }
            }
        }
    }
}

/// A node with a position.
///
/// Built on demand from a green node plus the offset it sits at and the
/// parent it hangs from. Cheap to make and cheap to drop; nothing caches
/// them.
#[derive(Debug, Clone)]
pub struct SyntaxNode {
    green: Arc<GreenNode>,
    offset: u32,
    parent: Option<Arc<SyntaxNode>>,
}

impl SyntaxNode {
    /// The root of a tree.
    #[must_use]
    pub const fn root(green: Arc<GreenNode>) -> Self {
        Self {
            green,
            offset: 0,
            parent: None,
        }
    }

    /// What it is.
    #[must_use]
    pub fn kind(&self) -> CstKind {
        self.green.kind()
    }

    /// Where it sits in the file, trivia included.
    #[must_use]
    pub fn span(&self) -> Span {
        Span::new(self.offset, self.offset.saturating_add(self.green.width()))
    }

    /// The green node behind it.
    #[must_use]
    pub fn green(&self) -> &Arc<GreenNode> {
        &self.green
    }

    /// Its parent, if it is not the root.
    #[must_use]
    pub fn parent(&self) -> Option<&Arc<Self>> {
        self.parent.as_ref()
    }

    /// The chain from this node up to the root, this node first.
    #[must_use]
    pub fn ancestors(&self) -> Vec<Arc<Self>> {
        let mut out = Vec::new();
        let mut current = self.parent.clone();
        while let Some(node) = current {
            current = node.parent.clone();
            out.push(node);
        }
        out
    }

    /// Its child nodes, with their positions filled in.
    #[must_use]
    pub fn children(self: &Arc<Self>) -> Vec<Arc<Self>> {
        let mut out = Vec::new();
        let mut at = self.offset;
        for child in self.green.children() {
            match child {
                GreenChild::Node(green) => {
                    out.push(Arc::new(Self {
                        green: green.clone(),
                        offset: at,
                        parent: Some(self.clone()),
                    }));
                    at = at.saturating_add(green.width());
                }
                GreenChild::Token(token) => at = at.saturating_add(token.width()),
            }
        }
        out
    }

    /// Its direct tokens, with their positions.
    #[must_use]
    pub fn tokens(&self) -> Vec<(Span, GreenToken)> {
        let mut out = Vec::new();
        let mut at = self.offset;
        for child in self.green.children() {
            let width = child.width();
            if let GreenChild::Token(token) = child {
                out.push((Span::new(at, at.saturating_add(width)), token.clone()));
            }
            at = at.saturating_add(width);
        }
        out
    }

    /// The node's own tokens with the trivia left out.
    ///
    /// [`SyntaxNode::tokens`] keeps everything, because the round trip
    /// needs it. A rule asking what a node says wants this one: the
    /// first token of `@ key` is `@`, and the one after it is `key`,
    /// not the space between them.
    #[must_use]
    pub fn syntax_tokens(&self) -> Vec<(Span, GreenToken)> {
        self.tokens()
            .into_iter()
            .filter(|(_, token)| !token.kind().is_trivia())
            .collect()
    }

    /// Reconstruct the source this node covers.
    #[must_use]
    pub fn text(&self) -> String {
        self.green.text()
    }

    /// Every node in the subtree, this one first, in source order.
    #[must_use]
    pub fn descendants(self: &Arc<Self>) -> Vec<Arc<Self>> {
        let mut out = alloc::vec![self.clone()];
        let mut index = 0;
        while index < out.len() {
            let node = out[index].clone();
            out.extend(node.children());
            index += 1;
        }
        out
    }

    /// Whether this node or any below it came from recovery.
    #[must_use]
    pub fn has_errors(self: &Arc<Self>) -> bool {
        self.descendants().iter().any(|node| node.kind().is_error())
    }
}

impl fmt::Display for SyntaxNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.text())
    }
}

/// A marked position among an open node's children.
///
/// Only meaningful to the builder that produced it and only while that
/// builder is at the same depth.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Checkpoint {
    depth: usize,
    children: usize,
}

/// Builds a green tree.
///
/// Nodes are opened and closed around the tokens they contain, so a
/// parser writes what it is doing rather than assembling children by
/// hand.
#[derive(Debug, Default)]
pub struct TreeBuilder {
    stack: Vec<(CstKind, Vec<GreenChild>)>,
    finished: Option<Arc<GreenNode>>,
}

impl TreeBuilder {
    /// An empty builder.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            stack: Vec::new(),
            finished: None,
        }
    }

    /// Begin a node.
    pub fn start(&mut self, kind: CstKind) {
        self.stack.push((kind, Vec::new()));
    }

    /// Add a token to the node in progress.
    pub fn token(&mut self, kind: TokenKind, text: &str) {
        if let Some((_, children)) = self.stack.last_mut() {
            children.push(GreenChild::Token(GreenToken::new(kind, text)));
        }
    }

    /// End the node in progress, attaching it to its parent.
    pub fn finish(&mut self) {
        let Some((kind, children)) = self.stack.pop() else {
            return;
        };
        let node = Arc::new(GreenNode::new(kind, children));
        match self.stack.last_mut() {
            Some((_, siblings)) => siblings.push(GreenChild::Node(node)),
            None => self.finished = Some(node),
        }
    }

    /// The completed tree.
    ///
    /// `None` if a node was left open, which is a parser defect rather
    /// than a malformed input — every `start` has to have its `finish`.
    #[must_use]
    pub fn build(mut self) -> Option<Arc<GreenNode>> {
        while !self.stack.is_empty() {
            self.finish();
        }
        self.finished
    }

    /// How deep the builder currently is.
    #[must_use]
    pub fn depth(&self) -> usize {
        self.stack.len()
    }

    /// Mark the current position among the open node's children.
    ///
    /// Precedence climbing does not know it is building a binary
    /// expression until it has already parsed the left operand, so it
    /// needs to wrap children it has emitted. A checkpoint plus
    /// [`TreeBuilder::start_at`] is that, and it is why the builder
    /// exists rather than callers assembling children by hand.
    #[must_use]
    pub fn checkpoint(&self) -> Checkpoint {
        Checkpoint {
            depth: self.stack.len(),
            children: self.stack.last().map_or(0, |(_, children)| children.len()),
        }
    }

    /// Wrap everything emitted since `checkpoint` in a new node.
    ///
    /// A checkpoint from a different nesting depth is ignored rather
    /// than acted on: acting on it would silently reparent a subtree.
    pub fn start_at(&mut self, checkpoint: Checkpoint, kind: CstKind) {
        if checkpoint.depth != self.stack.len() {
            self.start(kind);
            return;
        }
        let Some((_, children)) = self.stack.last_mut() else {
            self.start(kind);
            return;
        };
        if checkpoint.children > children.len() {
            self.start(kind);
            return;
        }
        let wrapped: Vec<GreenChild> = children.split_off(checkpoint.children);
        self.stack.push((kind, wrapped));
    }
}

/// Where a file's syntax is, with the trivia left out.
///
/// A node's [`span`](SyntaxNode::span) covers the trivia too, because
/// the round trip needs it: the lexer attaches whitespace and comments
/// leading, so the blank line and the indentation before a declaration
/// belong to the node that follows them. That is right for
/// reconstruction and wrong for a diagnostic — the span of the second
/// declarator of `long a,\n         b;` begins at the newline after the
/// comma, so a message about `b` names the line before it.
///
/// This is the index that turns such a span back into the text it names:
/// the first significant token at or after the start, and the last one
/// ending at or before the end. A span that holds no significant token
/// at all — an empty one, or one covering only trivia — is returned
/// unchanged rather than moved somewhere it did not point.
#[derive(Debug, Clone, Default)]
pub struct SyntaxSpans {
    /// Every non-trivia token's range, ascending. Tokens do not overlap,
    /// so this is sorted by `start` and by `end` at once.
    spans: Vec<Span>,
}

impl SyntaxSpans {
    /// Index the subtree under `root`.
    ///
    /// Iterative for the same reason [`GreenNode::text`] is: the grammar
    /// puts no bound on nesting, and this crate forbids panics, which a
    /// stack overflow would not respect.
    #[must_use]
    pub fn of(root: &Arc<SyntaxNode>) -> Self {
        let mut spans = Vec::new();
        let mut stack: Vec<(&GreenNode, u32)> = alloc::vec![(root.green(), root.span().start)];
        while let Some((green, offset)) = stack.pop() {
            let mut at = offset;
            for child in green.children() {
                let width = child.width();
                match child {
                    GreenChild::Token(token) => {
                        if !token.kind().is_trivia() {
                            spans.push(Span::new(at, at.saturating_add(width)));
                        }
                    }
                    GreenChild::Node(node) => stack.push((node, at)),
                }
                at = at.saturating_add(width);
            }
        }
        spans.sort_unstable_by_key(|span| span.start);
        Self { spans }
    }

    /// `span` narrowed to the text it covers.
    #[must_use]
    pub fn trim(&self, span: Span) -> Span {
        if span.is_empty() {
            return span;
        }
        let first = self.spans.partition_point(|token| token.start < span.start);
        let Some(head) = self.spans.get(first) else {
            return span;
        };
        if head.start >= span.end {
            return span;
        }
        let last = self.spans.partition_point(|token| token.end <= span.end);
        let end = match last.checked_sub(1).and_then(|index| self.spans.get(index)) {
            Some(tail) if tail.end > head.start => tail.end,
            _ => span.end,
        };
        Span::new(head.start, end)
    }
}