aboutsummaryrefslogtreecommitdiff
path: root/src/comp/front/codemap.rs
diff options
context:
space:
mode:
authorMarijn Haverbeke <[email protected]>2011-04-08 18:44:20 +0200
committerMarijn Haverbeke <[email protected]>2011-04-09 01:05:18 +0200
commit1af3174fe3b565371a5978381f604ea9c01e86d3 (patch)
tree1690b133b0690fdf6a2c005528355c2d00313129 /src/comp/front/codemap.rs
parentMake lexer buffer the whole file (diff)
downloadrust-1af3174fe3b565371a5978381f604ea9c01e86d3.tar.xz
rust-1af3174fe3b565371a5978381f604ea9c01e86d3.zip
Move to single-uint file-position representation.
This makes passing them around cheaper. There is now a table (see front/codemap.rs) that is needed to transform such an uint into an actual filename/line/col location. Also cleans up the span building in the parser a bit.
Diffstat (limited to 'src/comp/front/codemap.rs')
-rw-r--r--src/comp/front/codemap.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/comp/front/codemap.rs b/src/comp/front/codemap.rs
new file mode 100644
index 00000000..20b4e2ee
--- /dev/null
+++ b/src/comp/front/codemap.rs
@@ -0,0 +1,65 @@
+import std._vec;
+
+/* 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
+ * compiler.
+ */
+
+type filemap = @rec(str name,
+ uint start_pos,
+ mutable vec[uint] lines);
+type codemap = @rec(mutable vec[filemap] files);
+type loc = rec(str filename, uint line, uint col);
+
+fn new_codemap() -> codemap {
+ let vec[filemap] files = vec();
+ ret @rec(mutable files=files);
+}
+
+fn new_filemap(str filename, uint start_pos) -> filemap {
+ let vec[uint] lines = vec();
+ ret @rec(name=filename,
+ start_pos=start_pos,
+ mutable lines=lines);
+}
+
+fn next_line(filemap file, uint pos) {
+ _vec.push[uint](file.lines, pos);
+}
+
+fn lookup_pos(codemap map, uint pos) -> loc {
+ for (filemap f in map.files) {
+ if (f.start_pos < pos) {
+ auto line_num = 1u;
+ auto line_start = 0u;
+ // FIXME this can be a binary search if we need to be faster
+ for (uint line_start_ in f.lines) {
+ // FIXME duplicate code due to lack of working break
+ if (line_start_ > pos) {
+ ret rec(filename=f.name,
+ line=line_num,
+ col=pos-line_start);
+ }
+ line_start = line_start_;
+ line_num += 1u;
+ }
+ ret rec(filename=f.name,
+ line=line_num,
+ col=pos-line_start);
+ }
+ }
+ log #fmt("Failed to find a location for character %u", pos);
+ fail;
+}
+
+//
+// Local Variables:
+// mode: rust
+// fill-column: 78;
+// indent-tabs-mode: nil
+// c-basic-offset: 4
+// buffer-file-coding-system: utf-8-unix
+// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
+// End:
+//