pub fn recognize<'a, T, V, R: Recognizable<'a, T, V>>(
recognizable: R,
scanner: &mut Scanner<'a, T>,
) -> ParseResult<V>Expand description
Recognize an object for the given scanner.
§Type Parameters
V- The type of the object to recognizeR- The type of the recognizable object
§Arguments
recognizable- The recognizable object to use for recognitionscanner- The scanner to recognize the object for
§Returns
Ok(V)if the object was recognized,Err(ParseError)if an error occurred
This function calls the recognize method of the recognizable object and
returns its result. If the recognizable object was not recognized, an
Err(ParseError::UnexpectedToken) is returned. If the scanner is at the end
of its input and the recognizable object is longer than the remaining input,
an Err(ParseError::UnexpectedEndOfInput) is returned.
Examples found in repository?
More examples
examples/turbofish_data_visitor.rs (line 18)
16 fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
17 // recognize the turbofish operator start "::<".
18 recognize(Token::Colon, scanner)?;
19 recognize(Token::Colon, scanner)?;
20 recognize(Token::LessThan, scanner)?;
21 Ok(TurbofishStartTokens)
22 }
23}
24
25// Implement the `Visitor` trait for the turbofish operator.
26impl<'a> Visitor<'a, u8> for Turbofish {
27 fn accept(scanner: &mut elyze::scanner::Scanner<u8>) -> ParseResult<Self> {
28 // recognize the turbofish operator start "::<".
29 TurbofishStartTokens::accept(scanner)?;
30 // recognize the number
31 let number = Number::accept(scanner)?.0;
32 // recognize the turbofish operator end ">"
33 recognize(Token::GreaterThan, scanner)?;
34 Ok(Turbofish(number))
35 }examples/recognize.rs (line 25)
23fn main() {
24 let mut scanner = Scanner::new(b"hello world");
25 let data = recognize(Hello, &mut scanner);
26
27 if let Ok(hello) = data {
28 println!("found: {hello:?}"); // found: "Hello"
29 print!(
30 "remaining: {:?}",
31 String::from_utf8_lossy(scanner.remaining())
32 ); // remaining: " world"
33 } else {
34 println!("not found");
35 }
36}examples/tokens.rs (line 48)
45fn main() -> ParseResult<()> {
46 let data = b"((+-)*/)end";
47 let mut scanner = elyze::scanner::Scanner::new(data);
48 recognize(Token::LParen, &mut scanner)?;
49 recognize(Token::LParen, &mut scanner)?;
50 recognize(Token::Plus, &mut scanner)?;
51 recognize(Token::Minus, &mut scanner)?;
52 recognize(Token::RParen, &mut scanner)?;
53 recognize(Token::Star, &mut scanner)?;
54 recognize(Token::Slash, &mut scanner)?;
55 recognize(Token::RParen, &mut scanner)?;
56
57 print!("{:?}", String::from_utf8_lossy(scanner.remaining()));
58
59 Ok(())
60}Additional examples can be found in: