Struct Recognizer

Source
pub struct Recognizer<'a, 'container, T, U> { /* private fields */ }
Expand description

A Recognizer is a type that wraps a Scanner and holds a successfully recognized value.

When a value is successfully recognized, the Recognizer stores the value in its data field and returns itself. If a value is not recognized, the Recognizer rewinds the scanner to the previous position and returns itself.

§Type Parameters

  • T - The type of the data to scan.
  • U - The type of the value to recognize.
  • 'a - The lifetime of the data to scan.
  • 'container - The lifetime of the Recognizer.

Implementations§

Source§

impl<'a, 'b, T, U> Recognizer<'a, 'b, T, U>

Source

pub fn new(scanner: &'b mut Scanner<'a, T>) -> Self

Create a new Recognizer with the given scanner.

§Arguments
  • scanner - The scanner to use when recognizing input.
§Returns

A new Recognizer that uses the given scanner.

Examples found in repository?
examples/operators.rs (line 35)
32fn main() -> ParseResult<()> {
33    let data = b"== 2";
34    let mut scanner = Scanner::new(data);
35    let recognized = Recognizer::new(&mut scanner)
36        .try_or(OperatorTokens::NotEqual)?
37        .try_or(OperatorTokens::Equal)?
38        .finish()
39        .ok_or(ParseError::UnexpectedToken)?;
40
41    println!("{}", String::from_utf8_lossy(recognized)); // ==
42
43    let data = b"!= 2";
44    let mut scanner = Scanner::new(data);
45    let recognized = Recognizer::new(&mut scanner)
46        .try_or(OperatorTokens::NotEqual)?
47        .try_or(OperatorTokens::Equal)?
48        .finish()
49        .ok_or(ParseError::UnexpectedToken)?;
50
51    println!("{}", String::from_utf8_lossy(recognized)); // !=
52
53    let data = b"> 2";
54    let mut scanner = Scanner::new(data);
55    let recognized = Recognizer::new(&mut scanner)
56        .try_or(OperatorTokens::NotEqual)?
57        .try_or(OperatorTokens::Equal)?
58        .finish()
59        .ok_or(ParseError::UnexpectedToken);
60
61    println!("{:?}", recognized); // error (UnexpectedToken)
62
63    Ok(())
64}
Source

pub fn try_or<R: Recognizable<'a, T, U>>( self, element: R, ) -> ParseResult<Recognizer<'a, 'b, T, U>>

Attempt to recognize a U using the given element, and return the current recognizer if it fails.

§Arguments
  • element - A Recognizable that recognizes a U.
§Returns

If the U is successfully recognized, returns the current recognizer with the resulting value in data. If the U is not successfully recognized, returns the current recognizer with the current position of the scanner rewound to the position at which the U was attempted, and data is left None.

Examples found in repository?
examples/operators.rs (line 36)
32fn main() -> ParseResult<()> {
33    let data = b"== 2";
34    let mut scanner = Scanner::new(data);
35    let recognized = Recognizer::new(&mut scanner)
36        .try_or(OperatorTokens::NotEqual)?
37        .try_or(OperatorTokens::Equal)?
38        .finish()
39        .ok_or(ParseError::UnexpectedToken)?;
40
41    println!("{}", String::from_utf8_lossy(recognized)); // ==
42
43    let data = b"!= 2";
44    let mut scanner = Scanner::new(data);
45    let recognized = Recognizer::new(&mut scanner)
46        .try_or(OperatorTokens::NotEqual)?
47        .try_or(OperatorTokens::Equal)?
48        .finish()
49        .ok_or(ParseError::UnexpectedToken)?;
50
51    println!("{}", String::from_utf8_lossy(recognized)); // !=
52
53    let data = b"> 2";
54    let mut scanner = Scanner::new(data);
55    let recognized = Recognizer::new(&mut scanner)
56        .try_or(OperatorTokens::NotEqual)?
57        .try_or(OperatorTokens::Equal)?
58        .finish()
59        .ok_or(ParseError::UnexpectedToken);
60
61    println!("{:?}", recognized); // error (UnexpectedToken)
62
63    Ok(())
64}
Source

pub fn finish(self) -> Option<U>

Consume the recognizer and return the U that was recognized if the recognizer was successful.

§Returns

If the recognizer was successful (i.e., data is Some), returns the U that was recognized. Otherwise, returns None.

Examples found in repository?
examples/operators.rs (line 38)
32fn main() -> ParseResult<()> {
33    let data = b"== 2";
34    let mut scanner = Scanner::new(data);
35    let recognized = Recognizer::new(&mut scanner)
36        .try_or(OperatorTokens::NotEqual)?
37        .try_or(OperatorTokens::Equal)?
38        .finish()
39        .ok_or(ParseError::UnexpectedToken)?;
40
41    println!("{}", String::from_utf8_lossy(recognized)); // ==
42
43    let data = b"!= 2";
44    let mut scanner = Scanner::new(data);
45    let recognized = Recognizer::new(&mut scanner)
46        .try_or(OperatorTokens::NotEqual)?
47        .try_or(OperatorTokens::Equal)?
48        .finish()
49        .ok_or(ParseError::UnexpectedToken)?;
50
51    println!("{}", String::from_utf8_lossy(recognized)); // !=
52
53    let data = b"> 2";
54    let mut scanner = Scanner::new(data);
55    let recognized = Recognizer::new(&mut scanner)
56        .try_or(OperatorTokens::NotEqual)?
57        .try_or(OperatorTokens::Equal)?
58        .finish()
59        .ok_or(ParseError::UnexpectedToken);
60
61    println!("{:?}", recognized); // error (UnexpectedToken)
62
63    Ok(())
64}
Source

pub fn finish_with<F>(self, closure: F) -> ParseResult<U>
where F: FnOnce(&mut Scanner<'a, T>) -> ParseResult<U>,

Consume the recognizer and return the U that was recognized if the recognizer was successful, or run the given closure if the recognizer was not successful.

§Arguments
  • closure - A function that takes the Scanner and returns a ParseResult<U>.
§Returns

If the recognizer was successful (i.e., data is Some), returns the U that was recognized. If the recognizer was not successful, the closure is called with the Scanner and the result of the closure is returned.

Auto Trait Implementations§

§

impl<'a, 'container, T, U> Freeze for Recognizer<'a, 'container, T, U>
where U: Freeze,

§

impl<'a, 'container, T, U> RefUnwindSafe for Recognizer<'a, 'container, T, U>

§

impl<'a, 'container, T, U> Send for Recognizer<'a, 'container, T, U>
where U: Send, T: Sync,

§

impl<'a, 'container, T, U> Sync for Recognizer<'a, 'container, T, U>
where U: Sync, T: Sync,

§

impl<'a, 'container, T, U> Unpin for Recognizer<'a, 'container, T, U>
where U: Unpin,

§

impl<'a, 'container, T, U> !UnwindSafe for Recognizer<'a, 'container, T, U>

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.