Properly handle whitespace for Infinity and NaN, r=rnewman (#246)

Signed-off-by: Victor Porof <vporof@mozilla.com>
This commit is contained in:
Victor Porof 2017-02-09 18:13:44 +01:00 committed by GitHub
parent 7db74953d6
commit 42580539b8
3 changed files with 22 additions and 9 deletions

View file

@ -32,10 +32,10 @@ pub nil -> Value =
"nil" { Value::Nil } "nil" { Value::Nil }
pub nan -> Value = pub nan -> Value =
"#f" __ "NaN" { Value::Float(OrderedFloat(NAN)) } "#f" whitespace+ "NaN" { Value::Float(OrderedFloat(NAN)) }
pub infinity -> Value = pub infinity -> Value =
"#f" __ s:$(sign) "Infinity" { "#f" whitespace+ s:$(sign) "Infinity" {
Value::Float(OrderedFloat(if s == "+" { INFINITY } else { NEG_INFINITY })) Value::Float(OrderedFloat(if s == "+" { INFINITY } else { NEG_INFINITY }))
} }

View file

@ -55,14 +55,14 @@ impl Display for Value {
Boolean(v) => write!(f, "{}", v), Boolean(v) => write!(f, "{}", v),
Integer(v) => write!(f, "{}", v), Integer(v) => write!(f, "{}", v),
BigInteger(ref v) => write!(f, "{}N", v), BigInteger(ref v) => write!(f, "{}N", v),
// TODO: make sure float syntax is correct.
Float(ref v) => { Float(ref v) => {
// TODO: make sure float syntax is correct.
if *v == OrderedFloat(f64::INFINITY) { if *v == OrderedFloat(f64::INFINITY) {
write!(f, "#f {}", "+Infinity") write!(f, "#f +Infinity")
} else if *v == OrderedFloat(f64::NEG_INFINITY) { } else if *v == OrderedFloat(f64::NEG_INFINITY) {
write!(f, "#f {}", "-Infinity") write!(f, "#f -Infinity")
} else if *v == OrderedFloat(f64::NAN) { } else if *v == OrderedFloat(f64::NAN) {
write!(f, "#f {}", "NaN") write!(f, "#f NaN")
} else { } else {
write!(f, "{}", v) write!(f, "{}", v)
} }

View file

@ -53,20 +53,33 @@ fn test_nil() {
#[test] #[test]
fn test_nan() { fn test_nan() {
assert_eq!(nan("#fNaN").unwrap(), Float(OrderedFloat(f64::NAN))); assert!(nan("#fNaN").is_err());
assert!(nan("#f;x\nNaN").is_err());
assert_eq!(nan("#f NaN").unwrap(), Float(OrderedFloat(f64::NAN))); assert_eq!(nan("#f NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
assert_eq!(nan("#f\t NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
assert_eq!(nan("#f,NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
assert!(nan("true").is_err()); assert!(nan("true").is_err());
} }
#[test] #[test]
fn test_infinity() { fn test_infinity() {
assert_eq!(infinity("#f-Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY))); assert!(infinity("#f-Infinity").is_err());
assert_eq!(infinity("#f+Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY))); assert!(infinity("#f+Infinity").is_err());
assert!(infinity("#f;x\n-Infinity").is_err());
assert!(infinity("#f;x\n+Infinity").is_err());
assert_eq!(infinity("#f -Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY))); assert_eq!(infinity("#f -Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
assert_eq!(infinity("#f +Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY))); assert_eq!(infinity("#f +Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
assert_eq!(infinity("#f\t -Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
assert_eq!(infinity("#f\t +Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
assert_eq!(infinity("#f,-Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
assert_eq!(infinity("#f,+Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
assert!(infinity("true").is_err()); assert!(infinity("true").is_err());
} }