Skip to content

Allow for integral suffixes after a char literal #9302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustFail

syn match rustFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlLjzt]\|ll\|hh\)\=\([aAbdiuoxXDOUfFeEgGcCsSpn?]\|\[\^\=.[^]]*\]\)" contained
syn match rustFormat display "%%" contained
syn match rustSpecial display contained /\\\([nrt\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
syn match rustSpecial display contained /\\\([nrt\\'"0]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
syn match rustStringContinuation display contained /\\\n\s*/
syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustTodo,rustFormat,rustSpecial,rustStringContinuation

Expand Down Expand Up @@ -180,7 +180,7 @@ syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[

"rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
syn match rustCharacter /'\([^'\\]\|\\\([nrt\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustSpecial
syn match rustCharacter /'\([^'\\]\|\\\([nrt\\'"0]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\(u8\|u16\|u32\|u64\|u\|i8\|i16\|i32\|i64\|i\)\?/ contains=rustSpecial

syn region rustCommentML start="/\*" end="\*/" contains=rustTodo
syn region rustComment start="//" end="$" contains=rustTodo keepend
Expand Down
80 changes: 48 additions & 32 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,38 +409,9 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
base = 2u;
}
num_str = scan_digits(rdr, base);
c = rdr.curr;
nextch(rdr);
if c == 'u' || c == 'i' {
let signed = c == 'i';
let mut tp = {
if signed { either::Left(ast::ty_i) }
else { either::Right(ast::ty_u) }
};
bump(rdr);
c = rdr.curr;
if c == '8' {
bump(rdr);
tp = if signed { either::Left(ast::ty_i8) }
else { either::Right(ast::ty_u8) };
}
n = nextch(rdr);
if c == '1' && n == '6' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i16) }
else { either::Right(ast::ty_u16) };
} else if c == '3' && n == '2' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i32) }
else { either::Right(ast::ty_u32) };
} else if c == '6' && n == '4' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i64) }
else { either::Right(ast::ty_u64) };
}
let int_suff = scan_integral_suffix(rdr);
if int_suff.is_some() {
let tp = int_suff.unwrap();
if num_str.len() == 0u {
rdr.fatal(~"no valid digits found for number");
}
Expand Down Expand Up @@ -520,6 +491,43 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
}
}

fn scan_integral_suffix(rdr: @mut StringReader)
-> Option<either::Either<ast::int_ty, ast::uint_ty>> {
let mut c = rdr.curr;
if c == 'u' || c == 'i' {
let signed = c == 'i';
let mut tp = {
if signed { either::Left(ast::ty_i) }
else { either::Right(ast::ty_u) }
};
bump(rdr);
c = rdr.curr;
if c == '8' {
bump(rdr);
tp = if signed { either::Left(ast::ty_i8) }
else { either::Right(ast::ty_u8) };
}
let n = nextch(rdr);
if c == '1' && n == '6' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i16) }
else { either::Right(ast::ty_u16) };
} else if c == '3' && n == '2' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i32) }
else { either::Right(ast::ty_u32) };
} else if c == '6' && n == '4' {
bump(rdr);
bump(rdr);
tp = if signed { either::Left(ast::ty_i64) }
else { either::Right(ast::ty_u64) };
}
Some(tp)
} else { None }
}

fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
let mut accum_int = 0;
let mut i = n_hex_digits;
Expand Down Expand Up @@ -712,6 +720,14 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
rdr.fatal(~"unterminated character constant");
}
bump(rdr); // advance curr past token
let int_suff = scan_integral_suffix(rdr);
if int_suff.is_some() {
let tp = int_suff.unwrap();
match tp {
either::Left(t) => return token::LIT_INT(c2 as i64, t),
either::Right(t) => return token::LIT_UINT(c2 as u64, t)
}
}
return token::LIT_CHAR(c2 as u32);
}
'"' => {
Expand Down
32 changes: 32 additions & 0 deletions src/test/run-pass/char-literal-suffixes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let a: int = 'a'i;
assert_eq!(a, 97i);
let a: i8 = 'a'i8;
assert_eq!(a, 97i8);
let a: i16 = 'a'i16;
assert_eq!(a, 97i16);
let a: i32 = 'a'i32;
assert_eq!(a, 97i32);
let a: i64 = 'a'i64;
assert_eq!(a, 97i64);
let a: uint = 'a'u;
assert_eq!(a, 97u);
let a: u8 = 'a'u8;
assert_eq!(a, 97u8);
let a: u16 = 'a'u16;
assert_eq!(a, 97u16);
let a: u32 = 'a'u32;
assert_eq!(a, 97u32);
let a: u64 = 'a'u64;
assert_eq!(a, 97u64);
}