Function chumsky::primitive::end

source ·
pub fn end<E>() -> End<E>
Expand description

A parser that accepts only the end of input.

This parser is very useful when you wish to force a parser to consume all of the input. It is typically combined with Parser::then_ignore.

The output type of this parser is ().

Examples

assert_eq!(end::<Simple<char>>().parse(""), Ok(()));
assert!(end::<Simple<char>>().parse("hello").is_err());
let digits = text::digits::<_, Simple<char>>(10);

// This parser parses digits!
assert_eq!(digits.parse("1234"), Ok("1234".to_string()));

// However, parsers are lazy and do not consume trailing input.
// This can be inconvenient if we want to validate all of the input.
assert_eq!(digits.parse("1234AhasjADSJAlaDJKSDAK"), Ok("1234".to_string()));

// To fix this problem, we require that the end of input follows any successfully parsed input
let only_digits = digits.then_ignore(end());

// Now our parser correctly produces an error if any trailing input is found...
assert!(only_digits.parse("1234AhasjADSJAlaDJKSDAK").is_err());
// ...while still behaving correctly for inputs that only consist of valid patterns
assert_eq!(only_digits.parse("1234"), Ok("1234".to_string()));