Skip to content

Commit fe6baa9

Browse files
committed
added fresh-name fn
1 parent 5a158f1 commit fe6baa9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libsyntax/parse/token.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use core::char;
2222
use core::cmp::Equiv;
2323
use core::local_data;
2424
use core::str;
25+
use core::hashmap::HashSet;
26+
use core::rand;
27+
use core::rand::RngUtil;
2528
use core::to_bytes;
2629

2730
#[deriving(Encodable, Decodable, Eq)]
@@ -559,6 +562,20 @@ pub fn gensym_ident(str : &str) -> ast::ident {
559562
ast::new_ident(gensym(str))
560563
}
561564

565+
566+
// create a fresh name. In principle, this is just a
567+
// gensym, but for debugging purposes, you'd like the
568+
// resulting name to have a suggestive stringify, without
569+
// paying the cost of guaranteeing that the name is
570+
// truly unique. I'm going to try to strike a balance
571+
// by using a gensym with a name that has a random number
572+
// at the end. So, the gensym guarantees the uniqueness,
573+
// and the int helps to avoid confusion.
574+
pub fn fresh_name(src_name : &str) -> Name {
575+
let num = rand::rng().gen_uint_range(0,0xffff);
576+
gensym(fmt!("%s_%u",src_name,num))
577+
}
578+
562579
/**
563580
* All the valid words that have meaning in the Rust language.
564581
*
@@ -691,3 +708,14 @@ pub fn is_reserved_keyword(tok: &Token) -> bool {
691708
_ => false,
692709
}
693710
}
711+
712+
#[cfg(test)]
713+
mod test {
714+
use super::*;
715+
use std::io;
716+
#[test] fn t1() {
717+
let a = fresh_name("ghi");
718+
io::println(fmt!("interned name: %u,\ntextual name: %s\n",
719+
a,*interner_get(a)));
720+
}
721+
}

0 commit comments

Comments
 (0)