aboutsummaryrefslogtreecommitdiff
path: root/tools/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/src/main.rs')
-rw-r--r--tools/src/main.rs141
1 files changed, 141 insertions, 0 deletions
diff --git a/tools/src/main.rs b/tools/src/main.rs
new file mode 100644
index 000000000..671f05388
--- /dev/null
+++ b/tools/src/main.rs
@@ -0,0 +1,141 @@
1extern crate clap;
2#[macro_use]
3extern crate failure;
4extern crate ron;
5extern crate tera;
6extern crate walkdir;
7extern crate tools;
8
9use std::{collections::HashSet, fs, path::Path};
10use clap::{App, Arg, SubCommand};
11use tools::{collect_tests, Test};
12
13type Result<T> = ::std::result::Result<T, failure::Error>;
14
15const GRAMMAR_DIR: &str = "./src/parser/grammar";
16const INLINE_TESTS_DIR: &str = "tests/data/parser/inline";
17const GRAMMAR: &str = "./src/grammar.ron";
18const SYNTAX_KINDS: &str = "./src/syntax_kinds/generated.rs";
19const SYNTAX_KINDS_TEMPLATE: &str = "./src/syntax_kinds/generated.rs.tera";
20
21fn main() -> Result<()> {
22 let matches = App::new("tasks")
23 .setting(clap::AppSettings::SubcommandRequiredElseHelp)
24 .arg(
25 Arg::with_name("verify")
26 .long("--verify")
27 .help("Verify that generated code is up-to-date")
28 .global(true),
29 )
30 .subcommand(SubCommand::with_name("gen-kinds"))
31 .subcommand(SubCommand::with_name("gen-tests"))
32 .get_matches();
33 match matches.subcommand() {
34 (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?,
35 _ => unreachable!(),
36 }
37 Ok(())
38}
39
40fn run_gen_command(name: &str, verify: bool) -> Result<()> {
41 match name {
42 "gen-kinds" => update(Path::new(SYNTAX_KINDS), &get_kinds()?, verify),
43 "gen-tests" => gen_tests(verify),
44 _ => unreachable!(),
45 }
46}
47
48fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
49 match fs::read_to_string(path) {
50 Ok(ref old_contents) if old_contents == contents => {
51 return Ok(());
52 }
53 _ => (),
54 }
55 if verify {
56 bail!("`{}` is not up-to-date", path.display());
57 }
58 eprintln!("updating {}", path.display());
59 fs::write(path, contents)?;
60 Ok(())
61}
62
63fn get_kinds() -> Result<String> {
64 let grammar = grammar()?;
65 let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?;
66 let ret = tera::Tera::one_off(&template, &grammar, false)
67 .map_err(|e| format_err!("template error: {}", e))?;
68 Ok(ret)
69}
70
71fn grammar() -> Result<ron::value::Value> {
72 let text = fs::read_to_string(GRAMMAR)?;
73 let ret = ron::de::from_str(&text)?;
74 Ok(ret)
75}
76
77fn gen_tests(verify: bool) -> Result<()> {
78 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
79
80 let inline_tests_dir = Path::new(INLINE_TESTS_DIR);
81 if !inline_tests_dir.is_dir() {
82 fs::create_dir_all(inline_tests_dir)?;
83 }
84 let existing = existing_tests(inline_tests_dir)?;
85
86 for t in existing.difference(&tests) {
87 panic!("Test is deleted: {}\n{}", t.name, t.text);
88 }
89
90 let new_tests = tests.difference(&existing);
91 for (i, t) in new_tests.enumerate() {
92 let name = format!("{:04}_{}.rs", existing.len() + i + 1, t.name);
93 let path = inline_tests_dir.join(name);
94 update(&path, &t.text, verify)?;
95 }
96 Ok(())
97}
98
99
100impl ::std::hash::Hash for Test {
101 fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
102 self.name.hash(state)
103 }
104}
105
106fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> {
107 let mut res = HashSet::new();
108 for entry in ::walkdir::WalkDir::new(dir) {
109 let entry = entry.unwrap();
110 if !entry.file_type().is_file() {
111 continue;
112 }
113 if entry.path().extension().unwrap_or_default() != "rs" {
114 continue;
115 }
116 let text = fs::read_to_string(entry.path())?;
117
118 for test in collect_tests(&text) {
119 if let Some(old_test) = res.replace(test) {
120 bail!("Duplicate test: {}", old_test.name)
121 }
122 }
123 }
124 Ok(res)
125}
126
127fn existing_tests(dir: &Path) -> Result<HashSet<Test>> {
128 let mut res = HashSet::new();
129 for file in fs::read_dir(dir)? {
130 let file = file?;
131 let path = file.path();
132 if path.extension().unwrap_or_default() != "rs" {
133 continue;
134 }
135 let name = path.file_name().unwrap().to_str().unwrap();
136 let name = name["0000_".len()..name.len() - 3].to_string();
137 let text = fs::read_to_string(&path)?;
138 res.insert(Test { name, text });
139 }
140 Ok(res)
141}