Struct Acceptor

Source
pub struct Acceptor<'a, 'b, T, V> { /* private fields */ }
Expand description

A type that wraps a Scanner and holds a successfully accepted value.

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

§Type Parameters

  • T - The type of the data to scan.
  • V - The type of the value to accept.
  • 'a - The lifetime of the data to scan.
  • 'b - The lifetime of the acceptor.

Implementations§

Source§

impl<'a, 'b, T, V> Acceptor<'a, 'b, T, V>

Source

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

Create a new acceptor.

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

A new acceptor that uses the given scanner.

Examples found in repository?
examples/colors.rs (line 102)
101    fn accept(scanner: &mut Scanner<u8>) -> ParseResult<Self> {
102        let color = Acceptor::new(scanner)
103            .try_or(ColorInternal::Hex)?
104            .try_or(ColorInternal::Rgb)?
105            .try_or(ColorInternal::Tuple)?
106            .finish()
107            .ok_or(UnexpectedToken)?;
108        Ok(color.into())
109    }
More examples
Hide additional examples
examples/expression.rs (line 179)
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§

impl<'a, T, V> Acceptor<'a, '_, T, V>

Source

pub fn try_or<U: Visitor<'a, T>, F>(self, transformer: F) -> ParseResult<Self>
where F: Fn(U) -> V,

Attempt to accept a U using the given transformer, and rewind the scanner and return the current acceptor if it fails.

§Arguments
  • transformer - A function that takes a U and returns a ParseResult<V>.
§Returns

If the U is successfully accepted and the transformer returns Ok, returns the current acceptor with the resulting value in data. If the U is not successfully accepted, returns the current acceptor 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/colors.rs (line 103)
101    fn accept(scanner: &mut Scanner<u8>) -> ParseResult<Self> {
102        let color = Acceptor::new(scanner)
103            .try_or(ColorInternal::Hex)?
104            .try_or(ColorInternal::Rgb)?
105            .try_or(ColorInternal::Tuple)?
106            .finish()
107            .ok_or(UnexpectedToken)?;
108        Ok(color.into())
109    }
More examples
Hide additional examples
examples/expression.rs (line 180)
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 finish(self) -> Option<V>

Consume the acceptor and return the V that was accepted if the acceptor was successful.

§Returns

If the acceptor was successful (i.e., data is Some), returns the V that was accepted. Otherwise, returns None.

Examples found in repository?
examples/colors.rs (line 106)
101    fn accept(scanner: &mut Scanner<u8>) -> ParseResult<Self> {
102        let color = Acceptor::new(scanner)
103            .try_or(ColorInternal::Hex)?
104            .try_or(ColorInternal::Rgb)?
105            .try_or(ColorInternal::Tuple)?
106            .finish()
107            .ok_or(UnexpectedToken)?;
108        Ok(color.into())
109    }
More examples
Hide additional examples
examples/expression.rs (line 182)
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    }

Trait Implementations§

Source§

impl<'a, 'b, T: Debug, V: Debug> Debug for Acceptor<'a, 'b, T, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, 'b, T, V> Freeze for Acceptor<'a, 'b, T, V>
where V: Freeze,

§

impl<'a, 'b, T, V> RefUnwindSafe for Acceptor<'a, 'b, T, V>

§

impl<'a, 'b, T, V> Send for Acceptor<'a, 'b, T, V>
where V: Send, T: Sync,

§

impl<'a, 'b, T, V> Sync for Acceptor<'a, 'b, T, V>
where V: Sync, T: Sync,

§

impl<'a, 'b, T, V> Unpin for Acceptor<'a, 'b, T, V>
where V: Unpin,

§

impl<'a, 'b, T, V> !UnwindSafe for Acceptor<'a, 'b, T, V>

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.