zerodds-idl-parse 1.0.0-rc.7

Hand-written recursive-descent parser for OMG IDL 4.2, building a lossless CST with error recovery.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The recursive-descent driver. Spec §3.
//!
//! The parser never returns `Err` for a syntax error. It records a
//! diagnostic, wraps the offending range in an error node, and
//! resynchronises — because a frontend that stops at the first mistake
//! makes a large file take as many runs as it has mistakes.

use crate::diagnostic::{Diagnostic, DiagnosticCode};
use alloc::format;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use zerodds_idl_cst::{
    Checkpoint, CstKind, GreenNode, Span, SyntaxNode, Token, TokenKind, TreeBuilder, lex,
};

/// What came out of parsing a file.
#[derive(Debug, Clone)]
pub struct Parse {
    root: Arc<GreenNode>,
    diagnostics: Vec<Diagnostic>,
    /// Where each piece of the tree's text came from, in tree order: the
    /// piece's offset in the tree and its range in the source the parser
    /// read. See [`Parse::source_span`].
    placed: Vec<(u32, Span)>,
}

impl Parse {
    /// The tree, always present — a file that fails to parse still has
    /// one, with its bad ranges as error nodes.
    #[must_use]
    pub fn syntax(&self) -> Arc<SyntaxNode> {
        Arc::new(SyntaxNode::root(self.root.clone()))
    }

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

    /// Everything wrong with the input, in source order.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Whether anything was wrong.
    #[must_use]
    pub fn has_errors(&self) -> bool {
        self.diagnostics.iter().any(Diagnostic::is_error)
    }

    /// Put diagnostics ahead of the ones already there.
    ///
    /// For the preprocessor's, which explain most of what the parser
    /// then makes of the result.
    pub fn prepend_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
        let mut all = diagnostics;
        all.append(&mut self.diagnostics);
        self.diagnostics = all;
    }

    /// Reconstruct the source. Equals the input byte for byte, whether
    /// or not it parsed. Spec §2.2.
    #[must_use]
    pub fn text(&self) -> String {
        self.root.text()
    }

    /// A range of the tree as a range of the source the parser read.
    ///
    /// The same range for a file parsed with [`parse`], whose tree is the
    /// file byte for byte. Not for [`parse_preprocessed`]: the expanded
    /// text puts one space between two tokens and the tree holds the
    /// tokens without it, and a pragma's text stands after the token it
    /// leads there and before it here. A node's range in the tree — what
    /// lowering records — is an offset into neither file nor expanded
    /// text until it passes through this.
    ///
    /// Each end is moved by the piece of text that holds it; an end past
    /// the last piece stays at the end of that piece.
    #[must_use]
    pub fn source_span(&self, tree: Span) -> Span {
        let start = self.source_offset(tree.start, false);
        let end = if tree.end > tree.start {
            self.source_offset(tree.end, true).max(start)
        } else {
            start
        };
        Span::new(start, end)
    }

    /// One offset of the tree in the source. `end` reads the offset as
    /// the end of a range, so it belongs to the piece that holds the
    /// byte before it.
    fn source_offset(&self, offset: u32, end: bool) -> u32 {
        let probe = if end {
            offset.saturating_sub(1)
        } else {
            offset
        };
        let Some(index) = self
            .placed
            .partition_point(|(start, _)| *start <= probe)
            .checked_sub(1)
        else {
            return self
                .placed
                .first()
                .map_or(offset, |(_, source)| source.start);
        };
        let (start, source) = self.placed[index];
        source
            .start
            .saturating_add(offset.saturating_sub(start))
            .min(source.end)
    }
}

/// Parse a file.
#[must_use]
pub fn parse(source: &str) -> Parse {
    parse_with(source, crate::Dialect::ALL_VENDORS)
}

/// Parse under a chosen dialect.
///
/// [`parse`] admits every vendor extension, which is what a tool reading
/// files it did not write needs. `Dialect::STRICT` is OMG IDL 4.2 and
/// nothing else: a file that parses under it is one every vendor's
/// compiler also reads. Spec §3.5.
#[must_use]
pub fn parse_with(source: &str, dialect: crate::Dialect) -> Parse {
    let mut parser = Parser::new(source);
    parser.dialect = dialect;
    parser.parse_file()
}

/// Parse a preprocessed stream.
///
/// The tree covers the expanded text, not the file as written: an
/// `#include` brought other files into it and a conditional cut parts of
/// it out. [`parse`] is the byte-exact path; this is the one that sees
/// what a compiler would.
///
/// The preprocessor's own diagnostics come first, because a file that
/// failed to include something explains most of what follows.
#[must_use]
pub fn parse_preprocessed(preprocessed: &crate::preprocess::Preprocessed) -> Parse {
    parse_preprocessed_with(preprocessed, crate::Dialect::ALL_VENDORS)
}

/// Parse a preprocessed stream under a chosen dialect.
///
/// [`parse_preprocessed`] admits every vendor extension. This is the
/// entry `--strict` reaches: `Dialect::STRICT` turns every vendor
/// construct from a warning into an error, so a file that gets through
/// is one every other vendor's front end reads too. Spec §3.5.
#[must_use]
pub fn parse_preprocessed_with(
    preprocessed: &crate::preprocess::Preprocessed,
    dialect: crate::Dialect,
) -> Parse {
    let mut parser = Parser::over(&preprocessed.text, preprocessed.tokens.clone());
    parser.dialect = dialect;
    let mut parse = parser.parse_file();
    parse.prepend_diagnostics(preprocessed.diagnostics.clone());
    parse
}

/// The parser state.
pub struct Parser<'a> {
    pub(crate) source: &'a str,
    pub(crate) tokens: Vec<Token>,
    pub(crate) at: usize,
    pub(crate) builder: TreeBuilder,
    pub(crate) diagnostics: Vec<Diagnostic>,
    /// Which dialect this parse accepts. Spec §3.5.
    pub(crate) dialect: crate::Dialect,
    /// Guards against a rule that consumes nothing being retried
    /// forever. Bumped whenever the cursor moves.
    steps: usize,
    /// How long the tree's text is so far.
    tree_len: u32,
    /// Where each piece of the tree's text came from.
    /// [`Parse::source_span`].
    placed: Vec<(u32, Span)>,
}

/// How many parser steps one file may take.
///
/// A bound rather than trust: a rule that fails to consume is a defect,
/// and without this it hangs instead of being reported.
const MAX_STEPS: usize = 4_000_000;

impl<'a> Parser<'a> {
    /// A parser over `source`.
    #[must_use]
    pub fn new(source: &'a str) -> Self {
        Self {
            source,
            tokens: lex(source),
            at: 0,
            builder: TreeBuilder::new(),
            diagnostics: Vec::new(),
            dialect: crate::Dialect::ALL_VENDORS,
            steps: 0,
            tree_len: 0,
            placed: Vec::new(),
        }
    }

    /// A parser over an already-lexed stream.
    ///
    /// The preprocessor produces one: its tokens index into text it
    /// assembled from every file that contributed, so the parser lexes
    /// nothing and reads spans into that text instead.
    #[must_use]
    pub fn over(source: &'a str, tokens: Vec<Token>) -> Self {
        Self {
            source,
            tokens,
            at: 0,
            builder: TreeBuilder::new(),
            diagnostics: Vec::new(),
            dialect: crate::Dialect::ALL_VENDORS,
            steps: 0,
            tree_len: 0,
            placed: Vec::new(),
        }
    }

    /// Parse the whole file.
    #[must_use]
    pub fn parse_file(mut self) -> Parse {
        self.builder.start(CstKind::File);
        while !self.at_eof() {
            let before = self.at;
            self.parse_definition();
            if self.at == before {
                // A rule that consumed nothing would loop; take the
                // token as an error and move on.
                self.error_token("expected a declaration");
            }
            if self.steps > MAX_STEPS {
                break;
            }
        }
        self.eat_eof();
        self.builder.finish();

        let root = self
            .builder
            .build()
            .unwrap_or_else(|| Arc::new(GreenNode::new(CstKind::File, Vec::new())));
        Parse {
            root,
            diagnostics: self.diagnostics,
            placed: self.placed,
        }
    }

    // -- cursor ----------------------------------------------------------

    /// What the current token is.
    #[must_use]
    pub fn current(&self) -> TokenKind {
        self.tokens.get(self.at).map_or(TokenKind::Eof, |t| t.kind)
    }

    /// What the token `ahead` positions on is.
    #[must_use]
    pub fn peek(&self, ahead: usize) -> TokenKind {
        self.tokens
            .get(self.at + ahead)
            .map_or(TokenKind::Eof, |t| t.kind)
    }

    /// Whether the cursor is at the end.
    #[must_use]
    pub fn at_eof(&self) -> bool {
        self.current() == TokenKind::Eof
    }

    /// Where the current token is.
    #[must_use]
    pub fn current_span(&self) -> Span {
        self.tokens
            .get(self.at)
            .map_or_else(|| Span::empty_at(self.source_end()), |t| t.span)
    }

    /// The text of the current token.
    #[must_use]
    pub fn current_text(&self) -> &str {
        self.tokens
            .get(self.at)
            .and_then(|t| t.span.slice(self.source))
            .unwrap_or("")
    }

    #[allow(clippy::cast_possible_truncation)]
    fn source_end(&self) -> u32 {
        self.source.len() as u32
    }

    /// Consume the current token into the tree, trivia first.
    ///
    /// Trivia reaches the tree through `Parser::emit_leading_trivia`
    /// and nowhere else, which is what keeps the round trip exact
    /// without every rule having to think about it.
    pub fn bump(&mut self) {
        self.steps += 1;
        let Some(token) = self.tokens.get(self.at) else {
            return;
        };
        let text = token.span.slice(self.source).unwrap_or("");
        let kind = token.kind;
        let span = token.span;
        self.emit_leading_trivia();
        self.emit(kind, text, span);
        self.at += 1;
    }

    /// Put one piece of text into the tree, and note where in the source
    /// it came from ([`Parse::source_span`]).
    pub(crate) fn emit(&mut self, kind: TokenKind, text: &str, source: Span) {
        if !text.is_empty() {
            self.placed.push((self.tree_len, source));
        }
        self.builder.token(kind, text);
        #[allow(clippy::cast_possible_truncation)]
        let len = text.len() as u32;
        self.tree_len = self.tree_len.saturating_add(len);
    }

    /// Put the current token's leading trivia into the tree.
    ///
    /// Trivia goes into the tree here and nowhere else. A `//@` comment
    /// that reaches this point is one no declaration claimed as a suffix
    /// annotation, and it is reported where it stands: DDS-XTypes 1.3
    /// §7.3.1.2.5 admits the form only after the declaration it applies
    /// to, and a `//@key` that is read as a comment leaves the model
    /// saying "not a key" about a file that says "key".
    fn emit_leading_trivia(&mut self) {
        let Some(token) = self.tokens.get(self.at) else {
            return;
        };
        let leading: Vec<(zerodds_idl_cst::TriviaKind, Span)> = token
            .leading
            .iter()
            .map(|trivia| (trivia.kind, trivia.span))
            .collect();
        for (kind, span) in leading {
            let text = span.slice(self.source).unwrap_or("");
            if kind == zerodds_idl_cst::TriviaKind::LineComment
                && crate::suffix::is_suffix_comment(text)
            {
                self.diagnose_stray_suffix(span);
            }
            self.emit(trivia_token(kind), text, span);
        }
    }

    /// Consume the end-of-file token, so its trailing trivia lands in
    /// the tree.
    pub(crate) fn eat_eof(&mut self) {
        self.emit_leading_trivia();
    }

    /// The rest of a string literal whose first piece `kind` was just
    /// consumed: every adjacent literal of the same width.
    ///
    /// §7.2.6.3: "Adjacent string literals are concatenated", and
    /// §7.2.6.4 says the same of wide ones. A lexical rule, so it holds
    /// wherever a string literal stands — a constant, an annotation
    /// argument, a `typeid`, a `typeprefix`, an `import`, a `context`
    /// clause. The pieces stay separate tokens, so the round trip keeps
    /// them as written; the lowering joins their values, each decoded on
    /// its own, which keeps the characters of the pieces "distinct".
    /// A literal of the other width next to it is diagnosed: neither
    /// clause concatenates a narrow and a wide literal.
    pub(crate) fn eat_adjacent_string_literals(&mut self, kind: TokenKind) {
        loop {
            let next = self.current();
            if next == kind {
                self.bump();
            } else if matches!(next, TokenKind::StringLiteral | TokenKind::WStringLiteral) {
                self.diagnose(
                    crate::diagnostic::DiagnosticCode::UnexpectedToken,
                    "a string literal and a wide string literal are not concatenated; \
                     adjacent literals have to be of one width",
                    "OMG IDL 4.2 §7.2.6.3, §7.2.6.4",
                );
                self.bump();
            } else {
                return;
            }
        }
    }

    /// Consume the current token if it is `kind`.
    pub fn eat(&mut self, kind: TokenKind) -> bool {
        if self.current() == kind {
            self.bump();
            true
        } else {
            false
        }
    }

    /// Consume `kind`, or diagnose that it was missing.
    ///
    /// Does not consume anything on failure: the caller's recovery
    /// decides what to skip.
    pub fn expect(&mut self, kind: TokenKind, what: &str) -> bool {
        if self.eat(kind) {
            return true;
        }
        let span = Span::empty_at(self.current_span().start);
        self.diagnostics.push(Diagnostic::error(
            DiagnosticCode::MissingToken,
            span,
            format!("expected {what}"),
            "OMG IDL 4.2 §7.4",
        ));
        false
    }

    // -- tree ------------------------------------------------------------

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

    /// End the node in progress.
    pub fn finish(&mut self) {
        self.builder.finish();
    }

    /// Mark the current position, for wrapping already-emitted children.
    #[must_use]
    pub fn checkpoint(&self) -> Checkpoint {
        self.builder.checkpoint()
    }

    /// Wrap everything since `checkpoint` in a node.
    pub fn start_at(&mut self, checkpoint: Checkpoint, kind: CstKind) {
        self.builder.start_at(checkpoint, kind);
    }

    /// How many tokens are left.
    #[must_use]
    pub fn tokens_left(&self) -> usize {
        self.tokens.len().saturating_sub(self.at)
    }

    /// Consume the first half of a `>>` token, leaving the second.
    ///
    /// `sequence<sequence<long>>` ends in one token that closes two
    /// argument lists. The tree records two `>` covering one source
    /// token, so `text()` still reproduces the input. Spec §2.3.
    pub fn split_shift_right(&mut self) {
        self.steps += 1;
        let Some(token) = self.tokens.get(self.at).cloned() else {
            return;
        };
        if token.kind != TokenKind::GreaterGreater {
            return;
        }
        // The leading trivia belongs to the first half only.
        self.emit_leading_trivia();
        self.emit(
            TokenKind::Greater,
            ">",
            Span::new(token.span.start, token.span.start.saturating_add(1)),
        );

        // Replace the token with a bare `>` covering the second byte, so
        // the outer list closes on real input rather than on nothing.
        let rest = Token {
            kind: TokenKind::Greater,
            span: Span::new(token.span.start.saturating_add(1), token.span.end),
            leading: Vec::new(),
            value: token.value,
        };
        if let Some(slot) = self.tokens.get_mut(self.at) {
            *slot = rest;
        }
    }

    // -- diagnostics and recovery ---------------------------------------

    /// Record a diagnostic without consuming anything.
    pub fn diagnose(&mut self, code: DiagnosticCode, message: &str, spec_reference: &'static str) {
        self.diagnostics.push(Diagnostic::error(
            code,
            self.current_span(),
            message.to_string(),
            spec_reference,
        ));
    }

    /// Record that a construct outside OMG IDL 4.2 was written here.
    ///
    /// **Always recorded, whether or not the dialect admits it.** The
    /// severity is what the dialect decides: an error when the flag is
    /// off, a warning when it is on.
    ///
    /// Admitting a construct and saying nothing about it are two
    /// different things, and only the first is what `Dialect` is for.
    /// The gate here was written the other way round — silent when
    /// admitted — and `parse` passes `Dialect::ALL_VENDORS` while every
    /// caller in this tree calls `parse`, so the gate never fired for
    /// anybody and `keylist` reached every one of them with nothing said
    /// about it. A file that is not IDL 4.2 is an interoperability fact
    /// of the same kind as a byte difference, and a caller cannot act on
    /// a fact it is never given.
    ///
    /// Spec §3.5.
    pub(crate) fn diagnose_vendor(
        &mut self,
        flag: crate::dialect::VendorFlag,
        message: &str,
        spec_reference: &'static str,
    ) {
        let span = self.current_span();
        let diagnostic = if self.dialect.admits(flag) {
            Diagnostic::warning(
                DiagnosticCode::VendorExtension,
                span,
                message.to_string(),
                spec_reference,
            )
        } else {
            Diagnostic::error(
                DiagnosticCode::VendorExtension,
                span,
                message.to_string(),
                spec_reference,
            )
        };
        self.diagnostics.push(diagnostic);
    }

    /// Wrap the current token in an error node and move past it.
    pub fn error_token(&mut self, message: &str) {
        self.diagnose(DiagnosticCode::UnexpectedToken, message, "OMG IDL 4.2 §7.4");
        self.start(CstKind::Error);
        self.bump();
        self.finish();
    }

    /// Skip to the next place a declaration can begin.
    ///
    /// Recovery points are `;`, `}` and the top-level keywords. The
    /// skipped range becomes an error node so the round trip holds.
    /// Spec §3.2.
    pub fn recover_to_declaration(&mut self) {
        if self.at_eof() || self.at_recovery_point() {
            return;
        }
        self.start(CstKind::Error);
        let mut depth = 0i32;
        while !self.at_eof() {
            match self.current() {
                TokenKind::LBrace => depth += 1,
                TokenKind::RBrace => {
                    if depth == 0 {
                        break;
                    }
                    depth -= 1;
                }
                TokenKind::Semicolon if depth == 0 => {
                    self.bump();
                    break;
                }
                _ if depth == 0 && self.at_recovery_point() => break,
                _ => {}
            }
            self.bump();
            if self.steps > MAX_STEPS {
                break;
            }
        }
        self.finish();
    }

    /// Whether the current token can begin a declaration.
    #[must_use]
    pub fn at_recovery_point(&self) -> bool {
        matches!(
            self.current(),
            TokenKind::Module
                | TokenKind::Const
                | TokenKind::Typedef
                | TokenKind::Struct
                | TokenKind::Union
                | TokenKind::Enum
                | TokenKind::Bitmask
                | TokenKind::Bitset
                | TokenKind::Native
                | TokenKind::Exception
                | TokenKind::Interface
                | TokenKind::Valuetype
                | TokenKind::Eventtype
                | TokenKind::Component
                | TokenKind::Home
                | TokenKind::Connector
                | TokenKind::Porttype
                | TokenKind::Local
                | TokenKind::Abstract
                | TokenKind::Custom
                | TokenKind::Typeid
                | TokenKind::Typeprefix
                | TokenKind::Import
                | TokenKind::At
        )
    }

    /// The diagnostics gathered so far.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// The token kind one piece of trivia takes in the tree.
///
/// `Newline` and `Whitespace` are one kind here: the tree keeps the text
/// itself, so nothing about the round trip needs them apart.
pub(crate) const fn trivia_token(kind: zerodds_idl_cst::TriviaKind) -> TokenKind {
    match kind {
        zerodds_idl_cst::TriviaKind::Whitespace | zerodds_idl_cst::TriviaKind::Newline => {
            TokenKind::Whitespace
        }
        zerodds_idl_cst::TriviaKind::LineComment => TokenKind::LineComment,
        zerodds_idl_cst::TriviaKind::BlockComment => TokenKind::BlockComment,
        zerodds_idl_cst::TriviaKind::Directive => TokenKind::Directive,
    }
}