Skip to content

Commit b01ff76

Browse files
committed
---
yaml --- r: 152612 b: refs/heads/try2 c: b8a4c14 h: refs/heads/master v: v3
1 parent 602a94a commit b01ff76

File tree

8 files changed

+103
-6
lines changed

8 files changed

+103
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: d7e01b5809cd600a30bab29da698acb3d1b52409
8+
refs/heads/try2: b8a4c1415b154fa1e5bd8bb54e681f0f5e21e2a4
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
140140
}
141141

142142
// text literals
143-
t::LIT_BYTE(..) | t::LIT_BINARY(..) |
143+
t::LIT_BYTE(..) | t::LIT_BINARY(..) | t::LIT_BINARY_RAW(..) |
144144
t::LIT_CHAR(..) | t::LIT_STR(..) | t::LIT_STR_RAW(..) => "string",
145145

146146
// number literals

branches/try2/src/libsyntax/parse/lexer/mod.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,13 @@ impl<'a> StringReader<'a> {
650650
/// token, and updates the interner
651651
fn next_token_inner(&mut self) -> token::Token {
652652
let c = self.curr;
653-
if ident_start(c) && match (c.unwrap(), self.nextch()) {
653+
if ident_start(c) && match (c.unwrap(), self.nextch(), self.nextnextch()) {
654654
// Note: r as in r" or r#" is part of a raw string literal,
655655
// b as in b' is part of a byte literal.
656656
// They are not identifiers, and are handled further down.
657-
('r', Some('"')) | ('r', Some('#')) |
658-
('b', Some('"')) | ('b', Some('\'')) => false,
657+
('r', Some('"'), _) | ('r', Some('#'), _) |
658+
('b', Some('"'), _) | ('b', Some('\''), _) |
659+
('b', Some('r'), Some('"')) | ('b', Some('r'), Some('#')) => false,
659660
_ => true
660661
} {
661662
let start = self.last_pos;
@@ -863,6 +864,7 @@ impl<'a> StringReader<'a> {
863864
return match self.curr {
864865
Some('\'') => parse_byte(self),
865866
Some('"') => parse_byte_string(self),
867+
Some('r') => parse_raw_byte_string(self),
866868
_ => unreachable!() // Should have been a token::IDENT above.
867869
};
868870

@@ -978,6 +980,54 @@ impl<'a> StringReader<'a> {
978980
self_.bump();
979981
return token::LIT_BINARY(Rc::new(value));
980982
}
983+
984+
fn parse_raw_byte_string(self_: &mut StringReader) -> token::Token {
985+
let start_bpos = self_.last_pos;
986+
self_.bump();
987+
let mut hash_count = 0u;
988+
while self_.curr_is('#') {
989+
self_.bump();
990+
hash_count += 1;
991+
}
992+
993+
if self_.is_eof() {
994+
self_.fatal_span(start_bpos, self_.last_pos, "unterminated raw string");
995+
} else if !self_.curr_is('"') {
996+
self_.fatal_span_char(start_bpos, self_.last_pos,
997+
"only `#` is allowed in raw string delimitation; \
998+
found illegal character",
999+
self_.curr.unwrap());
1000+
}
1001+
self_.bump();
1002+
let content_start_bpos = self_.last_pos;
1003+
let mut content_end_bpos;
1004+
'outer: loop {
1005+
match self_.curr {
1006+
None => self_.fatal_span(start_bpos, self_.last_pos,
1007+
"unterminated raw string"),
1008+
Some('"') => {
1009+
content_end_bpos = self_.last_pos;
1010+
for _ in range(0, hash_count) {
1011+
self_.bump();
1012+
if !self_.curr_is('#') {
1013+
continue 'outer;
1014+
}
1015+
}
1016+
break;
1017+
},
1018+
Some(c) => if c > '\x7F' {
1019+
self_.err_span_char(self_.last_pos, self_.last_pos,
1020+
"raw byte string must be ASCII", c);
1021+
}
1022+
}
1023+
self_.bump();
1024+
}
1025+
self_.bump();
1026+
let bytes = self_.with_str_from_to(content_start_bpos,
1027+
content_end_bpos,
1028+
|s| s.as_bytes().to_owned());
1029+
return token::LIT_BINARY_RAW(Rc::new(bytes), hash_count);
1030+
}
9811031
}
9821032
'"' => {
9831033
let mut accum_str = String::new();

branches/try2/src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ impl<'a> Parser<'a> {
15291529
token::LIT_STR_RAW(s, n) => {
15301530
LitStr(self.id_to_interned_str(s), ast::RawStr(n))
15311531
}
1532+
token::LIT_BINARY_RAW(ref v, _) |
15321533
token::LIT_BINARY(ref v) => LitBinary(v.clone()),
15331534
token::LPAREN => { self.expect(&token::RPAREN); LitNil },
15341535
_ => { self.unexpected_last(tok); }

branches/try2/src/libsyntax/parse/token.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub enum Token {
8888
LIT_STR(ast::Ident),
8989
LIT_STR_RAW(ast::Ident, uint), /* raw str delimited by n hash symbols */
9090
LIT_BINARY(Rc<Vec<u8>>),
91+
LIT_BINARY_RAW(Rc<Vec<u8>>, uint), /* raw binary str delimited by n hash symbols */
9192

9293
/* Name components */
9394
// an identifier contains an "is_mod_name" boolean,
@@ -243,6 +244,10 @@ pub fn to_str(t: &Token) -> String {
243244
"b\"{}\"",
244245
v.iter().map(|&b| b as char).collect::<String>().escape_default())
245246
}
247+
LIT_BINARY_RAW(ref s, n) => {
248+
format!("br{delim}\"{string}\"{delim}",
249+
delim="#".repeat(n), string=s.as_slice().to_ascii().as_str_ascii())
250+
}
246251

247252
/* Name components */
248253
IDENT(s, _) => get_ident(s).get().to_string(),
@@ -298,6 +303,7 @@ pub fn can_begin_expr(t: &Token) -> bool {
298303
LIT_STR(_) => true,
299304
LIT_STR_RAW(_, _) => true,
300305
LIT_BINARY(_) => true,
306+
LIT_BINARY_RAW(_, _) => true,
301307
POUND => true,
302308
AT => true,
303309
NOT => true,
@@ -338,6 +344,7 @@ pub fn is_lit(t: &Token) -> bool {
338344
LIT_STR(_) => true,
339345
LIT_STR_RAW(_, _) => true,
340346
LIT_BINARY(_) => true,
347+
LIT_BINARY_RAW(_, _) => true,
341348
_ => false
342349
}
343350
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 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+
12+
pub fn main() {
13+
br##"a"#; //~ unterminated raw string
14+
}
15+
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 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+
12+
pub fn main() {
13+
br"é"; //~ raw byte string must be ASCII
14+
br##~"a"~##; //~ only `#` is allowed in raw string delimitation
15+
}
16+
17+

branches/try2/src/test/run-pass/byte-literals.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
static FOO: u8 = b'\xF0';
1313
static BAR: &'static [u8] = b"a\xF0\t";
14+
static BAZ: &'static [u8] = br"a\n";
1415

1516
pub fn main() {
1617
assert_eq!(b'a', 97u8);
@@ -24,7 +25,6 @@ pub fn main() {
2425
assert_eq!(b'\xF0', 240u8);
2526
assert_eq!(FOO, 240u8);
2627

27-
// FIXME: Do we want this to be valid?
2828
assert_eq!([42, ..b'\t'].as_slice(), &[42, 42, 42, 42, 42, 42, 42, 42, 42]);
2929

3030
match 42 {
@@ -47,4 +47,10 @@ pub fn main() {
4747
b"a\n" => {},
4848
_ => fail!(),
4949
}
50+
51+
assert_eq!(BAZ, &[97u8, 92u8, 110u8]);
52+
assert_eq!(br"a\n", &[97u8, 92u8, 110u8]);
53+
assert_eq!(br"a\n", b"a\\n");
54+
assert_eq!(br###"a"##b"###, &[97u8, 34u8, 35u8, 35u8, 98u8]);
55+
assert_eq!(br###"a"##b"###, b"a\"##b");
5056
}

0 commit comments

Comments
 (0)