Add some docs to codemap
This commit is contained in:
parent
2af088529f
commit
7c72fd89f1
1 changed files with 48 additions and 13 deletions
|
@ -1,11 +1,15 @@
|
||||||
/*! A codemap is a thing that maps uints to file/line/column positions
|
/*!
|
||||||
* in a crate. This to make it possible to represent the positions
|
|
||||||
* with single-word things, rather than passing records all over the
|
The CodeMap tracks all the source code used within a single crate, mapping
|
||||||
* compiler.
|
from integer byte positions to the original source code location. Each bit of
|
||||||
*
|
source parsed during crate parsing (typically files, in-memory strings, or
|
||||||
* All represented positions are *absolute* positions within the codemap,
|
various bits of macro expansion) cover a continuous range of bytes in the
|
||||||
* not relative positions within a single file.
|
CodeMap and are represented by FileMaps. Byte positions are stored in `spans`
|
||||||
*/
|
and used pervasively in the compiler. They are absolute positions within the
|
||||||
|
CodeMap, which upon request can be converted to line and column information,
|
||||||
|
source code snippets, etc.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
use dvec::DVec;
|
use dvec::DVec;
|
||||||
use std::serialization::{Serializable,
|
use std::serialization::{Serializable,
|
||||||
|
@ -18,9 +22,16 @@ trait Pos {
|
||||||
pure fn to_uint(&self) -> uint;
|
pure fn to_uint(&self) -> uint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A byte offset
|
||||||
pub enum BytePos = uint;
|
pub enum BytePos = uint;
|
||||||
|
/// A character offset. Because of multibyte utf8 characters, a byte offset
|
||||||
|
/// is not equivalent to a character offset. The CodeMap will convert BytePos
|
||||||
|
/// values to CharPos values as necessary.
|
||||||
pub enum CharPos = uint;
|
pub enum CharPos = uint;
|
||||||
|
|
||||||
|
// XXX: Lots of boilerplate in these impls, but so far my attempts to fix
|
||||||
|
// have been unsuccessful
|
||||||
|
|
||||||
impl BytePos: Pos {
|
impl BytePos: Pos {
|
||||||
static pure fn from_uint(n: uint) -> BytePos { BytePos(n) }
|
static pure fn from_uint(n: uint) -> BytePos { BytePos(n) }
|
||||||
pure fn to_uint(&self) -> uint { **self }
|
pure fn to_uint(&self) -> uint { **self }
|
||||||
|
@ -117,6 +128,12 @@ impl CharPos: to_bytes::IterBytes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Spans represent a region of code, used for error reporting. Positions in spans
|
||||||
|
are *absolute* positions from the beginning of the codemap, not positions
|
||||||
|
relative to FileMaps. Methods on the CodeMap can be used to relate spans back
|
||||||
|
to the original source.
|
||||||
|
*/
|
||||||
pub struct span {
|
pub struct span {
|
||||||
lo: BytePos,
|
lo: BytePos,
|
||||||
hi: BytePos,
|
hi: BytePos,
|
||||||
|
@ -141,10 +158,17 @@ impl<D: Deserializer> span: Deserializable<D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A source code location used for error reporting
|
||||||
pub struct Loc {
|
pub struct Loc {
|
||||||
file: @FileMap, line: uint, col: CharPos
|
/// Information about the original source
|
||||||
|
file: @FileMap,
|
||||||
|
/// The (1-based) line number
|
||||||
|
line: uint,
|
||||||
|
/// The (0-based) column offset
|
||||||
|
col: CharPos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extra information for tracking macro expansion of spans
|
||||||
pub enum ExpnInfo {
|
pub enum ExpnInfo {
|
||||||
ExpandedFrom({call_site: span,
|
ExpandedFrom({call_site: span,
|
||||||
callie: {name: ~str, span: Option<span>}})
|
callie: {name: ~str, span: Option<span>}})
|
||||||
|
@ -174,12 +198,21 @@ pub struct MultiByteChar {
|
||||||
sum: uint
|
sum: uint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A single source in the CodeMap
|
||||||
pub struct FileMap {
|
pub struct FileMap {
|
||||||
|
/// The name of the file that the source came from, source that doesn't
|
||||||
|
/// originate from files has names between angle brackets by convention,
|
||||||
|
/// e.g. `<anon>`
|
||||||
name: FileName,
|
name: FileName,
|
||||||
|
/// Extra information used by qquote
|
||||||
substr: FileSubstr,
|
substr: FileSubstr,
|
||||||
|
/// The complete source code
|
||||||
src: @~str,
|
src: @~str,
|
||||||
|
/// The start position of this source in the CodeMap
|
||||||
start_pos: BytePos,
|
start_pos: BytePos,
|
||||||
|
/// Locations of lines beginnings in the source code
|
||||||
mut lines: ~[BytePos],
|
mut lines: ~[BytePos],
|
||||||
|
/// Locations of multi-byte characters in the source code
|
||||||
multibyte_chars: DVec<MultiByteChar>
|
multibyte_chars: DVec<MultiByteChar>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,6 +259,11 @@ pub impl CodeMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a new FileMap to the CodeMap and return it
|
||||||
|
fn new_filemap(+filename: FileName, src: @~str) -> @FileMap {
|
||||||
|
return self.new_filemap_w_substr(filename, FssNone, src);
|
||||||
|
}
|
||||||
|
|
||||||
fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr,
|
fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr,
|
||||||
src: @~str) -> @FileMap {
|
src: @~str) -> @FileMap {
|
||||||
let start_pos = if self.files.len() == 0 {
|
let start_pos = if self.files.len() == 0 {
|
||||||
|
@ -248,16 +286,13 @@ pub impl CodeMap {
|
||||||
return filemap;
|
return filemap;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_filemap(+filename: FileName, src: @~str) -> @FileMap {
|
|
||||||
return self.new_filemap_w_substr(filename, FssNone, src);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mk_substr_filename(&self, sp: span) -> ~str {
|
pub fn mk_substr_filename(&self, sp: span) -> ~str {
|
||||||
let pos = self.lookup_char_pos(sp.lo);
|
let pos = self.lookup_char_pos(sp.lo);
|
||||||
return fmt!("<%s:%u:%u>", pos.file.name,
|
return fmt!("<%s:%u:%u>", pos.file.name,
|
||||||
pos.line, pos.col.to_uint());
|
pos.line, pos.col.to_uint());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lookup source information about a BytePos
|
||||||
pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc {
|
pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc {
|
||||||
return self.lookup_pos(pos);
|
return self.lookup_pos(pos);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue