Skip to content

Commit b33386d

Browse files
committed
libsyntax: De-@mut StringReader::pos
1 parent 3aa19a6 commit b33386d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/libsyntax/parse/lexer.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use parse::token;
1818
use parse::token::{str_to_ident};
1919

2020
use std::cast::transmute;
21+
use std::cell::Cell;
2122
use std::char;
2223
use std::num::from_str_radix;
2324
use std::util;
@@ -43,7 +44,7 @@ pub struct StringReader {
4344
span_diagnostic: @mut SpanHandler,
4445
src: @str,
4546
// The absolute offset within the codemap of the next character to read
46-
pos: BytePos,
47+
pos: Cell<BytePos>,
4748
// The absolute offset within the codemap of the last character read(curr)
4849
last_pos: BytePos,
4950
// The column of the next character to read
@@ -73,7 +74,7 @@ pub fn new_low_level_string_reader(span_diagnostic: @mut SpanHandler,
7374
let r = @mut StringReader {
7475
span_diagnostic: span_diagnostic,
7576
src: filemap.src,
76-
pos: filemap.start_pos,
77+
pos: Cell::new(filemap.start_pos),
7778
last_pos: filemap.start_pos,
7879
col: CharPos(0),
7980
curr: initial_char,
@@ -93,7 +94,7 @@ fn dup_string_reader(r: @mut StringReader) -> @mut StringReader {
9394
@mut StringReader {
9495
span_diagnostic: r.span_diagnostic,
9596
src: r.src,
96-
pos: r.pos,
97+
pos: Cell::new(r.pos.get()),
9798
last_pos: r.last_pos,
9899
col: r.col,
99100
curr: r.curr,
@@ -239,14 +240,14 @@ fn with_str_from_to<T>(
239240
// EFFECT: advance the StringReader by one character. If a newline is
240241
// discovered, add it to the FileMap's list of line start offsets.
241242
pub fn bump(rdr: &mut StringReader) {
242-
rdr.last_pos = rdr.pos;
243-
let current_byte_offset = byte_offset(rdr, rdr.pos).to_uint();
243+
rdr.last_pos = rdr.pos.get();
244+
let current_byte_offset = byte_offset(rdr, rdr.pos.get()).to_uint();
244245
if current_byte_offset < (rdr.src).len() {
245246
assert!(rdr.curr != unsafe { transmute(-1u32) }); // FIXME: #8971: unsound
246247
let last_char = rdr.curr;
247248
let next = rdr.src.char_range_at(current_byte_offset);
248249
let byte_offset_diff = next.next - current_byte_offset;
249-
rdr.pos = rdr.pos + Pos::from_uint(byte_offset_diff);
250+
rdr.pos.set(rdr.pos.get() + Pos::from_uint(byte_offset_diff));
250251
rdr.curr = next.ch;
251252
rdr.col = rdr.col + CharPos(1u);
252253
if last_char == '\n' {
@@ -266,7 +267,7 @@ pub fn is_eof(rdr: @mut StringReader) -> bool {
266267
rdr.curr == unsafe { transmute(-1u32) } // FIXME: #8971: unsound
267268
}
268269
pub fn nextch(rdr: @mut StringReader) -> char {
269-
let offset = byte_offset(rdr, rdr.pos).to_uint();
270+
let offset = byte_offset(rdr, rdr.pos.get()).to_uint();
270271
if offset < (rdr.src).len() {
271272
return rdr.src.char_at(offset);
272273
} else { return unsafe { transmute(-1u32) }; } // FIXME: #8971: unsound
@@ -318,7 +319,7 @@ fn consume_any_line_comment(rdr: @mut StringReader)
318319
bump(rdr);
319320
// line comments starting with "///" or "//!" are doc-comments
320321
if rdr.curr == '/' || rdr.curr == '!' {
321-
let start_bpos = rdr.pos - BytePos(3);
322+
let start_bpos = rdr.pos.get() - BytePos(3);
322323
while rdr.curr != '\n' && !is_eof(rdr) {
323324
bump(rdr);
324325
}
@@ -327,7 +328,7 @@ fn consume_any_line_comment(rdr: @mut StringReader)
327328
if !is_line_non_doc_comment(string) {
328329
Some(TokenAndSpan{
329330
tok: token::DOC_COMMENT(str_to_ident(string)),
330-
sp: codemap::mk_sp(start_bpos, rdr.pos)
331+
sp: codemap::mk_sp(start_bpos, rdr.pos.get())
331332
})
332333
} else {
333334
None
@@ -371,7 +372,7 @@ fn consume_block_comment(rdr: @mut StringReader)
371372
-> Option<TokenAndSpan> {
372373
// block comments starting with "/**" or "/*!" are doc-comments
373374
let is_doc_comment = rdr.curr == '*' || rdr.curr == '!';
374-
let start_bpos = rdr.pos - BytePos(if is_doc_comment {3} else {2});
375+
let start_bpos = rdr.pos.get() - BytePos(if is_doc_comment {3} else {2});
375376

376377
let mut level: int = 1;
377378
while level > 0 {
@@ -401,7 +402,7 @@ fn consume_block_comment(rdr: @mut StringReader)
401402
if !is_block_non_doc_comment(string) {
402403
Some(TokenAndSpan{
403404
tok: token::DOC_COMMENT(str_to_ident(string)),
404-
sp: codemap::mk_sp(start_bpos, rdr.pos)
405+
sp: codemap::mk_sp(start_bpos, rdr.pos.get())
405406
})
406407
} else {
407408
None
@@ -592,7 +593,7 @@ fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
592593
while i != 0u {
593594
let n = rdr.curr;
594595
if !is_hex_digit(n) {
595-
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
596+
fatal_span_char(rdr, rdr.last_pos, rdr.pos.get(),
596597
~"illegal character in numeric character escape",
597598
n);
598599
}
@@ -932,7 +933,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
932933
'^' => { return binop(rdr, token::CARET); }
933934
'%' => { return binop(rdr, token::PERCENT); }
934935
c => {
935-
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
936+
fatal_span_char(rdr, rdr.last_pos, rdr.pos.get(),
936937
~"unknown start of token", c);
937938
}
938939
}

0 commit comments

Comments
 (0)