-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.rs
178 lines (149 loc) · 6.45 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![allow(missing_docs)]
fn main() {
#[cfg(feature = "german")]
natural_languages::generate_word_lists();
hcl::build();
}
mod hcl {
/// The function body is mostly the output of `tree-sitter generate` (`tree-sitter`
/// version 0.22.5) inside of
/// <https://github.com/tree-sitter-grammars/tree-sitter-hcl>, see also
/// <https://tree-sitter.github.io/tree-sitter/creating-parsers#command-generate>.
/// The resulting `bindings/rust/build.rs` is the below code, slimmed down to only
/// what's strictly needed, e.g. not including any warning flags.
///
/// **Remove this code once
/// <https://github.com/tree-sitter-grammars/tree-sitter-hcl> is available on
/// <https://crates.io> and updated to a high enough `tree-sitter` version**.
pub fn build() {
let src_dir = std::path::Path::new("src/scoping/langs/tree_sitter_hcl/upstream-main/src");
let mut c_config = cc::Build::new();
c_config.include(src_dir);
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
c_config.warnings(false);
c_config.compile("parser");
}
}
#[cfg(feature = "german")]
#[allow(unreachable_pub)] // Cannot get this to play nice with clippy
mod natural_languages {
use std::env;
use std::fs::{self, File};
use std::io::{BufReader, BufWriter};
use std::path::Path;
pub fn generate_word_lists() {
let base_source_path = Path::new("data/word-lists");
// https://doc.rust-lang.org/cargo/reference/build-script-examples.html#code-generation
let out_dir = env::var_os("OUT_DIR").unwrap();
let base_destination_path = Path::new(&out_dir);
// Each of these might require different treatments, so do it separately.
{
// German
let source_file = base_source_path.join("de.txt");
println!("cargo::rerun-if-changed={}", source_file.display());
let destination_file = base_destination_path.join("de.fst");
destination_file
.parent()
.map(|p| fs::create_dir_all(p).expect("directory creation to succeed"))
.expect("parent directory to be present");
if destination_file.exists() {
println!("Output file already exists, skipping generation");
return;
}
german::process(
&mut BufReader::new(File::open(&source_file).unwrap()),
&mut BufWriter::new(File::create(destination_file).unwrap()),
);
}
}
#[cfg(feature = "german")]
mod german {
use std::collections::HashSet;
use std::env;
use std::io::{BufReader, BufWriter, Read, Write};
use std::sync::Mutex;
use decompound::{decompound, DecompositionOptions};
use rayon::prelude::*;
macro_rules! time_it {
($name:expr, $e:expr) => {{
let now = std::time::Instant::now();
let result = $e;
let duration = now.elapsed();
println!("{} - Time taken: {:?}", $name, duration);
result
}};
}
pub fn process<R, W>(source: &mut BufReader<R>, destination: &mut BufWriter<W>)
where
R: Read,
W: Write,
{
let mut contents = String::new();
let _ = source.read_to_string(&mut contents).unwrap();
let words: HashSet<&str> = time_it!(
"Constructing hashset of words",
contents.lines().map(str::trim).collect()
);
let keepers = Mutex::new(Vec::with_capacity(words.len()));
time_it!(
"Filtering words",
// Parallel iteration is a massive time-saver, more than an order of magnitude
// (approx. 2 minutes -> 5 seconds)
words.par_iter().for_each(|word| {
#[allow(clippy::single_match_else)]
match decompound(
word,
&|w| words.contains(w),
DecompositionOptions::TRY_TITLECASE_SUFFIX,
) {
Ok(_constituents) => {
// Hot loop IO: very costly, only use when debugging
// println!("Dropping '{}' ({})", word, _constituents.join("-"));
}
Err(_) => {
let mut keepers = keepers.lock().unwrap();
keepers.push(word.to_owned());
// Hot loop IO: very costly, only use when debugging
// println!("Keeping '{}'", word);
}
};
})
);
let mut keepers = keepers.into_inner().unwrap();
let dropped_words: HashSet<_> = words
.difference(&keepers.iter().copied().collect::<HashSet<_>>())
.copied()
.collect();
drop(words); // Prevent misuse; these are unfiltered!
let n_dropped = dropped_words.len();
if n_dropped > 0 {
println!(
"cargo::warning=Dropped {} compound words ({} remaining); see '{:?}' for a list.",
n_dropped,
keepers.len(),
{
let mut path: std::path::PathBuf = env::var_os("OUT_DIR").unwrap().into();
assert!(path.pop(), "no parent element"); // Remove "out"
path.push("output"); // The log file
path
},
);
}
time_it!("Sorting filtered words", keepers.sort_unstable());
// `fst::SetBuilder.insert` doesn't check for dupes, so be sure (?)
time_it!("Deduplicating filtered words", keepers.dedup());
time_it!("Building FST", {
let mut build = fst::SetBuilder::new(destination).unwrap();
for word in &keepers {
build.insert(word).unwrap();
}
build.finish().unwrap();
});
}
}
}