Skip to content

Commit bccdba0

Browse files
committed
Add a b'x' byte literal of type u8.
1 parent 2fd618e commit bccdba0

File tree

16 files changed

+169
-5
lines changed

16 files changed

+169
-5
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ pub fn lit_to_const(lit: &Lit) -> const_val {
506506
LitBinary(ref data) => {
507507
const_binary(Rc::new(data.iter().map(|x| *x).collect()))
508508
}
509+
LitByte(n) => const_uint(n as u64),
509510
LitChar(n) => const_uint(n as u64),
510511
LitInt(n, _) => const_int(n),
511512
LitUint(n, _) => const_uint(n),

src/librustc/middle/lint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ fn check_type_limits(cx: &Context, e: &ast::Expr) {
805805
} else { t };
806806
let (min, max) = uint_ty_range(uint_type);
807807
let lit_val: u64 = match lit.node {
808+
ast::LitByte(_v) => return, // _v is u8, within range by definition
808809
ast::LitInt(v, _) => v as u64,
809810
ast::LitUint(v, _) => v,
810811
ast::LitIntUnsuffixed(v) => v as u64,

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: ast::Lit)
4343
-> ValueRef {
4444
let _icx = push_ctxt("trans_lit");
4545
match lit.node {
46+
ast::LitByte(b) => C_integral(Type::uint_from_ty(cx, ast::TyU8), b as u64, false),
4647
ast::LitChar(i) => C_integral(Type::char(cx), i as u64, false),
4748
ast::LitInt(i, t) => C_integral(Type::int_from_ty(cx, t), i as u64, true),
4849
ast::LitUint(u, t) => C_integral(Type::uint_from_ty(cx, t), u, false),

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ pub fn check_lit(fcx: &FnCtxt, lit: &ast::Lit) -> ty::t {
17151715
ast::LitBinary(..) => {
17161716
ty::mk_slice(tcx, ty::ReStatic, ty::mt{ ty: ty::mk_u8(), mutbl: ast::MutImmutable })
17171717
}
1718+
ast::LitByte(_) => ty::mk_u8(),
17181719
ast::LitChar(_) => ty::mk_char(),
17191720
ast::LitInt(_, t) => ty::mk_mach_int(t),
17201721
ast::LitUint(_, t) => ty::mk_mach_uint(t),

src/librustdoc/clean/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,14 @@ fn lit_to_str(lit: &ast::Lit) -> String {
19241924
match lit.node {
19251925
ast::LitStr(ref st, _) => st.get().to_string(),
19261926
ast::LitBinary(ref data) => format!("{:?}", data.as_slice()),
1927+
ast::LitByte(b) => {
1928+
let mut res = String::from_str("b'");
1929+
(b as char).escape_default(|c| {
1930+
res.push_char(c);
1931+
});
1932+
res.push_char('\'');
1933+
res
1934+
},
19271935
ast::LitChar(c) => format!("'{}'", c),
19281936
ast::LitInt(i, _t) => i.to_str(),
19291937
ast::LitUint(u, _t) => u.to_str(),

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_CHAR(..) | t::LIT_STR(..) | t::LIT_STR_RAW(..) => "string",
143+
t::LIT_BYTE(..) | t::LIT_CHAR(..) | t::LIT_STR(..) | t::LIT_STR_RAW(..) => "string",
144144

145145
// number literals
146146
t::LIT_INT(..) | t::LIT_UINT(..) | t::LIT_INT_UNSUFFIXED(..) |

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ pub type Lit = Spanned<Lit_>;
616616
pub enum Lit_ {
617617
LitStr(InternedString, StrStyle),
618618
LitBinary(Rc<Vec<u8> >),
619+
LitByte(u8),
619620
LitChar(char),
620621
LitInt(i64, IntTy),
621622
LitUint(u64, UintTy),

src/libsyntax/ext/concat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
4747
ast::LitBool(b) => {
4848
accumulator.push_str(format!("{}", b).as_slice());
4949
}
50+
ast::LitByte(..) |
5051
ast::LitBinary(..) => {
5152
cx.span_err(e.span, "cannot concatenate a binary literal");
5253
}

src/libsyntax/ext/quote.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
436436
vec!(mk_binop(cx, sp, binop)));
437437
}
438438

439+
LIT_BYTE(i) => {
440+
let e_byte = cx.expr_lit(sp, ast::LitByte(i));
441+
442+
return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_BYTE"), vec!(e_byte));
443+
}
444+
439445
LIT_CHAR(i) => {
440446
let e_char = cx.expr_lit(sp, ast::LitChar(i));
441447

src/libsyntax/parse/lexer/mod.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +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) && !self.nextch_is('"') && !self.nextch_is('#') {
653+
if ident_start(c) && match (c.unwrap(), self.nextch()) {
654654
// Note: r as in r" or r#" is part of a raw string literal,
655-
// not an identifier, and is handled further down.
656-
655+
// b as in b' is part of a byte literal.
656+
// They are not identifiers, and are handled further down.
657+
('r', Some('"')) | ('r', Some('#')) | ('b', Some('\'')) => false,
658+
_ => true
659+
} {
657660
let start = self.last_pos;
658661
while ident_continue(self.curr) {
659662
self.bump();
@@ -854,6 +857,65 @@ impl<'a> StringReader<'a> {
854857
self.bump(); // advance curr past token
855858
return token::LIT_CHAR(c2);
856859
}
860+
'b' => {
861+
self.bump();
862+
assert!(self.curr_is('\''), "Should have been a token::IDENT");
863+
self.bump();
864+
let start = self.last_pos;
865+
866+
// the eof will be picked up by the final `'` check below
867+
let mut c2 = self.curr.unwrap_or('\x00');
868+
self.bump();
869+
870+
match c2 {
871+
'\\' => {
872+
// '\X' for some X must be a character constant:
873+
let escaped = self.curr;
874+
let escaped_pos = self.last_pos;
875+
self.bump();
876+
match escaped {
877+
None => {}
878+
Some(e) => {
879+
c2 = match e {
880+
'n' => '\n',
881+
'r' => '\r',
882+
't' => '\t',
883+
'\\' => '\\',
884+
'\'' => '\'',
885+
'"' => '"',
886+
'0' => '\x00',
887+
'x' => self.scan_numeric_escape(2u, '\''),
888+
c2 => {
889+
self.err_span_char(escaped_pos, self.last_pos,
890+
"unknown byte escape", c2);
891+
c2
892+
}
893+
}
894+
}
895+
}
896+
}
897+
'\t' | '\n' | '\r' | '\'' => {
898+
self.err_span_char( start, self.last_pos,
899+
"byte constant must be escaped", c2);
900+
}
901+
_ if c2 > '\x7F' => {
902+
self.err_span_char( start, self.last_pos,
903+
"byte constant must be ASCII. \
904+
Use a \\xHH escape for a non-ASCII byte", c2);
905+
}
906+
_ => {}
907+
}
908+
if !self.curr_is('\'') {
909+
self.fatal_span_verbose(
910+
// Byte offsetting here is okay because the
911+
// character before position `start` are an
912+
// ascii single quote and ascii 'b'.
913+
start - BytePos(2), self.last_pos,
914+
"unterminated byte constant".to_string());
915+
}
916+
self.bump(); // advance curr past token
917+
return token::LIT_BYTE(c2 as u8);
918+
}
857919
'"' => {
858920
let mut accum_str = String::new();
859921
let start_bpos = self.last_pos;

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod};
3333
use ast::{Ident, NormalFn, Inherited, Item, Item_, ItemStatic};
3434
use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl};
3535
use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy, Lit, Lit_};
36-
use ast::{LitBool, LitFloat, LitFloatUnsuffixed, LitInt, LitChar};
36+
use ast::{LitBool, LitFloat, LitFloatUnsuffixed, LitInt, LitChar, LitByte};
3737
use ast::{LitIntUnsuffixed, LitNil, LitStr, LitUint, Local, LocalLet};
3838
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal};
3939
use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability};
@@ -1512,6 +1512,7 @@ impl<'a> Parser<'a> {
15121512
// matches token_lit = LIT_INT | ...
15131513
pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ {
15141514
match *tok {
1515+
token::LIT_BYTE(i) => LitByte(i),
15151516
token::LIT_CHAR(i) => LitChar(i),
15161517
token::LIT_INT(i, it) => LitInt(i, it),
15171518
token::LIT_UINT(u, ut) => LitUint(u, ut),

src/libsyntax/parse/token.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub enum Token {
7878
DOLLAR,
7979

8080
/* Literals */
81+
LIT_BYTE(u8),
8182
LIT_CHAR(char),
8283
LIT_INT(i64, ast::IntTy),
8384
LIT_UINT(u64, ast::UintTy),
@@ -193,6 +194,14 @@ pub fn to_str(t: &Token) -> String {
193194
DOLLAR => "$".to_string(),
194195

195196
/* Literals */
197+
LIT_BYTE(b) => {
198+
let mut res = String::from_str("b'");
199+
(b as char).escape_default(|c| {
200+
res.push_char(c);
201+
});
202+
res.push_char('\'');
203+
res
204+
}
196205
LIT_CHAR(c) => {
197206
let mut res = String::from_str("'");
198207
c.escape_default(|c| {
@@ -273,6 +282,7 @@ pub fn can_begin_expr(t: &Token) -> bool {
273282
IDENT(_, _) => true,
274283
UNDERSCORE => true,
275284
TILDE => true,
285+
LIT_BYTE(_) => true,
276286
LIT_CHAR(_) => true,
277287
LIT_INT(_, _) => true,
278288
LIT_UINT(_, _) => true,
@@ -311,6 +321,7 @@ pub fn close_delimiter_for(t: &Token) -> Option<Token> {
311321

312322
pub fn is_lit(t: &Token) -> bool {
313323
match *t {
324+
LIT_BYTE(_) => true,
314325
LIT_CHAR(_) => true,
315326
LIT_INT(_, _) => true,
316327
LIT_UINT(_, _) => true,

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,12 @@ impl<'a> State<'a> {
23052305
}
23062306
match lit.node {
23072307
ast::LitStr(ref st, style) => self.print_string(st.get(), style),
2308+
ast::LitByte(byte) => {
2309+
let mut res = String::from_str("b'");
2310+
(byte as char).escape_default(|c| res.push_char(c));
2311+
res.push_char('\'');
2312+
word(&mut self.s, res.as_slice())
2313+
}
23082314
ast::LitChar(ch) => {
23092315
let mut res = String::from_str("'");
23102316
ch.escape_default(|c| res.push_char(c));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// ignore-tidy-tab
13+
14+
static FOO: u8 = b'\f'; //~ ERROR unknown byte escape
15+
16+
pub fn main() {
17+
b'\f'; //~ ERROR unknown byte escape
18+
b'\x0Z'; //~ ERROR illegal character in numeric character escape: Z
19+
b' '; //~ ERROR byte constant must be escaped
20+
b'''; //~ ERROR byte constant must be escaped
21+
b'é'; //~ ERROR byte constant must be ASCII
22+
b'a //~ ERROR unterminated byte constant
23+
}
24+
25+

src/test/compile-fail/concat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
fn main() {
12+
concat!(b'f'); //~ ERROR: cannot concatenate a binary literal
1213
concat!(foo); //~ ERROR: expected a literal
1314
concat!(foo()); //~ ERROR: expected a literal
1415
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
static FOO: u8 = b'\xF0';
13+
14+
pub fn main() {
15+
assert_eq!(b'a', 97u8);
16+
assert_eq!(b'\n', 10u8);
17+
assert_eq!(b'\r', 13u8);
18+
assert_eq!(b'\t', 9u8);
19+
assert_eq!(b'\\', 92u8);
20+
assert_eq!(b'\'', 39u8);
21+
assert_eq!(b'\"', 34u8);
22+
assert_eq!(b'\0', 0u8);
23+
assert_eq!(b'\xF0', 240u8);
24+
assert_eq!(FOO, 240u8);
25+
26+
// FIXME: Do we want this to be valid?
27+
assert_eq!([42, ..b'\t'].as_slice(), &[42, 42, 42, 42, 42, 42, 42, 42, 42]);
28+
29+
match 42 {
30+
b'*' => {},
31+
_ => fail!()
32+
}
33+
34+
match 100 {
35+
b'a' .. b'z' => {},
36+
_ => fail!()
37+
}
38+
}

0 commit comments

Comments
 (0)