1
Fork 0

Rename StringReader::last_pos as pos.

This is a [breaking-change] for libsyntax.
This commit is contained in:
Nicholas Nethercote 2016-10-04 11:46:54 +11:00
parent 94565a4409
commit cb92f5c6d6
3 changed files with 100 additions and 100 deletions

View file

@ -149,7 +149,7 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) {
comments.push(Comment { comments.push(Comment {
style: BlankLine, style: BlankLine,
lines: Vec::new(), lines: Vec::new(),
pos: rdr.last_pos, pos: rdr.pos,
}); });
} }
@ -167,7 +167,7 @@ fn read_shebang_comment(rdr: &mut StringReader,
code_to_the_left: bool, code_to_the_left: bool,
comments: &mut Vec<Comment>) { comments: &mut Vec<Comment>) {
debug!(">>> shebang comment"); debug!(">>> shebang comment");
let p = rdr.last_pos; let p = rdr.pos;
debug!("<<< shebang comment"); debug!("<<< shebang comment");
comments.push(Comment { comments.push(Comment {
style: if code_to_the_left { Trailing } else { Isolated }, style: if code_to_the_left { Trailing } else { Isolated },
@ -180,7 +180,7 @@ fn read_line_comments(rdr: &mut StringReader,
code_to_the_left: bool, code_to_the_left: bool,
comments: &mut Vec<Comment>) { comments: &mut Vec<Comment>) {
debug!(">>> line comments"); debug!(">>> line comments");
let p = rdr.last_pos; let p = rdr.pos;
let mut lines: Vec<String> = Vec::new(); let mut lines: Vec<String> = Vec::new();
while rdr.curr_is('/') && rdr.nextch_is('/') { while rdr.curr_is('/') && rdr.nextch_is('/') {
let line = rdr.read_one_line_comment(); let line = rdr.read_one_line_comment();
@ -240,7 +240,7 @@ fn read_block_comment(rdr: &mut StringReader,
code_to_the_left: bool, code_to_the_left: bool,
comments: &mut Vec<Comment>) { comments: &mut Vec<Comment>) {
debug!(">>> block comment"); debug!(">>> block comment");
let p = rdr.last_pos; let p = rdr.pos;
let mut lines: Vec<String> = Vec::new(); let mut lines: Vec<String> = Vec::new();
let col = rdr.col; let col = rdr.col;
rdr.bump(); rdr.bump();
@ -369,7 +369,7 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
} }
let bstart = rdr.last_pos; let bstart = rdr.pos;
rdr.next_token(); rdr.next_token();
// discard, and look ahead; we're working with internal state // discard, and look ahead; we're working with internal state
let TokenAndSpan { tok, sp } = rdr.peek(); let TokenAndSpan { tok, sp } = rdr.peek();

View file

@ -78,8 +78,8 @@ pub struct StringReader<'a> {
pub span_diagnostic: &'a Handler, pub span_diagnostic: &'a Handler,
/// The absolute offset within the codemap of the next character to read /// The absolute offset within the codemap of the next character to read
pub next_pos: BytePos, pub next_pos: BytePos,
/// The absolute offset within the codemap of the last character read(curr) /// The absolute offset within the codemap of the current character
pub last_pos: BytePos, pub pos: BytePos,
/// The column of the next character to read /// The column of the next character to read
pub col: CharPos, pub col: CharPos,
/// The last character to be read /// The last character to be read
@ -196,7 +196,7 @@ impl<'a> StringReader<'a> {
StringReader { StringReader {
span_diagnostic: span_diagnostic, span_diagnostic: span_diagnostic,
next_pos: filemap.start_pos, next_pos: filemap.start_pos,
last_pos: filemap.start_pos, pos: filemap.start_pos,
col: CharPos(0), col: CharPos(0),
curr: Some('\n'), curr: Some('\n'),
filemap: filemap, filemap: filemap,
@ -317,9 +317,9 @@ impl<'a> StringReader<'a> {
self.peek_tok = token::Eof; self.peek_tok = token::Eof;
self.peek_span = syntax_pos::mk_sp(self.filemap.end_pos, self.filemap.end_pos); self.peek_span = syntax_pos::mk_sp(self.filemap.end_pos, self.filemap.end_pos);
} else { } else {
let start_bytepos = self.last_pos; let start_bytepos = self.pos;
self.peek_tok = self.next_token_inner()?; self.peek_tok = self.next_token_inner()?;
self.peek_span = syntax_pos::mk_sp(start_bytepos, self.last_pos); self.peek_span = syntax_pos::mk_sp(start_bytepos, self.pos);
}; };
} }
} }
@ -331,19 +331,19 @@ impl<'a> StringReader<'a> {
} }
/// Calls `f` with a string slice of the source text spanning from `start` /// Calls `f` with a string slice of the source text spanning from `start`
/// up to but excluding `self.last_pos`, meaning the slice does not include /// up to but excluding `self.pos`, meaning the slice does not include
/// the character `self.curr`. /// the character `self.curr`.
pub fn with_str_from<T, F>(&self, start: BytePos, f: F) -> T pub fn with_str_from<T, F>(&self, start: BytePos, f: F) -> T
where F: FnOnce(&str) -> T where F: FnOnce(&str) -> T
{ {
self.with_str_from_to(start, self.last_pos, f) self.with_str_from_to(start, self.pos, f)
} }
/// Create a Name from a given offset to the current offset, each /// Create a Name from a given offset to the current offset, each
/// adjusted 1 towards each other (assumes that on either side there is a /// adjusted 1 towards each other (assumes that on either side there is a
/// single-byte delimiter). /// single-byte delimiter).
pub fn name_from(&self, start: BytePos) -> ast::Name { pub fn name_from(&self, start: BytePos) -> ast::Name {
debug!("taking an ident from {:?} to {:?}", start, self.last_pos); debug!("taking an ident from {:?} to {:?}", start, self.pos);
self.with_str_from(start, token::intern) self.with_str_from(start, token::intern)
} }
@ -414,7 +414,7 @@ impl<'a> StringReader<'a> {
/// Advance the StringReader by one character. If a newline is /// Advance the StringReader by one character. If a newline is
/// discovered, add it to the FileMap's list of line start offsets. /// discovered, add it to the FileMap's list of line start offsets.
pub fn bump(&mut self) { pub fn bump(&mut self) {
self.last_pos = self.next_pos; self.pos = self.next_pos;
let current_byte_offset = self.byte_offset(self.next_pos).to_usize(); let current_byte_offset = self.byte_offset(self.next_pos).to_usize();
if current_byte_offset < self.source_text.len() { if current_byte_offset < self.source_text.len() {
let last_char = self.curr.unwrap(); let last_char = self.curr.unwrap();
@ -425,13 +425,13 @@ impl<'a> StringReader<'a> {
self.col = self.col + CharPos(1); self.col = self.col + CharPos(1);
if last_char == '\n' { if last_char == '\n' {
if self.save_new_lines { if self.save_new_lines {
self.filemap.next_line(self.last_pos); self.filemap.next_line(self.pos);
} }
self.col = CharPos(0); self.col = CharPos(0);
} }
if byte_offset_diff > 1 { if byte_offset_diff > 1 {
self.filemap.record_multibyte_char(self.last_pos, byte_offset_diff); self.filemap.record_multibyte_char(self.pos, byte_offset_diff);
} }
} else { } else {
self.curr = None; self.curr = None;
@ -474,7 +474,7 @@ impl<'a> StringReader<'a> {
if !ident_start(self.curr) { if !ident_start(self.curr) {
return None; return None;
} }
let start = self.last_pos; let start = self.pos;
while ident_continue(self.curr) { while ident_continue(self.curr) {
self.bump(); self.bump();
} }
@ -493,7 +493,7 @@ impl<'a> StringReader<'a> {
fn scan_comment(&mut self) -> Option<TokenAndSpan> { fn scan_comment(&mut self) -> Option<TokenAndSpan> {
if let Some(c) = self.curr { if let Some(c) = self.curr {
if c.is_whitespace() { if c.is_whitespace() {
self.span_diagnostic.span_err(syntax_pos::mk_sp(self.last_pos, self.last_pos), self.span_diagnostic.span_err(syntax_pos::mk_sp(self.pos, self.pos),
"called consume_any_line_comment, but there \ "called consume_any_line_comment, but there \
was whitespace"); was whitespace");
} }
@ -507,7 +507,7 @@ impl<'a> StringReader<'a> {
// line comments starting with "///" or "//!" are doc-comments // line comments starting with "///" or "//!" are doc-comments
let doc_comment = self.curr_is('/') || self.curr_is('!'); let doc_comment = self.curr_is('/') || self.curr_is('!');
let start_bpos = self.last_pos - BytePos(2); let start_bpos = self.pos - BytePos(2);
while !self.is_eof() { while !self.is_eof() {
match self.curr.unwrap() { match self.curr.unwrap() {
@ -517,7 +517,7 @@ impl<'a> StringReader<'a> {
// CRLF // CRLF
break; break;
} else if doc_comment { } else if doc_comment {
self.err_span_(self.last_pos, self.err_span_(self.pos,
self.next_pos, self.next_pos,
"bare CR not allowed in doc-comment"); "bare CR not allowed in doc-comment");
} }
@ -538,13 +538,13 @@ impl<'a> StringReader<'a> {
Some(TokenAndSpan { Some(TokenAndSpan {
tok: tok, tok: tok,
sp: syntax_pos::mk_sp(start_bpos, self.last_pos), sp: syntax_pos::mk_sp(start_bpos, self.pos),
}) })
}) })
} else { } else {
Some(TokenAndSpan { Some(TokenAndSpan {
tok: token::Comment, tok: token::Comment,
sp: syntax_pos::mk_sp(start_bpos, self.last_pos), sp: syntax_pos::mk_sp(start_bpos, self.pos),
}) })
}; };
} }
@ -567,17 +567,17 @@ impl<'a> StringReader<'a> {
// we're at the beginning of the file... // we're at the beginning of the file...
let cmap = CodeMap::new(); let cmap = CodeMap::new();
cmap.files.borrow_mut().push(self.filemap.clone()); cmap.files.borrow_mut().push(self.filemap.clone());
let loc = cmap.lookup_char_pos_adj(self.last_pos); let loc = cmap.lookup_char_pos_adj(self.pos);
debug!("Skipping a shebang"); debug!("Skipping a shebang");
if loc.line == 1 && loc.col == CharPos(0) { if loc.line == 1 && loc.col == CharPos(0) {
// FIXME: Add shebang "token", return it // FIXME: Add shebang "token", return it
let start = self.last_pos; let start = self.pos;
while !self.curr_is('\n') && !self.is_eof() { while !self.curr_is('\n') && !self.is_eof() {
self.bump(); self.bump();
} }
return Some(TokenAndSpan { return Some(TokenAndSpan {
tok: token::Shebang(self.name_from(start)), tok: token::Shebang(self.name_from(start)),
sp: syntax_pos::mk_sp(start, self.last_pos), sp: syntax_pos::mk_sp(start, self.pos),
}); });
} }
} }
@ -599,13 +599,13 @@ impl<'a> StringReader<'a> {
c c
}, },
c if is_pattern_whitespace(Some(c)) => { c if is_pattern_whitespace(Some(c)) => {
let start_bpos = self.last_pos; let start_bpos = self.pos;
while is_pattern_whitespace(self.curr) { while is_pattern_whitespace(self.curr) {
self.bump(); self.bump();
} }
let c = Some(TokenAndSpan { let c = Some(TokenAndSpan {
tok: token::Whitespace, tok: token::Whitespace,
sp: syntax_pos::mk_sp(start_bpos, self.last_pos), sp: syntax_pos::mk_sp(start_bpos, self.pos),
}); });
debug!("scanning whitespace: {:?}", c); debug!("scanning whitespace: {:?}", c);
c c
@ -618,7 +618,7 @@ impl<'a> StringReader<'a> {
fn scan_block_comment(&mut self) -> Option<TokenAndSpan> { fn scan_block_comment(&mut self) -> Option<TokenAndSpan> {
// block comments starting with "/**" or "/*!" are doc-comments // block comments starting with "/**" or "/*!" are doc-comments
let is_doc_comment = self.curr_is('*') || self.curr_is('!'); let is_doc_comment = self.curr_is('*') || self.curr_is('!');
let start_bpos = self.last_pos - BytePos(2); let start_bpos = self.pos - BytePos(2);
let mut level: isize = 1; let mut level: isize = 1;
let mut has_cr = false; let mut has_cr = false;
@ -629,7 +629,7 @@ impl<'a> StringReader<'a> {
} else { } else {
"unterminated block comment" "unterminated block comment"
}; };
let last_bpos = self.last_pos; let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, msg)); panic!(self.fatal_span_(start_bpos, last_bpos, msg));
} }
let n = self.curr.unwrap(); let n = self.curr.unwrap();
@ -667,7 +667,7 @@ impl<'a> StringReader<'a> {
Some(TokenAndSpan { Some(TokenAndSpan {
tok: tok, tok: tok,
sp: syntax_pos::mk_sp(start_bpos, self.last_pos), sp: syntax_pos::mk_sp(start_bpos, self.pos),
}) })
}) })
} }
@ -694,7 +694,7 @@ impl<'a> StringReader<'a> {
// check that the hypothetical digit is actually // check that the hypothetical digit is actually
// in range for the true radix // in range for the true radix
if c.unwrap().to_digit(real_radix).is_none() { if c.unwrap().to_digit(real_radix).is_none() {
self.err_span_(self.last_pos, self.err_span_(self.pos,
self.next_pos, self.next_pos,
&format!("invalid digit for a base {} literal", real_radix)); &format!("invalid digit for a base {} literal", real_radix));
} }
@ -710,7 +710,7 @@ impl<'a> StringReader<'a> {
fn scan_number(&mut self, c: char) -> token::Lit { fn scan_number(&mut self, c: char) -> token::Lit {
let num_digits; let num_digits;
let mut base = 10; let mut base = 10;
let start_bpos = self.last_pos; let start_bpos = self.pos;
self.bump(); self.bump();
@ -747,7 +747,7 @@ impl<'a> StringReader<'a> {
if num_digits == 0 { if num_digits == 0 {
self.err_span_(start_bpos, self.err_span_(start_bpos,
self.last_pos, self.pos,
"no valid digits found for number"); "no valid digits found for number");
return token::Integer(token::intern("0")); return token::Integer(token::intern("0"));
} }
@ -766,15 +766,15 @@ impl<'a> StringReader<'a> {
self.scan_digits(10, 10); self.scan_digits(10, 10);
self.scan_float_exponent(); self.scan_float_exponent();
} }
let last_pos = self.last_pos; let pos = self.pos;
self.check_float_base(start_bpos, last_pos, base); self.check_float_base(start_bpos, pos, base);
return token::Float(self.name_from(start_bpos)); return token::Float(self.name_from(start_bpos));
} else { } else {
// it might be a float if it has an exponent // it might be a float if it has an exponent
if self.curr_is('e') || self.curr_is('E') { if self.curr_is('e') || self.curr_is('E') {
self.scan_float_exponent(); self.scan_float_exponent();
let last_pos = self.last_pos; let pos = self.pos;
self.check_float_base(start_bpos, last_pos, base); self.check_float_base(start_bpos, pos, base);
return token::Float(self.name_from(start_bpos)); return token::Float(self.name_from(start_bpos));
} }
// but we certainly have an integer! // but we certainly have an integer!
@ -786,19 +786,19 @@ impl<'a> StringReader<'a> {
/// error if too many or too few digits are encountered. /// error if too many or too few digits are encountered.
fn scan_hex_digits(&mut self, n_digits: usize, delim: char, below_0x7f_only: bool) -> bool { fn scan_hex_digits(&mut self, n_digits: usize, delim: char, below_0x7f_only: bool) -> bool {
debug!("scanning {} digits until {:?}", n_digits, delim); debug!("scanning {} digits until {:?}", n_digits, delim);
let start_bpos = self.last_pos; let start_bpos = self.pos;
let mut accum_int = 0; let mut accum_int = 0;
let mut valid = true; let mut valid = true;
for _ in 0..n_digits { for _ in 0..n_digits {
if self.is_eof() { if self.is_eof() {
let last_bpos = self.last_pos; let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, panic!(self.fatal_span_(start_bpos,
last_bpos, last_bpos,
"unterminated numeric character escape")); "unterminated numeric character escape"));
} }
if self.curr_is(delim) { if self.curr_is(delim) {
let last_bpos = self.last_pos; let last_bpos = self.pos;
self.err_span_(start_bpos, self.err_span_(start_bpos,
last_bpos, last_bpos,
"numeric character escape is too short"); "numeric character escape is too short");
@ -808,7 +808,7 @@ impl<'a> StringReader<'a> {
let c = self.curr.unwrap_or('\x00'); let c = self.curr.unwrap_or('\x00');
accum_int *= 16; accum_int *= 16;
accum_int += c.to_digit(16).unwrap_or_else(|| { accum_int += c.to_digit(16).unwrap_or_else(|| {
self.err_span_char(self.last_pos, self.err_span_char(self.pos,
self.next_pos, self.next_pos,
"invalid character in numeric character escape", "invalid character in numeric character escape",
c); c);
@ -821,7 +821,7 @@ impl<'a> StringReader<'a> {
if below_0x7f_only && accum_int >= 0x80 { if below_0x7f_only && accum_int >= 0x80 {
self.err_span_(start_bpos, self.err_span_(start_bpos,
self.last_pos, self.pos,
"this form of character escape may only be used with characters in \ "this form of character escape may only be used with characters in \
the range [\\x00-\\x7f]"); the range [\\x00-\\x7f]");
valid = false; valid = false;
@ -830,7 +830,7 @@ impl<'a> StringReader<'a> {
match char::from_u32(accum_int) { match char::from_u32(accum_int) {
Some(_) => valid, Some(_) => valid,
None => { None => {
let last_bpos = self.last_pos; let last_bpos = self.pos;
self.err_span_(start_bpos, last_bpos, "invalid numeric character escape"); self.err_span_(start_bpos, last_bpos, "invalid numeric character escape");
false false
} }
@ -852,7 +852,7 @@ impl<'a> StringReader<'a> {
'\\' => { '\\' => {
// '\X' for some X must be a character constant: // '\X' for some X must be a character constant:
let escaped = self.curr; let escaped = self.curr;
let escaped_pos = self.last_pos; let escaped_pos = self.pos;
self.bump(); self.bump();
match escaped { match escaped {
None => {} // EOF here is an error that will be checked later. None => {} // EOF here is an error that will be checked later.
@ -864,7 +864,7 @@ impl<'a> StringReader<'a> {
let valid = if self.curr_is('{') { let valid = if self.curr_is('{') {
self.scan_unicode_escape(delim) && !ascii_only self.scan_unicode_escape(delim) && !ascii_only
} else { } else {
let span = syntax_pos::mk_sp(start, self.last_pos); let span = syntax_pos::mk_sp(start, self.pos);
self.span_diagnostic self.span_diagnostic
.struct_span_err(span, "incorrect unicode escape sequence") .struct_span_err(span, "incorrect unicode escape sequence")
.span_help(span, .span_help(span,
@ -875,7 +875,7 @@ impl<'a> StringReader<'a> {
}; };
if ascii_only { if ascii_only {
self.err_span_(start, self.err_span_(start,
self.last_pos, self.pos,
"unicode escape sequences cannot be used as a \ "unicode escape sequences cannot be used as a \
byte or in a byte string"); byte or in a byte string");
} }
@ -891,9 +891,9 @@ impl<'a> StringReader<'a> {
true true
} }
c => { c => {
let last_pos = self.last_pos; let pos = self.pos;
let mut err = self.struct_err_span_char(escaped_pos, let mut err = self.struct_err_span_char(escaped_pos,
last_pos, pos,
if ascii_only { if ascii_only {
"unknown byte escape" "unknown byte escape"
} else { } else {
@ -902,13 +902,13 @@ impl<'a> StringReader<'a> {
}, },
c); c);
if e == '\r' { if e == '\r' {
err.span_help(syntax_pos::mk_sp(escaped_pos, last_pos), err.span_help(syntax_pos::mk_sp(escaped_pos, pos),
"this is an isolated carriage return; consider \ "this is an isolated carriage return; consider \
checking your editor and version control \ checking your editor and version control \
settings"); settings");
} }
if (e == '{' || e == '}') && !ascii_only { if (e == '{' || e == '}') && !ascii_only {
err.span_help(syntax_pos::mk_sp(escaped_pos, last_pos), err.span_help(syntax_pos::mk_sp(escaped_pos, pos),
"if used in a formatting string, curly braces \ "if used in a formatting string, curly braces \
are escaped with `{{` and `}}`"); are escaped with `{{` and `}}`");
} }
@ -920,9 +920,9 @@ impl<'a> StringReader<'a> {
} }
} }
'\t' | '\n' | '\r' | '\'' if delim == '\'' => { '\t' | '\n' | '\r' | '\'' if delim == '\'' => {
let last_pos = self.last_pos; let pos = self.pos;
self.err_span_char(start, self.err_span_char(start,
last_pos, pos,
if ascii_only { if ascii_only {
"byte constant must be escaped" "byte constant must be escaped"
} else { } else {
@ -937,16 +937,16 @@ impl<'a> StringReader<'a> {
return true; return true;
} else { } else {
self.err_span_(start, self.err_span_(start,
self.last_pos, self.pos,
"bare CR not allowed in string, use \\r instead"); "bare CR not allowed in string, use \\r instead");
return false; return false;
} }
} }
_ => { _ => {
if ascii_only && first_source_char > '\x7F' { if ascii_only && first_source_char > '\x7F' {
let last_pos = self.last_pos; let pos = self.pos;
self.err_span_(start, self.err_span_(start,
last_pos, pos,
"byte constant must be ASCII. Use a \\xHH escape for a \ "byte constant must be ASCII. Use a \\xHH escape for a \
non-ASCII byte"); non-ASCII byte");
return false; return false;
@ -962,7 +962,7 @@ impl<'a> StringReader<'a> {
/// will read at least one digit, and up to 6, and pass over the }. /// will read at least one digit, and up to 6, and pass over the }.
fn scan_unicode_escape(&mut self, delim: char) -> bool { fn scan_unicode_escape(&mut self, delim: char) -> bool {
self.bump(); // past the { self.bump(); // past the {
let start_bpos = self.last_pos; let start_bpos = self.pos;
let mut count = 0; let mut count = 0;
let mut accum_int = 0; let mut accum_int = 0;
let mut valid = true; let mut valid = true;
@ -972,18 +972,18 @@ impl<'a> StringReader<'a> {
Some(c) => c, Some(c) => c,
None => { None => {
panic!(self.fatal_span_(start_bpos, panic!(self.fatal_span_(start_bpos,
self.last_pos, self.pos,
"unterminated unicode escape (found EOF)")); "unterminated unicode escape (found EOF)"));
} }
}; };
accum_int *= 16; accum_int *= 16;
accum_int += c.to_digit(16).unwrap_or_else(|| { accum_int += c.to_digit(16).unwrap_or_else(|| {
if c == delim { if c == delim {
panic!(self.fatal_span_(self.last_pos, panic!(self.fatal_span_(self.pos,
self.next_pos, self.next_pos,
"unterminated unicode escape (needed a `}`)")); "unterminated unicode escape (needed a `}`)"));
} else { } else {
self.err_span_char(self.last_pos, self.err_span_char(self.pos,
self.next_pos, self.next_pos,
"invalid character in unicode escape", "invalid character in unicode escape",
c); c);
@ -997,14 +997,14 @@ impl<'a> StringReader<'a> {
if count > 6 { if count > 6 {
self.err_span_(start_bpos, self.err_span_(start_bpos,
self.last_pos, self.pos,
"overlong unicode escape (can have at most 6 hex digits)"); "overlong unicode escape (can have at most 6 hex digits)");
valid = false; valid = false;
} }
if valid && (char::from_u32(accum_int).is_none() || count == 0) { if valid && (char::from_u32(accum_int).is_none() || count == 0) {
self.err_span_(start_bpos, self.err_span_(start_bpos,
self.last_pos, self.pos,
"invalid unicode character escape"); "invalid unicode character escape");
valid = false; valid = false;
} }
@ -1021,7 +1021,7 @@ impl<'a> StringReader<'a> {
self.bump(); self.bump();
} }
if self.scan_digits(10, 10) == 0 { if self.scan_digits(10, 10) == 0 {
self.err_span_(self.last_pos, self.err_span_(self.pos,
self.next_pos, self.next_pos,
"expected at least one digit in exponent") "expected at least one digit in exponent")
} }
@ -1078,7 +1078,7 @@ impl<'a> StringReader<'a> {
('b', Some('r'), Some('#')) => false, ('b', Some('r'), Some('#')) => false,
_ => true, _ => true,
} { } {
let start = self.last_pos; let start = self.pos;
while ident_continue(self.curr) { while ident_continue(self.curr) {
self.bump(); self.bump();
} }
@ -1241,9 +1241,9 @@ impl<'a> StringReader<'a> {
} }
'\'' => { '\'' => {
// Either a character constant 'a' OR a lifetime name 'abc // Either a character constant 'a' OR a lifetime name 'abc
let start_with_quote = self.last_pos; let start_with_quote = self.pos;
self.bump(); self.bump();
let start = self.last_pos; let start = self.pos;
// the eof will be picked up by the final `'` check below // the eof will be picked up by the final `'` check below
let c2 = self.curr.unwrap_or('\x00'); let c2 = self.curr.unwrap_or('\x00');
@ -1277,7 +1277,7 @@ impl<'a> StringReader<'a> {
str_to_ident(lifetime_name) str_to_ident(lifetime_name)
}); });
let keyword_checking_token = &token::Ident(keyword_checking_ident); let keyword_checking_token = &token::Ident(keyword_checking_ident);
let last_bpos = self.last_pos; let last_bpos = self.pos;
if keyword_checking_token.is_any_keyword() && if keyword_checking_token.is_any_keyword() &&
!keyword_checking_token.is_keyword(keywords::Static) { !keyword_checking_token.is_keyword(keywords::Static) {
self.err_span_(start, last_bpos, "lifetimes cannot use keyword names"); self.err_span_(start, last_bpos, "lifetimes cannot use keyword names");
@ -1294,7 +1294,7 @@ impl<'a> StringReader<'a> {
if !self.curr_is('\'') { if !self.curr_is('\'') {
panic!(self.fatal_span_verbose( panic!(self.fatal_span_verbose(
start_with_quote, self.last_pos, start_with_quote, self.pos,
String::from("character literal may only contain one codepoint"))); String::from("character literal may only contain one codepoint")));
} }
@ -1319,18 +1319,18 @@ impl<'a> StringReader<'a> {
return Ok(token::Literal(lit, suffix)); return Ok(token::Literal(lit, suffix));
} }
'"' => { '"' => {
let start_bpos = self.last_pos; let start_bpos = self.pos;
let mut valid = true; let mut valid = true;
self.bump(); self.bump();
while !self.curr_is('"') { while !self.curr_is('"') {
if self.is_eof() { if self.is_eof() {
let last_bpos = self.last_pos; let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, panic!(self.fatal_span_(start_bpos,
last_bpos, last_bpos,
"unterminated double quote string")); "unterminated double quote string"));
} }
let ch_start = self.last_pos; let ch_start = self.pos;
let ch = self.curr.unwrap(); let ch = self.curr.unwrap();
self.bump(); self.bump();
valid &= self.scan_char_or_byte(ch_start, valid &= self.scan_char_or_byte(ch_start,
@ -1350,7 +1350,7 @@ impl<'a> StringReader<'a> {
return Ok(token::Literal(token::Str_(id), suffix)); return Ok(token::Literal(token::Str_(id), suffix));
} }
'r' => { 'r' => {
let start_bpos = self.last_pos; let start_bpos = self.pos;
self.bump(); self.bump();
let mut hash_count = 0; let mut hash_count = 0;
while self.curr_is('#') { while self.curr_is('#') {
@ -1359,10 +1359,10 @@ impl<'a> StringReader<'a> {
} }
if self.is_eof() { if self.is_eof() {
let last_bpos = self.last_pos; let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string")); panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
} else if !self.curr_is('"') { } else if !self.curr_is('"') {
let last_bpos = self.last_pos; let last_bpos = self.pos;
let curr_char = self.curr.unwrap(); let curr_char = self.curr.unwrap();
panic!(self.fatal_span_char(start_bpos, panic!(self.fatal_span_char(start_bpos,
last_bpos, last_bpos,
@ -1371,16 +1371,16 @@ impl<'a> StringReader<'a> {
curr_char)); curr_char));
} }
self.bump(); self.bump();
let content_start_bpos = self.last_pos; let content_start_bpos = self.pos;
let mut content_end_bpos; let mut content_end_bpos;
let mut valid = true; let mut valid = true;
'outer: loop { 'outer: loop {
if self.is_eof() { if self.is_eof() {
let last_bpos = self.last_pos; let last_bpos = self.pos;
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string")); panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
} }
// if self.curr_is('"') { // if self.curr_is('"') {
// content_end_bpos = self.last_pos; // content_end_bpos = self.pos;
// for _ in 0..hash_count { // for _ in 0..hash_count {
// self.bump(); // self.bump();
// if !self.curr_is('#') { // if !self.curr_is('#') {
@ -1388,7 +1388,7 @@ impl<'a> StringReader<'a> {
let c = self.curr.unwrap(); let c = self.curr.unwrap();
match c { match c {
'"' => { '"' => {
content_end_bpos = self.last_pos; content_end_bpos = self.pos;
for _ in 0..hash_count { for _ in 0..hash_count {
self.bump(); self.bump();
if !self.curr_is('#') { if !self.curr_is('#') {
@ -1399,7 +1399,7 @@ impl<'a> StringReader<'a> {
} }
'\r' => { '\r' => {
if !self.nextch_is('\n') { if !self.nextch_is('\n') {
let last_bpos = self.last_pos; let last_bpos = self.pos;
self.err_span_(start_bpos, self.err_span_(start_bpos,
last_bpos, last_bpos,
"bare CR not allowed in raw string, use \\r \ "bare CR not allowed in raw string, use \\r \
@ -1466,7 +1466,7 @@ impl<'a> StringReader<'a> {
return Ok(self.binop(token::Percent)); return Ok(self.binop(token::Percent));
} }
c => { c => {
let last_bpos = self.last_pos; let last_bpos = self.pos;
let bpos = self.next_pos; let bpos = self.next_pos;
let mut err = self.struct_fatal_span_char(last_bpos, let mut err = self.struct_fatal_span_char(last_bpos,
bpos, bpos,
@ -1518,7 +1518,7 @@ impl<'a> StringReader<'a> {
fn scan_byte(&mut self) -> token::Lit { fn scan_byte(&mut self) -> token::Lit {
self.bump(); self.bump();
let start = self.last_pos; let start = self.pos;
// the eof will be picked up by the final `'` check below // the eof will be picked up by the final `'` check below
let c2 = self.curr.unwrap_or('\x00'); let c2 = self.curr.unwrap_or('\x00');
@ -1533,9 +1533,9 @@ impl<'a> StringReader<'a> {
// Byte offsetting here is okay because the // Byte offsetting here is okay because the
// character before position `start` are an // character before position `start` are an
// ascii single quote and ascii 'b'. // ascii single quote and ascii 'b'.
let last_pos = self.last_pos; let pos = self.pos;
panic!(self.fatal_span_verbose(start - BytePos(2), panic!(self.fatal_span_verbose(start - BytePos(2),
last_pos, pos,
"unterminated byte constant".to_string())); "unterminated byte constant".to_string()));
} }
@ -1554,16 +1554,16 @@ impl<'a> StringReader<'a> {
fn scan_byte_string(&mut self) -> token::Lit { fn scan_byte_string(&mut self) -> token::Lit {
self.bump(); self.bump();
let start = self.last_pos; let start = self.pos;
let mut valid = true; let mut valid = true;
while !self.curr_is('"') { while !self.curr_is('"') {
if self.is_eof() { if self.is_eof() {
let last_pos = self.last_pos; let pos = self.pos;
panic!(self.fatal_span_(start, last_pos, "unterminated double quote byte string")); panic!(self.fatal_span_(start, pos, "unterminated double quote byte string"));
} }
let ch_start = self.last_pos; let ch_start = self.pos;
let ch = self.curr.unwrap(); let ch = self.curr.unwrap();
self.bump(); self.bump();
valid &= self.scan_char_or_byte(ch_start, valid &= self.scan_char_or_byte(ch_start,
@ -1582,7 +1582,7 @@ impl<'a> StringReader<'a> {
} }
fn scan_raw_byte_string(&mut self) -> token::Lit { fn scan_raw_byte_string(&mut self) -> token::Lit {
let start_bpos = self.last_pos; let start_bpos = self.pos;
self.bump(); self.bump();
let mut hash_count = 0; let mut hash_count = 0;
while self.curr_is('#') { while self.curr_is('#') {
@ -1591,28 +1591,28 @@ impl<'a> StringReader<'a> {
} }
if self.is_eof() { if self.is_eof() {
let last_pos = self.last_pos; let pos = self.pos;
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string")); panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"));
} else if !self.curr_is('"') { } else if !self.curr_is('"') {
let last_pos = self.last_pos; let pos = self.pos;
let ch = self.curr.unwrap(); let ch = self.curr.unwrap();
panic!(self.fatal_span_char(start_bpos, panic!(self.fatal_span_char(start_bpos,
last_pos, pos,
"found invalid character; only `#` is allowed in raw \ "found invalid character; only `#` is allowed in raw \
string delimitation", string delimitation",
ch)); ch));
} }
self.bump(); self.bump();
let content_start_bpos = self.last_pos; let content_start_bpos = self.pos;
let mut content_end_bpos; let mut content_end_bpos;
'outer: loop { 'outer: loop {
match self.curr { match self.curr {
None => { None => {
let last_pos = self.last_pos; let pos = self.pos;
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string")) panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"))
} }
Some('"') => { Some('"') => {
content_end_bpos = self.last_pos; content_end_bpos = self.pos;
for _ in 0..hash_count { for _ in 0..hash_count {
self.bump(); self.bump();
if !self.curr_is('#') { if !self.curr_is('#') {
@ -1623,8 +1623,8 @@ impl<'a> StringReader<'a> {
} }
Some(c) => { Some(c) => {
if c > '\x7F' { if c > '\x7F' {
let last_pos = self.last_pos; let pos = self.pos;
self.err_span_char(last_pos, last_pos, "raw byte string must be ASCII", c); self.err_span_char(pos, pos, "raw byte string must be ASCII", c);
} }
} }
} }
@ -1738,7 +1738,7 @@ mod tests {
assert_eq!(tok1, tok2); assert_eq!(tok1, tok2);
assert_eq!(string_reader.next_token().tok, token::Whitespace); assert_eq!(string_reader.next_token().tok, token::Whitespace);
// the 'main' id is already read: // the 'main' id is already read:
assert_eq!(string_reader.last_pos.clone(), BytePos(28)); assert_eq!(string_reader.pos.clone(), BytePos(28));
// read another token: // read another token:
let tok3 = string_reader.next_token(); let tok3 = string_reader.next_token();
let tok4 = TokenAndSpan { let tok4 = TokenAndSpan {
@ -1751,7 +1751,7 @@ mod tests {
}; };
assert_eq!(tok3, tok4); assert_eq!(tok3, tok4);
// the lparen is already read: // the lparen is already read:
assert_eq!(string_reader.last_pos.clone(), BytePos(29)) assert_eq!(string_reader.pos.clone(), BytePos(29))
} }
// check that the given reader produces the desired stream // check that the given reader produces the desired stream

View file

@ -234,7 +234,7 @@ pub fn check_for_substitution<'a>(reader: &StringReader<'a>,
.iter() .iter()
.find(|&&(c, _, _)| c == ch) .find(|&&(c, _, _)| c == ch)
.map(|&(_, u_name, ascii_char)| { .map(|&(_, u_name, ascii_char)| {
let span = make_span(reader.last_pos, reader.next_pos); let span = make_span(reader.pos, reader.next_pos);
match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) { match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) {
Some(&(ascii_char, ascii_name)) => { Some(&(ascii_char, ascii_name)) => {
let msg = let msg =