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 theRecognizer.
Implementations§
Source§impl<'a, 'b, T, U> Recognizer<'a, 'b, T, U>
impl<'a, 'b, T, U> Recognizer<'a, 'b, T, U>
Sourcepub fn new(scanner: &'b mut Scanner<'a, T>) -> Self
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?
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}Sourcepub fn try_or<R: Recognizable<'a, T, U>>(
self,
element: R,
) -> ParseResult<Recognizer<'a, 'b, T, U>>
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- ARecognizablethat recognizes aU.
§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?
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}Sourcepub fn finish(self) -> Option<U>
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?
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}Sourcepub fn finish_with<F>(self, closure: F) -> ParseResult<U>
pub fn finish_with<F>(self, closure: F) -> 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 theScannerand returns aParseResult<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.