libstd: Skip trailing whitespaces after JSON value

This commit is contained in:
Tycho Sci 2012-02-29 17:47:17 +09:00
parent 0465d5217d
commit ecf87c3180
1 changed files with 9 additions and 12 deletions

View File

@ -154,6 +154,8 @@ impl parser for parser {
fn parse() -> result::t<json, error> { fn parse() -> result::t<json, error> {
alt self.parse_value() { alt self.parse_value() {
ok(value) { ok(value) {
// Skip trailing whitespaces.
self.parse_whitespace();
// Make sure there is no trailing characters. // Make sure there is no trailing characters.
if self.eof() { if self.eof() {
ok(value) ok(value)
@ -392,7 +394,6 @@ impl parser for parser {
if self.ch == ']' { if self.ch == ']' {
self.bump(); self.bump();
self.parse_whitespace();
ret ok(list(values)); ret ok(list(values));
} }
@ -407,11 +408,7 @@ impl parser for parser {
alt self.ch { alt self.ch {
',' { self.bump(); } ',' { self.bump(); }
']' { ']' { self.bump(); ret ok(list(values)); }
self.bump();
self.parse_whitespace();
ret ok(list(values));
}
_ { ret self.error("expecting ',' or ']'"); } _ { ret self.error("expecting ',' or ']'"); }
} }
} }
@ -427,7 +424,6 @@ impl parser for parser {
if self.ch == '}' { if self.ch == '}' {
self.bump(); self.bump();
self.parse_whitespace();
ret ok(dict(values)); ret ok(dict(values));
} }
@ -459,11 +455,7 @@ impl parser for parser {
alt self.ch { alt self.ch {
',' { self.bump(); } ',' { self.bump(); }
'}' { '}' { self.bump(); ret ok(dict(values)); }
self.bump();
self.parse_whitespace();
ret ok(dict(values));
}
_ { _ {
if self.eof() { break; } if self.eof() { break; }
ret self.error("expecting ',' or '}'"); ret self.error("expecting ',' or '}'");
@ -637,6 +629,9 @@ mod tests {
assert from_str("null") == ok(null); assert from_str("null") == ok(null);
assert from_str("true") == ok(boolean(true)); assert from_str("true") == ok(boolean(true));
assert from_str("false") == ok(boolean(false)); assert from_str("false") == ok(boolean(false));
assert from_str(" null ") == ok(null);
assert from_str(" true ") == ok(boolean(true));
assert from_str(" false ") == ok(boolean(false));
} }
#[test] #[test]
@ -664,6 +659,7 @@ mod tests {
assert from_str("0.4e5") == ok(num(0.4e5f)); assert from_str("0.4e5") == ok(num(0.4e5f));
assert from_str("0.4e+15") == ok(num(0.4e15f)); assert from_str("0.4e+15") == ok(num(0.4e15f));
assert from_str("0.4e-01") == ok(num(0.4e-01f)); assert from_str("0.4e-01") == ok(num(0.4e-01f));
assert from_str(" 3 ") == ok(num(3f));
} }
#[test] #[test]
@ -680,6 +676,7 @@ mod tests {
assert from_str("\"\\n\"") == ok(string("\n")); assert from_str("\"\\n\"") == ok(string("\n"));
assert from_str("\"\\r\"") == ok(string("\r")); assert from_str("\"\\r\"") == ok(string("\r"));
assert from_str("\"\\t\"") == ok(string("\t")); assert from_str("\"\\t\"") == ok(string("\t"));
assert from_str(" \"foo\" ") == ok(string("foo"));
} }
#[test] #[test]