Peeking

Struct Peeking 

Source
pub struct Peeking<'a, T, S, E> {
    pub start: S,
    pub end: E,
    pub end_slice: usize,
    pub data: &'a [T],
}
Expand description

A successful peeking result.

A Peeking contains the start and end results of a successful peek, the length of the end slice, and a reference to the data that was peeked.

Fields§

§start: S

The start of the match.

§end: E

The end of the match.

§end_slice: usize

The length of peeked slice.

§data: &'a [T]

The data that was peeked.

Implementations§

Source§

impl<'a, T, S, E> Peeking<'a, T, S, E>
where S: MatchSize, E: MatchSize,

Source

pub fn peeked_slice(&self) -> &'a [T]

Get a slice of the data that was peeked.

Examples found in repository?
examples/delimited_group.rs (line 12)
4fn main() {
5    let data = b"(2 * 3)";
6    let mut scanner = noa_parser::scanner::Scanner::new(data);
7    let result = peek(GroupKind::Parenthesis, &mut scanner)
8        .expect("failed to parse")
9        .expect("failed to peek");
10    println!(
11        "{}",
12        String::from_utf8_lossy(result.peeked_slice()) // 2 * 3
13    );
14}
More examples
Hide additional examples
examples/expression.rs (line 172)
164    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
165        OptionalWhitespaces::accept(scanner)?;
166        // Check if there is a parenthesis
167        let result = peek(GroupKind::Parenthesis, scanner)?;
168
169        match result {
170            Some(peeked) => {
171                // Parse the inner expression
172                let mut inner_scanner = Scanner::new(peeked.peeked_slice());
173                let inner_result = Expression::accept(&mut inner_scanner)?;
174                scanner.bump_by(peeked.end_slice);
175                Ok(inner_result)
176            }
177            None => {
178                // Parse the reduced expression or the right expression
179                let accepted = Acceptor::new(scanner)
180                    .try_or(ExpressionInternal::RightExpression)?
181                    .try_or(ExpressionInternal::Reducted)?
182                    .finish()
183                    .ok_or(ParseError::UnexpectedToken)?;
184
185                Ok(accepted.into())
186            }
187        }
188    }
Source

pub fn data(&self) -> &'a [T]

Get the data that was peeked.

Returns a reference to the underlying data that was peeked.

Trait Implementations§

Source§

impl<'a, T: Debug, S: Debug, E: Debug> Debug for Peeking<'a, T, S, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: PartialEq, S: PartialEq, E: PartialEq> PartialEq for Peeking<'a, T, S, E>

Source§

fn eq(&self, other: &Peeking<'a, T, S, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T, S, E> StructuralPartialEq for Peeking<'a, T, S, E>

Auto Trait Implementations§

§

impl<'a, T, S, E> Freeze for Peeking<'a, T, S, E>
where S: Freeze, E: Freeze,

§

impl<'a, T, S, E> RefUnwindSafe for Peeking<'a, T, S, E>

§

impl<'a, T, S, E> Send for Peeking<'a, T, S, E>
where S: Send, E: Send, T: Sync,

§

impl<'a, T, S, E> Sync for Peeking<'a, T, S, E>
where S: Sync, E: Sync, T: Sync,

§

impl<'a, T, S, E> Unpin for Peeking<'a, T, S, E>
where S: Unpin, E: Unpin,

§

impl<'a, T, S, E> UnwindSafe for Peeking<'a, T, S, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.