typeward 0.2.0

A type-driven parser combinator library for Rust.
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
use crate::error::{ParseError, ParseResult};
use crate::input::{Input, TokenStream};
use crate::prelude::Span;
use std::{cmp::Reverse, mem};

/// Domain for offset tracking units.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ParseOffsetDomain {
    /// Byte offsets (`&str`, `&[u8]`).
    Bytes,
    /// Element offsets (`TokenStream`).
    Tokens,
}

/// Anchor metadata used to compute absolute parser offsets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParseOffsetAnchor {
    domain: ParseOffsetDomain,
    start: usize,
    len: usize,
    unit_size: usize,
}

impl ParseOffsetAnchor {
    #[must_use]
    pub const fn new(
        domain: ParseOffsetDomain,
        start: usize,
        len: usize,
        unit_size: usize,
    ) -> Self {
        Self {
            domain,
            start,
            len,
            unit_size,
        }
    }
}

/// Input support required for offset tracking through parse context.
///
/// This trait lives outside of [`Input`] so parser position tracking remains an
/// opt-in concern managed by parse orchestration.
pub trait ParseOffsetInput<'a>: Input<'a> {
    /// Returns an anchor describing the current input view.
    fn parse_offset_anchor(self) -> ParseOffsetAnchor;

    /// Returns the absolute offset from a root anchor when this input is inside it.
    fn parse_offset_from(self, root: ParseOffsetAnchor) -> Option<usize>;
}

/// Parse-scoped offset context passed through parse calls.
///
/// This context is explicitly owned by the parse entry point and passed to
/// nested parsers, avoiding any global state while remaining thread-safe.
#[derive(Debug, Default, Clone)]
pub struct ParseOffsetContext {
    roots: Vec<ParseOffsetAnchor>,
}

impl ParseOffsetContext {
    #[must_use]
    pub fn new() -> Self {
        Self { roots: Vec::new() }
    }

    fn has_scope_for<'a, I>(&self, input: I) -> bool
    where
        I: ParseOffsetInput<'a>,
    {
        self.roots
            .iter()
            .copied()
            .any(|root| input.parse_offset_from(root).is_some())
    }
}

pub(crate) fn with_parse_offset_scope<'a, I, R>(
    context: &mut ParseOffsetContext,
    input: I,
    parse: impl FnOnce(&mut ParseOffsetContext) -> R,
) -> R
where
    I: ParseOffsetInput<'a>,
{
    context.roots.push(input.parse_offset_anchor());
    let result = parse(context);
    context.roots.pop();
    result
}

pub(crate) fn with_parse_offset_scope_if_missing<'a, I, R>(
    context: &mut ParseOffsetContext,
    input: I,
    parse: impl FnOnce(&mut ParseOffsetContext) -> R,
) -> R
where
    I: ParseOffsetInput<'a>,
{
    if context.has_scope_for(input) {
        parse(context)
    } else {
        with_parse_offset_scope(context, input, parse)
    }
}

pub(crate) fn current_parse_offset<'a, I>(context: &ParseOffsetContext, input: I) -> usize
where
    I: ParseOffsetInput<'a>,
{
    context
        .roots
        .iter()
        .copied()
        .filter_map(|root| input.parse_offset_from(root).map(|offset| (offset, root)))
        .min_by_key(|(_, root)| (root.len, Reverse(root.start)))
        .map_or(0, |(offset, _)| offset)
}

impl<'a> ParseOffsetInput<'a> for &'a str {
    fn parse_offset_anchor(self) -> ParseOffsetAnchor {
        ParseOffsetAnchor::new(
            ParseOffsetDomain::Bytes,
            self.as_ptr() as usize,
            self.len(),
            1,
        )
    }

    fn parse_offset_from(self, root: ParseOffsetAnchor) -> Option<usize> {
        if root.domain != ParseOffsetDomain::Bytes || root.unit_size != 1 {
            return None;
        }

        let current_start = self.as_ptr() as usize;
        let current_end = current_start.saturating_add(self.len());
        let root_end = root.start.saturating_add(root.len);

        if current_start < root.start || current_end > root_end {
            return None;
        }

        Some(current_start.saturating_sub(root.start))
    }
}

impl<'a> ParseOffsetInput<'a> for &'a [u8] {
    fn parse_offset_anchor(self) -> ParseOffsetAnchor {
        ParseOffsetAnchor::new(
            ParseOffsetDomain::Bytes,
            self.as_ptr() as usize,
            self.len(),
            1,
        )
    }

    fn parse_offset_from(self, root: ParseOffsetAnchor) -> Option<usize> {
        if root.domain != ParseOffsetDomain::Bytes || root.unit_size != 1 {
            return None;
        }

        let current_start = self.as_ptr() as usize;
        let current_end = current_start.saturating_add(self.len());
        let root_end = root.start.saturating_add(root.len);

        if current_start < root.start || current_end > root_end {
            return None;
        }

        Some(current_start.saturating_sub(root.start))
    }
}

impl<'a, T> ParseOffsetInput<'a> for TokenStream<'a, T>
where
    T: AsRef<str>,
{
    fn parse_offset_anchor(self) -> ParseOffsetAnchor {
        ParseOffsetAnchor::new(
            ParseOffsetDomain::Tokens,
            self.as_slice().as_ptr() as usize,
            self.as_slice().len(),
            mem::size_of::<T>(),
        )
    }

    fn parse_offset_from(self, root: ParseOffsetAnchor) -> Option<usize> {
        if root.domain != ParseOffsetDomain::Tokens || root.unit_size != mem::size_of::<T>() {
            return None;
        }

        let current_tokens = self.as_slice();
        let current_start = current_tokens.as_ptr() as usize;
        let current_len = current_tokens.len();

        if root.unit_size == 0 {
            if current_start != root.start || current_len > root.len {
                return None;
            }
            return Some(root.len.saturating_sub(current_len));
        }

        let byte_diff = current_start.checked_sub(root.start)?;
        if byte_diff % root.unit_size != 0 {
            return None;
        }

        let consumed = byte_diff / root.unit_size;
        if consumed.saturating_add(current_len) > root.len {
            return None;
        }

        Some(consumed)
    }
}

/// A trait for types that can be parsed from an abstract input.
///
/// This is the main trait that structs should implement to become parseable.
/// The lifetime parameter `'a` represents the lifetime of the borrowed input.
///
/// The second generic parameter defaults to `&str`, which keeps string parsing
/// ergonomic while allowing additional input forms such as `&[u8]` and token
/// slices.
pub trait Parse<'a, I: Input<'a> = &'a str>: Sized {
    /// Parse a value from the input.
    ///
    /// Returns the parsed value and the remaining unconsumed input.
    fn parse(input: I) -> ParseResult<(Self, I)> {
        let mut context = ParseOffsetContext::new();
        Self::parse_with_context(input, &mut context)
    }

    /// Parse a value from the input using an explicit offset context.
    fn parse_with_context(input: I, context: &mut ParseOffsetContext) -> ParseResult<(Self, I)>;
}

impl<'a, I> Parse<'a, I> for ()
where
    I: Input<'a>,
{
    fn parse_with_context(input: I, _context: &mut ParseOffsetContext) -> ParseResult<(Self, I)> {
        Ok(((), input))
    }
}

impl<'a, I, T> Parse<'a, I> for Option<T>
where
    I: Input<'a>,
    T: Parse<'a, I>,
{
    fn parse_with_context(input: I, context: &mut ParseOffsetContext) -> ParseResult<(Self, I)> {
        match T::parse_with_context(input, context) {
            Ok((value, remaining)) => Ok((Some(value), remaining)),
            Err(_) => Ok((None, input)),
        }
    }
}

impl<'a, I, T> Parse<'a, I> for Vec<T>
where
    I: Input<'a>,
    T: Parse<'a, I>,
{
    fn parse_with_context(
        mut input: I,
        context: &mut ParseOffsetContext,
    ) -> ParseResult<(Self, I)> {
        let mut items = Vec::new();
        while let Ok((item, remaining)) = T::parse_with_context(input, context) {
            items.push(item);
            input = remaining;
        }
        Ok((items, input))
    }
}

macro_rules! parse_pointer {
    ($ty:ty $(; $($bound:ident),*)?) => {
        impl<'a, T, I> Parse<'a, I> for $ty
        where
            I: Input<'a>,
            T: Parse<'a, I> $($(+ $bound)*)?,
        {
            fn parse_with_context(
                input: I,
                context: &mut ParseOffsetContext,
            ) -> ParseResult<(Self, I)> {
                let (value, remaining) = T::parse_with_context(input, context)?;
                Ok((Self::from(value), remaining))
            }
        }
    };
}

parse_pointer!(std::rc::Rc<T>; Clone);
parse_pointer!(std::sync::Arc<T>; Clone);

/// Convenience function to parse complete input, ensuring everything is consumed.
///
/// This function parses the input and verifies that all meaningful content
/// has been consumed (trailing whitespace is allowed).
pub fn parse_complete<'a, T: Parse<'a>>(input: &'a str) -> ParseResult<T> {
    parse_complete_input::<_, T>(input)
}

/// Convenience function to parse complete string input into a spanned result.
///
/// This is equivalent to `parse_complete_input::<_, Span<T>>(input)`.
pub fn parse_complete_spanned<'a, T: Parse<'a>>(input: &'a str) -> ParseResult<Span<T>> {
    parse_complete_input::<_, Span<T>>(input)
}

/// Convenience function to parse and fully consume an abstract input.
///
/// Leading and trailing whitespace handling is delegated to the input type.
pub fn parse_complete_input<'a, I, T>(input: I) -> ParseResult<T>
where
    I: ParseOffsetInput<'a>,
    T: Parse<'a, I>,
{
    let mut context = ParseOffsetContext::new();
    with_parse_offset_scope(&mut context, input, |context| {
        let (result, remaining) = T::parse_with_context(input, context)?;
        let remaining = remaining.trim_start();
        if remaining.is_empty() {
            Ok(result)
        } else {
            let start = current_parse_offset(context, remaining);
            let span = crate::error::SourceSpan::new(start, start + remaining.input_len());
            Err(ParseError::custom(format!(
                "unexpected trailing input: '{}'",
                remaining.display()
            ))
            .with_span(span))
        }
    })
}

/// Convenience function to parse and fully consume input into a spanned result.
pub fn parse_complete_input_spanned<'a, I, T>(input: I) -> ParseResult<Span<T>>
where
    I: ParseOffsetInput<'a>,
    T: Parse<'a, I>,
{
    parse_complete_input::<_, Span<T>>(input)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::combinators::{span::Span, ws::Ws};
    use crate::error::SourceSpan;
    use crate::input::TokenStream;
    use crate::lit_token;
    use crate::literals::KwNull;

    lit_token!(HelloParser, "hello");

    #[test]
    fn test_parse_complete_success() {
        let result = parse_complete::<HelloParser>("hello").unwrap();
        let _ = result; // HelloParser has no fields to check
    }

    #[test]
    fn test_parse_complete_with_whitespace() {
        let result = parse_complete::<HelloParser>("hello   ").unwrap();
        let _ = result;
    }

    #[test]
    fn test_parse_complete_spanned() {
        let result = parse_complete_spanned::<HelloParser>("hello").unwrap();
        assert_eq!(result.span.start, 0);
        assert_eq!(result.span.end, 5);
    }

    #[test]
    fn test_parse_complete_trailing_input() {
        let result = parse_complete::<HelloParser>("hello world");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_complete_input_bytes() {
        let result = parse_complete_input::<_, HelloParser>(b"hello".as_slice()).unwrap();
        let _ = result;
    }

    #[test]
    fn test_parse_complete_input_tokens() {
        let tokens = ["hello"];
        let result = parse_complete_input::<_, HelloParser>(TokenStream::new(&tokens)).unwrap();
        let _ = result;
    }

    #[test]
    fn test_parse_complete_input_spanned_tokens() {
        let tokens = ["hello"];
        let result =
            parse_complete_input_spanned::<_, HelloParser>(TokenStream::new(&tokens)).unwrap();
        assert_eq!(result.span.start, 0);
        assert_eq!(result.span.end, 1);
    }

    #[test]
    fn test_parse_complete_input_spanned_bytes_uses_byte_offsets() {
        let result = parse_complete_input_spanned::<_, char>("\u{00E9}".as_bytes()).unwrap();
        assert_eq!(result.inner, '\u{00E9}');
        assert_eq!(result.span, SourceSpan::new(0, 2));
    }

    #[test]
    fn test_parse_complete_input_trailing_bytes_reports_byte_span() {
        let err = parse_complete_input::<_, HelloParser>("hello\u{00E9}".as_bytes())
            .err()
            .expect("expected trailing byte input error");
        assert_eq!(err.span(), Some(SourceSpan::new(5, 7)));
    }

    #[test]
    fn test_parse_complete_input_trailing_tokens_reports_token_span() {
        let tokens = ["hello", "world"];
        let err = parse_complete_input::<_, HelloParser>(TokenStream::new(&tokens))
            .err()
            .expect("expected trailing token input error");
        assert_eq!(err.span(), Some(SourceSpan::new(1, 2)));
    }

    #[test]
    fn test_with_parse_offset_scope_if_missing_reuses_existing_scope() {
        let input = "abcdef";
        let sub = &input[2..];
        let mut context = ParseOffsetContext::new();

        with_parse_offset_scope(&mut context, input, |context| {
            let roots_before = context.roots.len();
            with_parse_offset_scope_if_missing(context, sub, |context| {
                assert_eq!(context.roots.len(), roots_before);
                assert_eq!(current_parse_offset(context, sub), 2);
            });
            assert_eq!(context.roots.len(), roots_before);
        });

        assert!(context.roots.is_empty());
    }

    #[test]
    fn test_with_parse_offset_scope_if_missing_pushes_when_unscoped() {
        let input = "abcdef";
        let sub = &input[2..];
        let mut context = ParseOffsetContext::new();

        with_parse_offset_scope_if_missing(&mut context, sub, |context| {
            assert_eq!(context.roots.len(), 1);
            assert_eq!(current_parse_offset(context, sub), 0);
        });

        assert!(context.roots.is_empty());
    }

    #[test]
    fn test_current_parse_offset_prefers_innermost_scope() {
        let input = "abcdef";
        let inner = &input[2..5];
        let inner_tail = &inner[1..];
        let mut context = ParseOffsetContext::new();

        with_parse_offset_scope(&mut context, input, |context| {
            assert_eq!(current_parse_offset(context, inner), 2);

            with_parse_offset_scope(context, inner, |context| {
                assert_eq!(current_parse_offset(context, inner), 0);
                assert_eq!(current_parse_offset(context, inner_tail), 1);
            });

            assert_eq!(current_parse_offset(context, inner_tail), 3);
        });
    }

    #[test]
    fn test_parse_complete_tracks_nested_span_offset() {
        type Parser = crate::and!(Ws<i64>, Span<Ws<KwNull>>);
        let result = parse_complete::<Parser>("42 null").unwrap();
        let null = result.right;
        assert_eq!(null.span, SourceSpan::new(2, 7));
    }

    #[test]
    fn test_parse_complete_tracks_error_offset() {
        type Parser = crate::and!(Ws<i64>, Ws<KwNull>);
        let err = parse_complete::<Parser>("42 nope").unwrap_err();
        assert_eq!(err.span(), Some(SourceSpan::point(3)));
    }

    #[test]
    fn test_parse_complete_tracks_eof_after_partial_consumption() {
        type Parser = crate::and!(char, char);
        let err = parse_complete::<Parser>("a").unwrap_err();
        assert_eq!(err.span(), Some(SourceSpan::point(1)));
    }

    #[test]
    fn test_parse_offset_tracking_is_thread_safe() {
        type Parser = crate::and!(Ws<i64>, Span<Ws<KwNull>>);
        let strs = ["1 null", "42 null", "123 null"];

        std::thread::scope(|s| {
            let handles = [
                s.spawn(|| parse_complete::<Parser>(&strs[0]).unwrap().right.span),
                s.spawn(|| parse_complete::<Parser>(&strs[1]).unwrap().right.span),
                s.spawn(|| parse_complete::<Parser>(&strs[2]).unwrap().right.span),
            ];

            let spans: Vec<_> = handles
                .into_iter()
                .map(|handle| handle.join().expect("threaded parse should succeed"))
                .collect();

            assert_eq!(spans[0], SourceSpan::new(1, 6));
            assert_eq!(spans[1], SourceSpan::new(2, 7));
            assert_eq!(spans[2], SourceSpan::new(3, 8));
        });
    }
}