Nom 有一个解析浮点数的例子:
named!(unsigned_float <f32>, map_res!(
map_res!(
recognize!(
alt!(
delimited!(digit, tag!("."), opt!(complete!(digit))) |
delimited!(opt!(digit), tag!("."), digit)
)
),
str::from_utf8
),
FromStr::from_str
));
我想将此示例扩展为也支持解析"123"
为123.0
. 我试过这样的事情没有运气:
named!(unsigned_float_v1 <f32>,
map_res!(
map_res!(
alt!(
recognize!(
alt!(
delimited!(digit, tag!("."), opt!(complete!(digit))) |
delimited!(opt!(digit), tag!("."), digit)
)
) |
ws!(digit)
),
str::from_utf8
),
FromStr::from_str
)
);
named!(unsigned_float_v2 <f32>,
map_res!(
map_res!(
recognize!(
alt!(
delimited!(digit, tag!("."), opt!(complete!(digit))) |
delimited!(opt!(digit), tag!("."), digit) |
digit
)
),
str::from_utf8
),
FromStr::from_str
)
);