Skip to content

Commit

Permalink
added fresh-name fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jbclements committed Jun 5, 2013
1 parent 5a158f1 commit fe6baa9
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)));
}
}

0 comments on commit fe6baa9

Please sign in to comment.