gmeow-gts 0.9.5

GTS (Graph Transport Substrate) format engine: CBOR-sequence append-only RDF 1.2 log reader, folder, and verifier
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
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! The `trig -> gts` transform.
//!
//! The parser is deliberately strict and dependency-free. It accepts the TriG
//! form emitted by [`crate::trig::to_trig`] plus common prefixes and graph
//! blocks, then delegates the final graph import to [`crate::from_nquads`] so
//! RDF term validation and writer semantics stay shared.

use std::collections::HashMap;
use std::fmt;

use crate::from_nquads::from_nquads;
use crate::nquads::escape_literal;

const RDF_NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";

/// Raised when TriG input is malformed or uses unsupported abbreviation forms.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TriGParseError {
    detail: String,
}

impl TriGParseError {
    fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }
}

impl fmt::Display for TriGParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.detail)
    }
}

impl std::error::Error for TriGParseError {}

#[derive(Clone, Debug, PartialEq, Eq)]
enum Node {
    Iri(String),
    Bnode(String),
    Literal {
        value: String,
        lang: Option<String>,
        datatype: Option<String>,
    },
    Triple(Box<Node>, Box<Node>, Box<Node>),
}

impl Node {
    fn token(&self) -> String {
        match self {
            Node::Iri(iri) => format!("<{iri}>"),
            Node::Bnode(label) => format!("_:{label}"),
            Node::Literal {
                value,
                lang,
                datatype,
            } => {
                let lit = format!("\"{}\"", escape_literal(value));
                if let Some(lang) = lang {
                    format!("{lit}@{lang}")
                } else if let Some(datatype) = datatype {
                    format!("{lit}^^<{datatype}>")
                } else {
                    lit
                }
            }
            Node::Triple(s, p, o) => {
                format!("<<( {} {} {} )>>", s.token(), p.token(), o.token())
            }
        }
    }

    fn is_graph_name(&self) -> bool {
        matches!(self, Node::Iri(_) | Node::Bnode(_))
    }
}

struct Parser<'a> {
    text: &'a str,
    pos: usize,
    prefixes: HashMap<String, String>,
    nquads: Vec<String>,
}

impl<'a> Parser<'a> {
    fn new(text: &'a str) -> Self {
        Self {
            text,
            pos: 0,
            prefixes: HashMap::from([("rdf".to_string(), RDF_NS.to_string())]),
            nquads: Vec::new(),
        }
    }

    fn parse(mut self) -> Result<String, TriGParseError> {
        while !self.eof() {
            self.skip_ws_and_comments();
            if self.eof() {
                break;
            }
            if self.consume("@prefix") {
                self.prefix_directive(true)?;
                continue;
            }
            if self.consume_keyword("PREFIX") {
                self.prefix_directive(false)?;
                continue;
            }
            if self.consume_keyword("GRAPH") {
                let graph = self.term()?;
                self.graph_block(graph)?;
                continue;
            }

            let first = self.term()?;
            self.skip_ws_and_comments();
            if self.consume_char('{') {
                self.graph_block_after_open(first)?;
            } else {
                self.statement_after_subject(first, None)?;
            }
        }
        Ok(if self.nquads.is_empty() {
            String::new()
        } else {
            format!("{}\n", self.nquads.join("\n"))
        })
    }

    fn eof(&mut self) -> bool {
        self.skip_ws_and_comments();
        self.pos >= self.text.len()
    }

    fn skip_ws_and_comments(&mut self) {
        loop {
            while let Some(ch) = self.peek_char() {
                if ch.is_whitespace() {
                    self.bump_char();
                } else {
                    break;
                }
            }
            if self.peek_char() == Some('#') {
                while let Some(ch) = self.bump_char() {
                    if ch == '\n' {
                        break;
                    }
                }
                continue;
            }
            break;
        }
    }

    fn peek_char(&self) -> Option<char> {
        self.text[self.pos..].chars().next()
    }

    fn bump_char(&mut self) -> Option<char> {
        let ch = self.peek_char()?;
        self.pos += ch.len_utf8();
        Some(ch)
    }

    fn consume(&mut self, text: &str) -> bool {
        self.skip_ws_and_comments();
        if self.text[self.pos..].starts_with(text) {
            self.pos += text.len();
            true
        } else {
            false
        }
    }

    fn consume_keyword(&mut self, keyword: &str) -> bool {
        self.skip_ws_and_comments();
        let rest = &self.text[self.pos..];
        if rest.len() < keyword.len() || !rest[..keyword.len()].eq_ignore_ascii_case(keyword) {
            return false;
        }
        let boundary = rest[keyword.len()..]
            .chars()
            .next()
            .map(|ch| ch.is_whitespace() || matches!(ch, '{' | '}' | '<' | '_' | '"'))
            .unwrap_or(true);
        if boundary {
            self.pos += keyword.len();
            true
        } else {
            false
        }
    }

    fn consume_char(&mut self, ch: char) -> bool {
        self.skip_ws_and_comments();
        if self.peek_char() == Some(ch) {
            self.bump_char();
            true
        } else {
            false
        }
    }

    fn expect_char(&mut self, ch: char, context: &str) -> Result<(), TriGParseError> {
        if self.consume_char(ch) {
            Ok(())
        } else {
            Err(TriGParseError::new(format!(
                "expected {ch:?} {context} at byte {}",
                self.pos
            )))
        }
    }

    fn prefix_directive(&mut self, require_dot: bool) -> Result<(), TriGParseError> {
        let prefix = self.prefix_label()?;
        let iri = self.iri()?;
        self.prefixes.insert(prefix, iri);
        if require_dot {
            self.expect_char('.', "after @prefix directive")?;
        } else {
            self.consume_char('.');
        }
        Ok(())
    }

    fn prefix_label(&mut self) -> Result<String, TriGParseError> {
        self.skip_ws_and_comments();
        let start = self.pos;
        while let Some(ch) = self.peek_char() {
            if ch == ':' {
                let label = self.text[start..self.pos].to_string();
                self.bump_char();
                return Ok(label);
            }
            if ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-') {
                self.bump_char();
            } else {
                break;
            }
        }
        Err(TriGParseError::new(format!(
            "expected prefix label at byte {}",
            start
        )))
    }

    fn term(&mut self) -> Result<Node, TriGParseError> {
        self.skip_ws_and_comments();
        if self.text[self.pos..].starts_with("<<(") {
            return self.quoted_triple();
        }
        match self.peek_char() {
            Some('<') => self.iri().map(Node::Iri),
            Some('_') => self.bnode().map(Node::Bnode),
            Some('"') => self.literal(),
            Some(_) => self.prefixed_name().map(Node::Iri),
            None => Err(TriGParseError::new("unexpected end of TriG input")),
        }
    }

    fn predicate(&mut self) -> Result<Node, TriGParseError> {
        self.skip_ws_and_comments();
        if self.consume_keyword("a") {
            Ok(Node::Iri(RDF_TYPE.to_string()))
        } else {
            self.term()
        }
    }

    fn iri(&mut self) -> Result<String, TriGParseError> {
        self.skip_ws_and_comments();
        if self.bump_char() != Some('<') {
            return Err(TriGParseError::new(format!(
                "expected IRI at byte {}",
                self.pos
            )));
        }
        let start = self.pos;
        while let Some(ch) = self.bump_char() {
            if ch == '>' {
                let end = self.pos - 1;
                return Ok(self.text[start..end].to_string());
            }
        }
        Err(TriGParseError::new(format!(
            "unterminated IRI starting at byte {}",
            start.saturating_sub(1)
        )))
    }

    fn bnode(&mut self) -> Result<String, TriGParseError> {
        self.skip_ws_and_comments();
        if !self.text[self.pos..].starts_with("_:") {
            return Err(TriGParseError::new(format!(
                "expected blank node at byte {}",
                self.pos
            )));
        }
        self.pos += 2;
        let start = self.pos;
        while let Some(byte) = self.text.as_bytes().get(self.pos) {
            if byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.') {
                self.pos += 1;
            } else {
                break;
            }
        }
        if self.pos > start && self.text.as_bytes()[self.pos - 1] == b'.' {
            self.pos -= 1;
        }
        if self.pos == start {
            return Err(TriGParseError::new("empty blank node label"));
        }
        Ok(self.text[start..self.pos].to_string())
    }

    fn literal(&mut self) -> Result<Node, TriGParseError> {
        self.skip_ws_and_comments();
        if self.bump_char() != Some('"') {
            return Err(TriGParseError::new("expected literal"));
        }
        let mut value = String::new();
        loop {
            let Some(ch) = self.bump_char() else {
                return Err(TriGParseError::new("unterminated literal"));
            };
            match ch {
                '\\' => value.push(self.escape()?),
                '"' => break,
                _ => value.push(ch),
            }
        }

        let mut lang = None;
        let mut datatype = None;
        if self.peek_char() == Some('@') {
            self.bump_char();
            let start = self.pos;
            while let Some(byte) = self.text.as_bytes().get(self.pos) {
                if byte.is_ascii_alphanumeric() || *byte == b'-' {
                    self.pos += 1;
                } else {
                    break;
                }
            }
            if self.pos == start {
                return Err(TriGParseError::new("empty language tag"));
            }
            lang = Some(self.text[start..self.pos].to_string());
        } else if self.text[self.pos..].starts_with("^^") {
            self.pos += 2;
            datatype = Some(self.datatype_iri()?);
        }
        Ok(Node::Literal {
            value,
            lang,
            datatype,
        })
    }

    fn datatype_iri(&mut self) -> Result<String, TriGParseError> {
        self.skip_ws_and_comments();
        if self.peek_char() == Some('<') {
            self.iri()
        } else {
            self.prefixed_name()
        }
    }

    fn escape(&mut self) -> Result<char, TriGParseError> {
        let Some(ch) = self.bump_char() else {
            return Err(TriGParseError::new("bad escape at end of literal"));
        };
        match ch {
            '\\' => Ok('\\'),
            '"' => Ok('"'),
            'b' => Ok('\u{0008}'),
            'f' => Ok('\u{000c}'),
            'n' => Ok('\n'),
            'r' => Ok('\r'),
            't' => Ok('\t'),
            'u' | 'U' => {
                let width = if ch == 'u' { 4 } else { 8 };
                let end = self.pos + width;
                if end > self.text.len() || !self.text.is_char_boundary(end) {
                    return Err(TriGParseError::new("short or invalid unicode escape"));
                }
                let raw = &self.text[self.pos..end];
                if !raw.bytes().all(|b| b.is_ascii_hexdigit()) {
                    return Err(TriGParseError::new(format!(
                        "bad unicode escape \\{ch}{raw}"
                    )));
                }
                self.pos += width;
                let code = u32::from_str_radix(raw, 16)
                    .map_err(|e| TriGParseError::new(format!("bad unicode escape: {e}")))?;
                char::from_u32(code).ok_or_else(|| {
                    TriGParseError::new(format!("invalid unicode scalar \\{ch}{raw}"))
                })
            }
            other => Err(TriGParseError::new(format!("unsupported escape \\{other}"))),
        }
    }

    fn quoted_triple(&mut self) -> Result<Node, TriGParseError> {
        self.pos += 3;
        let s = self.term()?;
        let p = self.predicate()?;
        let o = self.term()?;
        self.skip_ws_and_comments();
        if !self.text[self.pos..].starts_with(")>>") {
            return Err(TriGParseError::new("unterminated quoted triple"));
        }
        self.pos += 3;
        Ok(Node::Triple(Box::new(s), Box::new(p), Box::new(o)))
    }

    fn prefixed_name(&mut self) -> Result<String, TriGParseError> {
        self.skip_ws_and_comments();
        let start = self.pos;
        while let Some(ch) = self.peek_char() {
            if ch.is_whitespace() || matches!(ch, '{' | '}' | '.' | ';' | ',' | ')') {
                break;
            }
            self.bump_char();
        }
        if self.pos == start {
            return Err(TriGParseError::new(format!(
                "expected term at byte {}",
                self.pos
            )));
        }
        let name = &self.text[start..self.pos];
        let Some((prefix, local)) = name.split_once(':') else {
            return Err(TriGParseError::new(format!(
                "unsupported bare token {name:?}; use an IRI or prefix"
            )));
        };
        let Some(base) = self.prefixes.get(prefix) else {
            return Err(TriGParseError::new(format!("unknown prefix {prefix:?}")));
        };
        Ok(format!("{base}{local}"))
    }

    fn graph_block(&mut self, graph: Node) -> Result<(), TriGParseError> {
        self.expect_char('{', "to open graph block")?;
        self.graph_block_after_open(graph)
    }

    fn graph_block_after_open(&mut self, graph: Node) -> Result<(), TriGParseError> {
        if !graph.is_graph_name() {
            return Err(TriGParseError::new(
                "graph block name must be an IRI or blank node",
            ));
        }
        while !self.consume_char('}') {
            if self.eof() {
                return Err(TriGParseError::new("unterminated graph block"));
            }
            let subject = self.term()?;
            self.statement_after_subject(subject, Some(&graph))?;
        }
        Ok(())
    }

    fn statement_after_subject(
        &mut self,
        subject: Node,
        graph: Option<&Node>,
    ) -> Result<(), TriGParseError> {
        let predicate = self.predicate()?;
        let object = self.term()?;
        self.skip_ws_and_comments();
        if matches!(self.peek_char(), Some(';') | Some(',')) {
            return Err(TriGParseError::new(
                "TriG predicate/object shorthand is not supported; write one statement per line",
            ));
        }
        self.expect_char('.', "to terminate statement")?;
        let mut line = format!(
            "{} {} {}",
            subject.token(),
            predicate.token(),
            object.token()
        );
        if let Some(graph) = graph {
            line.push(' ');
            line.push_str(&graph.token());
        }
        line.push_str(" .");
        self.nquads.push(line);
        Ok(())
    }
}

/// Parse TriG text into a canonical GTS file.
pub fn from_trig(text: &str) -> Result<Vec<u8>, TriGParseError> {
    let nquads = Parser::new(text).parse()?;
    from_nquads(&nquads).map_err(|err| TriGParseError::new(err.to_string()))
}