diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 11 | ||||
-rw-r--r-- | cli/src/main.rs | 68 |
2 files changed, 79 insertions, 0 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 000000000..a259eef63 --- /dev/null +++ b/cli/Cargo.toml | |||
@@ -0,0 +1,11 @@ | |||
1 | [package] | ||
2 | name = "cli" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Aleksey Kladov <[email protected]>"] | ||
5 | publish = false | ||
6 | |||
7 | [dependencies] | ||
8 | clap = "2.32.0" | ||
9 | failure = "0.1.1" | ||
10 | libsyntax2 = { path = "../" } | ||
11 | tools = { path = "../tools" } | ||
diff --git a/cli/src/main.rs b/cli/src/main.rs new file mode 100644 index 000000000..94183c552 --- /dev/null +++ b/cli/src/main.rs | |||
@@ -0,0 +1,68 @@ | |||
1 | extern crate clap; | ||
2 | #[macro_use] | ||
3 | extern crate failure; | ||
4 | extern crate libsyntax2; | ||
5 | extern crate tools; | ||
6 | |||
7 | use std::{fs, path::Path, io::Read}; | ||
8 | use clap::{App, Arg, SubCommand}; | ||
9 | use tools::collect_tests; | ||
10 | |||
11 | type Result<T> = ::std::result::Result<T, failure::Error>; | ||
12 | |||
13 | fn main() -> Result<()> { | ||
14 | let matches = App::new("libsyntax2-cli") | ||
15 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | ||
16 | .subcommand( | ||
17 | SubCommand::with_name("render-test") | ||
18 | .arg(Arg::with_name("line").long("--line").required(true).takes_value(true)) | ||
19 | .arg(Arg::with_name("file").long("--file").required(true).takes_value(true)) | ||
20 | ) | ||
21 | .subcommand(SubCommand::with_name("parse")) | ||
22 | .get_matches(); | ||
23 | match matches.subcommand() { | ||
24 | ("parse", _) => { | ||
25 | let tree = parse()?; | ||
26 | println!("{}", tree); | ||
27 | }, | ||
28 | ("render-test", Some(matches)) => { | ||
29 | let file = matches.value_of("file").unwrap(); | ||
30 | let file = Path::new(file); | ||
31 | let line: usize = matches.value_of("line").unwrap().parse()?; | ||
32 | let line = line - 1; | ||
33 | let (test, tree) = render_test(file, line)?; | ||
34 | println!("{}\n{}", test, tree); | ||
35 | } | ||
36 | _ => unreachable!(), | ||
37 | } | ||
38 | Ok(()) | ||
39 | |||
40 | } | ||
41 | |||
42 | fn parse() -> Result<String> { | ||
43 | let text = read_stdin()?; | ||
44 | let file = libsyntax2::parse(text); | ||
45 | let tree = libsyntax2::utils::dump_tree(&file); | ||
46 | Ok(tree) | ||
47 | } | ||
48 | |||
49 | fn read_stdin() -> Result<String> { | ||
50 | let mut buff = String::new(); | ||
51 | ::std::io::stdin().read_to_string(&mut buff)?; | ||
52 | Ok(buff) | ||
53 | } | ||
54 | |||
55 | fn render_test(file: &Path, line: usize) -> Result<(String, String)> { | ||
56 | let text = fs::read_to_string(file)?; | ||
57 | let tests = collect_tests(&text); | ||
58 | let test = tests.into_iter().find(|t| { | ||
59 | t.start_line <= line && line <= t.start_line + t.text.lines().count() | ||
60 | }); | ||
61 | let test = match test { | ||
62 | None => bail!("No test found at line {} at {}", line, file.display()), | ||
63 | Some(test) => test, | ||
64 | }; | ||
65 | let file = libsyntax2::parse(test.text.clone()); | ||
66 | let tree = libsyntax2::utils::dump_tree(&file); | ||
67 | Ok((test.text, tree)) | ||
68 | } | ||