Skip to content

Commit 9e94be3

Browse files
committed
---
yaml --- r: 60447 b: refs/heads/auto c: 7a2afb7 h: refs/heads/master i: 60445: e59e142 60443: 1650782 60439: 16e9f37 60431: 685ef65 60415: 3e4adb0 v: v3
1 parent 274960b commit 9e94be3

10 files changed

+153
-9
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 916942d006ce3a551505b7b0328a82b382249b7c
17+
refs/heads/auto: 7a2afb72884774f8e7865c625efc806fd09e693b
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libsyntax/ext/bytes.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,58 @@ use ext::base::*;
1616
use ext::base;
1717
use ext::build::{mk_u8, mk_slice_vec_e};
1818

19-
pub fn expand_syntax_ext(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
20-
-> base::MacResult {
21-
let var = get_single_str_from_tts(cx, sp, tts, "bytes!");
19+
pub fn expand_syntax_ext(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree]) -> base::MacResult {
20+
// Gather all argument expressions
21+
let exprs = get_exprs_from_tts(cx, tts);
2222
let mut bytes = ~[];
23-
for var.each |byte| {
24-
bytes.push(mk_u8(cx, sp, byte));
23+
24+
for exprs.each |expr| {
25+
match expr.node {
26+
// expression is a literal
27+
ast::expr_lit(lit) => match lit.node {
28+
// string literal, push each byte to vector expression
29+
ast::lit_str(s) => {
30+
for s.each |byte| {
31+
bytes.push(mk_u8(cx, sp, byte));
32+
}
33+
}
34+
35+
// u8 literal, push to vector expression
36+
ast::lit_uint(v, ast::ty_u8) => {
37+
if v > 0xFF {
38+
cx.span_err(sp, "Too large u8 literal in bytes!")
39+
} else {
40+
bytes.push(mk_u8(cx, sp, v as u8));
41+
}
42+
}
43+
44+
// integer literal, push to vector expression
45+
ast::lit_int_unsuffixed(v) => {
46+
if v > 0xFF {
47+
cx.span_err(sp, "Too large integer literal in bytes!")
48+
} else if v < 0 {
49+
cx.span_err(sp, "Negative integer literal in bytes!")
50+
} else {
51+
bytes.push(mk_u8(cx, sp, v as u8));
52+
}
53+
}
54+
55+
// char literal, push to vector expression
56+
ast::lit_int(v, ast::ty_char) => {
57+
if (v as char).is_ascii() {
58+
bytes.push(mk_u8(cx, sp, v as u8));
59+
} else {
60+
cx.span_err(sp, "Non-ascii char literal in bytes!")
61+
}
62+
}
63+
64+
_ => cx.span_err(sp, "Unsupported literal in bytes!")
65+
},
66+
67+
_ => cx.span_err(sp, "Non-literal in bytes!")
68+
}
2569
}
70+
2671
let e = mk_slice_vec_e(cx, sp, bytes);
2772
MRExpr(e)
2873
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!('λ'); //~ ERROR Non-ascii char literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(foo); //~ ERROR Non-literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(1024); //~ ERROR Too large integer literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(1024u8); //~ ERROR Too large u8 literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(-1024); //~ ERROR Non-literal in bytes
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(-1024u8); //~ ERROR Non-literal in bytes
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let vec = bytes!(45f); //~ ERROR Unsupported literal in bytes!
13+
}

branches/auto/src/test/run-pass/syntax-extension-bytes.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
static static_vec: &'static [u8] = bytes!("abc", 0xFF, '!');
12+
1113
fn main() {
1214
let vec = bytes!("abc");
13-
assert_eq!(vec[0], 97);
14-
assert_eq!(vec[1], 98);
15-
assert_eq!(vec[2], 99);
15+
assert_eq!(vec, &[97_u8, 98_u8, 99_u8]);
16+
17+
let vec = bytes!("null", 0);
18+
assert_eq!(vec, &[110_u8, 117_u8, 108_u8, 108_u8, 0_u8]);
19+
20+
let vec = bytes!(' ', " ", 32, 32u8);
21+
assert_eq!(vec, &[32_u8, 32_u8, 32_u8, 32_u8]);
22+
23+
assert_eq!(static_vec, &[97_u8, 98_u8, 99_u8, 255_u8, 33_u8]);
1624
}

0 commit comments

Comments
 (0)