diff options
| author | John Clements <clements@racket-lang.org> | 2013-05-14 11:34:17 -0700 |
|---|---|---|
| committer | John Clements <clements@racket-lang.org> | 2013-06-05 12:01:40 -0700 |
| commit | fe6baa9023cf599a7747e13c67d9adebc3161c59 (patch) | |
| tree | 47b0cca12d7e5bebb22a9ddd01086fdb34cb339c /src/libsyntax/parse | |
| parent | 5a158f1d19d93af4223fea2da49209e73a3ed002 (diff) | |
| download | rust-fe6baa9023cf599a7747e13c67d9adebc3161c59.tar.gz rust-fe6baa9023cf599a7747e13c67d9adebc3161c59.zip | |
added fresh-name fn
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/token.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index ef889d5e4bc..ecf83483c21 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -22,6 +22,9 @@ use core::char; use core::cmp::Equiv; use core::local_data; use core::str; +use core::hashmap::HashSet; +use core::rand; +use core::rand::RngUtil; use core::to_bytes; #[deriving(Encodable, Decodable, Eq)] @@ -559,6 +562,20 @@ pub fn gensym_ident(str : &str) -> ast::ident { ast::new_ident(gensym(str)) } + +// create a fresh name. In principle, this is just a +// gensym, but for debugging purposes, you'd like the +// resulting name to have a suggestive stringify, without +// paying the cost of guaranteeing that the name is +// truly unique. I'm going to try to strike a balance +// by using a gensym with a name that has a random number +// at the end. So, the gensym guarantees the uniqueness, +// and the int helps to avoid confusion. +pub fn fresh_name(src_name : &str) -> Name { + let num = rand::rng().gen_uint_range(0,0xffff); + gensym(fmt!("%s_%u",src_name,num)) +} + /** * All the valid words that have meaning in the Rust language. * @@ -691,3 +708,14 @@ pub fn is_reserved_keyword(tok: &Token) -> bool { _ => false, } } + +#[cfg(test)] +mod test { + use super::*; + use std::io; + #[test] fn t1() { + let a = fresh_name("ghi"); + io::println(fmt!("interned name: %u,\ntextual name: %s\n", + a,*interner_get(a))); + } +} |
