|
1 |
| -extern crate string_cache_shared; |
| 1 | +extern crate phf_shared; |
| 2 | +extern crate phf_generator; |
2 | 3 |
|
3 |
| -use string_cache_shared::{STATIC_ATOM_SET, ALL_NS, pack_static}; |
| 4 | +#[path = "src/shared.rs"] #[allow(dead_code)] mod shared; |
| 5 | +#[path = "src/static_atom_list.rs"] mod static_atom_list; |
4 | 6 |
|
5 | 7 | use std::env;
|
6 |
| -use std::ascii::AsciiExt; |
7 | 8 | use std::fs::File;
|
8 | 9 | use std::io::{BufWriter, Write};
|
9 | 10 | use std::path::Path;
|
10 | 11 |
|
11 | 12 | fn main() {
|
12 |
| - let path = Path::new(&env::var("OUT_DIR").unwrap()).join("ns_atom_macros_without_plugin.rs"); |
13 |
| - let mut file = BufWriter::new(File::create(&path).unwrap()); |
14 |
| - writeln!(file, r"#[macro_export]").unwrap(); |
15 |
| - writeln!(file, r"macro_rules! ns {{").unwrap(); |
16 |
| - writeln!(file, "(\"\") => {{ $crate::Namespace({}) }};", atom("")).unwrap(); |
17 |
| - for &(prefix, url) in ALL_NS { |
18 |
| - if !prefix.is_empty() { |
19 |
| - generate_combination("".to_owned(), prefix, url, &mut file); |
| 13 | + let hash_state = generate(); |
| 14 | + write_static_atom_set(&hash_state); |
| 15 | + write_atom_macro(&hash_state); |
| 16 | +} |
| 17 | + |
| 18 | +fn generate() -> phf_generator::HashState { |
| 19 | + let mut set = std::collections::HashSet::new(); |
| 20 | + for atom in static_atom_list::ATOMS { |
| 21 | + if !set.insert(atom) { |
| 22 | + panic!("duplicate static atom `{:?}`", atom); |
20 | 23 | }
|
21 | 24 | }
|
22 |
| - writeln!(file, r"}}").unwrap(); |
| 25 | + phf_generator::generate_hash(static_atom_list::ATOMS) |
| 26 | +} |
23 | 27 |
|
| 28 | +fn write_static_atom_set(hash_state: &phf_generator::HashState) { |
| 29 | + let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("static_atom_set.rs"); |
| 30 | + let mut file = BufWriter::new(File::create(&path).unwrap()); |
| 31 | + macro_rules! w { |
| 32 | + ($($arg: expr),+) => { (writeln!(&mut file, $($arg),+).unwrap()) } |
| 33 | + } |
| 34 | + w!("pub static STATIC_ATOM_SET: StaticAtomSet = StaticAtomSet {{"); |
| 35 | + w!(" key: {},", hash_state.key); |
| 36 | + w!(" disps: &["); |
| 37 | + for &(d1, d2) in &hash_state.disps { |
| 38 | + w!(" ({}, {}),", d1, d2); |
| 39 | + } |
| 40 | + w!(" ],"); |
| 41 | + w!(" atoms: &["); |
| 42 | + for &idx in &hash_state.map { |
| 43 | + w!(" {:?},", static_atom_list::ATOMS[idx]); |
| 44 | + } |
| 45 | + w!(" ],"); |
| 46 | + w!("}};"); |
| 47 | +} |
| 48 | + |
| 49 | +fn write_atom_macro(hash_state: &phf_generator::HashState) { |
| 50 | + let set = shared::StaticAtomSet { |
| 51 | + key: hash_state.key, |
| 52 | + disps: leak(hash_state.disps.clone()), |
| 53 | + atoms: leak(hash_state.map.iter().map(|&idx| static_atom_list::ATOMS[idx]).collect()), |
| 54 | + }; |
| 55 | + |
| 56 | + let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atom_macro.rs"); |
| 57 | + let mut file = BufWriter::new(File::create(&path).unwrap()); |
24 | 58 | writeln!(file, r"#[macro_export]").unwrap();
|
25 | 59 | writeln!(file, r"macro_rules! atom {{").unwrap();
|
26 |
| - for &s in STATIC_ATOM_SET.iter() { |
| 60 | + for &s in set.iter() { |
27 | 61 | if is_ident(s) {
|
28 |
| - writeln!(file, r"( {} ) => {{ {} }};", s, atom(s)).unwrap(); |
| 62 | + writeln!(file, r"( {} ) => {{ {} }};", s, atom(&set, s)).unwrap(); |
29 | 63 | }
|
30 |
| - writeln!(file, r"({:?}) => {{ {} }};", s, atom(s)).unwrap(); |
| 64 | + writeln!(file, r"({:?}) => {{ {} }};", s, atom(&set, s)).unwrap(); |
31 | 65 | }
|
32 | 66 | writeln!(file, r"}}").unwrap();
|
33 | 67 | }
|
34 | 68 |
|
35 |
| -fn generate_combination(prefix1: String, suffix: &str, url: &str, file: &mut BufWriter<File>) { |
36 |
| - if suffix.is_empty() { |
37 |
| - writeln!(file, r"({:?}) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap(); |
38 |
| - writeln!(file, r"( {} ) => {{ $crate::Namespace({}) }};", prefix1, atom(url)).unwrap(); |
39 |
| - } else { |
40 |
| - let prefix2 = prefix1.clone(); |
41 |
| - generate_combination(prefix1 + &*suffix[..1].to_ascii_lowercase(), &suffix[1..], url, file); |
42 |
| - generate_combination(prefix2 + &*suffix[..1].to_ascii_uppercase(), &suffix[1..], url, file); |
43 |
| - } |
| 69 | +fn leak<T>(v: Vec<T>) -> &'static [T] { |
| 70 | + let slice = unsafe { ::std::slice::from_raw_parts(v.as_ptr(), v.len()) }; |
| 71 | + ::std::mem::forget(v); |
| 72 | + slice |
44 | 73 | }
|
45 | 74 |
|
46 |
| -fn atom(s: &str) -> String { |
47 |
| - let data = pack_static(STATIC_ATOM_SET.get_index_or_hash(s).unwrap() as u32); |
| 75 | +fn atom(set: &shared::StaticAtomSet, s: &str) -> String { |
| 76 | + let data = shared::pack_static(set.get_index_or_hash(s).unwrap() as u32); |
48 | 77 | format!("$crate::Atom {{ data: 0x{:x} }}", data)
|
49 | 78 | }
|
50 | 79 |
|
|
0 commit comments