Skip to content

Commit 7c72fd8

Browse files
committed
Add some docs to codemap
1 parent 2af0885 commit 7c72fd8

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

src/libsyntax/codemap.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
/*! A codemap is a thing that maps uints to file/line/column positions
2-
* in a crate. This to make it possible to represent the positions
3-
* with single-word things, rather than passing records all over the
4-
* compiler.
5-
*
6-
* All represented positions are *absolute* positions within the codemap,
7-
* not relative positions within a single file.
8-
*/
1+
/*!
2+
3+
The CodeMap tracks all the source code used within a single crate, mapping
4+
from integer byte positions to the original source code location. Each bit of
5+
source parsed during crate parsing (typically files, in-memory strings, or
6+
various bits of macro expansion) cover a continuous range of bytes in the
7+
CodeMap and are represented by FileMaps. Byte positions are stored in `spans`
8+
and used pervasively in the compiler. They are absolute positions within the
9+
CodeMap, which upon request can be converted to line and column information,
10+
source code snippets, etc.
11+
12+
*/
913

1014
use dvec::DVec;
1115
use std::serialization::{Serializable,
@@ -18,9 +22,16 @@ trait Pos {
1822
pure fn to_uint(&self) -> uint;
1923
}
2024

25+
/// A byte offset
2126
pub enum BytePos = uint;
27+
/// A character offset. Because of multibyte utf8 characters, a byte offset
28+
/// is not equivalent to a character offset. The CodeMap will convert BytePos
29+
/// values to CharPos values as necessary.
2230
pub enum CharPos = uint;
2331

32+
// XXX: Lots of boilerplate in these impls, but so far my attempts to fix
33+
// have been unsuccessful
34+
2435
impl BytePos: Pos {
2536
static pure fn from_uint(n: uint) -> BytePos { BytePos(n) }
2637
pure fn to_uint(&self) -> uint { **self }
@@ -117,6 +128,12 @@ impl CharPos: to_bytes::IterBytes {
117128
}
118129
}
119130

131+
/**
132+
Spans represent a region of code, used for error reporting. Positions in spans
133+
are *absolute* positions from the beginning of the codemap, not positions
134+
relative to FileMaps. Methods on the CodeMap can be used to relate spans back
135+
to the original source.
136+
*/
120137
pub struct span {
121138
lo: BytePos,
122139
hi: BytePos,
@@ -141,10 +158,17 @@ impl<D: Deserializer> span: Deserializable<D> {
141158
}
142159
}
143160

161+
/// A source code location used for error reporting
144162
pub struct Loc {
145-
file: @FileMap, line: uint, col: CharPos
163+
/// Information about the original source
164+
file: @FileMap,
165+
/// The (1-based) line number
166+
line: uint,
167+
/// The (0-based) column offset
168+
col: CharPos
146169
}
147170

171+
/// Extra information for tracking macro expansion of spans
148172
pub enum ExpnInfo {
149173
ExpandedFrom({call_site: span,
150174
callie: {name: ~str, span: Option<span>}})
@@ -174,12 +198,21 @@ pub struct MultiByteChar {
174198
sum: uint
175199
}
176200

201+
/// A single source in the CodeMap
177202
pub struct FileMap {
203+
/// The name of the file that the source came from, source that doesn't
204+
/// originate from files has names between angle brackets by convention,
205+
/// e.g. `<anon>`
178206
name: FileName,
207+
/// Extra information used by qquote
179208
substr: FileSubstr,
209+
/// The complete source code
180210
src: @~str,
211+
/// The start position of this source in the CodeMap
181212
start_pos: BytePos,
213+
/// Locations of lines beginnings in the source code
182214
mut lines: ~[BytePos],
215+
/// Locations of multi-byte characters in the source code
183216
multibyte_chars: DVec<MultiByteChar>
184217
}
185218

@@ -226,6 +259,11 @@ pub impl CodeMap {
226259
}
227260
}
228261

262+
/// Add a new FileMap to the CodeMap and return it
263+
fn new_filemap(+filename: FileName, src: @~str) -> @FileMap {
264+
return self.new_filemap_w_substr(filename, FssNone, src);
265+
}
266+
229267
fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr,
230268
src: @~str) -> @FileMap {
231269
let start_pos = if self.files.len() == 0 {
@@ -248,16 +286,13 @@ pub impl CodeMap {
248286
return filemap;
249287
}
250288

251-
fn new_filemap(+filename: FileName, src: @~str) -> @FileMap {
252-
return self.new_filemap_w_substr(filename, FssNone, src);
253-
}
254-
255289
pub fn mk_substr_filename(&self, sp: span) -> ~str {
256290
let pos = self.lookup_char_pos(sp.lo);
257291
return fmt!("<%s:%u:%u>", pos.file.name,
258292
pos.line, pos.col.to_uint());
259293
}
260294
295+
/// Lookup source information about a BytePos
261296
pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc {
262297
return self.lookup_pos(pos);
263298
}

0 commit comments

Comments
 (0)