openbim-step 0.7.0

Generic ISO 10303 STEP Part 21 and EXPRESS syntax infrastructure
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Semantic parser and event/sink interface.
//!
//! Parsing is structural: record names and parameters are retained without a
//! domain schema. Unknown header and data records therefore survive a
//! parse/write/reparse cycle.

use std::borrow::Cow;

use crate::escape;
use crate::lexer::{Lexer, Token};
use crate::recovery::{Diagnostic, OnMalformed, ParseOptions, ParseOutcome};
use crate::references::ReferenceCheck;
use crate::{
    DataRecord, DataSection, Exchange, HeaderRecord, HeaderSection, InstanceId, Parameter, Record,
    Span, Spanned, StepError,
};

/// A semantic parser event.
#[derive(Debug, Clone, PartialEq)]
pub enum Event<S = String> {
    /// Entered `HEADER;`.
    StartHeader,
    /// Parsed one header record.
    HeaderRecord(HeaderRecord<S>),
    /// Reached the header `ENDSEC;`.
    EndHeader,
    /// Entered `DATA;`.
    StartData,
    /// Parsed one data record.
    DataRecord(DataRecord<S>),
    /// Reached the data `ENDSEC;`.
    EndData,
}

/// Consumer for parse events.
pub trait EventSink<S = String> {
    /// Receives one event. Events are delivered in source order.
    fn event(&mut self, event: Event<S>);
}

impl<S, F> EventSink<S> for F
where
    F: FnMut(Event<S>),
{
    fn event(&mut self, event: Event<S>) {
        self(event);
    }
}

/// Parses a physical file into an owned generic exchange structure.
/// # Errors
///
/// Returns [`StepError`] for the wrong
/// physical-file marker, or a spanned lexical/syntax diagnostic otherwise.
pub fn parse(input: &[u8]) -> Result<Exchange, StepError> {
    parse_with(input, ParseOptions::strict()).map(|outcome| outcome.exchange)
}

/// Parses a physical file under explicit [`ParseOptions`].
///
/// With [`OnMalformed::Skip`] an unparsable data record is reported as a
/// [`Diagnostic`] and the parser resynchronizes on the next record or section
/// end, so a consumer can load a damaged file and still show exactly what was
/// dropped. Header structure stays strict under every policy: mandatory header
/// records are file-level invariants, not recoverable payload.
/// # Errors
///
/// Returns [`StepError`] for the wrong physical-file marker, for any header or
/// section-structure defect, and for data defects when the policy is
/// [`OnMalformed::Abort`].
pub fn parse_with(input: &[u8], options: ParseOptions) -> Result<ParseOutcome, StepError> {
    #[derive(Default)]
    struct Builder {
        header: HeaderSection,
        data: DataSection,
    }

    impl EventSink for Builder {
        fn event(&mut self, event: Event) {
            match event {
                Event::HeaderRecord(record) => self.header.records.push(record),
                Event::DataRecord(record) => self.data.records.push(record),
                Event::StartHeader | Event::EndHeader | Event::StartData | Event::EndData => {}
            }
        }
    }

    let mut builder = Builder::default();
    let diagnostics = parse_events_with(input, &mut builder, options)?;
    Ok(ParseOutcome {
        exchange: Exchange {
            header: builder.header,
            data: builder.data,
        },
        diagnostics,
    })
}

/// Parses a physical file and sends semantic records to `sink`.
///
/// Unlike [`parse`], this API does not accumulate an [`Exchange`]. It is useful
/// for import pipelines that index, validate, or transform records as they are
/// read.
/// # Errors
///
/// Returns the same physical-file and syntax diagnostics as [`parse`].
pub fn parse_events(input: &[u8], sink: &mut impl EventSink) -> Result<(), StepError> {
    parse_events_with(input, sink, ParseOptions::strict()).map(|_| ())
}

/// Streams semantic records under explicit [`ParseOptions`], returning the
/// non-fatal diagnostics collected on the way.
/// # Errors
///
/// Returns the same physical-file, header, and structure diagnostics as
/// [`parse_with`].
pub fn parse_events_with(
    input: &[u8],
    sink: &mut impl EventSink,
    options: ParseOptions,
) -> Result<Vec<Diagnostic>, StepError> {
    if !crate::is_step_file(input) {
        return Err(StepError::not_step("missing ISO-10303-21 marker"));
    }
    let mut parser = Parser::new(input);
    parser.options = options;
    parser.references = options.check_references.then(ReferenceCheck::default);
    parser.parse(sink)?;
    Ok(parser.diagnostics)
}

/// Streams semantic records whose text borrows from `input` where it can.
///
/// Identical to [`parse_events_with`] -- same events, order, diagnostics and
/// errors -- except that names, numbers, enumerations and binaries are
/// `Cow::Borrowed` slices of `input` unless the source interrupted them with
/// an ignored control (TAB, LF, CR, FF), and strings are borrowed unless they
/// contain an escape or a quote. Name case is preserved as written (the owned
/// API upper-cases); compare with `eq_ignore_ascii_case`. A consumer that
/// converts every value anyway skips one allocation per value this way.
/// # Errors
///
/// Returns the same diagnostics as [`parse_events_with`].
pub fn parse_events_borrowed<'a>(
    input: &'a [u8],
    sink: &mut impl EventSink<Cow<'a, str>>,
    options: ParseOptions,
) -> Result<Vec<Diagnostic>, StepError> {
    if !crate::is_step_file(input) {
        return Err(StepError::not_step("missing ISO-10303-21 marker"));
    }
    let mut parser = Parser::new(input);
    parser.options = options;
    parser.references = options.check_references.then(ReferenceCheck::default);
    parser.parse(sink)?;
    Ok(parser.diagnostics)
}

/// How the parser turns lexemes into the caller's string type.
///
/// `String` reproduces the owned API exactly (names upper-cased, lossy UTF-8).
/// `Cow<'a, str>` borrows from the input whenever the bytes are valid UTF-8
/// and need no rewriting, and keeps name case as written.
trait Text<'a>: Sized {
    /// A record, typed-parameter or enumeration name.
    fn name(bytes: Cow<'a, [u8]>) -> Self;
    /// A number or binary lexeme.
    fn lexeme(bytes: Cow<'a, [u8]>) -> Self;
    /// An escaped string body.
    fn text(raw: Cow<'a, [u8]>) -> Self;
}

impl<'a> Text<'a> for String {
    fn name(bytes: Cow<'a, [u8]>) -> Self {
        upper(&bytes)
    }

    fn lexeme(bytes: Cow<'a, [u8]>) -> Self {
        lexeme_string(bytes)
    }

    fn text(raw: Cow<'a, [u8]>) -> Self {
        escape::decode(&raw)
    }
}

impl<'a> Text<'a> for Cow<'a, str> {
    fn name(bytes: Cow<'a, [u8]>) -> Self {
        borrowed_str(bytes)
    }

    fn lexeme(bytes: Cow<'a, [u8]>) -> Self {
        borrowed_str(bytes)
    }

    fn text(raw: Cow<'a, [u8]>) -> Self {
        // Without a quote or backslash there is nothing to decode, so the
        // decoded text is the raw body itself.
        match raw {
            Cow::Borrowed(bytes) if memchr::memchr2(b'\\', b'\'', bytes).is_none() => {
                borrowed_str(Cow::Borrowed(bytes))
            }
            raw => Cow::Owned(escape::decode(&raw)),
        }
    }
}

/// Borrows valid UTF-8 as `str`; otherwise the same lossy conversion as the
/// owned API.
fn borrowed_str(bytes: Cow<'_, [u8]>) -> Cow<'_, str> {
    match bytes {
        Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
        Cow::Owned(bytes) => Cow::Owned(lexeme_string(Cow::Owned(bytes))),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Phase {
    BeforeStart,
    BeforeHeader,
    Header,
    BeforeData,
    Data,
    BeforeEnd,
    Done,
}

struct Parser<'a> {
    input: &'a [u8],
    lexer: Lexer<'a>,
    lookahead: Option<Spanned<Token<'a>>>,
    last_end: usize,
    phase: Phase,
    header_records_seen: usize,
    options: ParseOptions,
    diagnostics: Vec<Diagnostic>,
    /// Present only when the caller opted into reference checking.
    references: Option<ReferenceCheck>,
    /// Where to stop early; only the parallel driver sets anything else.
    stop: Stop,
    /// The offset the parse stopped at, when it stopped as `stop` asked.
    stopped: Option<usize>,
    /// Source span of every data record emitted, when collected.
    record_spans: Option<Vec<Span>>,
}

/// Where a restricted parse ends. The parallel driver parses the header up
/// to `DATA;` and then each slice of the data section on its own thread.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Stop {
    /// Parse the whole input.
    Never,
    /// Stop right after `DATA;`, before the first data record.
    AfterDataStart,
    /// Stop when a record ends exactly at this offset.
    AtOffset(usize),
}

/// One slice of the data section, parsed on its own.
pub(crate) struct Chunk {
    pub(crate) records: Vec<DataRecord>,
    /// `records[i]` spans `spans[i]`, as the reference check needs.
    pub(crate) spans: Vec<Span>,
    pub(crate) diagnostics: Vec<Diagnostic>,
    /// Whether the slice ended exactly at the requested offset. `false`
    /// means the split point was not a record boundary for the parser.
    pub(crate) aligned: bool,
}

/// Parses the header through `DATA;`. Returns the header records and the
/// offset just past `DATA;`, or `None` when the parse never reaches a data
/// section. There are no diagnostics to return: header and structure
/// defects are fatal under every policy, and recovery and the reference
/// check only act inside `DATA`.
pub(crate) fn parse_prefix(
    input: &[u8],
    options: ParseOptions,
) -> Result<Option<(HeaderSection, usize)>, StepError> {
    let mut header = HeaderSection::default();
    let mut parser = Parser::new(input);
    parser.options = options;
    parser.stop = Stop::AfterDataStart;
    parser.parse(&mut |event: Event| {
        if let Event::HeaderRecord(record) = event {
            header.records.push(record);
        }
    })?;
    debug_assert!(parser.diagnostics.is_empty());
    Ok(parser.stopped.map(|offset| (header, offset)))
}

/// Parses the data section from `start`, which must be a record boundary,
/// to `end` (`None`: to the end of the file, including `ENDSEC` and the end
/// marker). The parser state at a record boundary is fully determined by
/// the offset, so this is exactly what a whole-file parse does there.
pub(crate) fn parse_chunk(
    input: &[u8],
    options: ParseOptions,
    start: usize,
    end: Option<usize>,
) -> Result<Chunk, StepError> {
    let mut records = Vec::new();
    let mut parser = Parser::new(input);
    parser.options = options;
    parser.phase = Phase::Data;
    parser.lexer.resume_at(start);
    parser.last_end = start;
    parser.record_spans = Some(Vec::new());
    parser.stop = end.map_or(Stop::Never, Stop::AtOffset);
    parser.parse(&mut |event: Event| {
        if let Event::DataRecord(record) = event {
            records.push(record);
        }
    })?;
    Ok(Chunk {
        records,
        spans: parser.record_spans.take().unwrap_or_default(),
        diagnostics: parser.diagnostics,
        aligned: end.is_none() || parser.stopped.is_some(),
    })
}

impl<'a> Parser<'a> {
    fn new(input: &'a [u8]) -> Self {
        Self {
            input,
            lexer: Lexer::new(input),
            lookahead: None,
            last_end: 0,
            phase: Phase::BeforeStart,
            header_records_seen: 0,
            options: ParseOptions::strict(),
            diagnostics: Vec::new(),
            references: None,
            stop: Stop::Never,
            stopped: None,
            record_spans: None,
        }
    }

    // Keeping section dispatch together makes the state-machine transitions auditable.
    #[allow(clippy::too_many_lines)]
    fn parse<S: Text<'a>>(&mut self, sink: &mut impl EventSink<S>) -> Result<(), StepError> {
        loop {
            if let Stop::AtOffset(end) = self.stop {
                // Between records there is no lookahead, and the lexer sits
                // just past the last `;`. Landing exactly on `end` is the
                // only success; passing it means the split point was inside
                // a record or literal, and the slice is not usable.
                if self.lookahead.is_none() {
                    let offset = self.lexer.offset();
                    if offset == end {
                        self.stopped = Some(end);
                        return Ok(());
                    }
                    if offset > end {
                        return Ok(());
                    }
                }
            }
            let token = match self.next() {
                Ok(Some(token)) => token,
                Ok(None) => break,
                Err(error) => {
                    // A lexical defect inside DATA is recoverable: the damaged
                    // bytes belong to one record, not to the file structure.
                    self.recover_or_fail(error, None)?;
                    continue;
                }
            };
            if self.phase == Phase::Done {
                return Err(StepError::syntax(
                    token.span,
                    "content after END-ISO-10303-21",
                ));
            }
            match token.value {
                Token::Name(name) if name.eq_ignore_ascii_case(b"ISO-10303-21") => {
                    if self.phase != Phase::BeforeStart {
                        return Err(StepError::syntax(
                            token.span,
                            "unexpected ISO-10303-21 marker",
                        ));
                    }
                    self.expect_semicolon("after ISO-10303-21")?;
                    self.phase = Phase::BeforeHeader;
                }
                Token::Name(name) if name.eq_ignore_ascii_case(b"HEADER") => {
                    if self.phase != Phase::BeforeHeader {
                        return Err(StepError::syntax(token.span, "unexpected HEADER section"));
                    }
                    self.expect_semicolon("after HEADER")?;
                    self.phase = Phase::Header;
                    sink.event(Event::StartHeader);
                }
                Token::Name(name) if name.eq_ignore_ascii_case(b"DATA") => {
                    if self.phase != Phase::BeforeData {
                        return Err(StepError::syntax(token.span, "unexpected DATA section"));
                    }
                    self.expect_semicolon("after DATA")?;
                    self.phase = Phase::Data;
                    sink.event(Event::StartData);
                    if self.stop == Stop::AfterDataStart {
                        self.stopped = Some(self.lexer.offset());
                        return Ok(());
                    }
                }
                Token::Name(name) if name.eq_ignore_ascii_case(b"ENDSEC") => {
                    self.expect_semicolon("after ENDSEC")?;
                    match self.phase {
                        Phase::Header => {
                            if self.header_records_seen < 3 {
                                return Err(StepError::syntax(
                                    token.span,
                                    "missing mandatory STEP header record",
                                ));
                            }
                            sink.event(Event::EndHeader);
                            self.phase = Phase::BeforeData;
                        }
                        Phase::Data => {
                            if let Some(check) = self.references.take() {
                                check.finish(&mut self.diagnostics);
                            }
                            sink.event(Event::EndData);
                            self.phase = Phase::BeforeEnd;
                        }
                        _ => {
                            return Err(StepError::syntax(token.span, "ENDSEC outside a section"));
                        }
                    }
                }
                Token::Name(name) if name.eq_ignore_ascii_case(b"END-ISO-10303-21") => {
                    if self.phase != Phase::BeforeEnd {
                        return Err(StepError::syntax(
                            token.span,
                            "unexpected END-ISO-10303-21 marker",
                        ));
                    }
                    self.expect_semicolon("after END-ISO-10303-21")?;
                    self.phase = Phase::Done;
                }
                Token::Name(name) if self.phase == Phase::Header => {
                    self.validate_header_record(&name, token.span)?;
                    let parameters = self.parse_arguments()?;
                    self.expect_semicolon("after header record")?;
                    sink.event(Event::HeaderRecord(HeaderRecord {
                        name: S::name(name),
                        parameters,
                    }));
                }
                Token::Id(id) if self.phase == Phase::Data => {
                    let start = token.span.start;
                    match self.parse_data_record(&id, token.span) {
                        Ok(record) => {
                            if let Some(check) = &mut self.references {
                                let span = Span::new(start, self.last_end);
                                check.record(&record, span, &mut self.diagnostics);
                            }
                            if let Some(spans) = &mut self.record_spans {
                                spans.push(Span::new(start, self.last_end));
                            }
                            sink.event(Event::DataRecord(record));
                        }
                        Err(error) => self.recover_or_fail(error, Some(start))?,
                    }
                }
                _ => {
                    let error = StepError::syntax(
                        token.span,
                        format!("unexpected token {:?} in {:?}", token.value, self.phase),
                    );
                    self.recover_or_fail(error, Some(token.span.start))?;
                }
            }
        }
        if self.phase != Phase::Done {
            let detail = if matches!(self.phase, Phase::Header | Phase::Data) {
                "unterminated section (expected ENDSEC)"
            } else {
                "physical file requires start, HEADER, DATA, and end markers"
            };
            return Err(StepError::syntax(self.eof_span(), detail));
        }
        Ok(())
    }

    /// Parses one `#id = ...;` data record, assuming the id token was consumed.
    fn parse_data_record<S: Text<'a>>(
        &mut self,
        id: &[u8],
        id_span: Span,
    ) -> Result<DataRecord<S>, StepError> {
        self.expect_equals()?;
        let record_token = self.next()?.ok_or_else(|| {
            StepError::syntax(Span::new(id_span.end, id_span.end), "missing record body")
        })?;
        let records = match record_token.value {
            Token::Name(name) => vec![self.parse_named_record(name)?],
            Token::OpenParen => {
                let mut records = Vec::new();
                loop {
                    if !self
                        .peek()?
                        .is_some_and(|next| next.value != Token::CloseParen)
                    {
                        break;
                    }
                    let component = self.next()?.ok_or_else(|| {
                        StepError::syntax(self.eof_span(), "missing complex record")
                    })?;
                    let Token::Name(name) = component.value else {
                        return Err(StepError::syntax(
                            component.span,
                            "expected complex record name",
                        ));
                    };
                    records.push(self.parse_named_record(name)?);
                }
                let close = self.next()?.ok_or_else(|| {
                    StepError::syntax(self.eof_span(), "unterminated complex instance")
                })?;
                if close.value != Token::CloseParen {
                    return Err(StepError::syntax(
                        close.span,
                        "expected ')' after complex instance",
                    ));
                }
                if records.is_empty() {
                    return Err(StepError::syntax(
                        record_token.span,
                        "complex instance must contain a record",
                    ));
                }
                records
            }
            _ => {
                return Err(StepError::syntax(
                    record_token.span,
                    "expected record name or complex instance",
                ));
            }
        };
        self.expect_semicolon("after data record")?;
        Ok(DataRecord {
            id: InstanceId::new(std::str::from_utf8(id).expect("instance digits are ASCII"))
                .expect("lexer validates instance ids"),
            records,
        })
    }

    /// Applies the malformed-record policy.
    ///
    /// Recovery is deliberately narrow. It applies only inside `DATA`, only
    /// when the caller opted in, and it always advances the cursor, so a
    /// damaged file cannot loop. Header and section-structure defects stay
    /// fatal under every policy: they describe the file, not one payload
    /// record, and silently continuing past them would produce a model whose
    /// provenance is unknown.
    fn recover_or_fail(
        &mut self,
        error: StepError,
        record_start: Option<usize>,
    ) -> Result<(), StepError> {
        if self.options.on_malformed_record != OnMalformed::Skip || self.phase != Phase::Data {
            return Err(error);
        }
        let start = record_start.unwrap_or_else(|| error.span().start);
        // Resynchronize from just past the record's first byte, NOT from the
        // end of the error span. A diagnostic can legitimately span the token
        // that follows the damage -- including `ENDSEC` -- and resuming past
        // it would swallow the section terminator.
        let resume = self.resync_from(start.saturating_add(1));
        self.lookahead = None;
        self.lexer.resume_at(resume);
        self.last_end = resume;
        self.diagnostics.push(Diagnostic::skipped_record(
            Span::new(start, resume),
            format!("skipped malformed data record: {}", error.detail()),
        ));
        Ok(())
    }

    /// Finds the next byte offset at which parsing can safely restart.
    ///
    /// Scans raw bytes rather than tokens because the tokenizer is what
    /// failed. All three literal kinds -- quoted strings, binary literals, and
    /// comments -- are tracked, so a `;` or `ENDSEC` inside a literal is not
    /// mistaken for a record boundary. Scanning a literal's payload as code
    /// would let recovery fabricate records that were never in the source.
    /// A section terminator stops the scan *before* it is consumed, so
    /// recovery can never swallow the end of `DATA`.
    fn resync_from(&self, from: usize) -> usize {
        // Which literal the scanner is currently inside. STEP has three, and
        // all of them can contain bytes that look like record syntax.
        enum Literal {
            None,
            // `'...'`, where `''` is an escaped apostrophe rather than a close.
            Text,
            // `"...."`, with no doubling rule: the first `"` closes it.
            Binary,
        }

        let mut position = from.min(self.input.len());
        let mut literal = Literal::None;
        while position < self.input.len() {
            let byte = self.input[position];
            match literal {
                Literal::Text => {
                    // Mirror `Lexer::lex_text`: `\\` is one escaped
                    // backslash, and `\S\` escapes the next byte even when it
                    // is an apostrophe. Upper case only, as in the lexer.
                    if self.input[position..].starts_with(br"\\") {
                        position += 2;
                        continue;
                    }
                    if self.input[position..].starts_with(br"\S\") {
                        position = (position + 4).min(self.input.len());
                        continue;
                    }
                    if byte == b'\'' {
                        if self.input.get(position + 1) == Some(&b'\'') {
                            position += 2;
                            continue;
                        }
                        literal = Literal::None;
                    }
                    position += 1;
                }
                Literal::Binary => {
                    if byte == b'"' {
                        literal = Literal::None;
                    }
                    position += 1;
                }
                Literal::None => match byte {
                    b'\'' => {
                        literal = Literal::Text;
                        position += 1;
                    }
                    b'"' => {
                        literal = Literal::Binary;
                        position += 1;
                    }
                    b'/' if self.input.get(position + 1) == Some(&b'*') => {
                        position = self.input[position + 2..]
                            .windows(2)
                            .position(|window| window == b"*/")
                            .map_or(self.input.len(), |offset| position + 2 + offset + 2);
                    }
                    b';' => return position + 1,
                    _ if self.section_end_at(position) => return position,
                    _ => position += 1,
                },
            }
        }
        self.input.len()
    }

    fn section_end_at(&self, position: usize) -> bool {
        let preceded_by_word = position
            .checked_sub(1)
            .and_then(|previous| self.input.get(previous))
            .is_some_and(|byte| byte.is_ascii_alphanumeric() || *byte == b'_');
        !preceded_by_word
            && self
                .input
                .get(position..position + 6)
                .is_some_and(|bytes| bytes.eq_ignore_ascii_case(b"ENDSEC"))
    }

    fn validate_header_record(&mut self, name: &[u8], span: Span) -> Result<(), StepError> {
        const REQUIRED: [&[u8]; 3] = [b"FILE_DESCRIPTION", b"FILE_NAME", b"FILE_SCHEMA"];
        if let Some(expected) = REQUIRED.get(self.header_records_seen) {
            if !name.eq_ignore_ascii_case(expected) {
                return Err(StepError::syntax(
                    span,
                    format!(
                        "expected mandatory {} header record",
                        String::from_utf8_lossy(expected)
                    ),
                ));
            }
        } else if REQUIRED
            .iter()
            .any(|required| name.eq_ignore_ascii_case(required))
        {
            return Err(StepError::syntax(span, "duplicate mandatory header record"));
        }
        self.header_records_seen += 1;
        Ok(())
    }

    fn parse_named_record<S: Text<'a>>(
        &mut self,
        name: Cow<'a, [u8]>,
    ) -> Result<Record<S>, StepError> {
        Ok(Record {
            name: S::name(name),
            parameters: self.parse_arguments()?,
        })
    }

    fn parse_arguments<S: Text<'a>>(&mut self) -> Result<Vec<Parameter<S>>, StepError> {
        let token = self
            .next()?
            .ok_or_else(|| StepError::syntax(self.eof_span(), "expected '(' after record name"))?;
        if token.value != Token::OpenParen {
            return Err(StepError::syntax(
                token.span,
                "expected '(' after record name",
            ));
        }
        self.parse_parameter_list(0)
    }

    fn parse_parameter_list<S: Text<'a>>(
        &mut self,
        depth: usize,
    ) -> Result<Vec<Parameter<S>>, StepError> {
        if depth > crate::MAX_PARAMETER_NESTING {
            let span = match self.peek()? {
                Some(token) => token.span,
                None => self.eof_span(),
            };
            return Err(StepError::syntax(span, "parameter nesting limit exceeded"));
        }
        let mut parameters = Vec::new();
        if self
            .peek()?
            .is_some_and(|token| token.value == Token::CloseParen)
        {
            let _ = self.next()?;
            return Ok(parameters);
        }
        loop {
            parameters.push(self.parse_parameter(depth)?);
            let separator = self
                .next()?
                .ok_or_else(|| StepError::syntax(self.eof_span(), "unterminated parameter list"))?;
            match separator.value {
                Token::Comma => {}
                Token::CloseParen => return Ok(parameters),
                _ => {
                    return Err(StepError::syntax(
                        separator.span,
                        "expected ',' or ')' after parameter",
                    ));
                }
            }
        }
    }

    fn parse_parameter<S: Text<'a>>(&mut self, depth: usize) -> Result<Parameter<S>, StepError> {
        let token = self
            .next()?
            .ok_or_else(|| StepError::syntax(self.eof_span(), "expected parameter"))?;
        match token.value {
            Token::Dollar => Ok(Parameter::Null),
            Token::Star => Ok(Parameter::Derived),
            Token::Id(id) => Ok(Parameter::Ref(
                InstanceId::new(std::str::from_utf8(&id).expect("instance digits are ASCII"))
                    .expect("lexer validates instance ids"),
            )),
            Token::Integer(value) => Ok(Parameter::Integer(S::lexeme(value))),
            Token::Real(value) => Ok(Parameter::Real(S::lexeme(value))),
            Token::Text(raw) => Ok(Parameter::Text(S::text(raw))),
            Token::Binary(raw) => Ok(Parameter::Binary(S::lexeme(raw))),
            Token::Keyword(keyword) if keyword.eq_ignore_ascii_case(b"T") => {
                Ok(Parameter::Bool(true))
            }
            Token::Keyword(keyword) if keyword.eq_ignore_ascii_case(b"F") => {
                Ok(Parameter::Bool(false))
            }
            Token::Keyword(keyword) if keyword.eq_ignore_ascii_case(b"U") => {
                Ok(Parameter::LogicalUnknown)
            }
            Token::Keyword(keyword) => Ok(Parameter::Enum(S::name(keyword))),
            Token::OpenParen => Ok(Parameter::List(self.parse_parameter_list(depth + 1)?)),
            Token::Name(name) => {
                let Some(next) = self.peek()? else {
                    return Err(StepError::syntax(
                        self.eof_span(),
                        "expected '(' after typed parameter name",
                    ));
                };
                if next.value != Token::OpenParen {
                    return Err(StepError::syntax(
                        next.span,
                        "expected '(' after typed parameter name",
                    ));
                }
                let _ = self.next()?;
                let mut parameters = self.parse_parameter_list(depth + 1)?;
                let value = if parameters.len() == 1 {
                    Box::new(parameters.remove(0))
                } else {
                    Box::new(Parameter::List(parameters))
                };
                Ok(Parameter::Typed {
                    type_name: S::name(name),
                    value,
                })
            }
            value => Err(StepError::syntax(
                token.span,
                format!("unexpected parameter token {value:?}"),
            )),
        }
    }

    fn expect_equals(&mut self) -> Result<(), StepError> {
        let token = self
            .next()?
            .ok_or_else(|| StepError::syntax(self.eof_span(), "expected '=' after instance id"))?;
        if token.value == Token::Equals {
            Ok(())
        } else {
            Err(StepError::syntax(
                token.span,
                "expected '=' after instance id",
            ))
        }
    }

    fn expect_semicolon(&mut self, context: &str) -> Result<(), StepError> {
        let token = self
            .next()?
            .ok_or_else(|| StepError::syntax(self.eof_span(), format!("expected ';' {context}")))?;
        if token.value == Token::Semicolon {
            Ok(())
        } else {
            Err(StepError::syntax(
                token.span,
                format!("expected ';' {context}"),
            ))
        }
    }

    fn next(&mut self) -> Result<Option<Spanned<Token<'a>>>, StepError> {
        let token = match self.lookahead.take() {
            Some(token) => Some(token),
            None => self.lexer.next_spanned()?,
        };
        if let Some(token) = &token {
            self.last_end = token.span.end;
        }
        Ok(token)
    }

    fn peek(&mut self) -> Result<Option<&Spanned<Token<'a>>>, StepError> {
        if self.lookahead.is_none() {
            self.lookahead = self.lexer.next_spanned()?;
        }
        Ok(self.lookahead.as_ref())
    }

    fn eof_span(&self) -> Span {
        let offset = self.last_end.max(self.lexer.offset());
        Span::new(offset, offset)
    }
}

fn upper(bytes: &[u8]) -> String {
    // Names reach here from the lexer, which admits only ASCII, so the lossy
    // conversion never replaces anything; the fast path skips its scan.
    match std::str::from_utf8(bytes) {
        Ok(text) => text.to_ascii_uppercase(),
        Err(_) => String::from_utf8_lossy(bytes).to_ascii_uppercase(),
    }
}

/// A lexeme as an owned `String`, reusing an owned buffer instead of copying.
/// Numbers and binaries are ASCII, so the lossy fallback never fires on them.
fn lexeme_string(bytes: Cow<'_, [u8]>) -> String {
    match bytes {
        Cow::Borrowed(bytes) => match std::str::from_utf8(bytes) {
            Ok(text) => text.to_owned(),
            Err(_) => String::from_utf8_lossy(bytes).into_owned(),
        },
        Cow::Owned(bytes) => String::from_utf8(bytes)
            .unwrap_or_else(|error| String::from_utf8_lossy(error.as_bytes()).into_owned()),
    }
}