mrs-tptp 0.2.2

A robust TPTP parser for Rust using winnow
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
//! Lexer/tokenizer for TPTP.
//!
//! This module provides low-level parsers for TPTP tokens: words, numbers,
//! strings, comments, and whitespace.

#[cfg(feature = "cancellation")]
use std::sync::atomic::{AtomicBool, Ordering};
use winnow::ascii::digit1;
use winnow::combinator::{alt, delimited, opt, preceded};
use winnow::error::ContextError;
use winnow::prelude::*;
use winnow::token::{one_of, take_while};

use crate::ast::{AtomicWord, Comment, DefinedWord, Name, Number, SystemWord};

/// Result type for internal parsing (with modal error handling)
pub type PResult<O> = winnow::error::ModalResult<O, ContextError>;

// Thread-local cancellation flag for cooperative cancellation
#[cfg(feature = "cancellation")]
thread_local! {
    static CANCEL_FLAG: std::cell::Cell<Option<*const AtomicBool>> = const { std::cell::Cell::new(None) };
}

/// Set the cancellation flag for the current thread.
/// The flag should remain valid for the duration of parsing.
///
/// # Safety
/// The caller must ensure the AtomicBool lives at least as long as parsing runs.
#[cfg(feature = "cancellation")]
pub fn set_cancel_flag(flag: &AtomicBool) {
    CANCEL_FLAG.with(|f| f.set(Some(flag as *const AtomicBool)));
}

/// Clear the cancellation flag for the current thread.
#[cfg(feature = "cancellation")]
pub fn clear_cancel_flag() {
    CANCEL_FLAG.with(|f| f.set(None));
}

/// Check if cancellation has been requested.
/// Returns true if cancelled, false otherwise.
#[cfg(feature = "cancellation")]
#[inline]
pub fn is_cancelled() -> bool {
    CANCEL_FLAG.with(|f| {
        if let Some(ptr) = f.get() {
            // SAFETY: The caller of set_cancel_flag guarantees the pointer is valid
            unsafe { (*ptr).load(Ordering::Relaxed) }
        } else {
            false
        }
    })
}

/// Check cancellation and return an error if cancelled.
/// This should be called periodically in parsing loops.
#[inline(always)]
pub fn check_cancel(_input: &mut &str) -> PResult<()> {
    #[cfg(feature = "cancellation")]
    if is_cancelled() {
        return Err(winnow::error::ErrMode::Cut(ContextError::new()));
    }
    Ok(())
}

/// Skip whitespace and comments -- hand-optimized fast path
#[inline(always)]
pub fn ws(input: &mut &str) -> PResult<()> {
    loop {
        // Fast skip ASCII whitespace
        let bytes = input.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            match bytes[i] {
                b' ' | b'\t' | b'\n' | b'\r' => i += 1,
                _ => break,
            }
        }
        if i > 0 {
            *input = &input[i..];
        }

        let bytes = input.as_bytes();
        if bytes.first() == Some(&b'%') {
            // Line comment: skip to newline
            let mut j = 1;
            while j < bytes.len() && bytes[j] != b'\n' {
                j += 1;
            }
            if j < bytes.len() {
                j += 1; // skip the newline
            }
            *input = &input[j..];
        } else if bytes.len() >= 2 && bytes[0] == b'/' && bytes[1] == b'*' {
            // Block comment: use existing parser
            block_comment.void().parse_next(input)?;
        } else {
            break;
        }
    }
    Ok(())
}

/// Parse a line comment: % ... newline
pub fn line_comment<'a>(input: &mut &'a str) -> PResult<Comment<'a>> {
    let content = preceded('%', take_while(0.., |c| c != '\n' && c != '\r')).parse_next(input)?;

    // Consume the newline if present
    opt(one_of(['\n', '\r'])).parse_next(input)?;

    Ok(Comment {
        content,
        is_block: false,
    })
}

/// Parse a block comment: /* ... */
pub fn block_comment<'a>(input: &mut &'a str) -> PResult<Comment<'a>> {
    "/*".parse_next(input)?;

    let start = *input;
    let mut depth = 1;

    while depth > 0 && !input.is_empty() {
        if input.starts_with("*/") {
            depth -= 1;
            if depth == 0 {
                let content = &start[..start.len() - input.len()];
                "*/".parse_next(input)?;
                return Ok(Comment {
                    content,
                    is_block: true,
                });
            }
            *input = &input[2..];
        } else if input.starts_with("/*") {
            depth += 1;
            *input = &input[2..];
        } else {
            *input = &input[1..];
        }
    }

    Err(winnow::error::ErrMode::Cut(
        winnow::error::ContextError::new(),
    ))
}

/// Parse a lower_word: [a-z][a-zA-Z0-9_']*
#[inline]
pub fn lower_word<'a>(input: &mut &'a str) -> PResult<&'a str> {
    (
        one_of('a'..='z'),
        take_while(0.., |c: char| {
            c.is_ascii_alphanumeric() || c == '_' || c == '\''
        }),
    )
        .take()
        .parse_next(input)
}

/// Parse an upper_word (variable): [A-Z][a-zA-Z0-9_']*
#[inline]
pub fn upper_word<'a>(input: &mut &'a str) -> PResult<&'a str> {
    (
        one_of('A'..='Z'),
        take_while(0.., |c: char| {
            c.is_ascii_alphanumeric() || c == '_' || c == '\''
        }),
    )
        .take()
        .parse_next(input)
}

/// Parse a single-quoted string: 'content'
/// Handles escape sequences: \' and \\
#[inline]
pub fn single_quoted<'a>(input: &mut &'a str) -> PResult<&'a str> {
    delimited('\'', single_quoted_content, '\'').parse_next(input)
}

fn single_quoted_content<'a>(input: &mut &'a str) -> PResult<&'a str> {
    let start = *input;

    loop {
        // Take non-special characters
        take_while(0.., |c| c != '\'' && c != '\\').parse_next(input)?;

        if input.starts_with('\'') {
            // End of string
            let len = start.len() - input.len();
            return Ok(&start[..len]);
        } else if input.starts_with('\\') {
            // Escape sequence
            *input = &input[1..];
            if !input.is_empty() {
                *input = &input[1..];
            } else {
                return Err(winnow::error::ErrMode::Cut(
                    winnow::error::ContextError::new(),
                ));
            }
        } else {
            // Unexpected end
            return Err(winnow::error::ErrMode::Cut(
                winnow::error::ContextError::new(),
            ));
        }
    }
}

/// Parse a distinct object: "content"
/// Handles escape sequences: \" and \\
#[inline]
pub fn distinct_object<'a>(input: &mut &'a str) -> PResult<&'a str> {
    delimited('"', distinct_object_content, '"').parse_next(input)
}

fn distinct_object_content<'a>(input: &mut &'a str) -> PResult<&'a str> {
    let start = *input;

    loop {
        take_while(0.., |c| c != '"' && c != '\\').parse_next(input)?;

        if input.starts_with('"') {
            let len = start.len() - input.len();
            return Ok(&start[..len]);
        } else if input.starts_with('\\') {
            *input = &input[1..];
            if !input.is_empty() {
                *input = &input[1..];
            } else {
                return Err(winnow::error::ErrMode::Cut(
                    winnow::error::ContextError::new(),
                ));
            }
        } else {
            return Err(winnow::error::ErrMode::Cut(
                winnow::error::ContextError::new(),
            ));
        }
    }
}

/// Parse a dollar_word: $[a-z][a-zA-Z0-9_]*
#[inline]
pub fn dollar_word<'a>(input: &mut &'a str) -> PResult<&'a str> {
    preceded(
        '$',
        (
            one_of('a'..='z'),
            take_while(0.., |c: char| c.is_ascii_alphanumeric() || c == '_'),
        )
            .take(),
    )
    .parse_next(input)
}

/// Parse a dollar_dollar_word: $$[a-z][a-zA-Z0-9_]*
#[inline]
pub fn dollar_dollar_word<'a>(input: &mut &'a str) -> PResult<&'a str> {
    preceded(
        "$$",
        (
            one_of('a'..='z'),
            take_while(0.., |c: char| c.is_ascii_alphanumeric() || c == '_'),
        )
            .take(),
    )
    .parse_next(input)
}

/// Parse an atomic word
#[inline]
pub fn atomic_word<'a>(input: &mut &'a str) -> PResult<AtomicWord<'a>> {
    alt((
        single_quoted.map(AtomicWord::SingleQuoted),
        lower_word.map(AtomicWord::Lower),
    ))
    .parse_next(input)
}

/// Parse a defined word as DefinedWord
#[inline]
pub fn defined_word<'a>(input: &mut &'a str) -> PResult<DefinedWord<'a>> {
    dollar_word.map(DefinedWord).parse_next(input)
}

/// Parse a system word as SystemWord
#[inline]
pub fn system_word<'a>(input: &mut &'a str) -> PResult<SystemWord<'a>> {
    dollar_dollar_word.map(SystemWord).parse_next(input)
}

/// Parse an integer (with optional sign) - returns string to handle arbitrary precision
pub fn integer_str(input: &mut &str) -> PResult<String> {
    let sign = opt(one_of(['+', '-'])).parse_next(input)?;
    let digits: &str = digit1.parse_next(input)?;

    let sign_str = if sign == Some('-') { "-" } else { "" };
    Ok(format!("{}{}", sign_str, digits))
}

/// Parse an integer (with optional sign) - for backward compatibility
pub fn integer(input: &mut &str) -> PResult<i64> {
    let sign = opt(one_of(['+', '-'])).parse_next(input)?;
    let digits: &str = digit1.parse_next(input)?;

    let value: i64 = digits
        .parse()
        .map_err(|_| winnow::error::ErrMode::Cut(winnow::error::ContextError::new()))?;

    Ok(if sign == Some('-') { -value } else { value })
}

/// Parse a number (integer, rational, or real) -- zero-copy
#[inline]
pub fn number<'a>(input: &mut &'a str) -> PResult<Number<'a>> {
    let start = *input;

    // Parse the initial sign and digits
    opt(one_of(['+', '-'])).parse_next(input)?;
    digit1.parse_next(input)?;

    // Check what follows
    if input.starts_with('/') {
        // Rational: integer/positive_integer
        *input = &input[1..];
        digit1.parse_next(input)?;
        let len = input.as_ptr() as usize - start.as_ptr() as usize;
        Ok(Number::Rational(&start[..len]))
    } else if input.starts_with('.') {
        // Real: decimal
        *input = &input[1..];
        digit1.parse_next(input)?;
        // Check for exponent
        if input.starts_with(['e', 'E']) {
            *input = &input[1..];
            opt(one_of(['+', '-'])).parse_next(input)?;
            digit1.parse_next(input)?;
        }
        let len = input.as_ptr() as usize - start.as_ptr() as usize;
        Ok(Number::Real(&start[..len]))
    } else if input.starts_with(['e', 'E']) {
        // Real with exponent but no decimal
        *input = &input[1..];
        opt(one_of(['+', '-'])).parse_next(input)?;
        digit1.parse_next(input)?;
        let len = input.as_ptr() as usize - start.as_ptr() as usize;
        Ok(Number::Real(&start[..len]))
    } else {
        // Integer
        let len = input.as_ptr() as usize - start.as_ptr() as usize;
        Ok(Number::Integer(&start[..len]))
    }
}

/// Parse a name (lower_word, single_quoted, or integer for anonymous)
#[inline]
pub fn name<'a>(input: &mut &'a str) -> PResult<Name<'a>> {
    alt((
        lower_word.map(Name::Lower),
        single_quoted.map(Name::SingleQuoted),
        // Anonymous names can be integers
        digit1.map(Name::Integer),
    ))
    .parse_next(input)
}

/// Parse a variable name
#[inline]
pub fn variable<'a>(input: &mut &'a str) -> PResult<&'a str> {
    upper_word.parse_next(input)
}

/// Parse a specific keyword, ensuring it's not part of a larger word
pub fn keyword<'a>(kw: &'static str) -> impl FnMut(&mut &'a str) -> PResult<&'a str> {
    move |input: &mut &'a str| {
        let result: &str = winnow::token::literal(kw).parse_next(input)?;
        // Ensure we're not in the middle of a word
        if let Some(c) = input.chars().next()
            && (c.is_ascii_alphanumeric() || c == '_')
        {
            return Err(winnow::error::ErrMode::Backtrack(
                winnow::error::ContextError::new(),
            ));
        }
        Ok(result)
    }
}

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

    #[test]
    fn test_lower_word() {
        assert_eq!(lower_word.parse_peek("hello"), Ok(("", "hello")));
        assert_eq!(lower_word.parse_peek("camelCase"), Ok(("", "camelCase")));
        assert_eq!(
            lower_word.parse_peek("with_underscore"),
            Ok(("", "with_underscore"))
        );
        assert_eq!(lower_word.parse_peek("x123"), Ok(("", "x123")));
        assert!(lower_word.parse_peek("123").is_err());
        assert!(lower_word.parse_peek("Upper").is_err());
    }

    #[test]
    fn test_upper_word() {
        assert_eq!(upper_word.parse_peek("X"), Ok(("", "X")));
        assert_eq!(upper_word.parse_peek("Variable"), Ok(("", "Variable")));
        assert_eq!(upper_word.parse_peek("VAR_1"), Ok(("", "VAR_1")));
        assert!(upper_word.parse_peek("lower").is_err());
    }

    #[test]
    fn test_single_quoted() {
        assert_eq!(single_quoted.parse_peek("'hello'"), Ok(("", "hello")));
        assert_eq!(
            single_quoted.parse_peek("'with space'"),
            Ok(("", "with space"))
        );
        assert_eq!(
            single_quoted.parse_peek("'escaped\\'quote'"),
            Ok(("", "escaped\\'quote"))
        );
    }

    #[test]
    fn test_distinct_object() {
        assert_eq!(distinct_object.parse_peek("\"object\""), Ok(("", "object")));
        assert_eq!(
            distinct_object.parse_peek("\"with space\""),
            Ok(("", "with space"))
        );
    }

    #[test]
    fn test_dollar_word() {
        assert_eq!(dollar_word.parse_peek("$true"), Ok(("", "true")));
        assert_eq!(dollar_word.parse_peek("$false"), Ok(("", "false")));
        assert_eq!(dollar_word.parse_peek("$ite"), Ok(("", "ite")));
    }

    #[test]
    fn test_number() {
        assert_eq!(number.parse_peek("42"), Ok(("", Number::Integer("42"))));
        assert_eq!(number.parse_peek("-17"), Ok(("", Number::Integer("-17"))));
        assert_eq!(number.parse_peek("3/4"), Ok(("", Number::Rational("3/4"))));

        assert_eq!(number.parse_peek("3.14"), Ok(("", Number::Real("3.14"))));
        assert_eq!(
            number.parse_peek("1.0e10"),
            Ok(("", Number::Real("1.0e10")))
        );
    }

    #[test]
    fn test_line_comment() {
        let result = line_comment.parse_peek("% this is a comment\n");
        assert!(result.is_ok());
        let (_rest, comment) = result.unwrap();
        assert_eq!(comment.content, " this is a comment");
        assert!(!comment.is_block);
    }

    #[test]
    fn test_block_comment() {
        let result = block_comment.parse_peek("/* block comment */");
        assert!(result.is_ok());
        let (_rest, comment) = result.unwrap();
        assert_eq!(comment.content, " block comment ");
        assert!(comment.is_block);
    }
}