Skip to content

Commit 29a6f23

Browse files
committed
---
yaml --- r: 44344 b: refs/heads/master c: 7fe6b1b h: refs/heads/master v: v3
1 parent bb67299 commit 29a6f23

File tree

4 files changed

+37
-165
lines changed

4 files changed

+37
-165
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 7736ed6c6251fcb3544d50c15290df6ac9542216
2+
refs/heads/master: 7fe6b1b6cfcdf52fd99f02b1d999c480e134e30f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/src/libstd/json.rs

Lines changed: 8 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -121,57 +121,26 @@ pub impl Encoder: serialize::Encoder {
121121
fn emit_owned(&self, f: fn()) { f() }
122122
fn emit_managed(&self, f: fn()) { f() }
123123

124-
fn emit_enum(&self, _name: &str, f: fn()) {
124+
fn emit_enum(&self, name: &str, f: fn()) {
125+
if name != "option" { die!(~"only supports option enum") }
125126
f()
126127
}
127-
128-
fn emit_enum_variant(&self, name: &str, _id: uint, _cnt: uint, f: fn()) {
129-
// encoding of enums is special-cased for Option. Specifically:
130-
// Some(34) => 34
131-
// None => null
132-
133-
// other enums are encoded as vectors:
134-
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
135-
136-
// the default expansion for enums is more verbose than I'd like;
137-
// specifically, the inner pair of brackets seems superfluous,
138-
// BUT the design of the enumeration framework and the requirements
139-
// of the special-case for Option mean that a first argument must
140-
// be encoded "naked"--with no commas--and that the option name
141-
// can't be followed by just a comma, because there might not
142-
// be any elements in the tuple.
143-
144-
// this would be more precise and less frightening
145-
// with fully-qualified option names. To get that information,
146-
// we'd have to change the expansion of auto-encode to pass
147-
// those along.
148-
149-
if (name == ~"Some") {
150-
f();
151-
} else if (name == ~"None") {
152-
self.wr.write_str(~"null");
128+
fn emit_enum_variant(&self, _name: &str, id: uint, _cnt: uint, f: fn()) {
129+
if id == 0 {
130+
self.emit_nil();
153131
} else {
154-
self.wr.write_char('[');
155-
self.wr.write_str(escape_str(name));
156-
self.wr.write_char(',');
157-
self.wr.write_char('[');
158-
f();
159-
self.wr.write_char(']');
160-
self.wr.write_char(']');
132+
f()
161133
}
162134
}
163-
164-
fn emit_enum_variant_arg(&self, idx: uint, f: fn()) {
165-
if (idx != 0) {self.wr.write_char(',');}
166-
f();
135+
fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) {
136+
f()
167137
}
168138
169139
fn emit_borrowed_vec(&self, _len: uint, f: fn()) {
170140
self.wr.write_char('[');
171141
f();
172142
self.wr.write_char(']');
173143
}
174-
175144
fn emit_owned_vec(&self, len: uint, f: fn()) {
176145
self.emit_borrowed_vec(len, f)
177146
}
@@ -1211,8 +1180,6 @@ mod tests {
12111180
12121181
use core::result;
12131182
use core::hashmap::linear::LinearMap;
1214-
use core::cmp;
1215-
12161183
12171184
fn mk_object(items: &[(~str, Json)]) -> Json {
12181185
let mut d = ~LinearMap::new();
@@ -1280,72 +1247,6 @@ mod tests {
12801247
assert a == b;
12811248
}
12821249

1283-
// two fns copied from libsyntax/util/testing.rs.
1284-
// Should they be in their own crate?
1285-
pub pure fn check_equal_ptr<T : cmp::Eq> (given : &T, expected: &T) {
1286-
if !((given == expected) && (expected == given )) {
1287-
die!(fmt!("given %?, expected %?",given,expected));
1288-
}
1289-
}
1290-
1291-
pub pure fn check_equal<T : cmp::Eq> (given : T, expected: T) {
1292-
if !((given == expected) && (expected == given )) {
1293-
die!(fmt!("given %?, expected %?",given,expected));
1294-
}
1295-
}
1296-
1297-
// testing both auto_encode's calling patterns
1298-
// and json... not sure where to put these tests.
1299-
#[test]
1300-
fn test_write_enum () {
1301-
let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
1302-
let bww : @io::Writer = (bw as @io::Writer);
1303-
let encoder = (@Encoder(bww) as @serialize::Encoder);
1304-
do encoder.emit_enum(~"animal") {
1305-
do encoder.emit_enum_variant (~"frog",37,1242) {
1306-
// name of frog:
1307-
do encoder.emit_enum_variant_arg (0) {
1308-
encoder.emit_owned_str(~"Henry")
1309-
}
1310-
// mass of frog in grams:
1311-
do encoder.emit_enum_variant_arg (1) {
1312-
encoder.emit_int(349);
1313-
}
1314-
}
1315-
}
1316-
check_equal(str::from_bytes(bw.bytes.data),
1317-
~"[\"frog\",[\"Henry\",349]]");
1318-
}
1319-
1320-
#[test]
1321-
fn test_write_some () {
1322-
let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
1323-
let bww : @io::Writer = (bw as @io::Writer);
1324-
let encoder = (@Encoder(bww) as @serialize::Encoder);
1325-
do encoder.emit_enum(~"Option") {
1326-
do encoder.emit_enum_variant (~"Some",37,1242) {
1327-
do encoder.emit_enum_variant_arg (0) {
1328-
encoder.emit_owned_str(~"jodhpurs")
1329-
}
1330-
}
1331-
}
1332-
check_equal(str::from_bytes(bw.bytes.data),
1333-
~"\"jodhpurs\"");
1334-
}
1335-
1336-
#[test]
1337-
fn test_write_none () {
1338-
let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
1339-
let bww : @io::Writer = (bw as @io::Writer);
1340-
let encoder = (@Encoder(bww) as @serialize::Encoder);
1341-
do encoder.emit_enum(~"Option") {
1342-
do encoder.emit_enum_variant (~"None",37,1242) {
1343-
}
1344-
}
1345-
check_equal(str::from_bytes(bw.bytes.data),
1346-
~"null");
1347-
}
1348-
13491250
#[test]
13501251
fn test_trailing_characters() {
13511252
assert from_str(~"nulla") ==

trunk/src/libsyntax/diagnostic.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn highlight_lines(cm: @codemap::CodeMap,
250250
io::stderr().write_str(out);
251251
}
252252

253-
253+
// FIXME (#3260)
254254
// If there's one line at fault we can easily point to the problem
255255
if vec::len(lines.lines) == 1u {
256256
let lo = cm.lookup_char_pos(sp.lo);
@@ -263,14 +263,26 @@ fn highlight_lines(cm: @codemap::CodeMap,
263263
// indent past |name:## | and the 0-offset column location
264264
let mut left = str::len(fm.name) + digits + lo.col.to_uint() + 3u;
265265
let mut s = ~"";
266-
while left > 0u { str::push_char(&mut s, ' '); left -= 1u; }
267-
266+
// Skip is the number of characters we need to skip because they are
267+
// part of the 'filename:line ' part of the previous line.
268+
let skip = str::len(fm.name) + digits + 3u;
269+
for skip.times() {
270+
s += ~" ";
271+
}
272+
let orig = fm.get_line(lines.lines[0] as int);
273+
for uint::range(0u,left-skip) |pos| {
274+
let curChar = (orig[pos] as char);
275+
s += match curChar { // Whenever a tab occurs on the previous
276+
'\t' => "\t", // line, we insert one on the error-point-
277+
_ => " " // -squigly-line as well (instead of a
278+
}; // space). This way the squigly-line will
279+
} // usually appear in the correct position.
268280
s += ~"^";
269281
let hi = cm.lookup_char_pos(sp.hi);
270282
if hi.col != lo.col {
271283
// the ^ already takes up one space
272-
let mut width = hi.col.to_uint() - lo.col.to_uint() - 1u;
273-
while width > 0u { str::push_char(&mut s, '~'); width -= 1u; }
284+
let num_squiglies = hi.col.to_uint()-lo.col.to_uint()-1u;
285+
for num_squiglies.times() { s += ~"~"; }
274286
}
275287
io::stderr().write_str(s + ~"\n");
276288
}

trunk/src/libsyntax/ext/auto_encode.rs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ For example, a type like:
2323
2424
would generate two implementations like:
2525
26-
impl<S: std::serialize::Encoder> Node: Encodable<S> {
27-
fn encode(&self, s: &S) {
28-
do s.emit_struct("Node", 1) {
29-
s.emit_field("id", 0, || s.emit_uint(self.id))
26+
impl<S: Encoder> node_id: Encodable<S> {
27+
fn encode(s: &S) {
28+
do s.emit_struct("Node", 1) {
29+
s.emit_field("id", 0, || s.emit_uint(self))
30+
}
3031
}
3132
}
32-
}
3333
34-
impl<D: Decoder> node_id: Decodable {
35-
static fn decode(d: &D) -> Node {
36-
do d.read_struct("Node", 1) {
37-
Node {
38-
id: d.read_field(~"x", 0, || decode(d))
34+
impl<D: Decoder> node_id: Decodable {
35+
static fn decode(d: &D) -> Node {
36+
do d.read_struct("Node", 1) {
37+
Node {
38+
id: d.read_field(~"x", 0, || decode(d))
39+
}
3940
}
4041
}
4142
}
42-
}
4343
4444
Other interesting scenarios are whe the item has type parameters or
4545
references other non-built-in types. A type definition like:
@@ -1150,44 +1150,3 @@ fn mk_enum_deser_body(
11501150
]
11511151
)
11521152
}
1153-
1154-
1155-
#[cfg(test)]
1156-
mod test {
1157-
use std::serialize::Encodable;
1158-
use core::dvec::*;
1159-
use util::testing::*;
1160-
use core::io;
1161-
use core::str;
1162-
use std;
1163-
1164-
#[auto_decode]
1165-
#[auto_encode]
1166-
struct Node {id: uint}
1167-
1168-
fn to_json_str (val: Encodable<std::json::Encoder>) -> ~str{
1169-
let bw = @io::BytesWriter {bytes: DVec(), pos: 0};
1170-
val.encode(~std::json::Encoder(bw as io::Writer));
1171-
str::from_bytes(bw.bytes.data)
1172-
}
1173-
1174-
#[test] fn encode_test () {
1175-
check_equal (to_json_str(Node{id:34}
1176-
as Encodable::<std::json::Encoder>),
1177-
~"{\"id\":34}");
1178-
}
1179-
1180-
#[auto_encode]
1181-
enum written {
1182-
Book(int),
1183-
Magazine(~str)
1184-
}
1185-
1186-
#[test] fn json_enum_encode_test () {
1187-
check_equal (to_json_str(Book(9) as Encodable::<std::json::Encoder>),
1188-
~"[\"Book\",9]");
1189-
check_equal (to_json_str(Magazine(~"Paris Match")
1190-
as Encodable::<std::json::Encoder>),
1191-
~"[\"Magazine\",\"Paris Match\"]");
1192-
}
1193-
}

0 commit comments

Comments
 (0)