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>
impl<'a, 'b, T, V> Acceptor<'a, 'b, T, V>
Sourcepub fn new(scanner: &'b mut Scanner<'a, T>) -> Acceptor<'a, 'b, T, V>
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?
More examples
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>
impl<'a, T, V> Acceptor<'a, '_, T, V>
Sourcepub fn try_or<U: Visitor<'a, T>, F>(self, transformer: F) -> ParseResult<Self>where
F: Fn(U) -> V,
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 aUand returns aParseResult<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?
More examples
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 }Sourcepub fn finish(self) -> Option<V>
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?
More examples
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 }