Skip to content

Commit b22f941

Browse files
committed
Make it clearer which codemap functions use chars vs. bytes
1 parent 2ec09c4 commit b22f941

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/libsyntax/codemap.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ use std::serialization::{Serializable,
1010
Serializer,
1111
Deserializer};
1212

13+
pub type byte_pos = uint;
14+
pub type char_pos = uint;
15+
1316
pub struct span {
14-
lo: uint,
15-
hi: uint,
17+
lo: char_pos,
18+
hi: char_pos,
1619
expn_info: Option<@expn_info>
1720
}
1821

@@ -34,30 +37,30 @@ impl<D: Deserializer> span: Deserializable<D> {
3437
}
3538
}
3639

37-
pub enum expn_info {
38-
expanded_from({call_site: span,
39-
callie: {name: ~str, span: Option<span>}})
40+
pub struct file_pos {
41+
ch: char_pos, byte: byte_pos
4042
}
4143

42-
pub type filename = ~str;
43-
44-
pub type lookup_fn = pure fn(file_pos) -> uint;
45-
4644
pub struct loc {
4745
file: @filemap, line: uint, col: uint
4846
}
4947

50-
pub struct file_pos {
51-
ch: uint, byte: uint
52-
}
53-
5448
impl file_pos : cmp::Eq {
5549
pure fn eq(other: &file_pos) -> bool {
5650
self.ch == (*other).ch && self.byte == (*other).byte
5751
}
5852
pure fn ne(other: &file_pos) -> bool { !self.eq(other) }
5953
}
6054

55+
pub enum expn_info {
56+
expanded_from({call_site: span,
57+
callie: {name: ~str, span: Option<span>}})
58+
}
59+
60+
pub type filename = ~str;
61+
62+
pub type lookup_fn = pure fn(file_pos) -> uint;
63+
6164
pub struct file_lines {
6265
file: @filemap,
6366
lines: ~[uint]
@@ -90,13 +93,13 @@ pub impl filemap {
9093
}
9194

9295
static fn new(+filename: filename, src: @~str,
93-
start_pos_ch: uint, start_pos_byte: uint)
96+
start_pos_ch: char_pos, start_pos_byte: byte_pos)
9497
-> filemap {
9598
return filemap::new_w_substr(filename, fss_none, src,
9699
start_pos_ch, start_pos_byte);
97100
}
98101

99-
fn next_line(@self, chpos: uint, byte_pos: uint) {
102+
fn next_line(@self, chpos: char_pos, byte_pos: byte_pos) {
100103
self.lines.push(file_pos {ch: chpos, byte: byte_pos + self.start_pos.byte});
101104
}
102105

@@ -127,17 +130,17 @@ pub impl CodeMap {
127130
return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
128131
}
129132

130-
pub fn lookup_char_pos(@self, pos: uint) -> loc {
133+
pub fn lookup_char_pos(@self, pos: char_pos) -> loc {
131134
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
132135
return self.lookup_pos(pos, lookup);
133136
}
134137

135-
pub fn lookup_byte_pos(@self, pos: uint) -> loc {
138+
pub fn lookup_byte_pos(@self, pos: byte_pos) -> loc {
136139
pure fn lookup(pos: file_pos) -> uint { return pos.byte; }
137140
return self.lookup_pos(pos, lookup);
138141
}
139142

140-
pub fn lookup_char_pos_adj(@self, pos: uint)
143+
pub fn lookup_char_pos_adj(@self, pos: char_pos)
141144
-> {filename: ~str, line: uint, col: uint, file: Option<@filemap>}
142145
{
143146
let loc = self.lookup_char_pos(pos);
@@ -195,8 +198,8 @@ pub impl CodeMap {
195198
return @file_lines {file: lo.file, lines: lines};
196199
}
197200

198-
fn lookup_byte_offset(@self, chpos: uint)
199-
-> {fm: @filemap, pos: uint} {
201+
fn lookup_byte_offset(@self, chpos: char_pos)
202+
-> {fm: @filemap, pos: byte_pos} {
200203
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
201204
let {fm, line} = self.lookup_line(chpos, lookup);
202205
let line_offset = fm.lines[line].byte - fm.start_pos.byte;

0 commit comments

Comments
 (0)