about summary refs log tree commit diff
path: root/src/libsyntax/util
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-05-20 12:04:47 -0700
committerbors <bors@rust-lang.org>2013-05-20 12:04:47 -0700
commit26babaafcdbcfdf2e842d84dbeabbed0dae6efef (patch)
tree51ba2c1f043882643de9ca2957d9efab24335a91 /src/libsyntax/util
parentf3b458b5c5a555067e9d013066fac9c8de50c3f8 (diff)
parentb71a1ecea2de87cff3089f9f261be71cd314aac9 (diff)
downloadrust-26babaafcdbcfdf2e842d84dbeabbed0dae6efef.tar.gz
rust-26babaafcdbcfdf2e842d84dbeabbed0dae6efef.zip
auto merge of #6559 : jbclements/rust/hygiene-fns-and-cleanup, r=jbclements
This includes new, tested, hygiene support functions. It also removes the interner_key! macro and replaces it with a function, which should be inline-able. It also contains some parser patch-ups and some docfixes.

On my machine, this patch passes all tests.
Diffstat (limited to 'src/libsyntax/util')
-rw-r--r--src/libsyntax/util/interner.rs16
-rw-r--r--src/libsyntax/util/parser_testing.rs60
2 files changed, 62 insertions, 14 deletions
diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs
index cca2ec89fd4..5d49c8cd75d 100644
--- a/src/libsyntax/util/interner.rs
+++ b/src/libsyntax/util/interner.rs
@@ -12,9 +12,6 @@
 // allows bidirectional lookup; i.e. given a value, one can easily find the
 // type, and vice versa.
 
-// allow the interner_key macro to escape this module:
-#[macro_escape];
-
 use core::cmp::Equiv;
 use core::hashmap::HashMap;
 use syntax::parse::token::StringRef;
@@ -78,6 +75,8 @@ pub impl<T:Eq + IterBytes + Hash + Const + Copy> Interner<T> {
     }
 }
 
+// A StrInterner differs from Interner<String> in that it accepts
+// borrowed pointers rather than @ ones, resulting in less allocation.
 pub struct StrInterner {
     priv map: @mut HashMap<@~str, uint>,
     priv vect: @mut ~[@~str],
@@ -133,17 +132,6 @@ pub impl StrInterner {
     }
 }
 
-/* Key for thread-local data for sneaking interner information to the
-* encoder/decoder. It sounds like a hack because it is one.
-* Bonus ultra-hack: functions as keys don't work across crates,
-* so we have to use a unique number. See taskgroup_key! in task.rs
-* for another case of this. */
-macro_rules! interner_key (
-    () => (cast::transmute::<(uint, uint),
-           &fn(v: @@::parse::token::ident_interner)>(
-        (-3 as uint, 0u)))
-)
-
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs
new file mode 100644
index 00000000000..1c2210c96b6
--- /dev/null
+++ b/src/libsyntax/util/parser_testing.rs
@@ -0,0 +1,60 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use core::option::{Option,None};
+use ast;
+use parse::parser::Parser;
+use parse::{new_parse_sess};
+
+use syntax::parse::{ParseSess,string_to_filemap,filemap_to_tts};
+use syntax::parse::{new_parser_from_source_str};
+
+// map a string to tts, using a made-up filename: return both the token_trees
+// and the ParseSess
+pub fn string_to_tts_and_sess (source_str : @~str) -> (~[ast::token_tree],@mut ParseSess) {
+    let ps = new_parse_sess(None);
+    (filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps)
+}
+
+pub fn string_to_parser_and_sess(source_str: @~str) -> (Parser,@mut ParseSess) {
+    let ps = new_parse_sess(None);
+    (new_parser_from_source_str(ps,~[],~"bogofile",source_str),ps)
+}
+
+// map string to parser (via tts)
+pub fn string_to_parser(source_str: @~str) -> Parser {
+    let (p,_) = string_to_parser_and_sess(source_str);
+    p
+}
+
+pub fn string_to_crate (source_str : @~str) -> @ast::crate {
+    string_to_parser(source_str).parse_crate_mod()
+}
+
+// parse a string, return an expr
+pub fn string_to_expr (source_str : @~str) -> @ast::expr {
+    string_to_parser(source_str).parse_expr()
+}
+
+// parse a string, return an item
+pub fn string_to_item (source_str : @~str) -> Option<@ast::item> {
+    string_to_parser(source_str).parse_item(~[])
+}
+
+// parse a string, return an item and the ParseSess
+pub fn string_to_item_and_sess (source_str : @~str) -> (Option<@ast::item>,@mut ParseSess) {
+    let (p,ps) = string_to_parser_and_sess(source_str);
+    (p.parse_item(~[]),ps)
+}
+
+pub fn string_to_stmt (source_str : @~str) -> @ast::stmt {
+    string_to_parser(source_str).parse_stmt(~[])
+}
+