fn integer_number_parser(input: &mut &str) -> PResult<i64> {
let sign = opt(one_of(['-', '+']));
let digits_with_underscores = || {
repeat(1.., alt((digit1.map(Some), '_'.map(|_| None))))
.map(|vd: Vec<Option<&str>>| vd.into_iter().flatten().collect::<Vec<_>>().join(""))
};
let signed_digits_with_underscores =
(sign, digits_with_underscores()).map(|(sign, digits): (_, String)| {
if let Some(sign) = sign {
format!("{sign}{digits}")
} else {
digits
}
});
let exponent = preceded("e", (opt('+').void(), digits_with_underscores()));
(signed_digits_with_underscores, opt(exponent))
.try_map(|(base, maybe_exp)| {
let base = base.parse::<i64>()?;
let exponent = if let Some((_, exponent)) = maybe_exp {
exponent.parse::<u32>()?
} else {
0
};
Ok::<_, ParseIntError>(base * 10i64.pow(exponent))
})
.parse_next(input)
}
fn parse_integer(lexer: &mut Lexer<Token>) -> Result<i64, LexError> {
let target = lexer.slice();
integer_number_parser.parse(target).map_err(|e| {
// TODO A proper lex error
dbg!(std::any::type_name_of_val(&e));
LexError::Unknown
})
}