lexer-rs 0.0.4

Lexical anaylzer framework for context-free text parsing into tokens
Documentation
//a Imports
use std::ops::Range;

use crate::PosnInCharStream;

//a StreamCharSpan
//tp StreamCharSpan
/// This provides a span between two byte offsets within a stream; the start and end have
/// an associated position that might also ccurately provide line and
/// column numbers
///
/// This can be used as within tokens for a lexer; if
/// [crate::LineColumn] is used for the generic argument then the
/// lexer will correctly track the line and column of the span of
/// tokens, and parsers can correctly track real spans of the tokens,
/// which allows for suitable formatting of errors etc within their
/// context (see [crate::FmtContext] also).
#[derive(Debug, Clone, Copy)]
pub struct StreamCharSpan<P>
where
    P: PosnInCharStream,
{
    start: P,
    end: P,
}

//ip StreamCharSpan
impl<P> StreamCharSpan<P>
where
    P: PosnInCharStream,
{
    //fp new
    /// Create a new [StreamCharSpan]
    pub fn new(start: P, end: P) -> Self {
        Self { start, end }
    }

    //fp new_at
    /// Create a new [StreamCharSpan]
    pub fn new_at(posn: &P) -> Self {
        Self {
            start: *posn,
            end: *posn,
        }
    }

    //cp end_at
    /// Create a new [StreamCharSpan]
    pub fn end_at(mut self, posn: &P) -> Self {
        self.end = *posn;
        self
    }

    //ap start
    /// Get the start of the span
    pub fn start(&self) -> &P {
        &self.start
    }

    //ap end
    /// Get the end of the span
    pub fn end(&self) -> &P {
        &self.end
    }

    //mp byte_range
    /// Get the range between the two positions of this [StreamCharSpan]
    pub fn byte_range(&self) -> Range<usize> {
        let start = self.start.byte_ofs();
        let end = self.end.byte_ofs();
        Range { start, end }
    }
}