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
+ */
9
13
10
14
use dvec:: DVec ;
11
15
use std:: serialization:: { Serializable ,
@@ -18,9 +22,16 @@ trait Pos {
18
22
pure fn to_uint ( & self ) -> uint ;
19
23
}
20
24
25
+ /// A byte offset
21
26
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.
22
30
pub enum CharPos = uint;
23
31
32
+ // XXX: Lots of boilerplate in these impls, but so far my attempts to fix
33
+ // have been unsuccessful
34
+
24
35
impl BytePos : Pos {
25
36
static pure fn from_uint( n : uint ) -> BytePos { BytePos ( n ) }
26
37
pure fn to_uint ( & self ) -> uint { * * self }
@@ -117,6 +128,12 @@ impl CharPos: to_bytes::IterBytes {
117
128
}
118
129
}
119
130
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
+ */
120
137
pub struct span {
121
138
lo : BytePos ,
122
139
hi : BytePos ,
@@ -141,10 +158,17 @@ impl<D: Deserializer> span: Deserializable<D> {
141
158
}
142
159
}
143
160
161
+ /// A source code location used for error reporting
144
162
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
146
169
}
147
170
171
+ /// Extra information for tracking macro expansion of spans
148
172
pub enum ExpnInfo {
149
173
ExpandedFrom ( { call_site : span ,
150
174
callie : { name : ~str , span : Option < span > } } )
@@ -174,12 +198,21 @@ pub struct MultiByteChar {
174
198
sum : uint
175
199
}
176
200
201
+ /// A single source in the CodeMap
177
202
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>`
178
206
name : FileName ,
207
+ /// Extra information used by qquote
179
208
substr : FileSubstr ,
209
+ /// The complete source code
180
210
src : @~str ,
211
+ /// The start position of this source in the CodeMap
181
212
start_pos : BytePos ,
213
+ /// Locations of lines beginnings in the source code
182
214
mut lines: ~[ BytePos ] ,
215
+ /// Locations of multi-byte characters in the source code
183
216
multibyte_chars : DVec < MultiByteChar >
184
217
}
185
218
@@ -226,6 +259,11 @@ pub impl CodeMap {
226
259
}
227
260
}
228
261
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
+
229
267
fn new_filemap_w_substr( +filename : FileName , +substr : FileSubstr ,
230
268
src : @~str ) -> @FileMap {
231
269
let start_pos = if self . files. len( ) == 0 {
@@ -248,16 +286,13 @@ pub impl CodeMap {
248
286
return filemap;
249
287
}
250
288
251
- fn new_filemap( +filename : FileName , src : @~str ) -> @FileMap {
252
- return self. new_filemap_w_substr( filename, FssNone, src) ;
253
- }
254
-
255
289
pub fn mk_substr_filename( & self , sp: span) -> ~str {
256
290
let pos = self . lookup_char_pos( sp. lo) ;
257
291
return fmt!( "<%s: %u: %u>", pos.file.name,
258
292
pos.line, pos.col.to_uint());
259
293
}
260
294
295
+ /// Lookup source information about a BytePos
261
296
pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc {
262
297
return self.lookup_pos(pos);
263
298
}
0 commit comments