diff options
-rw-r--r-- | src/bin/cli.rs (renamed from src/bin/cli/parse.rs) | 4 | ||||
-rw-r--r-- | src/lib.rs | 52 | ||||
-rw-r--r-- | src/utils.rs | 46 | ||||
-rw-r--r-- | tests/parser.rs | 4 | ||||
-rw-r--r-- | tests/testutils/Cargo.toml | 1 | ||||
-rw-r--r-- | tests/testutils/src/lib.rs | 17 |
6 files changed, 61 insertions, 63 deletions
diff --git a/src/bin/cli/parse.rs b/src/bin/cli.rs index 563ea92f6..9e513edb2 100644 --- a/src/bin/cli/parse.rs +++ b/src/bin/cli.rs | |||
@@ -3,13 +3,13 @@ extern crate libsyntax2; | |||
3 | use std::io::Read; | 3 | use std::io::Read; |
4 | 4 | ||
5 | use libsyntax2::{ | 5 | use libsyntax2::{ |
6 | parse, utils::dump_tree_green | 6 | parse, utils::dump_tree |
7 | }; | 7 | }; |
8 | 8 | ||
9 | fn main() { | 9 | fn main() { |
10 | let text = read_input(); | 10 | let text = read_input(); |
11 | let file = parse(text); | 11 | let file = parse(text); |
12 | let tree = dump_tree_green(&file); | 12 | let tree = dump_tree(&file); |
13 | println!("{}", tree); | 13 | println!("{}", tree); |
14 | } | 14 | } |
15 | 15 | ||
diff --git a/src/lib.rs b/src/lib.rs index 91d060169..f85ee1eef 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
@@ -27,6 +27,8 @@ mod lexer; | |||
27 | mod parser; | 27 | mod parser; |
28 | mod syntax_kinds; | 28 | mod syntax_kinds; |
29 | mod yellow; | 29 | mod yellow; |
30 | /// Utilities for simple uses of the parser. | ||
31 | pub mod utils; | ||
30 | 32 | ||
31 | pub use { | 33 | pub use { |
32 | lexer::{tokenize, Token}, | 34 | lexer::{tokenize, Token}, |
@@ -42,53 +44,3 @@ pub fn parse(text: String) -> SyntaxNode { | |||
42 | parser::parse::<yellow::GreenBuilder>(text, &tokens) | 44 | parser::parse::<yellow::GreenBuilder>(text, &tokens) |
43 | } | 45 | } |
44 | 46 | ||
45 | /// Utilities for simple uses of the parser. | ||
46 | pub mod utils { | ||
47 | use std::{collections::BTreeSet, fmt::Write}; | ||
48 | |||
49 | use {SyntaxError, SyntaxNode, SyntaxNodeRef}; | ||
50 | |||
51 | /// Parse a file and create a string representation of the resulting parse tree. | ||
52 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { | ||
53 | let syntax = syntax.borrow(); | ||
54 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); | ||
55 | let mut result = String::new(); | ||
56 | go(syntax, &mut result, 0, &mut errors); | ||
57 | return result; | ||
58 | |||
59 | fn go( | ||
60 | node: SyntaxNodeRef, | ||
61 | buff: &mut String, | ||
62 | level: usize, | ||
63 | errors: &mut BTreeSet<SyntaxError>, | ||
64 | ) { | ||
65 | buff.push_str(&String::from(" ").repeat(level)); | ||
66 | write!(buff, "{:?}\n", node).unwrap(); | ||
67 | let my_errors: Vec<_> = errors | ||
68 | .iter() | ||
69 | .filter(|e| e.offset == node.range().start()) | ||
70 | .cloned() | ||
71 | .collect(); | ||
72 | for err in my_errors { | ||
73 | errors.remove(&err); | ||
74 | buff.push_str(&String::from(" ").repeat(level)); | ||
75 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
76 | } | ||
77 | |||
78 | for child in node.children() { | ||
79 | go(child, buff, level + 1, errors) | ||
80 | } | ||
81 | |||
82 | let my_errors: Vec<_> = errors | ||
83 | .iter() | ||
84 | .filter(|e| e.offset == node.range().end()) | ||
85 | .cloned() | ||
86 | .collect(); | ||
87 | for err in my_errors { | ||
88 | errors.remove(&err); | ||
89 | buff.push_str(&String::from(" ").repeat(level)); | ||
90 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | } | ||
diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 000000000..20991659a --- /dev/null +++ b/src/utils.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | use std::{collections::BTreeSet, fmt::Write}; | ||
2 | use {SyntaxError, SyntaxNode, SyntaxNodeRef}; | ||
3 | |||
4 | /// Parse a file and create a string representation of the resulting parse tree. | ||
5 | pub fn dump_tree(syntax: &SyntaxNode) -> String { | ||
6 | let syntax = syntax.borrow(); | ||
7 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); | ||
8 | let mut result = String::new(); | ||
9 | go(syntax, &mut result, 0, &mut errors); | ||
10 | return result; | ||
11 | |||
12 | fn go( | ||
13 | node: SyntaxNodeRef, | ||
14 | buff: &mut String, | ||
15 | level: usize, | ||
16 | errors: &mut BTreeSet<SyntaxError>, | ||
17 | ) { | ||
18 | buff.push_str(&String::from(" ").repeat(level)); | ||
19 | write!(buff, "{:?}\n", node).unwrap(); | ||
20 | let my_errors: Vec<_> = errors | ||
21 | .iter() | ||
22 | .filter(|e| e.offset == node.range().start()) | ||
23 | .cloned() | ||
24 | .collect(); | ||
25 | for err in my_errors { | ||
26 | errors.remove(&err); | ||
27 | buff.push_str(&String::from(" ").repeat(level)); | ||
28 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
29 | } | ||
30 | |||
31 | for child in node.children() { | ||
32 | go(child, buff, level + 1, errors) | ||
33 | } | ||
34 | |||
35 | let my_errors: Vec<_> = errors | ||
36 | .iter() | ||
37 | .filter(|e| e.offset == node.range().end()) | ||
38 | .cloned() | ||
39 | .collect(); | ||
40 | for err in my_errors { | ||
41 | errors.remove(&err); | ||
42 | buff.push_str(&String::from(" ").repeat(level)); | ||
43 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
44 | } | ||
45 | } | ||
46 | } | ||
diff --git a/tests/parser.rs b/tests/parser.rs index eb955278e..770610974 100644 --- a/tests/parser.rs +++ b/tests/parser.rs | |||
@@ -2,13 +2,13 @@ extern crate libsyntax2; | |||
2 | extern crate testutils; | 2 | extern crate testutils; |
3 | 3 | ||
4 | use libsyntax2::parse; | 4 | use libsyntax2::parse; |
5 | use libsyntax2::utils::dump_tree_green; | 5 | use libsyntax2::utils::dump_tree; |
6 | use testutils::dir_tests; | 6 | use testutils::dir_tests; |
7 | 7 | ||
8 | #[test] | 8 | #[test] |
9 | fn parser_tests() { | 9 | fn parser_tests() { |
10 | dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { | 10 | dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { |
11 | let file = parse(text.to_string()); | 11 | let file = parse(text.to_string()); |
12 | dump_tree_green(&file) | 12 | dump_tree(&file) |
13 | }) | 13 | }) |
14 | } | 14 | } |
diff --git a/tests/testutils/Cargo.toml b/tests/testutils/Cargo.toml index 806c3de52..53b20f17b 100644 --- a/tests/testutils/Cargo.toml +++ b/tests/testutils/Cargo.toml | |||
@@ -4,5 +4,4 @@ version = "0.1.0" | |||
4 | authors = ["Aleksey Kladov <[email protected]>"] | 4 | authors = ["Aleksey Kladov <[email protected]>"] |
5 | 5 | ||
6 | [dependencies] | 6 | [dependencies] |
7 | file = "1.0" | ||
8 | difference = "2.0.0" | 7 | difference = "2.0.0" |
diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs index 43b806541..7c481156f 100644 --- a/tests/testutils/src/lib.rs +++ b/tests/testutils/src/lib.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | extern crate difference; | 1 | extern crate difference; |
2 | extern crate file; | ||
3 | 2 | ||
4 | use std::fs::read_dir; | 3 | use std::{ |
5 | use std::path::{Path, PathBuf}; | 4 | fs, |
5 | path::{Path, PathBuf} | ||
6 | }; | ||
6 | 7 | ||
7 | use difference::Changeset; | 8 | use difference::Changeset; |
8 | 9 | ||
@@ -18,7 +19,7 @@ use difference::Changeset; | |||
18 | /// | 19 | /// |
19 | /// so this should always be correct. | 20 | /// so this should always be correct. |
20 | fn read_text(path: &Path) -> String { | 21 | fn read_text(path: &Path) -> String { |
21 | file::get_text(path).unwrap().replace("\r\n", "\n") | 22 | fs::read_to_string(path).unwrap().replace("\r\n", "\n") |
22 | } | 23 | } |
23 | 24 | ||
24 | pub fn dir_tests<F>(paths: &[&str], f: F) | 25 | pub fn dir_tests<F>(paths: &[&str], f: F) |
@@ -33,7 +34,7 @@ where | |||
33 | println!("\nfile: {}", path.display()); | 34 | println!("\nfile: {}", path.display()); |
34 | println!("No .txt file with expected result, creating...\n"); | 35 | println!("No .txt file with expected result, creating...\n"); |
35 | println!("{}\n{}", input_code, parse_tree); | 36 | println!("{}\n{}", input_code, parse_tree); |
36 | file::put_text(&path, parse_tree).unwrap(); | 37 | fs::write(&path, parse_tree).unwrap(); |
37 | panic!("No expected result") | 38 | panic!("No expected result") |
38 | } | 39 | } |
39 | let expected = read_text(&path); | 40 | let expected = read_text(&path); |
@@ -61,7 +62,7 @@ fn collect_tests(paths: &[&str]) -> Vec<PathBuf> { | |||
61 | 62 | ||
62 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { | 63 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { |
63 | let mut acc = Vec::new(); | 64 | let mut acc = Vec::new(); |
64 | for file in read_dir(&dir).unwrap() { | 65 | for file in fs::read_dir(&dir).unwrap() { |
65 | let file = file.unwrap(); | 66 | let file = file.unwrap(); |
66 | let path = file.path(); | 67 | let path = file.path(); |
67 | if path.extension().unwrap_or_default() == "rs" { | 68 | if path.extension().unwrap_or_default() == "rs" { |
@@ -80,12 +81,12 @@ fn print_difference(expected: &str, actual: &str, path: &Path) { | |||
80 | if expected.trim() == actual.trim() { | 81 | if expected.trim() == actual.trim() { |
81 | println!("whitespace difference, rewriting"); | 82 | println!("whitespace difference, rewriting"); |
82 | println!("file: {}\n", path.display()); | 83 | println!("file: {}\n", path.display()); |
83 | file::put_text(path, actual).unwrap(); | 84 | fs::write(path, actual).unwrap(); |
84 | return; | 85 | return; |
85 | } | 86 | } |
86 | if REWRITE { | 87 | if REWRITE { |
87 | println!("rewriting {}", path.display()); | 88 | println!("rewriting {}", path.display()); |
88 | file::put_text(path, actual).unwrap(); | 89 | fs::write(path, actual).unwrap(); |
89 | return; | 90 | return; |
90 | } | 91 | } |
91 | let changeset = Changeset::new(actual, expected, "\n"); | 92 | let changeset = Changeset::new(actual, expected, "\n"); |