-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39: Tools r=matklad a=matklad closes #34 bors r+
- Loading branch information
Showing
15 changed files
with
188 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[alias] | ||
parse = "run --package tools --bin parse" | ||
gen = "run --package tools --bin gen" | ||
collect-tests = "run --package tools --bin collect-tests --" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ install: | |
build: false | ||
|
||
test_script: | ||
- cargo collect-tests --verify | ||
- cargo test | ||
|
||
branches: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const unsafe fn foo() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FILE@[0; 25) | ||
FN_ITEM@[0; 25) | ||
CONST_KW@[0; 5) | ||
WHITESPACE@[5; 6) | ||
UNSAFE_KW@[6; 12) | ||
WHITESPACE@[12; 13) | ||
FN_KW@[13; 15) | ||
WHITESPACE@[15; 16) | ||
IDENT@[16; 19) "foo" | ||
L_PAREN@[19; 20) | ||
R_PAREN@[20; 21) | ||
WHITESPACE@[21; 22) | ||
L_CURLY@[22; 23) | ||
R_CURLY@[23; 24) | ||
WHITESPACE@[24; 25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const fn foo() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FILE@[0; 18) | ||
FN_ITEM@[0; 18) | ||
CONST_KW@[0; 5) | ||
WHITESPACE@[5; 6) | ||
FN_KW@[6; 8) | ||
WHITESPACE@[8; 9) | ||
IDENT@[9; 12) "foo" | ||
L_PAREN@[12; 13) | ||
R_PAREN@[13; 14) | ||
WHITESPACE@[14; 15) | ||
L_CURLY@[15; 16) | ||
R_CURLY@[16; 17) | ||
WHITESPACE@[17; 18) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
extern crate file; | ||
extern crate walkdir; | ||
extern crate itertools; | ||
|
||
use walkdir::WalkDir; | ||
use itertools::Itertools; | ||
|
||
use std::path::{PathBuf, Path}; | ||
use std::collections::HashSet; | ||
use std::fs; | ||
|
||
fn main() { | ||
let verify = ::std::env::args().any(|arg| arg == "--verify"); | ||
|
||
let d = grammar_dir(); | ||
let tests = tests_from_dir(&d); | ||
let existing = existing_tests(); | ||
|
||
for t in existing.difference(&tests) { | ||
panic!("Test is deleted: {}\n{}", t.name, t.text); | ||
} | ||
|
||
let new_tests = tests.difference(&existing); | ||
for (i, t) in new_tests.enumerate() { | ||
if verify { | ||
panic!("Inline test is not recorded: {}", t.name); | ||
} | ||
|
||
let name = format!("{:04}_{}.rs", existing.len() + i + 1, t.name); | ||
println!("Creating {}", name); | ||
let path = inline_tests_dir().join(name); | ||
file::put_text(&path, &t.text).unwrap(); | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug, Eq)] | ||
struct Test { | ||
name: String, | ||
text: String, | ||
} | ||
|
||
impl PartialEq for Test { | ||
fn eq(&self, other: &Test) -> bool { | ||
self.name.eq(&other.name) | ||
} | ||
} | ||
|
||
impl ::std::hash::Hash for Test { | ||
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) { | ||
self.name.hash(state) | ||
} | ||
} | ||
|
||
fn tests_from_dir(dir: &Path) -> HashSet<Test> { | ||
let mut res = HashSet::new(); | ||
for entry in WalkDir::new(dir) { | ||
let entry = entry.unwrap(); | ||
if !entry.file_type().is_file() { | ||
continue | ||
} | ||
if entry.path().extension().unwrap_or_default() != "rs" { | ||
continue | ||
} | ||
let text = file::get_text(entry.path()) | ||
.unwrap(); | ||
|
||
for test in collect_tests(&text) { | ||
if let Some(old_test) = res.replace(test) { | ||
panic!("Duplicate test: {}", old_test.name) | ||
} | ||
} | ||
} | ||
res | ||
} | ||
|
||
fn collect_tests(s: &str) -> Vec<Test> { | ||
let mut res = vec![]; | ||
let prefix = "// "; | ||
let comment_blocks = s.lines() | ||
.map(str::trim_left) | ||
.group_by(|line| line.starts_with(prefix)); | ||
|
||
for (is_comment, block) in comment_blocks.into_iter() { | ||
if !is_comment { | ||
continue; | ||
} | ||
let mut block = block.map(|line| &line[prefix.len()..]); | ||
let first = block.next().unwrap(); | ||
if !first.starts_with("test ") { | ||
continue | ||
} | ||
let name = first["test ".len()..].to_string(); | ||
let text: String = itertools::join(block.chain(::std::iter::once("")), "\n"); | ||
assert!(!text.trim().is_empty() && text.ends_with("\n")); | ||
res.push(Test { name, text }) | ||
} | ||
res | ||
} | ||
|
||
fn existing_tests() -> HashSet<Test> { | ||
let mut res = HashSet::new(); | ||
for file in fs::read_dir(&inline_tests_dir()).unwrap() { | ||
let file = file.unwrap(); | ||
let path = file.path(); | ||
if path.extension().unwrap_or_default() != "rs" { | ||
continue | ||
} | ||
let name = path.file_name().unwrap().to_str().unwrap(); | ||
let name = name["0000_".len()..name.len() - 3].to_string(); | ||
let text = file::get_text(&path).unwrap(); | ||
res.insert(Test { name, text }); | ||
} | ||
res | ||
} | ||
|
||
fn inline_tests_dir() -> PathBuf { | ||
let res = base_dir().join("tests/data/parser/inline"); | ||
if !res.is_dir() { | ||
fs::create_dir_all(&res).unwrap(); | ||
} | ||
res | ||
} | ||
|
||
fn grammar_dir() -> PathBuf { | ||
base_dir().join("src/parser/event_parser/grammar") | ||
} | ||
|
||
fn base_dir() -> PathBuf { | ||
let dir = env!("CARGO_MANIFEST_DIR"); | ||
PathBuf::from(dir).parent().unwrap().to_owned() | ||
} | ||
|
||
|