futilehdl/src/parser/literals.rs

35 lines
816 B
Rust

use nom::{
character::complete::{char, one_of},
combinator::{map, recognize},
multi::{many0, many1},
sequence::{preceded, terminated},
};
use crate::parser::{IResult, Span};
pub fn hexadecimal(input: Span) -> IResult<Span, u64> {
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)
}
#[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);
}
}