pub fn number_parser<'a,>() -> impl Parser<'a, &'a str, Dynamic, Extra<'a>> + Clone {
let bin = just('0').ignore_then(one_of("bB")).ignore_then(any().filter(|c| *c == '0' || *c == '1',).repeated().at_least(1,)
.collect::<String>(),).map(|s| Dynamic::from(i64::from_str_radix(&s, 2).unwrap()));
let oct = just('0').ignore_then(one_of("oO")).ignore_then(any().filter(|c| ('0'..='7').contains(c,),).repeated().at_least(1,)
.collect::<String>(),).map(|s| Dynamic::from(i64::from_str_radix(&s, 8).unwrap()));
let hex = just('0').ignore_then(one_of("xX")).ignore_then(any().filter(|c: &char| c.is_ascii_hexdigit(),).repeated().at_least(1,)
.collect::<String>(),).map(|s| Dynamic::from(i64::from_str_radix(&s, 16).unwrap()));
let int_part = text::int(10,).from_str::<i64>().unwrapped();
let frac_part = just('.',).ignore_then(text::digits(10,).collect::<String>(),).map(|s| s.parse::<f64>().unwrap() / 10f64.powi(s.len() as i32,),).or_not();
let exp_part = one_of("eE",).ignore_then(just('+',).to(1,).or(just('-',).to(-1,),).or_not().map(|opt| opt.unwrap_or(1,),).then(text::int(10,).from_str::<i32>().unwrapped(),),).map(|(sign, exp,)| sign as f64 * exp as f64,).or_not();
let decimal = int_part.then(frac_part,).then(exp_part,).map(|((int, frac,), exp,)| {
if frac.is_some() || exp.is_some() {
let frac_val = frac.unwrap_or(0.0,);
let exp_val = exp.unwrap_or(0.0,);
Dynamic::from((int as f64 + frac_val) * 10f64.powf(exp_val,))
} else {
Dynamic::from(int)
}
});
bin.or(oct,).or(hex,).or(decimal)
评论