rustyfi-syntax 0.1.0

Lexer, token stream, and syan2-based surface grammar for SATySFi
Documentation
//! The parse source for the SATySFi surface grammar.
//!
//! The parse source is the eagerly lexed `Vec<Atom>`, wrapped by
//! [`AtomStream`]: syan core has no `IntoParseStream for Vec<_>`, so the
//! buffering lives here.
//!
//! Neither stream erasure (a `&mut dyn ParseStream` tower) nor a failure
//! high-water mark belongs here, obsoleted by syan on both counts:
//! `Parse::parse_stream` takes `&mut S` and recursion reborrows, so `S` is a
//! genuine fixed point and the instantiation set is finite without erasing
//! anything (and no stream operation is a virtual call); and `ParseError` is
//! span-generic, every variant carrying the position it failed at, so the
//! error reports itself.

use crate::token::Atom;
use std::convert::Infallible;
use syan::parse::tape::Tape;
use syan::parse::ParseStream;

/// A parse source over an eagerly lexed token vector.
///
/// Backtracking runs through syan's [`Tape`], which owns the pushback and the
/// checkpoint scopes, so this is a thin forwarding shim and nothing more.
pub struct AtomStream {
    tape: Tape<std::vec::IntoIter<Atom>>,
}

impl AtomStream {
    pub fn new(atoms: Vec<Atom>) -> Self {
        AtomStream {
            tape: Tape::new(atoms.into_iter()),
        }
    }
}

impl ParseStream for AtomStream {
    type Atom = Atom;
    type Error = Infallible;

    fn next(&mut self) -> Option<Self::Atom> {
        self.tape.next()
    }

    fn peek(&mut self) -> Option<&Self::Atom> {
        self.tape.peek()
    }

    fn push(&mut self, atom: Self::Atom) {
        self.tape.push(atom);
    }

    fn checkpoint_raw(&mut self) -> u64 {
        self.tape.checkpoint()
    }

    fn rollback_raw(&mut self, raw: u64) {
        self.tape.rollback(raw);
    }

    fn commit_raw(&mut self, raw: u64) {
        self.tape.commit(raw);
    }

    fn get_error(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    fn skip_sep(&mut self) -> bool {
        // Already lexed: there is no separator atom to skip.
        false
    }
}