Skip to content

Commit

Permalink
update ebml interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Feb 11, 2012
1 parent efd393d commit 5d57fa3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
43 changes: 21 additions & 22 deletions src/comp/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import std::{io, ebml, map, list};
import io::writer_util;
import ebml::writer_util;
import syntax::ast::*;
import syntax::ast_util;
import syntax::ast_util::local_def;
Expand All @@ -21,15 +22,22 @@ type encode_ctxt = {ccx: @crate_ctxt, type_abbrevs: abbrev_map};

// Path table encoding
fn encode_name(ebml_w: ebml::writer, name: str) {
ebml::start_tag(ebml_w, tag_paths_data_name);
ebml_w.writer.write(str::bytes(name));
ebml::end_tag(ebml_w);
ebml_w.wr_tag(tag_paths_data_name) {||
ebml_w.wr_str(name);
}
}

fn encode_def_id(ebml_w: ebml::writer, id: def_id) {
ebml::start_tag(ebml_w, tag_def_id);
ebml_w.writer.write(str::bytes(def_to_str(id)));
ebml::end_tag(ebml_w);
ebml_w.wr_tag(tag_def_id) {||
ebml_w.wr_str(def_to_str(id));
}
}

fn encode_named_def_id(ebml_w: ebml::writer, name: str, id: def_id) {
ebml_w.wr_tag(tag_paths_data_item) {||
encode_name(ebml_w, name);
encode_def_id(ebml_w, id);
}
}

type entry<T> = {val: T, pos: uint};
Expand All @@ -38,10 +46,10 @@ fn encode_enum_variant_paths(ebml_w: ebml::writer, variants: [variant],
path: [str], &index: [entry<str>]) {
for variant: variant in variants {
add_to_index(ebml_w, path, index, variant.node.name);
ebml::start_tag(ebml_w, tag_paths_data_item);
encode_name(ebml_w, variant.node.name);
encode_def_id(ebml_w, local_def(variant.node.id));
ebml::end_tag(ebml_w);
ebml_w.wr_tag(tag_paths_data_item) {||
encode_name(ebml_w, variant.node.name);
encode_def_id(ebml_w, local_def(variant.node.id));
}
}
}

Expand All @@ -56,10 +64,7 @@ fn encode_native_module_item_paths(ebml_w: ebml::writer, nmod: native_mod,
path: [str], &index: [entry<str>]) {
for nitem: @native_item in nmod.items {
add_to_index(ebml_w, path, index, nitem.ident);
ebml::start_tag(ebml_w, tag_paths_data_item);
encode_name(ebml_w, nitem.ident);
encode_def_id(ebml_w, local_def(nitem.id));
ebml::end_tag(ebml_w);
encode_named_def_id(ebml_w, nitem.ident, local_def(nitem.id));
}
}

Expand All @@ -71,17 +76,11 @@ fn encode_module_item_paths(ebml_w: ebml::writer, module: _mod, path: [str],
alt it.node {
item_const(_, _) {
add_to_index(ebml_w, path, index, it.ident);
ebml::start_tag(ebml_w, tag_paths_data_item);
encode_name(ebml_w, it.ident);
encode_def_id(ebml_w, local_def(it.id));
ebml::end_tag(ebml_w);
encode_named_def_id(ebml_w, it.ident, local_def(it.id));
}
item_fn(_, tps, _) {
add_to_index(ebml_w, path, index, it.ident);
ebml::start_tag(ebml_w, tag_paths_data_item);
encode_name(ebml_w, it.ident);
encode_def_id(ebml_w, local_def(it.id));
ebml::end_tag(ebml_w);
encode_named_def_id(ebml_w, it.ident, local_def(it.id));
}
item_mod(_mod) {
add_to_index(ebml_w, path, index, it.ident);
Expand Down
1 change: 1 addition & 0 deletions src/etc/ctags.rust
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
--regex-rust=/[ \t]*enum[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-rust=/[ \t]*resource[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-rust=/[ \t]*mod[ \t]+([a-zA-Z0-9_]+)/\1/m,modules/
--regex-rust=/[ \t]*const[ \t]+([a-zA-Z0-9_]+)/\1/m,consts/
24 changes: 22 additions & 2 deletions src/libstd/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ fn create_writer(w: io::writer) -> writer {
// TODO: Provide a function to write the standard ebml header.
fn start_tag(w: writer, tag_id: uint) {
// Write the enum ID:

write_vint(w.writer, tag_id);
// Write a placeholder four-byte size.

// Write a placeholder four-byte size.
w.size_positions += [w.writer.tell()];
let zeroes: [u8] = [0u8, 0u8, 0u8, 0u8];
w.writer.write(zeroes);
Expand All @@ -170,5 +169,26 @@ fn end_tag(w: writer) {
write_sized_vint(w.writer, cur_pos - last_size_pos - 4u, 4u);
w.writer.seek(cur_pos as int, io::seek_set);
}

impl writer_util for writer {
fn wr_tag(tag_id: uint, blk: fn()) {
start_tag(self, tag_id);
blk();
end_tag(self);
}

fn wr_uint(id: uint) {
write_vint(self.writer, id);
}

fn wr_bytes(b: [u8]) {
self.writer.write(b);
}

fn wr_str(s: str) {
self.wr_bytes(str::bytes(s));
}
}

// TODO: optionally perform "relaxations" on end_tag to more efficiently
// encode sizes; this is a fixed point iteration

0 comments on commit 5d57fa3

Please sign in to comment.