Skip to content

Commit

Permalink
enum parser should support negative integer numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
x87 committed Jan 8, 2023
1 parent d6af3d0 commit 10101e9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/namespaces/enum_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use nom::character::complete::char;
use nom::character::complete::digit1;
use nom::character::complete::line_ending;
use nom::character::complete::multispace0;
use nom::character::complete::one_of;
use nom::character::complete::space0;
use nom::character::complete::space1;
use nom::combinator::map;
Expand Down Expand Up @@ -200,7 +201,7 @@ fn enum_value(input: &str) -> IResult<&str, EnumItemValueRaw> {
}

fn number(input: &str) -> IResult<&str, EnumItemValueRaw> {
let (input, d) = digit1(input)?;
let (input, d) = recognize(tuple((opt(one_of("+-")),digit1)))(input)?;
match i32::from_str_radix(d, 10) {
Ok(d) => Ok((input, EnumItemValueRaw::Int(d))),
_ => Err(nom::Err::Error(nom::error::Error {
Expand Down Expand Up @@ -339,3 +340,23 @@ fn test_enum5() {
))
)
}

#[test]
fn test_negative_numbers() {
assert_eq!(
parse_enums(
r#"
enum T
F=-1
end
"#
),
Ok((
"",
vec![Enum {
name: "T",
items: EnumItems::Int(vec![("F", -1)])
}]
))
)
}

0 comments on commit 10101e9

Please sign in to comment.