futilehdl/src/literals.rs

37 lines
867 B
Rust

use std::ops::{RangeFrom, RangeTo};
use nom::{
character::complete::{char, one_of},
combinator::{map_res, recognize},
multi::{many0, many1},
sequence::{preceded, terminated},
AsChar, FindToken, InputIter, InputLength, Offset, Slice
};
use crate::parser::{IResult, Span};
pub fn hexadecimal(input: Span) -> IResult<Span, u64>
{
map_res(
preceded(
char('h'),
recognize(many1(terminated(
one_of("0123456789abcdefABCDEF"),
many0(char('_')),
))),
),
|out: Span| u64::from_str_radix(&str::replace(&out.fragment(), "_", ""), 16),
)(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);
}
}