Function chumsky::text::int

source · []
pub fn int<C: Character, E: Error<C>>(
    radix: u32
) -> impl Parser<C, C::Collection, Error = E> + Copy + Clone
Expand description

A parser that accepts a positive integer.

An integer is defined as a non-empty sequence of ASCII digits, where the first digit is non-zero or the sequence has length one.

The output type of this parser is Character::Collection (i.e: String when C is char, and Vec<u8> when C is u8).

The radix parameter functions identically to char::is_digit. If in doubt, choose 10.

Examples

let dec = text::int::<_, Simple<char>>(10)
    .then_ignore(end());

assert_eq!(dec.parse("0"), Ok("0".to_string()));
assert_eq!(dec.parse("1"), Ok("1".to_string()));
assert_eq!(dec.parse("1452"), Ok("1452".to_string()));
// No leading zeroes are permitted!
assert!(dec.parse("04").is_err());

let hex = text::int::<_, Simple<char>>(16)
    .then_ignore(end());

assert_eq!(hex.parse("2A"), Ok("2A".to_string()));
assert_eq!(hex.parse("d"), Ok("d".to_string()));
assert_eq!(hex.parse("b4"), Ok("b4".to_string()));
assert!(hex.parse("0B").is_err());