oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Generic parsing toolkit for RDF formats
//!
//! This module provides reusable parsing infrastructure including lexers, parsers,
//! and error handling utilities adapted from OxiGraph's parsing toolkit.

use super::error::{ParseResult, RdfParseError, TextPosition};
use std::io::BufRead;

/// A generic lexer that tokenizes input streams
#[allow(dead_code)]
pub struct Lexer<B, TR> {
    buffer: B,
    tokenizer: TR,
    position: TextPosition,
    current_char: Option<char>,
    peek_char: Option<char>,
}

/// Trait for tokenizing rules
pub trait TokenRecognizer {
    type Token;

    /// Recognize the next token from the current position
    fn recognize_next_token(
        &mut self,
        buffer: &mut dyn BufferProvider,
        position: &mut TextPosition,
    ) -> ParseResult<Option<Self::Token>>;
}

/// Trait for parsing rules that build AST nodes
pub trait RuleRecognizer<Node> {
    /// Recognize the next rule/node from token stream
    fn recognize_next_node<Token>(
        &mut self,
        parser: &mut Parser<Token>,
    ) -> ParseResult<Option<Node>>;
}

/// Buffer provider trait for reading characters
pub trait BufferProvider {
    /// Get the current character without advancing
    fn current(&self) -> Option<char>;

    /// Get the next character without advancing
    fn peek(&self) -> Option<char>;

    /// Advance to the next character and return it
    fn advance(&mut self) -> Option<char>;

    /// Get current position for error reporting
    fn position(&self) -> &TextPosition;

    /// Update position tracking
    fn update_position(&mut self, ch: char);
}

/// String buffer implementation
pub struct StringBuffer {
    content: String,
    position: TextPosition,
    current: Option<char>,
    peek: Option<char>,
    char_position: usize, // Current position in chars
}

impl StringBuffer {
    pub fn new(content: String) -> Self {
        let mut buffer = Self {
            content,
            position: TextPosition::start(),
            current: None,
            peek: None,
            char_position: 0,
        };
        // Initialize current and peek
        buffer.current = buffer.get_char_at(0);
        buffer.peek = buffer.get_char_at(1);
        buffer
    }

    fn get_char_at(&self, index: usize) -> Option<char> {
        self.content.chars().nth(index)
    }
}

impl BufferProvider for StringBuffer {
    fn current(&self) -> Option<char> {
        self.current
    }

    fn peek(&self) -> Option<char> {
        self.peek
    }

    fn advance(&mut self) -> Option<char> {
        if let Some(ch) = self.current {
            self.update_position(ch);
        }

        self.current = self.peek;
        self.char_position += 1;
        self.peek = self.get_char_at(self.char_position + 1);
        self.current
    }

    fn position(&self) -> &TextPosition {
        &self.position
    }

    fn update_position(&mut self, ch: char) {
        match ch {
            '\n' => {
                self.position.line += 1;
                self.position.column = 1;
                self.position.offset += 1;
            }
            '\r' => {
                // Handle Windows line endings
                if self.peek == Some('\n') {
                    // Don't increment position for \r if followed by \n
                } else {
                    self.position.line += 1;
                    self.position.column = 1;
                }
                self.position.offset += 1;
            }
            _ => {
                self.position.column += 1;
                self.position.offset += 1;
            }
        }
    }
}

/// Reader buffer implementation for streaming
pub struct ReaderBuffer<R: BufRead> {
    reader: R,
    position: TextPosition,
    current: Option<char>,
    peek: Option<char>,
    char_buffer: Vec<char>,
    buffer_pos: usize,
}

impl<R: BufRead> ReaderBuffer<R> {
    pub fn new(reader: R) -> ParseResult<Self> {
        let mut buffer = Self {
            reader,
            position: TextPosition::start(),
            current: None,
            peek: None,
            char_buffer: Vec::new(),
            buffer_pos: 0,
        };

        buffer.fill_buffer()?;
        buffer.advance(); // Load first character
        Ok(buffer)
    }

    fn fill_buffer(&mut self) -> ParseResult<()> {
        let mut line = String::new();
        match self.reader.read_line(&mut line) {
            Ok(0) => Ok(()), // EOF
            Ok(_) => {
                self.char_buffer.extend(line.chars());
                Ok(())
            }
            Err(e) => Err(RdfParseError::Io(e)),
        }
    }

    #[allow(dead_code)]
    fn ensure_chars_available(&mut self) -> ParseResult<()> {
        if self.buffer_pos + 1 >= self.char_buffer.len() {
            self.fill_buffer()?;
        }
        Ok(())
    }
}

impl<R: BufRead> BufferProvider for ReaderBuffer<R> {
    fn current(&self) -> Option<char> {
        self.current
    }

    fn peek(&self) -> Option<char> {
        self.peek
    }

    fn advance(&mut self) -> Option<char> {
        if let Some(ch) = self.current {
            self.update_position(ch);
        }

        self.current = self.peek;

        // Try to get next peek character
        self.buffer_pos += 1;
        if self.buffer_pos < self.char_buffer.len() {
            self.peek = Some(self.char_buffer[self.buffer_pos]);
        } else {
            // Try to read more
            if self.fill_buffer().is_ok() && self.buffer_pos < self.char_buffer.len() {
                self.peek = Some(self.char_buffer[self.buffer_pos]);
            } else {
                self.peek = None;
            }
        }

        self.current
    }

    fn position(&self) -> &TextPosition {
        &self.position
    }

    fn update_position(&mut self, ch: char) {
        match ch {
            '\n' => {
                self.position.line += 1;
                self.position.column = 1;
                self.position.offset += 1;
            }
            '\r' => {
                if self.peek == Some('\n') {
                    // Don't increment position for \r if followed by \n
                } else {
                    self.position.line += 1;
                    self.position.column = 1;
                }
                self.position.offset += 1;
            }
            _ => {
                self.position.column += 1;
                self.position.offset += 1;
            }
        }
    }
}

impl<B: BufferProvider, TR> Lexer<B, TR> {
    pub fn new(buffer: B, tokenizer: TR) -> Self {
        Self {
            buffer,
            tokenizer,
            position: TextPosition::start(),
            current_char: None,
            peek_char: None,
        }
    }
}

impl<B: BufferProvider, TR: TokenRecognizer> Lexer<B, TR> {
    /// Get the next token from the input
    pub fn next_token(&mut self) -> ParseResult<Option<TR::Token>> {
        self.tokenizer
            .recognize_next_token(&mut self.buffer, &mut self.position)
    }

    /// Get current position for error reporting
    pub fn position(&self) -> &TextPosition {
        self.buffer.position()
    }
}

/// Generic parser combining lexer with grammar rules
pub struct Parser<Token> {
    tokens: Vec<Token>,
    position: usize,
}

impl<Token> Parser<Token> {
    pub fn new(tokens: Vec<Token>) -> Self {
        Self {
            tokens,
            position: 0,
        }
    }

    /// Peek at current token without consuming
    pub fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.position)
    }

    /// Advance and return current token
    pub fn next_token(&mut self) -> Option<&Token> {
        if self.position < self.tokens.len() {
            let token = &self.tokens[self.position];
            self.position += 1;
            Some(token)
        } else {
            None
        }
    }

    /// Check if we're at the end of input
    pub fn is_at_end(&self) -> bool {
        self.position >= self.tokens.len()
    }

    /// Get current position in token stream
    pub fn token_position(&self) -> usize {
        self.position
    }

    /// Reset to a previous position (for backtracking)
    pub fn reset_to(&mut self, pos: usize) {
        self.position = pos.min(self.tokens.len());
    }
}

/// Utility functions for character classification
pub mod char_utils {
    /// Check if character is whitespace in Turtle/N3
    pub fn is_whitespace(ch: char) -> bool {
        matches!(ch, ' ' | '\t' | '\n' | '\r')
    }

    /// Check if character can start an IRI
    pub fn is_iri_start(ch: char) -> bool {
        ch == '<'
    }

    /// Check if character can be in an IRI
    pub fn is_iri_char(ch: char) -> bool {
        !matches!(
            ch,
            '<' | '>' | '"' | '{' | '}' | '|' | '^' | '`' | '\\' | '\x00'..='\x20'
        )
    }

    /// Check if character can start a blank node
    pub fn is_blank_node_start(ch: char) -> bool {
        ch == '_'
    }

    /// Check if character can start a prefix name
    pub fn is_pn_chars_base(ch: char) -> bool {
        matches!(ch, 'A'..='Z' | 'a'..='z' | '\u{00C0}'..='\u{00D6}' | '\u{00D8}'..='\u{00F6}' | '\u{00F8}'..='\u{02FF}' | '\u{0370}'..='\u{037D}' | '\u{037F}'..='\u{1FFF}' | '\u{200C}'..='\u{200D}' | '\u{2070}'..='\u{218F}' | '\u{2C00}'..='\u{2FEF}' | '\u{3001}'..='\u{D7FF}' | '\u{F900}'..='\u{FDCF}' | '\u{FDF0}'..='\u{FFFD}')
    }

    /// Check if character can be in a prefix name (after first character)
    pub fn is_pn_chars(ch: char) -> bool {
        is_pn_chars_base(ch)
            || matches!(ch, '-' | '0'..='9' | '\u{00B7}' | '\u{0300}'..='\u{036F}' | '\u{203F}'..='\u{2040}')
    }

    /// Check if character can start a numeric literal
    pub fn is_numeric_start(ch: char) -> bool {
        matches!(ch, '0'..='9' | '+' | '-' | '.')
    }

    /// Check if character is a digit
    pub fn is_digit(ch: char) -> bool {
        ch.is_ascii_digit()
    }

    /// Check if character is hexadecimal
    pub fn is_hex_digit(ch: char) -> bool {
        ch.is_ascii_hexdigit()
    }
}

/// Utility functions for string processing
pub mod string_utils {
    use super::ParseResult;
    use crate::format::error::{RdfParseError, RdfSyntaxError, TextPosition};

    /// Unescape string literal with Turtle escape sequences
    pub fn unescape_string(input: &str, position: &TextPosition) -> ParseResult<String> {
        let mut result = String::new();
        let mut chars = input.chars();

        while let Some(ch) = chars.next() {
            if ch == '\\' {
                match chars.next() {
                    Some('t') => result.push('\t'),
                    Some('n') => result.push('\n'),
                    Some('r') => result.push('\r'),
                    Some('b') => result.push('\u{0008}'),
                    Some('f') => result.push('\u{000C}'),
                    Some('"') => result.push('"'),
                    Some('\'') => result.push('\''),
                    Some('\\') => result.push('\\'),
                    Some('u') => {
                        // Unicode escape \uXXXX
                        let mut unicode_chars = String::new();
                        for _ in 0..4 {
                            match chars.next() {
                                Some(c) if c.is_ascii_hexdigit() => unicode_chars.push(c),
                                _ => {
                                    return Err(RdfParseError::Syntax(
                                        RdfSyntaxError::with_position(
                                            "Invalid Unicode escape sequence".to_string(),
                                            *position,
                                        ),
                                    ))
                                }
                            }
                        }
                        let code_point = u32::from_str_radix(&unicode_chars, 16).map_err(|_| {
                            RdfParseError::Syntax(RdfSyntaxError::with_position(
                                "Invalid Unicode code point".to_string(),
                                *position,
                            ))
                        })?;
                        match char::from_u32(code_point) {
                            Some(unicode_char) => result.push(unicode_char),
                            None => {
                                return Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                                    "Invalid Unicode code point".to_string(),
                                    *position,
                                )))
                            }
                        }
                    }
                    Some('U') => {
                        // Unicode escape \UXXXXXXXX
                        let mut unicode_chars = String::new();
                        for _ in 0..8 {
                            match chars.next() {
                                Some(c) if c.is_ascii_hexdigit() => unicode_chars.push(c),
                                _ => {
                                    return Err(RdfParseError::Syntax(
                                        RdfSyntaxError::with_position(
                                            "Invalid Unicode escape sequence".to_string(),
                                            *position,
                                        ),
                                    ))
                                }
                            }
                        }
                        let code_point = u32::from_str_radix(&unicode_chars, 16).map_err(|_| {
                            RdfParseError::Syntax(RdfSyntaxError::with_position(
                                "Invalid Unicode code point".to_string(),
                                *position,
                            ))
                        })?;
                        match char::from_u32(code_point) {
                            Some(unicode_char) => result.push(unicode_char),
                            None => {
                                return Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                                    "Invalid Unicode code point".to_string(),
                                    *position,
                                )))
                            }
                        }
                    }
                    Some(other) => {
                        return Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                            format!("Invalid escape sequence: \\{other}"),
                            *position,
                        )));
                    }
                    None => {
                        return Err(RdfParseError::Syntax(RdfSyntaxError::with_position(
                            "Incomplete escape sequence".to_string(),
                            *position,
                        )));
                    }
                }
            } else {
                result.push(ch);
            }
        }

        Ok(result)
    }

    /// Escape string for Turtle output
    pub fn escape_string(input: &str) -> String {
        let mut result = String::new();
        for ch in input.chars() {
            match ch {
                '\t' => result.push_str("\\t"),
                '\n' => result.push_str("\\n"),
                '\r' => result.push_str("\\r"),
                '\u{0008}' => result.push_str("\\b"),
                '\u{000C}' => result.push_str("\\f"),
                '"' => result.push_str("\\\""),
                '\\' => result.push_str("\\\\"),
                c if c.is_control() => {
                    if (c as u32) <= 0xFFFF {
                        result.push_str(&format!("\\u{:04X}", c as u32));
                    } else {
                        result.push_str(&format!("\\U{:08X}", c as u32));
                    }
                }
                c => result.push(c),
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::char_utils::*;
    use super::string_utils::*;
    use super::*;

    #[test]
    fn test_string_buffer() {
        let mut buffer = StringBuffer::new("hello\nworld".to_string());

        assert_eq!(buffer.current(), Some('h'));
        assert_eq!(buffer.peek(), Some('e'));

        buffer.advance();
        assert_eq!(buffer.current(), Some('e'));
        assert_eq!(buffer.position().column, 2);

        // Advance to newline
        for _ in 0..4 {
            buffer.advance();
        }
        assert_eq!(buffer.current(), Some('\n'));
        assert_eq!(buffer.position().line, 1);
        assert_eq!(buffer.position().column, 6);

        buffer.advance();
        assert_eq!(buffer.current(), Some('w'));
        assert_eq!(buffer.position().line, 2);
        assert_eq!(buffer.position().column, 1);
    }

    #[test]
    fn test_char_classification() {
        assert!(is_whitespace(' '));
        assert!(is_whitespace('\t'));
        assert!(is_whitespace('\n'));
        assert!(!is_whitespace('a'));

        assert!(is_iri_start('<'));
        assert!(!is_iri_start('a'));

        assert!(is_pn_chars_base('A'));
        assert!(is_pn_chars_base('z'));
        assert!(!is_pn_chars_base('1'));

        assert!(is_pn_chars('A'));
        assert!(is_pn_chars('1'));
        assert!(is_pn_chars('-'));

        assert!(is_numeric_start('1'));
        assert!(is_numeric_start('+'));
        assert!(is_numeric_start('.'));
        assert!(!is_numeric_start('a'));
    }

    #[test]
    fn test_string_escaping() {
        let position = TextPosition::start();

        // Test basic escapes
        assert_eq!(
            unescape_string("hello\\nworld", &position).expect("unescape should succeed"),
            "hello\nworld"
        );
        assert_eq!(
            unescape_string("say \\\"hello\\\"", &position).expect("unescape should succeed"),
            "say \"hello\""
        );

        // Test Unicode escapes
        assert_eq!(
            unescape_string("\\u0041", &position).expect("unescape should succeed"),
            "A"
        );
        assert_eq!(
            unescape_string("\\U00000041", &position).expect("unescape should succeed"),
            "A"
        );

        // Test escape string
        assert_eq!(escape_string("hello\nworld"), "hello\\nworld");
        assert_eq!(escape_string("say \"hello\""), "say \\\"hello\\\"");
    }
}