Move the world over to using the new style string literals and types. Closes #2907.
This commit is contained in:
parent
5c5065e8bd
commit
92743dc2a6
424 changed files with 8132 additions and 8064 deletions
|
@ -29,17 +29,17 @@ export null;
|
|||
/// Represents a json value
|
||||
enum json {
|
||||
num(float),
|
||||
string(@str/~),
|
||||
string(@~str),
|
||||
boolean(bool),
|
||||
list(@~[json]),
|
||||
dict(map::hashmap<str, json>),
|
||||
dict(map::hashmap<~str, json>),
|
||||
null,
|
||||
}
|
||||
|
||||
type error = {
|
||||
line: uint,
|
||||
col: uint,
|
||||
msg: @str/~,
|
||||
msg: @~str,
|
||||
};
|
||||
|
||||
/// Serializes a json value into a io::writer
|
||||
|
@ -50,14 +50,14 @@ fn to_writer(wr: io::writer, j: json) {
|
|||
wr.write_str(escape_str(*s));
|
||||
}
|
||||
boolean(b) {
|
||||
wr.write_str(if b { "true" } else { "false" });
|
||||
wr.write_str(if b { ~"true" } else { ~"false" });
|
||||
}
|
||||
list(v) {
|
||||
wr.write_char('[');
|
||||
let mut first = true;
|
||||
for (*v).each |item| {
|
||||
if !first {
|
||||
wr.write_str(", ");
|
||||
wr.write_str(~", ");
|
||||
}
|
||||
first = false;
|
||||
to_writer(wr, item);
|
||||
|
@ -66,51 +66,51 @@ fn to_writer(wr: io::writer, j: json) {
|
|||
}
|
||||
dict(d) {
|
||||
if d.size() == 0u {
|
||||
wr.write_str("{}");
|
||||
wr.write_str(~"{}");
|
||||
ret;
|
||||
}
|
||||
|
||||
wr.write_str("{ ");
|
||||
wr.write_str(~"{ ");
|
||||
let mut first = true;
|
||||
for d.each |key, value| {
|
||||
if !first {
|
||||
wr.write_str(", ");
|
||||
wr.write_str(~", ");
|
||||
}
|
||||
first = false;
|
||||
wr.write_str(escape_str(key));
|
||||
wr.write_str(": ");
|
||||
wr.write_str(~": ");
|
||||
to_writer(wr, value);
|
||||
};
|
||||
wr.write_str(" }");
|
||||
wr.write_str(~" }");
|
||||
}
|
||||
null {
|
||||
wr.write_str("null");
|
||||
wr.write_str(~"null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_str(s: str) -> str {
|
||||
let mut escaped = "\"";
|
||||
fn escape_str(s: ~str) -> ~str {
|
||||
let mut escaped = ~"\"";
|
||||
do str::chars_iter(s) |c| {
|
||||
alt c {
|
||||
'"' { escaped += "\\\""; }
|
||||
'\\' { escaped += "\\\\"; }
|
||||
'\x08' { escaped += "\\b"; }
|
||||
'\x0c' { escaped += "\\f"; }
|
||||
'\n' { escaped += "\\n"; }
|
||||
'\r' { escaped += "\\r"; }
|
||||
'\t' { escaped += "\\t"; }
|
||||
'"' { escaped += ~"\\\""; }
|
||||
'\\' { escaped += ~"\\\\"; }
|
||||
'\x08' { escaped += ~"\\b"; }
|
||||
'\x0c' { escaped += ~"\\f"; }
|
||||
'\n' { escaped += ~"\\n"; }
|
||||
'\r' { escaped += ~"\\r"; }
|
||||
'\t' { escaped += ~"\\t"; }
|
||||
_ { escaped += str::from_char(c); }
|
||||
}
|
||||
};
|
||||
|
||||
escaped += "\"";
|
||||
escaped += ~"\"";
|
||||
|
||||
escaped
|
||||
}
|
||||
|
||||
/// Serializes a json value into a string
|
||||
fn to_str(j: json) -> str {
|
||||
fn to_str(j: json) -> ~str {
|
||||
io::with_str_writer(|wr| to_writer(wr, j))
|
||||
}
|
||||
|
||||
|
@ -140,7 +140,7 @@ impl parser for parser {
|
|||
self.ch
|
||||
}
|
||||
|
||||
fn error<T>(+msg: str) -> result<T, error> {
|
||||
fn error<T>(+msg: ~str) -> result<T, error> {
|
||||
err({ line: self.line, col: self.col, msg: @msg })
|
||||
}
|
||||
|
||||
|
@ -153,7 +153,7 @@ impl parser for parser {
|
|||
if self.eof() {
|
||||
ok(value)
|
||||
} else {
|
||||
self.error("trailing characters")
|
||||
self.error(~"trailing characters")
|
||||
}
|
||||
}
|
||||
e { e }
|
||||
|
@ -163,12 +163,12 @@ impl parser for parser {
|
|||
fn parse_value() -> result<json, error> {
|
||||
self.parse_whitespace();
|
||||
|
||||
if self.eof() { ret self.error("EOF while parsing value"); }
|
||||
if self.eof() { ret self.error(~"EOF while parsing value"); }
|
||||
|
||||
alt self.ch {
|
||||
'n' { self.parse_ident("ull", null) }
|
||||
't' { self.parse_ident("rue", boolean(true)) }
|
||||
'f' { self.parse_ident("alse", boolean(false)) }
|
||||
'n' { self.parse_ident(~"ull", null) }
|
||||
't' { self.parse_ident(~"rue", boolean(true)) }
|
||||
'f' { self.parse_ident(~"alse", boolean(false)) }
|
||||
'0' to '9' | '-' { self.parse_number() }
|
||||
'"' {
|
||||
alt self.parse_str() {
|
||||
|
@ -178,7 +178,7 @@ impl parser for parser {
|
|||
}
|
||||
'[' { self.parse_list() }
|
||||
'{' { self.parse_object() }
|
||||
_ { self.error("invalid syntax") }
|
||||
_ { self.error(~"invalid syntax") }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,12 +186,12 @@ impl parser for parser {
|
|||
while char::is_whitespace(self.ch) { self.bump(); }
|
||||
}
|
||||
|
||||
fn parse_ident(ident: str, value: json) -> result<json, error> {
|
||||
fn parse_ident(ident: ~str, value: json) -> result<json, error> {
|
||||
if str::all(ident, |c| c == self.next_char()) {
|
||||
self.bump();
|
||||
ok(value)
|
||||
} else {
|
||||
self.error("invalid syntax")
|
||||
self.error(~"invalid syntax")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,7 +234,7 @@ impl parser for parser {
|
|||
|
||||
// There can be only one leading '0'.
|
||||
alt self.ch {
|
||||
'0' to '9' { ret self.error("invalid number"); }
|
||||
'0' to '9' { ret self.error(~"invalid number"); }
|
||||
_ {}
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ impl parser for parser {
|
|||
}
|
||||
}
|
||||
}
|
||||
_ { ret self.error("invalid number"); }
|
||||
_ { ret self.error(~"invalid number"); }
|
||||
}
|
||||
|
||||
ok(res)
|
||||
|
@ -263,7 +263,7 @@ impl parser for parser {
|
|||
// Make sure a digit follows the decimal place.
|
||||
alt self.ch {
|
||||
'0' to '9' {}
|
||||
_ { ret self.error("invalid number"); }
|
||||
_ { ret self.error(~"invalid number"); }
|
||||
}
|
||||
|
||||
let mut res = res;
|
||||
|
@ -299,7 +299,7 @@ impl parser for parser {
|
|||
// Make sure a digit follows the exponent place.
|
||||
alt self.ch {
|
||||
'0' to '9' {}
|
||||
_ { ret self.error("invalid number"); }
|
||||
_ { ret self.error(~"invalid number"); }
|
||||
}
|
||||
|
||||
while !self.eof() {
|
||||
|
@ -324,9 +324,9 @@ impl parser for parser {
|
|||
ok(res)
|
||||
}
|
||||
|
||||
fn parse_str() -> result<@str/~, error> {
|
||||
fn parse_str() -> result<@~str, error> {
|
||||
let mut escape = false;
|
||||
let mut res = "";
|
||||
let mut res = ~"";
|
||||
|
||||
while !self.eof() {
|
||||
self.bump();
|
||||
|
@ -351,19 +351,19 @@ impl parser for parser {
|
|||
n = n * 10u +
|
||||
(self.ch as uint) - ('0' as uint);
|
||||
}
|
||||
_ { ret self.error("invalid \\u escape"); }
|
||||
_ { ret self.error(~"invalid \\u escape"); }
|
||||
}
|
||||
i += 1u;
|
||||
}
|
||||
|
||||
// Error out if we didn't parse 4 digits.
|
||||
if i != 4u {
|
||||
ret self.error("invalid \\u escape");
|
||||
ret self.error(~"invalid \\u escape");
|
||||
}
|
||||
|
||||
str::push_char(res, n as char);
|
||||
}
|
||||
_ { ret self.error("invalid escape"); }
|
||||
_ { ret self.error(~"invalid escape"); }
|
||||
}
|
||||
escape = false;
|
||||
} else if self.ch == '\\' {
|
||||
|
@ -377,7 +377,7 @@ impl parser for parser {
|
|||
}
|
||||
}
|
||||
|
||||
self.error("EOF while parsing string")
|
||||
self.error(~"EOF while parsing string")
|
||||
}
|
||||
|
||||
fn parse_list() -> result<json, error> {
|
||||
|
@ -399,13 +399,13 @@ impl parser for parser {
|
|||
|
||||
self.parse_whitespace();
|
||||
if self.eof() {
|
||||
ret self.error("EOF while parsing list");
|
||||
ret self.error(~"EOF while parsing list");
|
||||
}
|
||||
|
||||
alt self.ch {
|
||||
',' { self.bump(); }
|
||||
']' { self.bump(); ret ok(list(@values)); }
|
||||
_ { ret self.error("expected `,` or `]`"); }
|
||||
_ { ret self.error(~"expected `,` or `]`"); }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -425,7 +425,7 @@ impl parser for parser {
|
|||
self.parse_whitespace();
|
||||
|
||||
if self.ch != '"' {
|
||||
ret self.error("key must be a string");
|
||||
ret self.error(~"key must be a string");
|
||||
}
|
||||
|
||||
let key = alt self.parse_str() {
|
||||
|
@ -437,7 +437,7 @@ impl parser for parser {
|
|||
|
||||
if self.ch != ':' {
|
||||
if self.eof() { break; }
|
||||
ret self.error("expected `:`");
|
||||
ret self.error(~"expected `:`");
|
||||
}
|
||||
self.bump();
|
||||
|
||||
|
@ -452,12 +452,12 @@ impl parser for parser {
|
|||
'}' { self.bump(); ret ok(dict(values)); }
|
||||
_ {
|
||||
if self.eof() { break; }
|
||||
ret self.error("expected `,` or `}`");
|
||||
ret self.error(~"expected `,` or `}`");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret self.error("EOF while parsing object");
|
||||
ret self.error(~"EOF while parsing object");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -474,7 +474,7 @@ fn from_reader(rdr: io::reader) -> result<json, error> {
|
|||
}
|
||||
|
||||
/// Deserializes a json value from a string
|
||||
fn from_str(s: str) -> result<json, error> {
|
||||
fn from_str(s: ~str) -> result<json, error> {
|
||||
io::with_str_reader(s, from_reader)
|
||||
}
|
||||
|
||||
|
@ -575,11 +575,11 @@ impl of to_json for bool {
|
|||
fn to_json() -> json { boolean(self) }
|
||||
}
|
||||
|
||||
impl of to_json for str {
|
||||
impl of to_json for ~str {
|
||||
fn to_json() -> json { string(@copy self) }
|
||||
}
|
||||
|
||||
impl of to_json for @str/~ {
|
||||
impl of to_json for @~str {
|
||||
fn to_json() -> json { string(self) }
|
||||
}
|
||||
|
||||
|
@ -602,7 +602,7 @@ impl <A: to_json> of to_json for ~[A] {
|
|||
fn to_json() -> json { list(@self.map(|elt| elt.to_json())) }
|
||||
}
|
||||
|
||||
impl <A: to_json copy> of to_json for hashmap<str, A> {
|
||||
impl <A: to_json copy> of to_json for hashmap<~str, A> {
|
||||
fn to_json() -> json {
|
||||
let d = map::str_hash();
|
||||
for self.each() |key, value| {
|
||||
|
@ -622,18 +622,18 @@ impl <A: to_json> of to_json for option<A> {
|
|||
}
|
||||
|
||||
impl of to_str::to_str for json {
|
||||
fn to_str() -> str { to_str(self) }
|
||||
fn to_str() -> ~str { to_str(self) }
|
||||
}
|
||||
|
||||
impl of to_str::to_str for error {
|
||||
fn to_str() -> str {
|
||||
fn to_str() -> ~str {
|
||||
#fmt("%u:%u: %s", self.line, self.col, *self.msg)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn mk_dict(items: ~[(str, json)]) -> json {
|
||||
fn mk_dict(items: ~[(~str, json)]) -> json {
|
||||
let d = map::str_hash();
|
||||
|
||||
do vec::iter(items) |item| {
|
||||
|
@ -646,229 +646,230 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_write_null() {
|
||||
assert to_str(null) == "null";
|
||||
assert to_str(null) == ~"null";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_num() {
|
||||
assert to_str(num(3f)) == "3";
|
||||
assert to_str(num(3.1f)) == "3.1";
|
||||
assert to_str(num(-1.5f)) == "-1.5";
|
||||
assert to_str(num(0.5f)) == "0.5";
|
||||
assert to_str(num(3f)) == ~"3";
|
||||
assert to_str(num(3.1f)) == ~"3.1";
|
||||
assert to_str(num(-1.5f)) == ~"-1.5";
|
||||
assert to_str(num(0.5f)) == ~"0.5";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_str() {
|
||||
assert to_str(string(@""/~)) == "\"\""/~;
|
||||
assert to_str(string(@"foo"/~)) == "\"foo\""/~;
|
||||
assert to_str(string(@~"")) == ~"\"\"";
|
||||
assert to_str(string(@~"foo")) == ~"\"foo\"";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_bool() {
|
||||
assert to_str(boolean(true)) == "true";
|
||||
assert to_str(boolean(false)) == "false";
|
||||
assert to_str(boolean(true)) == ~"true";
|
||||
assert to_str(boolean(false)) == ~"false";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_list() {
|
||||
assert to_str(list(@~[])) == "[]";
|
||||
assert to_str(list(@~[boolean(true)])) == "[true]";
|
||||
assert to_str(list(@~[])) == ~"[]";
|
||||
assert to_str(list(@~[boolean(true)])) == ~"[true]";
|
||||
assert to_str(list(@~[
|
||||
boolean(false),
|
||||
null,
|
||||
list(@~[string(@"foo\nbar"/~), num(3.5f)])
|
||||
])) == "[false, null, [\"foo\\nbar\", 3.5]]";
|
||||
list(@~[string(@~"foo\nbar"), num(3.5f)])
|
||||
])) == ~"[false, null, [\"foo\\nbar\", 3.5]]";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_dict() {
|
||||
assert to_str(mk_dict(~[])) == "{}";
|
||||
assert to_str(mk_dict(~[("a", boolean(true))])) == "{ \"a\": true }";
|
||||
assert to_str(mk_dict(~[])) == ~"{}";
|
||||
assert to_str(mk_dict(~[(~"a", boolean(true))]))
|
||||
== ~"{ \"a\": true }";
|
||||
assert to_str(mk_dict(~[
|
||||
("a", boolean(true)),
|
||||
("b", list(@~[
|
||||
mk_dict(~[("c", string(@"\x0c\r"/~))]),
|
||||
mk_dict(~[("d", string(@""/~))])
|
||||
(~"a", boolean(true)),
|
||||
(~"b", list(@~[
|
||||
mk_dict(~[(~"c", string(@~"\x0c\r"))]),
|
||||
mk_dict(~[(~"d", string(@~""))])
|
||||
]))
|
||||
])) ==
|
||||
"{ " +
|
||||
"\"a\": true, " +
|
||||
"\"b\": [" +
|
||||
"{ \"c\": \"\\f\\r\" }, " +
|
||||
"{ \"d\": \"\" }" +
|
||||
"]" +
|
||||
" }";
|
||||
~"{ " +
|
||||
~"\"a\": true, " +
|
||||
~"\"b\": [" +
|
||||
~"{ \"c\": \"\\f\\r\" }, " +
|
||||
~"{ \"d\": \"\" }" +
|
||||
~"]" +
|
||||
~" }";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailing_characters() {
|
||||
assert from_str("nulla") ==
|
||||
err({line: 1u, col: 5u, msg: @"trailing characters"/~});
|
||||
assert from_str("truea") ==
|
||||
err({line: 1u, col: 5u, msg: @"trailing characters"/~});
|
||||
assert from_str("falsea") ==
|
||||
err({line: 1u, col: 6u, msg: @"trailing characters"/~});
|
||||
assert from_str("1a") ==
|
||||
err({line: 1u, col: 2u, msg: @"trailing characters"/~});
|
||||
assert from_str("[]a") ==
|
||||
err({line: 1u, col: 3u, msg: @"trailing characters"/~});
|
||||
assert from_str("{}a") ==
|
||||
err({line: 1u, col: 3u, msg: @"trailing characters"/~});
|
||||
assert from_str(~"nulla") ==
|
||||
err({line: 1u, col: 5u, msg: @~"trailing characters"});
|
||||
assert from_str(~"truea") ==
|
||||
err({line: 1u, col: 5u, msg: @~"trailing characters"});
|
||||
assert from_str(~"falsea") ==
|
||||
err({line: 1u, col: 6u, msg: @~"trailing characters"});
|
||||
assert from_str(~"1a") ==
|
||||
err({line: 1u, col: 2u, msg: @~"trailing characters"});
|
||||
assert from_str(~"[]a") ==
|
||||
err({line: 1u, col: 3u, msg: @~"trailing characters"});
|
||||
assert from_str(~"{}a") ==
|
||||
err({line: 1u, col: 3u, msg: @~"trailing characters"});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_identifiers() {
|
||||
assert from_str("n") ==
|
||||
err({line: 1u, col: 2u, msg: @"invalid syntax"/~});
|
||||
assert from_str("nul") ==
|
||||
err({line: 1u, col: 4u, msg: @"invalid syntax"/~});
|
||||
assert from_str(~"n") ==
|
||||
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
||||
assert from_str(~"nul") ==
|
||||
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
||||
|
||||
assert from_str("t") ==
|
||||
err({line: 1u, col: 2u, msg: @"invalid syntax"/~});
|
||||
assert from_str("truz") ==
|
||||
err({line: 1u, col: 4u, msg: @"invalid syntax"/~});
|
||||
assert from_str(~"t") ==
|
||||
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
||||
assert from_str(~"truz") ==
|
||||
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
||||
|
||||
assert from_str("f") ==
|
||||
err({line: 1u, col: 2u, msg: @"invalid syntax"/~});
|
||||
assert from_str("faz") ==
|
||||
err({line: 1u, col: 3u, msg: @"invalid syntax"/~});
|
||||
assert from_str(~"f") ==
|
||||
err({line: 1u, col: 2u, msg: @~"invalid syntax"});
|
||||
assert from_str(~"faz") ==
|
||||
err({line: 1u, col: 3u, msg: @~"invalid syntax"});
|
||||
|
||||
assert from_str("null") == ok(null);
|
||||
assert from_str("true") == ok(boolean(true));
|
||||
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));
|
||||
assert from_str(~"null") == ok(null);
|
||||
assert from_str(~"true") == ok(boolean(true));
|
||||
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]
|
||||
fn test_read_num() {
|
||||
assert from_str("+") ==
|
||||
err({line: 1u, col: 1u, msg: @"invalid syntax"/~});
|
||||
assert from_str(".") ==
|
||||
err({line: 1u, col: 1u, msg: @"invalid syntax"/~});
|
||||
assert from_str(~"+") ==
|
||||
err({line: 1u, col: 1u, msg: @~"invalid syntax"});
|
||||
assert from_str(~".") ==
|
||||
err({line: 1u, col: 1u, msg: @~"invalid syntax"});
|
||||
|
||||
assert from_str("-") ==
|
||||
err({line: 1u, col: 2u, msg: @"invalid number"/~});
|
||||
assert from_str("00") ==
|
||||
err({line: 1u, col: 2u, msg: @"invalid number"/~});
|
||||
assert from_str("1.") ==
|
||||
err({line: 1u, col: 3u, msg: @"invalid number"/~});
|
||||
assert from_str("1e") ==
|
||||
err({line: 1u, col: 3u, msg: @"invalid number"/~});
|
||||
assert from_str("1e+") ==
|
||||
err({line: 1u, col: 4u, msg: @"invalid number"/~});
|
||||
assert from_str(~"-") ==
|
||||
err({line: 1u, col: 2u, msg: @~"invalid number"});
|
||||
assert from_str(~"00") ==
|
||||
err({line: 1u, col: 2u, msg: @~"invalid number"});
|
||||
assert from_str(~"1.") ==
|
||||
err({line: 1u, col: 3u, msg: @~"invalid number"});
|
||||
assert from_str(~"1e") ==
|
||||
err({line: 1u, col: 3u, msg: @~"invalid number"});
|
||||
assert from_str(~"1e+") ==
|
||||
err({line: 1u, col: 4u, msg: @~"invalid number"});
|
||||
|
||||
assert from_str("3") == ok(num(3f));
|
||||
assert from_str("3.1") == ok(num(3.1f));
|
||||
assert from_str("-1.2") == ok(num(-1.2f));
|
||||
assert from_str("0.4") == ok(num(0.4f));
|
||||
assert from_str("0.4e5") == ok(num(0.4e5f));
|
||||
assert from_str("0.4e+15") == ok(num(0.4e15f));
|
||||
assert from_str("0.4e-01") == ok(num(0.4e-01f));
|
||||
assert from_str(" 3 ") == ok(num(3f));
|
||||
assert from_str(~"3") == ok(num(3f));
|
||||
assert from_str(~"3.1") == ok(num(3.1f));
|
||||
assert from_str(~"-1.2") == ok(num(-1.2f));
|
||||
assert from_str(~"0.4") == ok(num(0.4f));
|
||||
assert from_str(~"0.4e5") == ok(num(0.4e5f));
|
||||
assert from_str(~"0.4e+15") == ok(num(0.4e15f));
|
||||
assert from_str(~"0.4e-01") == ok(num(0.4e-01f));
|
||||
assert from_str(~" 3 ") == ok(num(3f));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_str() {
|
||||
assert from_str("\"") ==
|
||||
err({line: 1u, col: 2u, msg: @"EOF while parsing string"/~});
|
||||
assert from_str("\"lol") ==
|
||||
err({line: 1u, col: 5u, msg: @"EOF while parsing string"/~});
|
||||
assert from_str(~"\"") ==
|
||||
err({line: 1u, col: 2u, msg: @~"EOF while parsing string"});
|
||||
assert from_str(~"\"lol") ==
|
||||
err({line: 1u, col: 5u, msg: @~"EOF while parsing string"});
|
||||
|
||||
assert from_str("\"\"") == ok(string(@""/~));
|
||||
assert from_str("\"foo\"") == ok(string(@"foo"/~));
|
||||
assert from_str("\"\\\"\"") == ok(string(@"\""/~));
|
||||
assert from_str("\"\\b\"") == ok(string(@"\x08"/~));
|
||||
assert from_str("\"\\n\"") == ok(string(@"\n"/~));
|
||||
assert from_str("\"\\r\"") == ok(string(@"\r"/~));
|
||||
assert from_str("\"\\t\"") == ok(string(@"\t"/~));
|
||||
assert from_str(" \"foo\" ") == ok(string(@"foo"/~));
|
||||
assert from_str(~"\"\"") == ok(string(@~""));
|
||||
assert from_str(~"\"foo\"") == ok(string(@~"foo"));
|
||||
assert from_str(~"\"\\\"\"") == ok(string(@~"\""));
|
||||
assert from_str(~"\"\\b\"") == ok(string(@~"\x08"));
|
||||
assert from_str(~"\"\\n\"") == ok(string(@~"\n"));
|
||||
assert from_str(~"\"\\r\"") == ok(string(@~"\r"));
|
||||
assert from_str(~"\"\\t\"") == ok(string(@~"\t"));
|
||||
assert from_str(~" \"foo\" ") == ok(string(@~"foo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_list() {
|
||||
assert from_str("[") ==
|
||||
err({line: 1u, col: 2u, msg: @"EOF while parsing value"/~});
|
||||
assert from_str("[1") ==
|
||||
err({line: 1u, col: 3u, msg: @"EOF while parsing list"/~});
|
||||
assert from_str("[1,") ==
|
||||
err({line: 1u, col: 4u, msg: @"EOF while parsing value"/~});
|
||||
assert from_str("[1,]") ==
|
||||
err({line: 1u, col: 4u, msg: @"invalid syntax"/~});
|
||||
assert from_str("[6 7]") ==
|
||||
err({line: 1u, col: 4u, msg: @"expected `,` or `]`"/~});
|
||||
assert from_str(~"[") ==
|
||||
err({line: 1u, col: 2u, msg: @~"EOF while parsing value"});
|
||||
assert from_str(~"[1") ==
|
||||
err({line: 1u, col: 3u, msg: @~"EOF while parsing list"});
|
||||
assert from_str(~"[1,") ==
|
||||
err({line: 1u, col: 4u, msg: @~"EOF while parsing value"});
|
||||
assert from_str(~"[1,]") ==
|
||||
err({line: 1u, col: 4u, msg: @~"invalid syntax"});
|
||||
assert from_str(~"[6 7]") ==
|
||||
err({line: 1u, col: 4u, msg: @~"expected `,` or `]`"});
|
||||
|
||||
assert from_str("[]") == ok(list(@~[]));
|
||||
assert from_str("[ ]") == ok(list(@~[]));
|
||||
assert from_str("[true]") == ok(list(@~[boolean(true)]));
|
||||
assert from_str("[ false ]") == ok(list(@~[boolean(false)]));
|
||||
assert from_str("[null]") == ok(list(@~[null]));
|
||||
assert from_str("[3, 1]") == ok(list(@~[num(3f), num(1f)]));
|
||||
assert from_str("\n[3, 2]\n") == ok(list(@~[num(3f), num(2f)]));
|
||||
assert from_str("[2, [4, 1]]") ==
|
||||
assert from_str(~"[]") == ok(list(@~[]));
|
||||
assert from_str(~"[ ]") == ok(list(@~[]));
|
||||
assert from_str(~"[true]") == ok(list(@~[boolean(true)]));
|
||||
assert from_str(~"[ false ]") == ok(list(@~[boolean(false)]));
|
||||
assert from_str(~"[null]") == ok(list(@~[null]));
|
||||
assert from_str(~"[3, 1]") == ok(list(@~[num(3f), num(1f)]));
|
||||
assert from_str(~"\n[3, 2]\n") == ok(list(@~[num(3f), num(2f)]));
|
||||
assert from_str(~"[2, [4, 1]]") ==
|
||||
ok(list(@~[num(2f), list(@~[num(4f), num(1f)])]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_dict() {
|
||||
assert from_str("{") ==
|
||||
err({line: 1u, col: 2u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str("{ ") ==
|
||||
err({line: 1u, col: 3u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str("{1") ==
|
||||
err({line: 1u, col: 2u, msg: @"key must be a string"/~});
|
||||
assert from_str("{ \"a\"") ==
|
||||
err({line: 1u, col: 6u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str("{\"a\"") ==
|
||||
err({line: 1u, col: 5u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str("{\"a\" ") ==
|
||||
err({line: 1u, col: 6u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str(~"{") ==
|
||||
err({line: 1u, col: 2u, msg: @~"EOF while parsing object"});
|
||||
assert from_str(~"{ ") ==
|
||||
err({line: 1u, col: 3u, msg: @~"EOF while parsing object"});
|
||||
assert from_str(~"{1") ==
|
||||
err({line: 1u, col: 2u, msg: @~"key must be a string"});
|
||||
assert from_str(~"{ \"a\"") ==
|
||||
err({line: 1u, col: 6u, msg: @~"EOF while parsing object"});
|
||||
assert from_str(~"{\"a\"") ==
|
||||
err({line: 1u, col: 5u, msg: @~"EOF while parsing object"});
|
||||
assert from_str(~"{\"a\" ") ==
|
||||
err({line: 1u, col: 6u, msg: @~"EOF while parsing object"});
|
||||
|
||||
assert from_str("{\"a\" 1") ==
|
||||
err({line: 1u, col: 6u, msg: @"expected `:`"/~});
|
||||
assert from_str("{\"a\":") ==
|
||||
err({line: 1u, col: 6u, msg: @"EOF while parsing value"/~});
|
||||
assert from_str("{\"a\":1") ==
|
||||
err({line: 1u, col: 7u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str("{\"a\":1 1") ==
|
||||
err({line: 1u, col: 8u, msg: @"expected `,` or `}`"/~});
|
||||
assert from_str("{\"a\":1,") ==
|
||||
err({line: 1u, col: 8u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str(~"{\"a\" 1") ==
|
||||
err({line: 1u, col: 6u, msg: @~"expected `:`"});
|
||||
assert from_str(~"{\"a\":") ==
|
||||
err({line: 1u, col: 6u, msg: @~"EOF while parsing value"});
|
||||
assert from_str(~"{\"a\":1") ==
|
||||
err({line: 1u, col: 7u, msg: @~"EOF while parsing object"});
|
||||
assert from_str(~"{\"a\":1 1") ==
|
||||
err({line: 1u, col: 8u, msg: @~"expected `,` or `}`"});
|
||||
assert from_str(~"{\"a\":1,") ==
|
||||
err({line: 1u, col: 8u, msg: @~"EOF while parsing object"});
|
||||
|
||||
assert eq(result::get(from_str("{}")), mk_dict(~[]));
|
||||
assert eq(result::get(from_str("{\"a\": 3}")),
|
||||
mk_dict(~[("a", num(3.0f))]));
|
||||
assert eq(result::get(from_str(~"{}")), mk_dict(~[]));
|
||||
assert eq(result::get(from_str(~"{\"a\": 3}")),
|
||||
mk_dict(~[(~"a", num(3.0f))]));
|
||||
|
||||
assert eq(result::get(from_str("{ \"a\": null, \"b\" : true }")),
|
||||
assert eq(result::get(from_str(~"{ \"a\": null, \"b\" : true }")),
|
||||
mk_dict(~[
|
||||
("a", null),
|
||||
("b", boolean(true))]));
|
||||
assert eq(result::get(from_str("\n{ \"a\": null, \"b\" : true }\n")),
|
||||
(~"a", null),
|
||||
(~"b", boolean(true))]));
|
||||
assert eq(result::get(from_str(~"\n{ \"a\": null, \"b\" : true }\n")),
|
||||
mk_dict(~[
|
||||
("a", null),
|
||||
("b", boolean(true))]));
|
||||
assert eq(result::get(from_str("{\"a\" : 1.0 ,\"b\": [ true ]}")),
|
||||
(~"a", null),
|
||||
(~"b", boolean(true))]));
|
||||
assert eq(result::get(from_str(~"{\"a\" : 1.0 ,\"b\": [ true ]}")),
|
||||
mk_dict(~[
|
||||
("a", num(1.0)),
|
||||
("b", list(@~[boolean(true)]))
|
||||
(~"a", num(1.0)),
|
||||
(~"b", list(@~[boolean(true)]))
|
||||
]));
|
||||
assert eq(result::get(from_str(
|
||||
"{" +
|
||||
"\"a\": 1.0, " +
|
||||
"\"b\": [" +
|
||||
"true," +
|
||||
"\"foo\\nbar\", " +
|
||||
"{ \"c\": {\"d\": null} } " +
|
||||
"]" +
|
||||
"}")),
|
||||
~"{" +
|
||||
~"\"a\": 1.0, " +
|
||||
~"\"b\": [" +
|
||||
~"true," +
|
||||
~"\"foo\\nbar\", " +
|
||||
~"{ \"c\": {\"d\": null} } " +
|
||||
~"]" +
|
||||
~"}")),
|
||||
mk_dict(~[
|
||||
("a", num(1.0f)),
|
||||
("b", list(@~[
|
||||
(~"a", num(1.0f)),
|
||||
(~"b", list(@~[
|
||||
boolean(true),
|
||||
string(@"foo\nbar"/~),
|
||||
string(@~"foo\nbar"),
|
||||
mk_dict(~[
|
||||
("c", mk_dict(~[("d", null)]))
|
||||
(~"c", mk_dict(~[(~"d", null)]))
|
||||
])
|
||||
]))
|
||||
]));
|
||||
|
@ -876,7 +877,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_multiline_errors() {
|
||||
assert from_str("{\n \"foo\":\n \"bar\"") ==
|
||||
err({line: 3u, col: 8u, msg: @"EOF while parsing object"/~});
|
||||
assert from_str(~"{\n \"foo\":\n \"bar\"") ==
|
||||
err({line: 3u, col: 8u, msg: @~"EOF while parsing object"});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue