Skip to content

Commit

Permalink
rustc: Parse definition IDs from crates; add a function to parse unsi…
Browse files Browse the repository at this point in the history
…gned ints to the standard library
  • Loading branch information
pcwalton committed Mar 26, 2011
1 parent ee686da commit 24a75ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/comp/front/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import util.common;
import util.common.span;

import std._str;
import std._uint;
import std._vec;
import std.ebml;
import std.fs;
Expand Down Expand Up @@ -222,8 +223,22 @@ impure fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty.arg], @ty.t) {

// Rust metadata parsing

fn parse_def_id(str s) -> ast.def_id {
ret tup(1, 0); // TODO
fn parse_def_id(vec[u8] buf) -> ast.def_id {
auto colon_idx = 0u;
auto len = _vec.len[u8](buf);
while (colon_idx < len && buf.(colon_idx) != (':' as u8)) {
colon_idx += 1u;
}
if (colon_idx == len) {
log "didn't find ':' when parsing def id";
fail;
}

auto crate_part = _vec.slice[u8](buf, 0u, colon_idx);
auto def_part = _vec.slice[u8](buf, colon_idx + 1u, len);
auto crate_num = _uint.parse_buf(crate_part, 10u) as int;
auto def_num = _uint.parse_buf(def_part, 10u) as int;
ret tup(crate_num, def_num);
}

// Given a path and serialized crate metadata, returns the ID of the
Expand Down Expand Up @@ -259,9 +274,7 @@ impure fn resolve_path(vec[ast.ident] path, vec[u8] data) -> resolve_result {
ebml.move_to_first_child(ebml_r);
auto did_data = ebml.read_data(ebml_r);
ebml.move_to_parent(ebml_r);
auto did_str = _str.unsafe_from_bytes(did_data);
log "did_str: " + did_str;
did_opt = some[ast.def_id](parse_def_id(did_str));
did_opt = some[ast.def_id](parse_def_id(did_data));
}
ebml.move_to_next_sibling(ebml_r);
}
Expand Down Expand Up @@ -395,6 +408,9 @@ fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
}
}

log #fmt("resolved '%s' to %d:%d", _str.connect(path, "."), did._0,
did._1);

// TODO: Look up item type, use that to determine the type of def.

fail;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ fn next_power_of_two(uint n) -> uint {
ret tmp + 1u;
}

fn parse_buf(vec[u8] buf, uint radix) -> uint {
auto i = _vec.len[u8](buf) - 1u;
auto power = 1u;
auto n = 0u;
while (i >= 0u) {
n += (((buf.(i)) - ('0' as u8)) as uint) * power;
power *= radix;
i -= 1u;
}
ret n;
}

fn to_str(uint num, uint radix) -> str
{
auto n = num;
Expand Down

0 comments on commit 24a75ee

Please sign in to comment.