mipl 0.2.1

Minimal Imperative Parsing Library
Documentation
//! The Parser.
use crate::lexer::prelude::*;
use std::iter::Peekable;

/// Contains both the token stream and the parsing functionalities.
#[derive(Debug, Clone)]
pub struct Parser {
    tokens: Peekable<Tokens>
}
impl Parser{
    /// Look up the next token in the stream
    /// without consuming it.
    pub fn peek(&mut self) -> Option<Token> {
        self.tokens.peek().cloned()
    }

    /// Extend the parser with [Tokens].
    /// 
    /// Current implementation *could be*
    /// costly as it relies on cloning.
    pub fn extend(&mut self, tokens: Tokens) {
        let mut toks_buf = Tokens::new_empty();

        for token in self.clone() {
            toks_buf.add_token(token);
        }

        for token in tokens.clone() {
            toks_buf.add_token(token);
        }

        self.tokens = tokens.peekable();
    }
    
    /// Get a new empty [Parser].
    pub fn new_empty() -> Parser {
        let tokens = Tokens::new_empty();
        Self::new(tokens)
    }

    /// Create a new [Parser] from tokens.
    pub fn new(tokens: Tokens) -> Parser {
        Parser { tokens: tokens.peekable() }
    }

    /// Create a [Parser] from string value and
    /// delimiters.
    pub fn from<T: AsRef<str>>
    (
        value: T,
        delimiters: DelimitersParam
    ) -> Parser {
        let tokens = Tokens::new(value, delimiters);
        Parser::new(tokens)
    }
}
impl Iterator for Parser {
    type Item = Token;

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

pub mod concrete_parser;
pub mod subparser;