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, R: RecognizeSelf<'a, T, R>> Recognizer<'a, 'b, T, R>

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/expression.rs (line 38)
34    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
35        OptionalWhitespaces::accept(scanner)?;
36        let lhs = Number::accept(scanner)?.0;
37        OptionalWhitespaces::accept(scanner)?;
38        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
39            .try_or(BinaryOperator::Add)?
40            .try_or(BinaryOperator::Mul)?
41            .finish()
42            .ok_or(ParseError::UnexpectedToken)?;
43        OptionalWhitespaces::accept(scanner)?;
44        let rhs = Number::accept(scanner)?.0;
45        OptionalWhitespaces::accept(scanner)?;
46        Ok(Reducted { lhs, op, rhs })
47    }
48}
49
50// ------------------------------------------------------------
51//  +++ RightExpression
52// ------------------------------------------------------------
53
54#[derive(Debug)]
55struct RightExpression {
56    lhs: usize,
57    op: BinaryOperator,
58    rhs: Box<Expression>,
59}
60
61impl<'a> Visitor<'a, u8> for RightExpression {
62    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
63        OptionalWhitespaces::accept(scanner)?;
64        let lhs = Number::accept(scanner)?.0;
65        OptionalWhitespaces::accept(scanner)?;
66        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
67            .try_or(BinaryOperator::Add)?
68            .try_or(BinaryOperator::Mul)?
69            .finish()
70            .ok_or(ParseError::UnexpectedToken)?;
71        OptionalWhitespaces::accept(scanner)?;
72        let rhs = Expression::accept(scanner)?;
73        OptionalWhitespaces::accept(scanner)?;
74        Ok(RightExpression {
75            lhs,
76            op,
77            rhs: Box::new(rhs),
78        })
79    }
More examples
Hide additional examples
examples/operators.rs (line 36)
33fn main() -> ParseResult<()> {
34    let data = b"== 2";
35    let mut scanner = Scanner::new(data);
36    let recognized = Recognizer::new(&mut scanner)
37        .try_or(OperatorTokens::NotEqual)?
38        .try_or(OperatorTokens::Equal)?
39        .finish()
40        .ok_or(ParseError::UnexpectedToken)?;
41
42    println!("{:?}", recognized); // ==
43
44    let data = b"!= 2";
45    let mut scanner = Scanner::new(data);
46    let recognized = Recognizer::new(&mut scanner)
47        .try_or(OperatorTokens::NotEqual)?
48        .try_or(OperatorTokens::Equal)?
49        .finish()
50        .ok_or(ParseError::UnexpectedToken)?;
51
52    println!("{:?}", recognized); // !=
53
54    let data = b"> 2";
55    let mut scanner = Scanner::new(data);
56    let recognized = Recognizer::new(&mut scanner)
57        .try_or(OperatorTokens::NotEqual)?
58        .try_or(OperatorTokens::Equal)?
59        .finish()
60        .ok_or(ParseError::UnexpectedToken);
61
62    println!("{:?}", recognized); // error (UnexpectedToken)
63
64    Ok(())
65}
Source

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

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/expression.rs (line 39)
34    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
35        OptionalWhitespaces::accept(scanner)?;
36        let lhs = Number::accept(scanner)?.0;
37        OptionalWhitespaces::accept(scanner)?;
38        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
39            .try_or(BinaryOperator::Add)?
40            .try_or(BinaryOperator::Mul)?
41            .finish()
42            .ok_or(ParseError::UnexpectedToken)?;
43        OptionalWhitespaces::accept(scanner)?;
44        let rhs = Number::accept(scanner)?.0;
45        OptionalWhitespaces::accept(scanner)?;
46        Ok(Reducted { lhs, op, rhs })
47    }
48}
49
50// ------------------------------------------------------------
51//  +++ RightExpression
52// ------------------------------------------------------------
53
54#[derive(Debug)]
55struct RightExpression {
56    lhs: usize,
57    op: BinaryOperator,
58    rhs: Box<Expression>,
59}
60
61impl<'a> Visitor<'a, u8> for RightExpression {
62    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
63        OptionalWhitespaces::accept(scanner)?;
64        let lhs = Number::accept(scanner)?.0;
65        OptionalWhitespaces::accept(scanner)?;
66        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
67            .try_or(BinaryOperator::Add)?
68            .try_or(BinaryOperator::Mul)?
69            .finish()
70            .ok_or(ParseError::UnexpectedToken)?;
71        OptionalWhitespaces::accept(scanner)?;
72        let rhs = Expression::accept(scanner)?;
73        OptionalWhitespaces::accept(scanner)?;
74        Ok(RightExpression {
75            lhs,
76            op,
77            rhs: Box::new(rhs),
78        })
79    }
More examples
Hide additional examples
examples/operators.rs (line 37)
33fn main() -> ParseResult<()> {
34    let data = b"== 2";
35    let mut scanner = Scanner::new(data);
36    let recognized = Recognizer::new(&mut scanner)
37        .try_or(OperatorTokens::NotEqual)?
38        .try_or(OperatorTokens::Equal)?
39        .finish()
40        .ok_or(ParseError::UnexpectedToken)?;
41
42    println!("{:?}", recognized); // ==
43
44    let data = b"!= 2";
45    let mut scanner = Scanner::new(data);
46    let recognized = Recognizer::new(&mut scanner)
47        .try_or(OperatorTokens::NotEqual)?
48        .try_or(OperatorTokens::Equal)?
49        .finish()
50        .ok_or(ParseError::UnexpectedToken)?;
51
52    println!("{:?}", recognized); // !=
53
54    let data = b"> 2";
55    let mut scanner = Scanner::new(data);
56    let recognized = Recognizer::new(&mut scanner)
57        .try_or(OperatorTokens::NotEqual)?
58        .try_or(OperatorTokens::Equal)?
59        .finish()
60        .ok_or(ParseError::UnexpectedToken);
61
62    println!("{:?}", recognized); // error (UnexpectedToken)
63
64    Ok(())
65}
Source

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

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/expression.rs (line 41)
34    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
35        OptionalWhitespaces::accept(scanner)?;
36        let lhs = Number::accept(scanner)?.0;
37        OptionalWhitespaces::accept(scanner)?;
38        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
39            .try_or(BinaryOperator::Add)?
40            .try_or(BinaryOperator::Mul)?
41            .finish()
42            .ok_or(ParseError::UnexpectedToken)?;
43        OptionalWhitespaces::accept(scanner)?;
44        let rhs = Number::accept(scanner)?.0;
45        OptionalWhitespaces::accept(scanner)?;
46        Ok(Reducted { lhs, op, rhs })
47    }
48}
49
50// ------------------------------------------------------------
51//  +++ RightExpression
52// ------------------------------------------------------------
53
54#[derive(Debug)]
55struct RightExpression {
56    lhs: usize,
57    op: BinaryOperator,
58    rhs: Box<Expression>,
59}
60
61impl<'a> Visitor<'a, u8> for RightExpression {
62    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
63        OptionalWhitespaces::accept(scanner)?;
64        let lhs = Number::accept(scanner)?.0;
65        OptionalWhitespaces::accept(scanner)?;
66        let op = Recognizer::<u8, BinaryOperator>::new(scanner)
67            .try_or(BinaryOperator::Add)?
68            .try_or(BinaryOperator::Mul)?
69            .finish()
70            .ok_or(ParseError::UnexpectedToken)?;
71        OptionalWhitespaces::accept(scanner)?;
72        let rhs = Expression::accept(scanner)?;
73        OptionalWhitespaces::accept(scanner)?;
74        Ok(RightExpression {
75            lhs,
76            op,
77            rhs: Box::new(rhs),
78        })
79    }
More examples
Hide additional examples
examples/operators.rs (line 39)
33fn main() -> ParseResult<()> {
34    let data = b"== 2";
35    let mut scanner = Scanner::new(data);
36    let recognized = Recognizer::new(&mut scanner)
37        .try_or(OperatorTokens::NotEqual)?
38        .try_or(OperatorTokens::Equal)?
39        .finish()
40        .ok_or(ParseError::UnexpectedToken)?;
41
42    println!("{:?}", recognized); // ==
43
44    let data = b"!= 2";
45    let mut scanner = Scanner::new(data);
46    let recognized = Recognizer::new(&mut scanner)
47        .try_or(OperatorTokens::NotEqual)?
48        .try_or(OperatorTokens::Equal)?
49        .finish()
50        .ok_or(ParseError::UnexpectedToken)?;
51
52    println!("{:?}", recognized); // !=
53
54    let data = b"> 2";
55    let mut scanner = Scanner::new(data);
56    let recognized = Recognizer::new(&mut scanner)
57        .try_or(OperatorTokens::NotEqual)?
58        .try_or(OperatorTokens::Equal)?
59        .finish()
60        .ok_or(ParseError::UnexpectedToken);
61
62    println!("{:?}", recognized); // error (UnexpectedToken)
63
64    Ok(())
65}
Source

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

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.