diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-02-03 09:54:52 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-02-03 09:54:52 +0000 |
commit | 3c70ae2e26f654ce536f00fde8c159e4ebe6901a (patch) | |
tree | 89fe4c9e08b76f69d39f4a565848c4ba054b6da7 | |
parent | 6d9753bf548b22ab1a54462f72c9c0bf4ff69382 (diff) | |
parent | 9435ea4b8e990521ee7a6206b6106bb3ce392746 (diff) |
Merge #38
38: Move tools to a separate package r=matklad a=matklad
-rw-r--r-- | .cargo/config | 3 | ||||
-rw-r--r-- | Cargo.toml | 8 | ||||
-rw-r--r-- | docs/TOOLS.md | 30 | ||||
-rw-r--r-- | tests/lexer.rs | 1 | ||||
-rw-r--r-- | tests/parser.rs | 1 | ||||
-rw-r--r-- | tests/testutils/src/lib.rs | 34 | ||||
-rw-r--r-- | tools/Cargo.toml | 12 | ||||
-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 |
9 files changed, 75 insertions, 30 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] | ||
2 | parse = "run --package tools --bin parse" | ||
3 | gen = "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" | |||
4 | authors = ["Aleksey Kladov <[email protected]>"] | 4 | authors = ["Aleksey Kladov <[email protected]>"] |
5 | license = "MIT OR Apache-2.0" | 5 | license = "MIT OR Apache-2.0" |
6 | 6 | ||
7 | [workspace] | ||
8 | members = [ "tools" ] | ||
9 | |||
7 | [dependencies] | 10 | [dependencies] |
8 | unicode-xid = "0.1.0" | 11 | unicode-xid = "0.1.0" |
9 | 12 | ||
10 | serde = "1.0.26" | ||
11 | serde_derive = "1.0.26" | ||
12 | file = "1.1.1" | ||
13 | ron = "0.1.5" | ||
14 | |||
15 | [dev-dependencies] | 13 | [dev-dependencies] |
16 | testutils = { path = "./tests/testutils" } | 14 | testutils = { 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 | |||
3 | libsyntax uses several tools to help with development. | ||
4 | |||
5 | Each tool is a binary in the [tools/](../tools) package. | ||
6 | You can run them via `cargo run` command. | ||
7 | |||
8 | ``` | ||
9 | cargo run --package tools --bin tool | ||
10 | ``` | ||
11 | |||
12 | There are also aliases in [./cargo/config](../.cargo/config), | ||
13 | so the following also works: | ||
14 | |||
15 | ``` | ||
16 | cargo tool | ||
17 | ``` | ||
18 | |||
19 | |||
20 | # Tool: `gen` | ||
21 | |||
22 | This tool reads a "grammar" from [grammar.ron](../grammar.ron) and | ||
23 | generates the `syntax_kinds.rs` file. You should run this tool if you | ||
24 | add new keywords or syntax elements. | ||
25 | |||
26 | |||
27 | # Tool: 'parse' | ||
28 | |||
29 | This tool reads rust source code from the standard input, parses it, | ||
30 | and prints the result to stdout. | ||
diff --git a/tests/lexer.rs b/tests/lexer.rs index 397ebafdd..46ac9fedd 100644 --- a/tests/lexer.rs +++ b/tests/lexer.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | extern crate file; | ||
2 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
3 | extern crate testutils; | 2 | extern crate testutils; |
4 | 3 | ||
diff --git a/tests/parser.rs b/tests/parser.rs index 37c9021ef..f681c066f 100644 --- a/tests/parser.rs +++ b/tests/parser.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | extern crate file; | ||
2 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
3 | extern crate testutils; | 2 | extern crate testutils; |
4 | 3 | ||
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 @@ | |||
1 | extern crate difference; | 1 | extern crate difference; |
2 | extern crate file; | 2 | extern crate file; |
3 | 3 | ||
4 | use std::path::{PathBuf, Path}; | 4 | use std::path::{Path, PathBuf}; |
5 | use std::fs::read_dir; | 5 | use std::fs::read_dir; |
6 | 6 | ||
7 | use difference::Changeset; | 7 | use 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 | ||
24 | pub fn dir_tests<F>( | 24 | pub fn dir_tests<F>(paths: &[&str], f: F) |
25 | paths: &[&str], | ||
26 | f: F | ||
27 | ) | ||
28 | where | 25 | where |
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 | ||
50 | fn assert_equal_text( | 47 | fn 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 | ||
60 | fn collect_tests(paths: &[&str]) -> Vec<PathBuf> { | 53 | fn 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 | ||
67 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { | 63 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { |
@@ -95,11 +91,13 @@ fn print_difference(expected: &str, actual: &str, path: &Path) { | |||
95 | fn project_dir() -> PathBuf { | 91 | fn 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 | ||
103 | fn test_data_dir() -> PathBuf { | 101 | fn 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] | ||
2 | name = "tools" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Aleksey Kladov <[email protected]>"] | ||
5 | publish = false | ||
6 | |||
7 | [dependencies] | ||
8 | serde = "1.0.26" | ||
9 | serde_derive = "1.0.26" | ||
10 | file = "1.1.1" | ||
11 | ron = "0.1.5" | ||
12 | libsyntax2 = { 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; | |||
11 | fn main() { | 11 | fn 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 | ||
96 | fn grammar_file() -> PathBuf { | 99 | fn 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 | ||
101 | fn generated_file() -> PathBuf { | 103 | fn 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 | ||
106 | fn scream(word: &str) -> String { | 107 | fn scream(word: &str) -> String { |
@@ -110,3 +111,8 @@ fn scream(word: &str) -> String { | |||
110 | fn kw_token(keyword: &str) -> String { | 111 | fn kw_token(keyword: &str) -> String { |
111 | format!("{}_KW", scream(keyword)) | 112 | format!("{}_KW", scream(keyword)) |
112 | } | 113 | } |
114 | |||
115 | fn 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 | |||