athene-owlapi 0.0.1

This package provides a Rust API for OWL that adheres as closely as possible to the OWL 2 Web Ontology Language Structural Specification.
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
//!
//! Stage 1 lexer for the OWL 2 functional-style syntax.
//!
use crate::{
    reader::{
        ast::{Position, PrefixedIriRef, Span},
        error::ParseError,
    },
    syntax::{
        ANONYMOUS_NAMESPACE, DELIM_COMMENT_START, DELIM_FN_ARGS_END, DELIM_FN_ARGS_START,
        DELIM_IRI_END, DELIM_IRI_START, DELIM_LITERAL_DATATYPE, DELIM_LITERAL_LANGUAGE,
        DELIM_PREFIX_ASSIGN, DELIM_QUOTED_STRING, NAMESPACE_NAME_SEPARATOR,
    },
};

#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, format, string::String, vec::Vec};

// ------------------------------------------------------------------------------------------------
// Crate Public Types
// ------------------------------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum TokenKind {
    /// `<http://example.org/>`
    FullIri(String),
    /// `owl:Thing` or `:local`
    PrefixedName(PrefixedIriRef),
    /// `owl:` or `:` — prefix name without a local part
    Namespace(Option<String>),
    /// `_:name`
    NodeId(String),
    /// Bare name / keyword / function name
    Name(String),
    /// `"…"` with escape sequences resolved
    QuotedString(String),
    /// Language tag text (without the leading `@`)
    LangTag(String),
    /// `^^`
    DataTypeSep,
    /// Non-negative integer
    Integer(u32),
    /// `=` (used in `Prefix(…)` declarations)
    Equals,
    LParen,
    RParen,
    /// Line comment text (trimmed, without the leading `#`)
    Comment(String),
    Eof,
}

#[derive(Clone, Debug)]
pub(crate) struct Token {
    pub kind: TokenKind,
    pub span: Span,
}

pub(crate) struct Lexer {
    chars: Vec<char>,
    pos: usize,
    /// Byte offset of `chars[pos]` from the start of the input.
    byte_offset: u32,
    line: u32,
    column: u32,
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Lexer {
    pub(super) fn new(input: &str) -> Self {
        Self {
            chars: input.chars().collect(),
            pos: 0,
            byte_offset: 0,
            line: 1,
            column: 1,
        }
    }

    // ── Position helpers ──────────────────────────────────────────────────────

    fn current_position(&self) -> Position {
        Position {
            line: self.line,
            column: self.column,
            offset: self.byte_offset,
        }
    }

    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    fn peek2(&self) -> Option<char> {
        self.chars.get(self.pos + 1).copied()
    }

    /// Advance past the current character and return it.
    fn advance(&mut self) -> Option<char> {
        let ch = self.chars.get(self.pos).copied()?;
        self.pos += 1;
        self.byte_offset += ch.len_utf8() as u32;
        if ch == '\n' {
            self.line += 1;
            self.column = 1;
        } else {
            self.column += 1;
        }
        Some(ch)
    }

    fn skip_whitespace(&mut self) {
        while matches!(self.peek(), Some(c) if c.is_ascii_whitespace()) {
            self.advance();
        }
    }

    fn make_token(&self, kind: TokenKind, start: Position) -> Token {
        Token {
            kind,
            span: Span::new(start, self.current_position()),
        }
    }

    // ── Public interface ──────────────────────────────────────────────────────

    pub(super) fn next_token(&mut self) -> Result<Token, ParseError> {
        self.skip_whitespace();

        let start = self.current_position();

        let ch = match self.peek() {
            None => return Ok(self.make_token(TokenKind::Eof, start)),
            Some(c) => c,
        };

        match ch {
            DELIM_FN_ARGS_START => {
                self.advance();
                Ok(self.make_token(TokenKind::LParen, start))
            }
            DELIM_FN_ARGS_END => {
                self.advance();
                Ok(self.make_token(TokenKind::RParen, start))
            }
            DELIM_PREFIX_ASSIGN => {
                self.advance();
                Ok(self.make_token(TokenKind::Equals, start))
            }
            DELIM_COMMENT_START => self.lex_comment(start),
            DELIM_IRI_START => self.lex_full_iri(start),
            DELIM_QUOTED_STRING => self.lex_quoted_string(start),
            DELIM_LITERAL_LANGUAGE => self.lex_lang_tag(start),
            DELIM_LITERAL_DATATYPE => self.lex_datatype_sep(start),
            ANONYMOUS_NAMESPACE if self.peek2() == Some(NAMESPACE_NAME_SEPARATOR) => {
                self.lex_node_id(start)
            }
            NAMESPACE_NAME_SEPARATOR => {
                // Default-prefix bare namespace ":"
                self.advance(); // consume ':'
                if self.peek().map_or(false, is_pn_local_start) {
                    let local = self.read_pn_local();
                    Ok(self.make_token(
                        TokenKind::PrefixedName(PrefixedIriRef {
                            prefix: None,
                            local,
                        }),
                        start,
                    ))
                } else {
                    Ok(self.make_token(TokenKind::Namespace(None), start))
                }
            }
            c if is_name_start(c) => self.lex_name_or_prefixed(start),
            c if c.is_ascii_digit() => self.lex_integer(start),
            c => {
                self.advance();
                Err(ParseError::UnexpectedChar {
                    ch: c,
                    span: Span::at(start),
                })
            }
        }
    }

    // ── Lexer sub-routines ────────────────────────────────────────────────────

    fn lex_comment(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // consume '#'
        let mut text = String::new();
        while let Some(c) = self.peek() {
            if c == '\n' {
                break;
            }
            text.push(c);
            self.advance();
        }
        Ok(self.make_token(TokenKind::Comment(text.trim().to_owned()), start))
    }

    fn lex_full_iri(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // consume '<'
        let mut iri = String::new();
        loop {
            match self.peek() {
                None => {
                    return Err(ParseError::UnclosedString {
                        span: Span::at(start),
                    });
                }
                Some(DELIM_IRI_END) => {
                    self.advance();
                    break;
                }
                Some('\\') => {
                    self.advance();
                    match self.advance() {
                        Some(c) => iri.push(c),
                        None => {
                            return Err(ParseError::UnclosedString {
                                span: Span::at(start),
                            });
                        }
                    }
                }
                Some(c) => {
                    iri.push(c);
                    self.advance();
                }
            }
        }
        Ok(self.make_token(TokenKind::FullIri(iri), start))
    }

    fn lex_quoted_string(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // consume '"'
        let mut s = String::new();
        loop {
            match self.peek() {
                None => {
                    return Err(ParseError::UnclosedString {
                        span: Span::at(start),
                    });
                }
                Some(DELIM_QUOTED_STRING) => {
                    self.advance();
                    break;
                }
                Some('\\') => {
                    self.advance();
                    let escaped = match self.advance() {
                        None => {
                            return Err(ParseError::UnclosedString {
                                span: Span::at(start),
                            });
                        }
                        Some('n') => '\n',
                        Some('t') => '\t',
                        Some('r') => '\r',
                        Some('"') => '"',
                        Some('\\') => '\\',
                        Some('u') => self.read_unicode_escape(start, 4)?,
                        Some('U') => self.read_unicode_escape(start, 8)?,
                        Some(c) => c,
                    };
                    s.push(escaped);
                }
                Some(c) => {
                    s.push(c);
                    self.advance();
                }
            }
        }
        Ok(self.make_token(TokenKind::QuotedString(s), start))
    }

    fn read_unicode_escape(&mut self, start: Position, digits: usize) -> Result<char, ParseError> {
        let mut code = 0u32;
        for _ in 0..digits {
            match self.advance() {
                Some(c) if c.is_ascii_hexdigit() => {
                    code = code * 16 + c.to_digit(16).unwrap();
                }
                _ => {
                    return Err(ParseError::InvalidArgument {
                        message: "invalid unicode escape".to_owned(),
                        span: Span::at(start),
                    });
                }
            }
        }
        char::from_u32(code).ok_or_else(|| ParseError::InvalidArgument {
            message: format!("invalid unicode code point U+{code:X}"),
            span: Span::at(start),
        })
    }

    fn lex_lang_tag(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // consume '@'
        let mut tag = String::new();
        while let Some(c) = self.peek() {
            if c.is_alphanumeric() || c == '-' {
                tag.push(c);
                self.advance();
            } else {
                break;
            }
        }
        if tag.is_empty() {
            return Err(ParseError::InvalidArgument {
                message: "empty language tag".to_owned(),
                span: Span::at(start),
            });
        }
        Ok(self.make_token(TokenKind::LangTag(tag), start))
    }

    fn lex_datatype_sep(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // consume first '^'
        match self.peek() {
            Some(DELIM_LITERAL_DATATYPE) => {
                self.advance();
                Ok(self.make_token(TokenKind::DataTypeSep, start))
            }
            Some(c) => Err(ParseError::UnexpectedChar {
                ch: c,
                span: self.current_span(),
            }),
            None => Err(ParseError::UnexpectedEof { expected: "^" }),
        }
    }

    fn lex_node_id(&mut self, start: Position) -> Result<Token, ParseError> {
        self.advance(); // '_'
        self.advance(); // ':'
        let name = self.read_pn_local();
        Ok(self.make_token(TokenKind::NodeId(name), start))
    }

    /// Lex a bare name, which may be:
    /// - `prefix:local` → `PrefixedName`
    /// - `prefix:` (no local) → `Namespace`
    /// - `name` (no colon) → `Name`
    /// - a non-negative integer → `Integer`
    fn lex_name_or_prefixed(&mut self, start: Position) -> Result<Token, ParseError> {
        let name = self.read_name();

        match self.peek() {
            Some(NAMESPACE_NAME_SEPARATOR) => {
                self.advance(); // consume ':'
                // Decide: namespace or prefixed name
                if self.peek().map_or(false, is_pn_local_start) {
                    let local = self.read_pn_local();
                    Ok(self.make_token(
                        TokenKind::PrefixedName(PrefixedIriRef {
                            prefix: Some(name),
                            local,
                        }),
                        start,
                    ))
                } else {
                    Ok(self.make_token(TokenKind::Namespace(Some(name)), start))
                }
            }
            _ => Ok(self.make_token(TokenKind::Name(name), start)),
        }
    }

    fn lex_integer(&mut self, start: Position) -> Result<Token, ParseError> {
        let mut s = String::new();
        while let Some(c) = self.peek() {
            if c.is_ascii_digit() {
                s.push(c);
                self.advance();
            } else {
                break;
            }
        }
        let n: u32 = s.parse().map_err(|_| ParseError::InvalidArgument {
            message: format!("integer {s:?} out of range"),
            span: Span::at(start),
        })?;
        Ok(self.make_token(TokenKind::Integer(n), start))
    }

    // ── String readers ────────────────────────────────────────────────────────

    fn read_name(&mut self) -> String {
        let mut s = String::new();
        while let Some(c) = self.peek() {
            if is_name_char(c) {
                s.push(c);
                self.advance();
            } else {
                break;
            }
        }
        s
    }

    /// Read the local part of a prefixed name (after `prefix:`).
    fn read_pn_local(&mut self) -> String {
        let mut s = String::new();
        while let Some(c) = self.peek() {
            if is_pn_local_char(c) {
                s.push(c);
                self.advance();
            } else {
                break;
            }
        }
        s
    }

    fn current_span(&self) -> Span {
        let p = self.current_position();
        Span::at(p)
    }
}

// ------------------------------------------------------------------------------------------------
// Private Functions  ❯ Character classification
// ------------------------------------------------------------------------------------------------

fn is_name_start(c: char) -> bool {
    c.is_alphabetic() || c == '_'
}

fn is_name_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_' || c == '-'
}

fn is_pn_local_start(c: char) -> bool {
    c.is_alphanumeric() || c == '_' || c == '%' || c == '\\'
}

fn is_pn_local_char(c: char) -> bool {
    c.is_alphanumeric() || matches!(c, '_' | '-' | '.' | ':' | '%' | '\\')
}