sophia_turtle 0.10.0

A Rust toolkit for RDF and Linked Data - parsers and serializers for the Turtle-family of syntaxes
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
//! [TriG] parser
//!
//! [TriG]: https://www.w3.org/TR/rdf12-trig

use std::io::BufRead;

use sophia_api::{
    parser::QuadParser,
    quad::Spog,
    source::{Source, StreamError::SinkError, StreamResult},
    version::Version,
};
use sophia_iri::resolve::BaseIriRef;

use crate::{
    _term::StashedTerm,
    lazy_regex,
    parser::{
        _common::{Extra, GenericSource, Inner, State, TxSource},
        _error::{Error, ErrorKind, ResultExt},
    },
};

mod _state;
use _state::*;

sophia_api::def_mod_functions_for_bufread_parser!(TriGParser, QuadParser);

/// [TriG] parser.
///
/// [TriG]: https://www.w3.org/TR/rdf12-trig/
#[derive(Clone, Debug, Default)]
pub struct TriGParser {
    /// The default base IRI, if any.
    ///
    /// Will be applied until overridden with a `@base`/`BASE` directive.
    pub base: Option<BaseIriRef<Box<str>>>,
    /// The default version specifier.
    ///
    /// Will be applied until overridden with a `@version`/`VERSION` directive.
    pub version: Version,
    /// Whether the parser should preserve blank node labels.
    pub preserve_bn_labels: bool,
}

impl TriGParser {
    /// Construct a [`TriGParser`] with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Change the [`base`](TriGParser::base) option of this parser.
    #[must_use]
    pub fn with_base(self, base: Option<BaseIriRef<Box<str>>>) -> Self {
        Self { base, ..self }
    }

    /// Change the [`version`](TriGParser::version) option of this parser.
    #[must_use]
    pub fn with_version(self, version: Version) -> Self {
        Self { version, ..self }
    }

    /// Change the [`preserve_bn_labels`](TriGParser::preserve_bn_labels) option of this parser.
    #[must_use]
    pub fn with_preserve_bn_labels(self, preserve_bn_labels: bool) -> Self {
        Self {
            preserve_bn_labels,
            ..self
        }
    }
}

impl<I: BufRead> QuadParser<I> for TriGParser {
    type Source = TriGSource<I>;

    fn parse(&self, data: I) -> Self::Source {
        TriGSource {
            input: data,
            inner: Inner::new(self.version, self.preserve_bn_labels),
            extra: Extra {
                base: self.base.clone(),
                ..Default::default()
            },
        }
    }
}

/// [`QuadSource`](sophia_api::prelude::QuadSource) returned by a [`TriGParser`]
#[derive(Clone, Debug)]
pub struct TriGSource<I> {
    input: I,
    inner: Inner,
    extra: Extra<TriGState>,
}

impl<I> GenericSource for TriGSource<I> {
    type Output<'x> = Spog<StashedTerm<'x>>;

    fn inner(&self) -> &Inner {
        &self.inner
    }

    fn inner_mut(&mut self) -> &mut Inner {
        &mut self.inner
    }

    fn emit_tuple<C, E, E2>(&self, callback: &mut C, offset: usize) -> StreamResult<usize, E, E2>
    where
        C: FnMut(Self::Output<'_>) -> Result<(), E2>,
        E: std::error::Error,
        E2: std::error::Error,
    {
        let g = match self.extra.state[0] {
            TriGState::NamedGraph(has_name) => {
                debug_assert!(has_name);
                debug_assert!(!self.inner.terms.is_empty());
                Some(StashedTerm {
                    terms: &self.inner.terms,
                    buffers: &self.inner.buffers,
                    idx: 0,
                })
            }
            _ => None,
        };
        let spo = StashedTerm::new_triple(&self.inner.terms, &self.inner.buffers);
        callback((spo, g)).map_err(SinkError).map(|_| offset)
    }
}

impl<I> TxSource<TriGState> for TriGSource<I> {
    fn extra(&self) -> &Extra<TriGState> {
        &self.extra
    }

    fn extra_mut(&mut self) -> &mut Extra<TriGState> {
        &mut self.extra
    }

    fn inner_and_extra_mut(&mut self) -> (&mut Inner, &mut Extra<TriGState>) {
        (&mut self.inner, &mut self.extra)
    }

    fn emit_reification_tuple<C, E, E2>(
        &self,
        callback: &mut C,
        offset: usize,
    ) -> StreamResult<usize, E, E2>
    where
        C: FnMut(Self::Output<'_>) -> Result<(), E2>,
        E: std::error::Error,
        E2: std::error::Error,
    {
        debug_assert!(!self.extra.state.is_empty());
        let g = match self.extra.state[0] {
            TriGState::NamedGraph(has_name) => {
                debug_assert!(has_name);
                debug_assert!(!self.inner.terms.is_empty());
                Some(StashedTerm {
                    terms: &self.inner.terms,
                    buffers: &self.inner.buffers,
                    idx: 0,
                })
            }
            _ => None,
        };
        let [o, p, s] = StashedTerm::new_triple(&self.inner.terms, &self.inner.buffers);
        callback(([s, p, o], g)).map_err(SinkError).map(|_| offset)
    }

    fn parse_token<C, E>(&mut self, txt: &str, callback: &mut C) -> StreamResult<usize, Error, E>
    where
        C: FnMut(<Self as GenericSource>::Output<'_>) -> Result<(), E>,
        E: std::error::Error + Send + Sync + 'static,
    {
        use TriGState::*;

        debug_assert!(
            !txt.is_empty()
                && (self.ws(txt) == 0
                    || matches!(self.extra.state.last(), Some(StringLiteralLong(_))))
        );

        match self.extra.state.last() {
            None => self.enter_trig_doc(txt),
            Some(DefaultGraph) => self.in_default_graph(txt),
            Some(NamedGraph(has_name)) => self.in_named_graph(txt, *has_name),
            Some(Block(square_bracket)) => self.in_block(txt, *square_bracket),
            Some(LabelNotSubject) => self.after_label_not_subject(txt),
            Some(AtDirective) => self.end_at_directive(txt),
            Some(BaseIri) => self.in_base_iri(txt),
            Some(PrefixDeclaration) => self.in_prefix_declaration(txt),
            Some(PrefixIri) => self.in_prefix_iri(txt),
            Some(VersionSpecifier) => self.in_version_specifier(txt),
            Some(Triples(require_pol)) => self.in_triples(txt, *require_pol),
            Some(PredicateObjectList) => self.in_predicate_object_list(txt),
            Some(ObjectList) => self.in_object_list(txt, callback),
            Some(Verb) => self.exit_verb(txt, callback),
            Some(Object) => self.exit_object(txt, callback),
            Some(BlankNodePropertyListOrAnon) => self.in_blank_node_property_list_or_anon(txt),
            Some(Collection) => self.in_collection(txt, callback),
            Some(RdfLiteral(dt)) => self.in_rdf_literal(txt, *dt),
            Some(Reifier) => self.in_reifier(txt, callback),
            Some(ReifiedTriple(true)) => self.enter_rt_subject(txt),
            Some(ReifiedTriple(false)) => self.exit_reified_triple(txt),
            Some(RtSubject) => self.exit_rt_subject(txt),
            Some(RtObject) => self.exit_rt_object(txt, callback),
            Some(TripleTerm) => self.enter_tt_subject(txt),
            Some(TtSubject) => self.exit_tt_subject(txt),
            Some(TtObject) => self.exit_tt_object(txt),
            Some(AnnotationBlock) => self.enter_predicate_object_list(txt),
            Some(StringLiteralLong(quote)) => self.in_string_literal_long(txt, *quote),
            Some(Anon(false)) => self.in_anon(txt),
            Some(Anon(true)) => self.in_anon_reifier(txt, callback),
        }
    }

    fn in_predicate_object_list<E>(&mut self, txt: &str) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        if txt.starts_with('}') {
            if !(3..=4).contains(&self.extra.state.len()) {
                return Err(ErrorKind::Expected("verb, ';' or closing bracket".into()))
                    .wrap_in(self);
            }
            debug_assert!(
                self.extra.state[self.extra.state.len() - 2].in_triples(),
                "{:?}",
                self.extra.state
            );
            debug_assert!(
                self.extra.state.len() == 3
                    || matches!(
                        self.extra.state[self.extra.state.len() - 3],
                        TriGState::DefaultGraph | TriGState::NamedGraph(true)
                    ),
                "{:?}",
                self.extra.state
            );
            self.pop_triple_components(1);
            if self.extra.state[0] == TriGState::NamedGraph(true) {
                self.pop_triple_components(1);
            }
            debug_assert!(self.inner.terms.is_empty());
            for _ in 0..3 {
                self.extra.state.pop();
            }
            Ok(1)
        } else {
            self.in_predicate_object_list_common(txt)
        }
    }

    fn in_object_list<E, C>(&mut self, txt: &str, callback: &mut C) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
        C: FnMut(Self::Output<'_>) -> Result<(), E>,
    {
        if txt.starts_with('}') {
            if self.extra.state.len() != 4 {
                return Err(ErrorKind::Expected(
                    "reifier, annotation, punctuation".into(),
                ))
                .wrap_in(self);
            }
            debug_assert!(self.extra.state[self.extra.state.len() - 2].in_predicate_object_list());
            debug_assert!(self.extra.state[self.extra.state.len() - 3].in_triples());
            debug_assert!(
                self.extra.state.len() == 3
                    || matches!(
                        self.extra.state[self.extra.state.len() - 4],
                        TriGState::DefaultGraph | TriGState::NamedGraph(true)
                    ),
                "{:?}",
                self.extra.state
            );
            self.pop_triple_components(3);
            if self.extra.state[0] == TriGState::NamedGraph(true) {
                self.pop_triple_components(1);
            }
            debug_assert!(self.inner.terms.is_empty());
            for _ in 0..self.extra.state.len() {
                self.extra.state.pop();
            }
            Ok(1)
        } else {
            self.in_object_list_common::<E, C>(txt, callback)
        }
    }

    /// Expect the final '.' at the end of a `triples` production.
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with '.'.
    ///
    /// In TriG, in a graph context, the final period before the closing bracket '}' can be omitted.
    fn end_triples<E2>(&mut self, txt: &str) -> StreamResult<usize, Error, E2>
    where
        E2: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty());
        debug_assert!((1..=2).contains(&self.extra.state.len()));
        debug_assert!(matches!(
            self.extra.state.last(),
            Some(&TriGState::Triples(false))
        ));

        if self.extra.state.len() == 1 {
            if txt.starts_with('.') {
                self.extra.state.pop();
                self.pop_term();
                Ok(1)
            } else {
                Err(ErrorKind::Expected("'.'".into())).wrap_in(self)
            }
        } else {
            debug_assert!(matches!(
                self.extra.state[0],
                TriGState::DefaultGraph | TriGState::NamedGraph(true)
            ));
            let close_graph = match txt.as_bytes()[0] {
                b'.' => false,
                b'}' => true,
                _ => return Err(ErrorKind::Expected("'.' or '}'".into())).wrap_in(self),
            };
            self.extra.state.pop();
            self.pop_term();
            if close_graph {
                self.extra.state.pop();
            }
            Ok(1)
        }
    }
}

impl<I: BufRead> Source for TriGSource<I> {
    type Item<'x> = Spog<StashedTerm<'x>>;

    type Error = Error;

    fn try_for_some_item<E, F>(&mut self, f: F) -> StreamResult<bool, Self::Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
        F: FnMut(Self::Item<'_>) -> Result<(), E>,
    {
        let mut line = String::with_capacity(1024);
        self.inner.col = 0;
        let read = self.input.read_line(&mut line).wrap_in(self)?;
        if read == 0 {
            if let Some(state) = self.extra.state.pop() {
                Err(ErrorKind::EOF(format!("{state:?}"))).wrap_in(self)
            } else {
                Ok(false)
            }
        } else {
            self.parse_line(&line, f)?;
            self.inner.line += 1;
            Ok(true)
        }
    }
}

impl<I> TriGSource<I> {
    #[cfg(test)]
    fn new(input: I) -> Self {
        TriGSource {
            input,
            inner: Default::default(),
            extra: Default::default(),
        }
    }

    /// Enter production https://www.w3.org/TR/rdf12-trig/#grammar-production-trigDoc
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn enter_trig_doc<E>(&mut self, txt: &str) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert!(self.extra.state.last().is_none(), "{:?}", self.extra.state);

        match txt.as_bytes()[0] {
            b'@' => self.enter_at_keyword(txt),
            b'<' => {
                if txt[1..].starts_with('<') {
                    self.extra.state.push(TriGState::Triples(false));
                    self.extra.state.push(TriGState::ReifiedTriple(true));
                    Ok(2)
                } else {
                    self.extra.state.push(TriGState::Block(false));
                    self.iriref(txt)
                }
            }
            b'[' => {
                self.extra.state.push(TriGState::Block(true));
                self.mint_bnode_label();
                Ok(1)
            }
            b'_' => {
                self.extra.state.push(TriGState::Block(false));
                self.blank_node_label(txt)
            }
            b'(' => {
                self.extra.state.push(TriGState::Triples(true));
                self.extra.state.push(TriGState::Collection);
                Ok(1)
            }
            b'{' => {
                self.extra.state.push(TriGState::DefaultGraph);
                Ok(1)
            }
            _ => {
                lazy_regex!(
                    SPARQL_KW = [
                        r"^(?i)BASE[ \n\r\t#<]",
                        r"^(?i)PREFIX[ \n\r\t#]",
                        r#"^(?i)VERSION[ \n\r\t#"']"#,
                        r"^(?i)GRAPH[ \n\r\t#<]"
                    ]
                );
                match SPARQL_KW.matches(txt).iter().next() {
                    Some(0) => {
                        self.extra.state.push(TriGState::BaseIri);
                        Ok("BASE".len())
                    }
                    Some(1) => {
                        self.extra.state.push(TriGState::PrefixDeclaration);
                        Ok("PREFIX".len())
                    }
                    Some(2) => {
                        self.extra.state.push(TriGState::VersionSpecifier);
                        Ok("VERSION".len())
                    }
                    Some(3) => {
                        self.extra.state.push(TriGState::NamedGraph(false));
                        Ok("GRAPH".len())
                    }
                    None => {
                        self.extra.state.push(TriGState::Block(false));
                        self.prefixed_name(txt)
                    }
                    Some(_) => unreachable!(),
                }
            }
        }
    }

    /// Expect any production corresponding to a subject in the default graph.
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn in_default_graph<E>(&mut self, txt: &str) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert!(matches!(
            self.extra.state.last(),
            Some(TriGState::DefaultGraph)
        ));
        debug_assert!(self.extra.state.len() == 1);
        if txt.starts_with("}") {
            self.extra.state.pop();
            Ok(1)
        } else {
            self.subject(txt)
        }
    }

    /// Expect any production corresponding to a subject.
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn subject<E>(&mut self, txt: &str) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        match txt.as_bytes()[0] {
            b'<' => {
                if txt[1..].starts_with('<') {
                    self.extra.state.push(TriGState::Triples(false));
                    self.extra.state.push(TriGState::ReifiedTriple(true));
                    Ok(2)
                } else {
                    self.extra.state.push(TriGState::Triples(true));
                    self.iriref(txt)
                }
            }
            b'[' => {
                self.extra.state.push(TriGState::Triples(false));
                self.extra
                    .state
                    .push(TriGState::BlankNodePropertyListOrAnon);
                self.mint_bnode_label();
                Ok(1)
            }
            b'_' => {
                self.extra.state.push(TriGState::Triples(true));
                self.blank_node_label(txt)
            }
            b'(' => {
                self.extra.state.push(TriGState::Triples(true));
                self.extra.state.push(TriGState::Collection);
                Ok(1)
            }
            _ => {
                self.extra.state.push(TriGState::Triples(true));
                self.prefixed_name(txt)
            }
        }
    }

    /// Expect any production corresponding to a named graph.
    ///
    /// If `has_name` is false, the graph name is expected,
    /// otherwise, a subject is expected.
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn in_named_graph<E>(&mut self, txt: &str, has_name: bool) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert_eq!(
            self.extra.state.last(),
            Some(&TriGState::NamedGraph(has_name))
        );
        if !has_name {
            debug_assert!(self.extra.state.len() == 1);
            self.extra.state[0] = TriGState::NamedGraph(true);
            self.extra.state.push(TriGState::LabelNotSubject);
            match txt.as_bytes()[0] {
                b'<' => self.iriref(txt),
                b'[' => {
                    self.extra.state.push(TriGState::Anon(false));
                    Ok(1)
                }
                b'_' => self.blank_node_label(txt),
                _ => self.prefixed_name(txt),
            }
        } else {
            debug_assert!(has_name);
            if txt.starts_with("}") {
                self.extra.state.pop();
                self.pop_triple_components(1);
                Ok(1)
            } else {
                self.subject(txt)
            }
        }
    }

    /// Expect production https://www.w3.org/TR/rdf12-trig/#grammar-production-block
    /// possibly after reading a leading '[' (if square_bracket is true).
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn in_block<E>(&mut self, txt: &str, square_bracket: bool) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        let state = &mut self.extra.state;
        debug_assert_eq!(state.last(), Some(&TriGState::Block(square_bracket)));
        debug_assert_eq!(state.len(), 1);
        #[allow(clippy::collapsible_else_if)]
        if square_bracket {
            if txt.starts_with(']') {
                state[0] = TriGState::Block(false);
                Ok(1)
            } else {
                state[0] = TriGState::Triples(false);
                state.push(TriGState::BlankNodePropertyListOrAnon);
                self.enter_predicate_object_list(txt)
            }
        } else {
            debug_assert!(!square_bracket);
            if txt.starts_with('{') {
                state[0] = TriGState::NamedGraph(true);
                Ok(1)
            } else {
                state[0] = TriGState::Triples(true);
                self.enter_predicate_object_list(txt)
            }
        }
    }

    /// Expect production https://www.w3.org/TR/rdf12-trig/#grammar-production-labelOrSubject
    /// just after the keyword "GRAPH".
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn after_label_not_subject<E>(&mut self, txt: &str) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert_eq!(self.extra.state.last(), Some(&TriGState::LabelNotSubject));
        if txt.starts_with('{') {
            self.extra.state.pop();
            Ok(1)
        } else {
            Err(ErrorKind::Expected("'{'".into())).wrap_in(self)
        }
    }

    /// Compute the next context after production https://www.w3.org/TR/rdf12-trig/#grammar-production-verb
    /// based on context.
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn exit_verb<E, C>(&mut self, txt: &str, callback: &mut C) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
        C: FnMut(<Self as GenericSource>::Output<'_>) -> Result<(), E>,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert!(
            matches!(self.extra.state.last(), Some(TriGState::Verb)),
            "{:?}",
            self.extra.state
        );

        self.extra.state.pop();
        match self.extra.state.last() {
            Some(TriGState::PredicateObjectList) => self.enter_object_list(txt, callback),
            Some(TriGState::ReifiedTriple(true)) => self.enter_rt_object(txt),
            Some(TriGState::TripleTerm) => self.enter_tt_object(txt),
            _ => unreachable!(),
        }
    }

    /// Compute the next context after production https://www.w3.org/TR/rdf12-trig/#grammar-production-object,
    /// after emitting the last triple in stack.
    ///
    /// See also [`TriGSource::enter_object`]
    ///
    /// `txt` is the remaining of a line of text as read by [`BufRead::read_line`],
    /// starting at self.col, and starting with a non-white-space character.
    fn exit_object<E, C>(&mut self, txt: &str, callback: &mut C) -> StreamResult<usize, Error, E>
    where
        E: std::error::Error + Send + Sync + 'static,
        C: FnMut(<Self as GenericSource>::Output<'_>) -> Result<(), E>,
    {
        debug_assert!(!txt.is_empty() && self.ws(txt) == 0);
        debug_assert!(
            matches!(self.extra.state.last(), Some(TriGState::Object)),
            "{:?}",
            self.extra.state
        );

        self.extra.state.pop();
        match self.extra.state.last() {
            Some(TriGState::ObjectList) => {
                self.emit_tuple(callback, 0)?;
                self.in_object_list(txt, callback)
            }
            Some(TriGState::Collection) => {
                self.emit_tuple(callback, 0)?;
                self.grow_collection()?;
                self.in_collection(txt, callback)
            }
            _ => unreachable!(),
        }
    }
}

#[cfg(test)]
mod test;