From fe6baa9023cf599a7747e13c67d9adebc3161c59 Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 14 May 2013 11:34:17 -0700 Subject: [PATCH] added fresh-name fn --- src/libsyntax/parse/token.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index ef889d5e4bcb2..ecf83483c21fe 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))); + } +}