Use "terminated" and "preceded" macros
i would recommend to use nom's preceded
(and terminated
) macros for the signs (like #
, $
) because they are discarded anyway
example (for ints): from:
tag!("#") >>
sign: opt!(tag!("-")) >>
reg_num: digit >>
(
{
let mut tmp = String::from("");
if sign.is_some() {
tmp.push_str("-");
}
tmp.push_str(®_num.to_string());
let converted = tmp.parse::<i32>().unwrap();
Token::IntegerOperand{value: converted}
}
)
to:
number: preceded!(
tag!("#"),
recognize!(
pair!( // i dont know the code style, so this *could* be made into a single line
opt!(tag!("-")),
digit
)
)
) >>
(
{
let converted = number.parse::<i32>().unwrap();
Token::IntegerOperand{value: converted}
}
)
Edited by hasezoey