use nom::{ branch::alt, bytes::complete::tag, character::complete::{alpha1, alphanumeric1, char, multispace0, one_of}, combinator::{map, recognize}, error::ParseError, multi::{many0, many1}, sequence::{delimited, pair, preceded, terminated}, }; use crate::parser::{IResult, Span}; pub fn hexadecimal(input: Span) -> IResult { map( preceded( char('h'), recognize(many1(terminated( one_of("0123456789abcdefABCDEF"), many0(char('_')), ))), ), |out: Span| { u64::from_str_radix(&str::replace(out.fragment(), "_", ""), 16) .expect("error parsing literal") }, )(input) } pub fn ws0<'a, F: 'a, O, E: ParseError>>( inner: F, ) -> impl FnMut(Span<'a>) -> IResult, O, E> where F: FnMut(Span<'a>) -> IResult, O, E>, { delimited(multispace0, inner, multispace0) } pub fn identifier(input: Span) -> IResult { recognize(pair( alt((alpha1, tag("_"))), many0(alt((alphanumeric1, tag("_")))), ))(input) } #[cfg(test)] mod test { use super::*; #[test] fn test_hex() { assert_eq!(hexadecimal("hfF".into()).unwrap().1, 0xff); assert_eq!(hexadecimal("hF".into()).unwrap().1, 0xf); } }