2021-12-30 19:46:28 +00:00
|
|
|
use nom::{
|
|
|
|
character::complete::{char, one_of},
|
2022-02-01 01:00:27 +00:00
|
|
|
combinator::{map, recognize},
|
2021-12-30 19:46:28 +00:00
|
|
|
multi::{many0, many1},
|
|
|
|
sequence::{preceded, terminated},
|
|
|
|
};
|
|
|
|
|
2022-01-04 19:05:10 +00:00
|
|
|
use crate::parser::{IResult, Span};
|
2021-12-30 19:46:28 +00:00
|
|
|
|
2022-01-05 01:09:08 +00:00
|
|
|
pub fn hexadecimal(input: Span) -> IResult<Span, u64> {
|
2022-02-01 01:00:27 +00:00
|
|
|
map(
|
2021-12-30 19:46:28 +00:00
|
|
|
preceded(
|
2022-01-04 18:09:33 +00:00
|
|
|
char('h'),
|
2021-12-30 19:46:28 +00:00
|
|
|
recognize(many1(terminated(
|
|
|
|
one_of("0123456789abcdefABCDEF"),
|
|
|
|
many0(char('_')),
|
|
|
|
))),
|
|
|
|
),
|
2022-02-01 01:00:27 +00:00
|
|
|
|out: Span| {
|
|
|
|
u64::from_str_radix(&str::replace(out.fragment(), "_", ""), 16).expect("error parsing literal")
|
|
|
|
},
|
2021-12-30 19:46:28 +00:00
|
|
|
)(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hex() {
|
2022-01-04 19:05:10 +00:00
|
|
|
assert_eq!(hexadecimal("hfF".into()).unwrap().1, 0xff);
|
|
|
|
assert_eq!(hexadecimal("hF".into()).unwrap().1, 0xf);
|
2021-12-30 19:46:28 +00:00
|
|
|
}
|
|
|
|
}
|