Skip to content

Commit 3c70ae2

Browse files
committed
Merge #38
38: Move tools to a separate package r=matklad a=matklad
2 parents 6d9753b + 9435ea4 commit 3c70ae2

File tree

9 files changed

+75
-30
lines changed

9 files changed

+75
-30
lines changed

.cargo/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[alias]
2+
parse = "run --package tools --bin parse"
3+
gen = "run --package tools --bin gen"

Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ version = "0.1.0"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
license = "MIT OR Apache-2.0"
66

7+
[workspace]
8+
members = [ "tools" ]
9+
710
[dependencies]
811
unicode-xid = "0.1.0"
912

10-
serde = "1.0.26"
11-
serde_derive = "1.0.26"
12-
file = "1.1.1"
13-
ron = "0.1.5"
14-
1513
[dev-dependencies]
1614
testutils = { path = "./tests/testutils" }

docs/TOOLS.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

tests/lexer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate file;
21
extern crate libsyntax2;
32
extern crate testutils;
43

tests/parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate file;
21
extern crate libsyntax2;
32
extern crate testutils;
43

tests/testutils/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate difference;
22
extern crate file;
33

4-
use std::path::{PathBuf, Path};
4+
use std::path::{Path, PathBuf};
55
use std::fs::read_dir;
66

77
use difference::Changeset;
@@ -21,12 +21,9 @@ fn read_text(path: &Path) -> String {
2121
file::get_text(path).unwrap().replace("\r\n", "\n")
2222
}
2323

24-
pub fn dir_tests<F>(
25-
paths: &[&str],
26-
f: F
27-
)
24+
pub fn dir_tests<F>(paths: &[&str], f: F)
2825
where
29-
F: Fn(&str) -> String
26+
F: Fn(&str) -> String,
3027
{
3128
for path in collect_tests(paths) {
3229
let actual = {
@@ -47,21 +44,20 @@ where
4744
}
4845
}
4946

50-
fn assert_equal_text(
51-
expected: &str,
52-
actual: &str,
53-
path: &Path
54-
) {
47+
fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
5548
if expected != actual {
5649
print_difference(expected, actual, path)
5750
}
5851
}
5952

6053
fn collect_tests(paths: &[&str]) -> Vec<PathBuf> {
61-
paths.iter().flat_map(|path| {
62-
let path = test_data_dir().join(path);
63-
test_from_dir(&path).into_iter()
64-
}).collect()
54+
paths
55+
.iter()
56+
.flat_map(|path| {
57+
let path = test_data_dir().join(path);
58+
test_from_dir(&path).into_iter()
59+
})
60+
.collect()
6561
}
6662

6763
fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
@@ -95,11 +91,13 @@ fn print_difference(expected: &str, actual: &str, path: &Path) {
9591
fn project_dir() -> PathBuf {
9692
let dir = env!("CARGO_MANIFEST_DIR");
9793
PathBuf::from(dir)
98-
.parent().unwrap()
99-
.parent().unwrap()
94+
.parent()
95+
.unwrap()
96+
.parent()
97+
.unwrap()
10098
.to_owned()
10199
}
102100

103101
fn test_data_dir() -> PathBuf {
104102
project_dir().join("tests/data")
105-
}
103+
}

tools/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "../" }

src/bin/gen.rs renamed to tools/src/bin/gen.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use std::fmt::Write;
1111
fn main() {
1212
let grammar = Grammar::read();
1313
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+
}
1518
}
1619

1720
#[derive(Deserialize)]
@@ -94,13 +97,11 @@ impl Grammar {
9497
}
9598

9699
fn grammar_file() -> PathBuf {
97-
let dir = env!("CARGO_MANIFEST_DIR");
98-
PathBuf::from(dir).join("grammar.ron")
100+
base_dir().join("grammar.ron")
99101
}
100102

101103
fn generated_file() -> PathBuf {
102-
let dir = env!("CARGO_MANIFEST_DIR");
103-
PathBuf::from(dir).join("src/syntax_kinds.rs")
104+
base_dir().join("src/syntax_kinds.rs")
104105
}
105106

106107
fn scream(word: &str) -> String {
@@ -110,3 +111,8 @@ fn scream(word: &str) -> String {
110111
fn kw_token(keyword: &str) -> String {
111112
format!("{}_KW", scream(keyword))
112113
}
114+
115+
fn base_dir() -> PathBuf {
116+
let dir = env!("CARGO_MANIFEST_DIR");
117+
PathBuf::from(dir).parent().unwrap().to_owned()
118+
}
File renamed without changes.

0 commit comments

Comments
 (0)