zerodds-idl 1.0.0-rc.1

OMG IDL 4.2 (ISO/IEC 19516) Parser + AST + Semantik-Modell für ZeroDDS — Lexer, Grammar-Engine, CST→AST-Builder, Spec-Validators, Annotations.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! CST-Konstruktion aus Recognizer-Output.
//!
//! Aus den Earley-State-Sets eines erfolgreichen [`recognize`-Laufs](crate::engine::Recognizer::recognize)
//! wird der Concrete Syntax Tree per **Backtracking-Search** rekonstruiert:
//!
//! 1. Im finalen State-Set `Sₙ` ein abgeschlossenes Item finden, das die
//!    Start-Production matched (Origin 0, Dot am Ende).
//! 2. Top-down Tree-Konstruktion durch die Alternative-Symbole:
//!    - **Terminal**: passender Token wird als Leaf eingefuegt.
//!    - **Nonterminal `B`**: alle in den State-Sets auffindbaren
//!      abgeschlossenen `B`-Items mit `origin == aktuelle Position`
//!      werden als Kandidaten ausprobiert. Der erste, der sich rekursiv
//!      bauen laesst **und** dessen Rest-Symbole zum Ziel-Endpunkt fuehren,
//!      gewinnt.
//!
//! Greedy-Longest-Match wird bewusst **nicht** verwendet: bei
//! linksrekursiven Grammars (E ::= E "+" T) wuerde der Greedy-Pfad das
//! Sub-E ueber das gesamte Rest-Input ziehen und nichts fuer die
//! verbleibenden Symbole `"+" T` lassen. Backtracking probiert kleinere
//! Sub-Spans und findet die korrekte Links-Assoziativitaet.
//!
//! Komplexitaet ohne Memoization: worst-case exponentiell wenn die
//! Grammar viele nullable Nonterminals hat (jedes davon multipliziert die
//! Position-Kandidaten fuer den naechsten Symbol-Match). Fuer
//! IDL-4.2-Grammar mit `<annotation_appl_seq>`-Hooks ueberall ist das
//! relevant.
//!
//! Mit der Memoization aus T6.0 (`(production, alternative, start, end)
//! -> Option<CstNode>`) laeuft die Reconstruction in O(productions ×
//! alternatives × n²) — polynomial. Cache lebt nur fuer einen
//! `build_cst`-Call und wird beim Return verworfen.
//!
//! Repeat- und Choice-Symbole werden hier nicht behandelt — analog zur
//! .  Desugaring-Pass folgt in einem spaeteren
//! Task.
//!
//! Siehe RFC 0001 §4.1 und §5.4.

use std::collections::HashMap;

use crate::engine::{EarleyItem, RecognitionResult, StateSet};
use crate::errors::Span;
use crate::grammar::{GrammarLike, ProductionId, Symbol};
use crate::lexer::Token;

use super::node::CstNode;

/// Cache-Key fuer [`Memo`]: `(production_id, alternative_index, start, end)`.
type MemoKey = (ProductionId, u32, u32, u32);

/// Memoization-Tabelle fuer `build_internal_node`. Verhindert
/// exponentielles Re-Exploration bei nullable-reichen Grammars (T6.0).
/// `Option`-Wert reflektiert "kein gueltiger Build" — Misserfolge werden
/// genauso gecacht wie Erfolge.
type Memo<'src> = HashMap<MemoKey, Option<CstNode<'src>>>;

/// Baut den CST aus einem Recognizer-Resultat und der zugehoerigen
/// Token-Sequenz.
///
/// Liefert `None`, wenn das Recognition-Ergebnis abgelehnt wurde
/// (`!result.accepted`) oder wenn die Reconstruction fehlschlaegt
/// (z.B. wegen Grammar-Konstrukten, die die aktuelle Engine nicht
/// behandelt — Repeat/Choice).
#[must_use]
pub fn build_cst<'src, G: GrammarLike + ?Sized>(
    grammar: &G,
    tokens: &[Token<'src>],
    result: &RecognitionResult,
) -> Option<CstNode<'src>> {
    if !result.accepted {
        return None;
    }
    let n = tokens.len();
    let final_set = result.state_sets.get(n)?;
    let final_item = find_accepting_item(final_set, grammar)?;
    let mut memo: Memo<'src> = HashMap::new();
    build_internal_node(
        grammar,
        tokens,
        &result.state_sets,
        final_item,
        0,
        n,
        &mut memo,
    )
}

/// Sucht im finalen State-Set ein abgeschlossenes Item der Start-Production
/// mit Origin 0.
fn find_accepting_item<G: GrammarLike + ?Sized>(set: &StateSet, grammar: &G) -> Option<EarleyItem> {
    set.items()
        .iter()
        .copied()
        .find(|it| it.production == grammar.start() && it.origin == 0 && it.is_complete(grammar))
}

/// Baut einen Internal-Node fuer ein bestimmtes Earley-Item, das die
/// Tokens `[start..end]` ueberdeckt.
///
/// Mit Memoization: bei Cache-Hit wird das gespeicherte Ergebnis
/// (Erfolg oder Misserfolg) ohne erneute Backtracking-Suche
/// zurueckgegeben. Cache-Key: `(production_id, alternative_index,
/// start, end)`.
fn build_internal_node<'src, G: GrammarLike + ?Sized>(
    grammar: &G,
    tokens: &[Token<'src>],
    state_sets: &[StateSet],
    item: EarleyItem,
    start: usize,
    end: usize,
    memo: &mut Memo<'src>,
) -> Option<CstNode<'src>> {
    let key: MemoKey = (
        item.production,
        item.alternative_index as u32,
        start as u32,
        end as u32,
    );
    if let Some(cached) = memo.get(&key) {
        return cached.clone();
    }

    let result = build_internal_node_uncached(grammar, tokens, state_sets, item, start, end, memo);
    memo.insert(key, result.clone());
    result
}

fn build_internal_node_uncached<'src, G: GrammarLike + ?Sized>(
    grammar: &G,
    tokens: &[Token<'src>],
    state_sets: &[StateSet],
    item: EarleyItem,
    start: usize,
    end: usize,
    memo: &mut Memo<'src>,
) -> Option<CstNode<'src>> {
    let production = grammar.production(item.production)?;
    let alternative = production.alternatives.get(item.alternative_index)?;

    let mut children: Vec<CstNode<'src>> = Vec::new();
    let final_pos = try_match_symbols(
        grammar,
        tokens,
        state_sets,
        alternative.symbols,
        start,
        end,
        &mut children,
        memo,
    )?;
    if final_pos != end {
        return None;
    }

    let span = compute_span(tokens, start, end);
    let mut node = CstNode::internal(item.production, item.alternative_index, span);
    node.children = children;
    Some(node)
}

/// Versucht, die `symbols`-Sequenz beginnend bei Token-Position `start` zu
/// matchen, sodass die Konsumation an Position `target_end` endet.
///
/// Bei Erfolg sind die Children-Nodes in `accumulated` angehaengt und der
/// Returnwert ist `Some(target_end)`.
#[allow(clippy::too_many_arguments)] // Builder-Recursion mit Memo-State.
/// zerodds-lint: recursion-depth 64 (Parser/AST-Walk; bounded by IDL nesting)
fn try_match_symbols<'src, G: GrammarLike + ?Sized>(
    grammar: &G,
    tokens: &[Token<'src>],
    state_sets: &[StateSet],
    symbols: &[Symbol],
    start: usize,
    target_end: usize,
    accumulated: &mut Vec<CstNode<'src>>,
    memo: &mut Memo<'src>,
) -> Option<usize> {
    if symbols.is_empty() {
        return if start == target_end {
            Some(start)
        } else {
            None
        };
    }

    let head = &symbols[0];
    let tail = &symbols[1..];

    match head {
        Symbol::Terminal(expected_kind) => {
            let token = tokens.get(start)?;
            if token.kind != *expected_kind {
                return None;
            }
            accumulated.push(CstNode::token(*token));
            match try_match_symbols(
                grammar,
                tokens,
                state_sets,
                tail,
                start + 1,
                target_end,
                accumulated,
                memo,
            ) {
                Some(end) => Some(end),
                None => {
                    accumulated.pop();
                    None
                }
            }
        }
        Symbol::Nonterminal(nonterminal) => {
            // Probiere alle abgeschlossenen B-Items mit Origin = start
            // ueber alle moeglichen Endpunkte k in [start, target_end].
            // Greedy-longest-first wuerde bei Linksrekursion fehlschlagen;
            // wir gehen kuerzeste-zuerst, damit linksrekursive E-Sub-Trees
            // nicht das ganze Rest-Input schlucken.
            //
            // k == start ist noetig fuer nullable Nonterminals (z.B.
            // empty `<annotation_appl_seq>`, `<definition_list>`,
            // `<member_list>`). Earley liefert in dem Fall ein
            // complete-Item mit origin == end == start, und die
            // empty-Alternative der Production matcht via
            // `symbols.is_empty() && start == target_end`.
            for k in start..=target_end {
                if let Some(set) = state_sets.get(k) {
                    for candidate in set.items() {
                        if candidate.production == *nonterminal
                            && candidate.origin == start
                            && candidate.is_complete(grammar)
                        {
                            if let Some(child) = build_internal_node(
                                grammar, tokens, state_sets, *candidate, start, k, memo,
                            ) {
                                accumulated.push(child);
                                if let Some(end) = try_match_symbols(
                                    grammar,
                                    tokens,
                                    state_sets,
                                    tail,
                                    k,
                                    target_end,
                                    accumulated,
                                    memo,
                                ) {
                                    return Some(end);
                                }
                                accumulated.pop();
                            }
                        }
                    }
                }
            }
            None
        }
        Symbol::Repeat(_, _) | Symbol::Choice(_) => {
            // Repeat/Choice werden nicht direkt
            // gehandelt. Die Reconstruction schlaegt hier fehl; eine
            // Desugaring-Pass-Erweiterung folgt in einem spaeteren Task.
            None
        }
    }
}

/// Berechnet die Span ueber die Tokens `[start..end]`.
fn compute_span(tokens: &[Token<'_>], start: usize, end: usize) -> Span {
    if start >= end || tokens.is_empty() {
        return Span::SYNTHETIC;
    }
    let first = tokens.get(start).map(|t| t.span).unwrap_or(Span::SYNTHETIC);
    let last = tokens
        .get(end.saturating_sub(1))
        .map(|t| t.span)
        .unwrap_or(first);
    first.merge(last)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::panic)]

    use super::*;
    use crate::cst::CstKind;
    use crate::engine::Recognizer;
    use crate::grammar::Grammar;
    use crate::grammar::ProductionId;
    use crate::grammar::TokenKind;
    use crate::grammar::toy::TOY;

    fn t(kind: TokenKind) -> Token<'static> {
        Token::synthetic(kind)
    }

    fn parse_to_cst<'src>(grammar: &Grammar, tokens: &[Token<'src>]) -> Option<CstNode<'src>> {
        let result = Recognizer::new(grammar).recognize(tokens);
        build_cst(grammar, tokens, &result)
    }

    // -----------------------------------------------------------------
    // Akzeptanz-Pfade auf Toy-Grammar
    // -----------------------------------------------------------------

    #[test]
    fn cst_for_single_n_is_three_levels_deep() {
        // n → E → T → F → "n"
        let cst = parse_to_cst(&TOY, &[t(TokenKind::Keyword("n"))]).expect("must build");
        assert!(cst.is_internal());
        assert_eq!(cst.production(), Some(ProductionId(0))); // E
        // E → T (just_term) → F (number) → Token("n")
        let mut depth = 0;
        let mut current = &cst;
        while let Some(child) = current.children.first() {
            depth += 1;
            current = child;
            if current.is_token() {
                break;
            }
        }
        assert!(depth >= 3, "Expected depth >=3 (E→T→F→Token), got {depth}");
        assert_eq!(current.token_kind(), Some(TokenKind::Keyword("n")));
    }

    #[test]
    fn cst_for_addition_has_e_plus_t_structure() {
        // n + n → E (alt 0 = plus) mit Children [E, "+", T]
        let cst = parse_to_cst(
            &TOY,
            &[
                t(TokenKind::Keyword("n")),
                t(TokenKind::Punct("+")),
                t(TokenKind::Keyword("n")),
            ],
        )
        .expect("must build");
        assert_eq!(cst.production(), Some(ProductionId(0))); // E
        // E (plus) hat 3 Children: E, +, T
        assert_eq!(cst.children.len(), 3);
        assert_eq!(cst.children[0].production(), Some(ProductionId(0))); // sub-E
        assert_eq!(cst.children[1].token_kind(), Some(TokenKind::Punct("+")));
        assert_eq!(cst.children[2].production(), Some(ProductionId(1))); // T
    }

    #[test]
    fn cst_for_multiplication_uses_times_alternative() {
        // n * n → E (just_term) → T (times) mit Children [T, "*", F]
        let cst = parse_to_cst(
            &TOY,
            &[
                t(TokenKind::Keyword("n")),
                t(TokenKind::Punct("*")),
                t(TokenKind::Keyword("n")),
            ],
        )
        .expect("must build");
        // Top-Level E sollte just_term-Alt nutzen (alt_index 1).
        let CstKind::Internal {
            alternative_index, ..
        } = cst.kind
        else {
            panic!("expected Internal");
        };
        assert_eq!(alternative_index, 1, "E sollte just_term-Alt nutzen");
        // T sollte times-Alt nutzen.
        let t_node = &cst.children[0];
        let CstKind::Internal {
            alternative_index: t_alt,
            ..
        } = t_node.kind
        else {
            panic!("expected Internal");
        };
        assert_eq!(t_alt, 0, "T sollte times-Alt nutzen");
        assert_eq!(t_node.children.len(), 3);
    }

    #[test]
    fn cst_left_associative_addition() {
        // n + n + n  ⇒  (n + n) + n
        // Top-E (plus): [Sub-E (plus), "+", T]
        // Sub-E (plus): [Sub-Sub-E (just_term), "+", T]
        let cst = parse_to_cst(
            &TOY,
            &[
                t(TokenKind::Keyword("n")),
                t(TokenKind::Punct("+")),
                t(TokenKind::Keyword("n")),
                t(TokenKind::Punct("+")),
                t(TokenKind::Keyword("n")),
            ],
        )
        .expect("must build");

        // Top-E: alt=plus
        let CstKind::Internal {
            alternative_index, ..
        } = cst.kind
        else {
            panic!("expected Internal");
        };
        assert_eq!(alternative_index, 0, "Top-E muss plus-Alt sein");

        // Sub-E (Top-E.children[0]): alt=plus (linksrekursiv)
        let sub_e = &cst.children[0];
        let CstKind::Internal {
            alternative_index: sub_alt,
            ..
        } = sub_e.kind
        else {
            panic!("expected Internal");
        };
        assert_eq!(
            sub_alt, 0,
            "Sub-E muss auch plus-Alt sein (Linksassoziativitaet)"
        );
    }

    #[test]
    fn cst_parens_wrap_inner_expression() {
        // (n) → E → T → F (paren) → ["(", E, ")"]
        let cst = parse_to_cst(
            &TOY,
            &[
                t(TokenKind::Punct("(")),
                t(TokenKind::Keyword("n")),
                t(TokenKind::Punct(")")),
            ],
        )
        .expect("must build");
        // Steige hinab zu F-Knoten.
        let f_node = cst
            .children
            .first()
            .and_then(|t| t.children.first())
            .expect("E→T→F path");
        assert_eq!(f_node.production(), Some(ProductionId(2))); // F
        let CstKind::Internal {
            alternative_index, ..
        } = f_node.kind
        else {
            panic!("expected Internal");
        };
        assert_eq!(alternative_index, 1, "F muss paren-Alt sein");
        assert_eq!(f_node.children.len(), 3);
        assert_eq!(f_node.children[0].token_kind(), Some(TokenKind::Punct("(")));
        assert_eq!(f_node.children[2].token_kind(), Some(TokenKind::Punct(")")));
    }

    // -----------------------------------------------------------------
    // Reject-Pfade
    // -----------------------------------------------------------------

    #[test]
    fn no_cst_for_rejected_input() {
        let cst = parse_to_cst(&TOY, &[t(TokenKind::Punct("+"))]);
        assert!(cst.is_none());
    }

    #[test]
    fn no_cst_for_empty_input_when_grammar_requires_terminal() {
        let cst = parse_to_cst(&TOY, &[]);
        assert!(cst.is_none());
    }

    // -----------------------------------------------------------------
    // Token-Span-Propagation
    // -----------------------------------------------------------------

    #[test]
    fn token_leaves_carry_their_lexer_span() {
        // Tokens mit echten (statt synthetischen) Spans bauen.
        let tokens = vec![
            Token::new(TokenKind::Keyword("n"), Span::new(0, 1), "n"),
            Token::new(TokenKind::Punct("+"), Span::new(2, 3), "+"),
            Token::new(TokenKind::Keyword("n"), Span::new(4, 5), "n"),
        ];
        let result = Recognizer::new(&TOY).recognize(&tokens);
        let cst = build_cst(&TOY, &tokens, &result).expect("must build");

        // Sammle alle Token-Leaf-Spans via Pre-order.
        let mut spans: Vec<Span> = Vec::new();
        cst.walk_preorder(&mut |n| {
            if n.is_token() {
                spans.push(n.span);
            }
        });
        assert_eq!(
            spans,
            vec![Span::new(0, 1), Span::new(2, 3), Span::new(4, 5)]
        );
    }

    #[test]
    fn internal_node_span_covers_its_token_range() {
        let tokens = vec![
            Token::new(TokenKind::Keyword("n"), Span::new(0, 1), "n"),
            Token::new(TokenKind::Punct("+"), Span::new(2, 3), "+"),
            Token::new(TokenKind::Keyword("n"), Span::new(4, 5), "n"),
        ];
        let result = Recognizer::new(&TOY).recognize(&tokens);
        let cst = build_cst(&TOY, &tokens, &result).expect("must build");
        // Top-E sollte Span 0..5 ueberdecken.
        assert_eq!(cst.span, Span::new(0, 5));
    }

    // -----------------------------------------------------------------
    // E2E ueber Tokenizer (echter Lexer-Output → CST)
    // -----------------------------------------------------------------

    #[test]
    fn end_to_end_lexer_to_cst_for_arithmetic_expression() {
        use crate::lexer::Tokenizer;

        let tokenizer = Tokenizer::for_grammar(&TOY);
        let stream = tokenizer
            .tokenize("n + n * (n + n)")
            .expect("tokenize must succeed");
        let result = Recognizer::new(&TOY).recognize(stream.tokens());
        assert!(result.accepted);

        let cst = build_cst(&TOY, stream.tokens(), &result).expect("build must succeed");
        assert_eq!(cst.production(), Some(ProductionId(0)));
        // Knotenanzahl > 10 (Top-E + sub-Es + sub-Ts + sub-Fs + Token-Leaves)
        assert!(cst.count_nodes() > 10);
    }
}