← core

core/cursor.kur

//! Text cursors for incremental parsing.
// core::cursor -- checked traversal over text (`Str['k]`): position,
// `peek`, `next`, `starts_with`, `skip_while`, `take_while`, `take_exact`,
// and `mark`/`text_since`.
//
// `Cursor` is plain `Copy` data: text plus a position. It is exposed
// because building one from a struct literal grants no authority: its
// `Str` cannot be forged, and every read goes through `ReadBytes` or
// `Str::sub`, which check bounds whatever `pos` holds. An out-of-range
// `pos` therefore reads as end of input, never out of bounds.
//
// The methods that read bytes take the context that permits it by place,
// `&cx: C` with `where ReadBytes[C, Str['k]]`: the text's
// `Exclusive!['k]`, a `Shared!` node of it, `StaticRead` for a literal, or
// any context with such an instance.
//
// Every `Cursor` this module builds keeps `pos <= view.len()`. Methods
// clamp or bounds-check rather than rely on it, so soundness does not rest
// on the invariant.
//
// End of input is always an explicit `Option` or `bool`, never a NUL
// sentinel: NUL is a valid byte.

use package as core;

use core::slice::ReadBytes;
use core::str::Str;
use core::option::Option;

/// A position in text, for reading it from front to back.
forall 'k.
exposed struct Cursor {
    view: Str['k],
    pos: usize,
}

forall 'k.
impl Cursor['k] {
    /// A cursor at the start of `view`.
    fn new(view: Str['k]) -> Cursor['k] {
        Cursor.{ view, pos: 0usize }
    }

    /// How far into `view` this cursor is.
    fn pos(self) -> usize {
        let { view: v, pos: p } = self;
        p
    }

    /// Bytes left to read; 0 rather than a wrapped value if `pos` is past
    /// the end.
    fn remaining(self) -> usize {
        let { view: v, pos: p } = self;
        let n = v.len();
        if p >= n { 0usize } else { n - p }
    }

    /// Whether no bytes are left to read.
    fn at_end(self) -> bool {
        self.remaining() == 0usize
    }

    /// A checkpoint to measure from later: `pos` itself; see `text_since`.
    fn mark(self) -> usize {
        self.pos()
    }

    /// The text from a previous `mark()` up to the current position;
    /// `None` if `mark` is after it or the range is out of bounds.
    fn text_since(self, mark: usize) -> Option[Str['k]] {
        let { view: v, pos: p } = self;
        if mark > p {
            Option::None
        } else {
            v.sub(mark, p - mark)
        }
    }

    /// The byte at the current position, without advancing.
    forall C where ReadBytes[C, Str['k]].
    fn peek(self, &cx: C) -> Option[u8] {
        let { view: v, pos: p } = self;
        ReadBytes[C, Str['k]]::get(&cx, v, p)
    }

    /// The byte `offset` positions ahead of the current one, without
    /// advancing; `None` past the end, however large `offset` is.
    forall C where ReadBytes[C, Str['k]].
    fn peek_at(self, &cx: C, offset: usize) -> Option[u8] {
        let { view: v, pos: p } = self;
        if p >= v.len() || offset >= v.len() - p {
            return Option::None;
        }
        ReadBytes[C, Str['k]]::get(&cx, v, p + offset)
    }

    /// Reads the current byte and advances past it; `None`, cursor
    /// unchanged, at end of input.
    forall C where ReadBytes[C, Str['k]].
    fn next(&self, &cx: C) -> Option[u8] {
        let { view: v, pos: p } = self;
        let out = ReadBytes[C, Str['k]]::get(&cx, v, p);
        let p2 = if out.is_some() { p + 1usize } else { p };
        self = Cursor.{ view: v, pos: p2 };
        out
    }

    /// Whether the remaining input starts with `lit`'s bytes, without
    /// advancing; `false` when fewer than `lit.len()` bytes remain.
    forall C where ReadBytes[C, Str['k]].
    fn starts_with(self, &cx: C, lit: Str['static]) -> bool {
        let { view: v, pos: p } = self;
        match v.sub(p, lit.len()) {
            Some(candidate) => candidate.is(&cx, lit),
            None => false,
        }
    }

    /// The cursor advanced past every byte, from here on, that satisfies
    /// `pred`; unchanged if the current byte does not or input has ended.
    forall C where ReadBytes[C, Str['k]].
    fn skip_while(self, &cx: C, pred: fn(u8) -> bool) -> Cursor['k] {
        let { view: v, pos: p } = self;
        let p2 = skip_while_go(&cx, v, p, pred);
        Cursor.{ view: v, pos: p2 }
    }

    /// The text of every byte, from here on, that satisfies `pred`;
    /// advances the cursor past it.
    forall C where ReadBytes[C, Str['k]].
    fn take_while(&self, &cx: C, pred: fn(u8) -> bool) -> Str['k] {
        let { view: v, pos: p } = self;
        let p_start = if p > v.len() { v.len() } else { p };
        let p_end = skip_while_go(&cx, v, p_start, pred);
        let taken = v.slice(p_start, p_end);
        self = Cursor.{ view: v, pos: p_end };
        taken
    }

    /// The cursor advanced past the current byte, with that byte.
    ///
    /// `next` returning the cursor, for `&`-sugar:
    /// `let stepped = cur.step(&cx);`.
    forall C where ReadBytes[C, Str['k]].
    fn step(self, &cx: C) -> (Cursor['k], Option[u8]) {
        var cursor = self;
        let byte = &cursor.next(&cx);
        (cursor, byte)
    }

    /// The cursor advanced past every byte that satisfies `pred`, with those
    /// bytes.
    ///
    /// `take_while` shaped for `&`-sugar:
    /// `let taken = cur.take_matching(&cx, is_digit);`.
    forall C where ReadBytes[C, Str['k]].
    fn take_matching(self, &cx: C, pred: fn(u8) -> bool) -> (Cursor['k], Str['k]) {
        var cursor = self;
        let taken = &cursor.take_while(&cx, pred);
        (cursor, taken)
    }

    /// Exactly `count` bytes from here and the cursor advanced past them;
    /// `None` if fewer than `count` remain. `Str::sub` checks the range,
    /// so no capability is needed.
    fn take_exact(self, count: usize) -> Option[(Cursor['k], Str['k])] {
        let { view: v, pos: p } = self;
        match v.sub(p, count) {
            Some(s) => Option::Some.((Cursor.{ view: v, pos: p + count }, s)),
            None => Option::None,
        }
    }

    /// The whole remaining input as one text, without advancing.
    fn remaining_view(self) -> Str['k] {
        let { view: v, pos: p } = self;
        v.skip(p)
    }
}

/// The first position from `p` on in `v` whose byte `pred` rejects, or the
/// end. Used by `Cursor::skip_while` and `take_while`; not meant for direct
/// use.
forall 'k, C where ReadBytes[C, Str['k]].
fn skip_while_go(&cx: C, v: Str['k], p: usize, pred: fn(u8) -> bool) -> usize {
    var at = p;
    while true {
        match ReadBytes[C, Str['k]]::get(&cx, v, at) {
            Some(b) => if pred(b) {
                at = at + 1usize;
            } else {
                return at;
            },
            None => {
                return at;
            },
        }
    }
    at
}