Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
parse = "run --package tools --bin parse"
gen = "run --package tools --bin gen"
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ version = "0.1.0"
authors = ["Aleksey Kladov <[email protected]>"]
license = "MIT OR Apache-2.0"

[workspace]
members = [ "tools" ]

[dependencies]
unicode-xid = "0.1.0"

serde = "1.0.26"
serde_derive = "1.0.26"
file = "1.1.1"
ron = "0.1.5"

[dev-dependencies]
testutils = { path = "./tests/testutils" }
30 changes: 30 additions & 0 deletions docs/TOOLS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Tools used to implement libsyntax

libsyntax uses several tools to help with development.

Each tool is a binary in the [tools/](../tools) package.
You can run them via `cargo run` command.

```
cargo run --package tools --bin tool
```

There are also aliases in [./cargo/config](../.cargo/config),
so the following also works:

```
cargo tool
```


# Tool: `gen`

This tool reads a "grammar" from [grammar.ron](../grammar.ron) and
generates the `syntax_kinds.rs` file. You should run this tool if you
add new keywords or syntax elements.


# Tool: 'parse'

This tool reads rust source code from the standard input, parses it,
and prints the result to stdout.
1 change: 0 additions & 1 deletion tests/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate file;
extern crate libsyntax2;
extern crate testutils;

Expand Down
1 change: 0 additions & 1 deletion tests/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate file;
extern crate libsyntax2;
extern crate testutils;

Expand Down
34 changes: 16 additions & 18 deletions tests/testutils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate difference;
extern crate file;

use std::path::{PathBuf, Path};
use std::path::{Path, PathBuf};
use std::fs::read_dir;

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

pub fn dir_tests<F>(
paths: &[&str],
f: F
)
pub fn dir_tests<F>(paths: &[&str], f: F)
where
F: Fn(&str) -> String
F: Fn(&str) -> String,
{
for path in collect_tests(paths) {
let actual = {
Expand All @@ -47,21 +44,20 @@ where
}
}

fn assert_equal_text(
expected: &str,
actual: &str,
path: &Path
) {
fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
if expected != actual {
print_difference(expected, actual, path)
}
}

fn collect_tests(paths: &[&str]) -> Vec<PathBuf> {
paths.iter().flat_map(|path| {
let path = test_data_dir().join(path);
test_from_dir(&path).into_iter()
}).collect()
paths
.iter()
.flat_map(|path| {
let path = test_data_dir().join(path);
test_from_dir(&path).into_iter()
})
.collect()
}

fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
Expand Down Expand Up @@ -95,11 +91,13 @@ fn print_difference(expected: &str, actual: &str, path: &Path) {
fn project_dir() -> PathBuf {
let dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(dir)
.parent().unwrap()
.parent().unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.to_owned()
}

fn test_data_dir() -> PathBuf {
project_dir().join("tests/data")
}
}
12 changes: 12 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "tools"
version = "0.1.0"
authors = ["Aleksey Kladov <[email protected]>"]
publish = false

[dependencies]
serde = "1.0.26"
serde_derive = "1.0.26"
file = "1.1.1"
ron = "0.1.5"
libsyntax2 = { path = "../" }
16 changes: 11 additions & 5 deletions src/bin/gen.rs → tools/src/bin/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use std::fmt::Write;
fn main() {
let grammar = Grammar::read();
let text = grammar.to_syntax_kinds();
file::put_text(&generated_file(), &text).unwrap();
let target = generated_file();
if text != file::get_text(&target).unwrap_or_default() {
file::put_text(&target, &text).unwrap();
}
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -94,13 +97,11 @@ impl Grammar {
}

fn grammar_file() -> PathBuf {
let dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(dir).join("grammar.ron")
base_dir().join("grammar.ron")
}

fn generated_file() -> PathBuf {
let dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(dir).join("src/syntax_kinds.rs")
base_dir().join("src/syntax_kinds.rs")
}

fn scream(word: &str) -> String {
Expand All @@ -110,3 +111,8 @@ fn scream(word: &str) -> String {
fn kw_token(keyword: &str) -> String {
format!("{}_KW", scream(keyword))
}

fn base_dir() -> PathBuf {
let dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(dir).parent().unwrap().to_owned()
}
File renamed without changes.