Skip to content

Commit

Permalink
Merge pull request #1 from nick96/parse-scientific-notation-floats
Browse files Browse the repository at this point in the history
Handle scientific notation floats
  • Loading branch information
timothyb89 authored Mar 22, 2021
2 parents d5cda30 + 847de35 commit c09d570
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/prometheus.pest
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ expression = {
duration_value = ${ ASCII_DIGIT+ }
duration_unit = ${ "s" | "m" | "h" | "d" | "w" | "y" }

// allow 1, 1.0, -1, -1.0, .1, -.1 but don't allow empty / '.' / '-' / '-.'
// allow 1, 1.0, -1, -1.0, .1, -.1, 1e1, -1e1, 1e-1, -1e-1 but don't allow empty / '.' / '-' / '-.'
signed_float = @{
("+" | "-")? ~ (
("Inf" | "NaN") |
(ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)?) |
(ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? ~ (^"e" ~ ("+" | "-")? ~ ASCII_DIGIT+)?) |
("." ~ ASCII_DIGIT+)
)
}
Expand Down
40 changes: 40 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,43 @@ fn parse_weird_floats() -> Result<()> {

Ok(())
}

#[test]
fn parse_scientific_notation_floats() -> Result<()> {
match parse_expr("1e1")? {
Expression::Float(f) => assert_eq!(f, (1e1 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("1e-1")? {
Expression::Float(f) => assert_eq!(f, (1e-1 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("-1e1")? {
Expression::Float(f) => assert_eq!(f, (-1e1 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("-1e-1")? {
Expression::Float(f) => assert_eq!(f, (-1e-1 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("1.0e1")? {
Expression::Float(f) => assert_eq!(f, (1.0e1 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("1e01")? {
Expression::Float(f) => assert_eq!(f, (1e01 as f64)),
_ => assert!(false, "must be a float")
};

match parse_expr("1E01")? {
Expression::Float(f) => assert_eq!(f, (1e01 as f64)),
_ => assert!(false, "must be a float")
};

Ok(())
}

0 comments on commit c09d570

Please sign in to comment.