aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.cargo/config3
-rw-r--r--Cargo.toml8
-rw-r--r--docs/TOOLS.md30
-rw-r--r--tests/testutils/src/lib.rs34
-rw-r--r--tools/Cargo.toml12
-rw-r--r--tools/src/bin/gen.rs (renamed from src/bin/gen.rs)16
-rw-r--r--tools/src/bin/parse.rs (renamed from src/bin/parse-rust.rs)0
7 files changed, 75 insertions, 28 deletions
diff --git a/.cargo/config b/.cargo/config
new file mode 100644
index 000000000..1ebc0f748
--- /dev/null
+++ b/.cargo/config
@@ -0,0 +1,3 @@
1[alias]
2parse = "run --package tools --bin parse"
3gen = "run --package tools --bin gen"
diff --git a/Cargo.toml b/Cargo.toml
index e5caa5d12..622abcc42 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,13 +4,11 @@ version = "0.1.0"
4authors = ["Aleksey Kladov <[email protected]>"] 4authors = ["Aleksey Kladov <[email protected]>"]
5license = "MIT OR Apache-2.0" 5license = "MIT OR Apache-2.0"
6 6
7[workspace]
8members = [ "tools" ]
9
7[dependencies] 10[dependencies]
8unicode-xid = "0.1.0" 11unicode-xid = "0.1.0"
9 12
10serde = "1.0.26"
11serde_derive = "1.0.26"
12file = "1.1.1"
13ron = "0.1.5"
14
15[dev-dependencies] 13[dev-dependencies]
16testutils = { path = "./tests/testutils" } 14testutils = { path = "./tests/testutils" }
diff --git a/docs/TOOLS.md b/docs/TOOLS.md
new file mode 100644
index 000000000..1fcfa2dec
--- /dev/null
+++ b/docs/TOOLS.md
@@ -0,0 +1,30 @@
1# Tools used to implement libsyntax
2
3libsyntax uses several tools to help with development.
4
5Each tool is a binary in the [tools/](../tools) package.
6You can run them via `cargo run` command.
7
8```
9cargo run --package tools --bin tool
10```
11
12There are also aliases in [./cargo/config](../.cargo/config),
13so the following also works:
14
15```
16cargo tool
17```
18
19
20# Tool: `gen`
21
22This tool reads a "grammar" from [grammar.ron](../grammar.ron) and
23generates the `syntax_kinds.rs` file. You should run this tool if you
24add new keywords or syntax elements.
25
26
27# Tool: 'parse'
28
29This tool reads rust source code from the standard input, parses it,
30and prints the result to stdout.
diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs
index 80a94fb66..f829b243b 100644
--- a/tests/testutils/src/lib.rs
+++ b/tests/testutils/src/lib.rs
@@ -1,7 +1,7 @@
1extern crate difference; 1extern crate difference;
2extern crate file; 2extern crate file;
3 3
4use std::path::{PathBuf, Path}; 4use std::path::{Path, PathBuf};
5use std::fs::read_dir; 5use std::fs::read_dir;
6 6
7use difference::Changeset; 7use difference::Changeset;
@@ -21,12 +21,9 @@ fn read_text(path: &Path) -> String {
21 file::get_text(path).unwrap().replace("\r\n", "\n") 21 file::get_text(path).unwrap().replace("\r\n", "\n")
22} 22}
23 23
24pub fn dir_tests<F>( 24pub fn dir_tests<F>(paths: &[&str], f: F)
25 paths: &[&str],
26 f: F
27)
28where 25where
29 F: Fn(&str) -> String 26 F: Fn(&str) -> String,
30{ 27{
31 for path in collect_tests(paths) { 28 for path in collect_tests(paths) {
32 let actual = { 29 let actual = {
@@ -47,21 +44,20 @@ where
47 } 44 }
48} 45}
49 46
50fn assert_equal_text( 47fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
51 expected: &str,
52 actual: &str,
53 path: &Path
54) {
55 if expected != actual { 48 if expected != actual {
56 print_difference(expected, actual, path) 49 print_difference(expected, actual, path)
57 } 50 }
58} 51}
59 52
60fn collect_tests(paths: &[&str]) -> Vec<PathBuf> { 53fn collect_tests(paths: &[&str]) -> Vec<PathBuf> {
61 paths.iter().flat_map(|path| { 54 paths
62 let path = test_data_dir().join(path); 55 .iter()
63 test_from_dir(&path).into_iter() 56 .flat_map(|path| {
64 }).collect() 57 let path = test_data_dir().join(path);
58 test_from_dir(&path).into_iter()
59 })
60 .collect()
65} 61}
66 62
67fn test_from_dir(dir: &Path) -> Vec<PathBuf> { 63fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
@@ -95,11 +91,13 @@ fn print_difference(expected: &str, actual: &str, path: &Path) {
95fn project_dir() -> PathBuf { 91fn project_dir() -> PathBuf {
96 let dir = env!("CARGO_MANIFEST_DIR"); 92 let dir = env!("CARGO_MANIFEST_DIR");
97 PathBuf::from(dir) 93 PathBuf::from(dir)
98 .parent().unwrap() 94 .parent()
99 .parent().unwrap() 95 .unwrap()
96 .parent()
97 .unwrap()
100 .to_owned() 98 .to_owned()
101} 99}
102 100
103fn test_data_dir() -> PathBuf { 101fn test_data_dir() -> PathBuf {
104 project_dir().join("tests/data") 102 project_dir().join("tests/data")
105} \ No newline at end of file 103}
diff --git a/tools/Cargo.toml b/tools/Cargo.toml
new file mode 100644
index 000000000..e46874929
--- /dev/null
+++ b/tools/Cargo.toml
@@ -0,0 +1,12 @@
1[package]
2name = "tools"
3version = "0.1.0"
4authors = ["Aleksey Kladov <[email protected]>"]
5publish = false
6
7[dependencies]
8serde = "1.0.26"
9serde_derive = "1.0.26"
10file = "1.1.1"
11ron = "0.1.5"
12libsyntax2 = { path = "../" }
diff --git a/src/bin/gen.rs b/tools/src/bin/gen.rs
index e32f5044e..17cdea7a1 100644
--- a/src/bin/gen.rs
+++ b/tools/src/bin/gen.rs
@@ -11,7 +11,10 @@ use std::fmt::Write;
11fn main() { 11fn main() {
12 let grammar = Grammar::read(); 12 let grammar = Grammar::read();
13 let text = grammar.to_syntax_kinds(); 13 let text = grammar.to_syntax_kinds();
14 file::put_text(&generated_file(), &text).unwrap(); 14 let target = generated_file();
15 if text != file::get_text(&target).unwrap_or_default() {
16 file::put_text(&target, &text).unwrap();
17 }
15} 18}
16 19
17#[derive(Deserialize)] 20#[derive(Deserialize)]
@@ -94,13 +97,11 @@ impl Grammar {
94} 97}
95 98
96fn grammar_file() -> PathBuf { 99fn grammar_file() -> PathBuf {
97 let dir = env!("CARGO_MANIFEST_DIR"); 100 base_dir().join("grammar.ron")
98 PathBuf::from(dir).join("grammar.ron")
99} 101}
100 102
101fn generated_file() -> PathBuf { 103fn generated_file() -> PathBuf {
102 let dir = env!("CARGO_MANIFEST_DIR"); 104 base_dir().join("src/syntax_kinds.rs")
103 PathBuf::from(dir).join("src/syntax_kinds.rs")
104} 105}
105 106
106fn scream(word: &str) -> String { 107fn scream(word: &str) -> String {
@@ -110,3 +111,8 @@ fn scream(word: &str) -> String {
110fn kw_token(keyword: &str) -> String { 111fn kw_token(keyword: &str) -> String {
111 format!("{}_KW", scream(keyword)) 112 format!("{}_KW", scream(keyword))
112} 113}
114
115fn base_dir() -> PathBuf {
116 let dir = env!("CARGO_MANIFEST_DIR");
117 PathBuf::from(dir).parent().unwrap().to_owned()
118}
diff --git a/src/bin/parse-rust.rs b/tools/src/bin/parse.rs
index af1325bfc..af1325bfc 100644
--- a/src/bin/parse-rust.rs
+++ b/tools/src/bin/parse.rs