1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
extern crate clap;
#[macro_use]
extern crate failure;
extern crate libsyntax2;
extern crate tools;
use clap::{App, Arg, SubCommand};
use std::time::Instant;
use std::{fs, io::Read, path::Path};
use tools::collect_tests;
type Result<T> = ::std::result::Result<T, failure::Error>;
fn main() -> Result<()> {
let matches = App::new("libsyntax2-cli")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("render-test")
.arg(
Arg::with_name("line")
.long("--line")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("file")
.long("--file")
.required(true)
.takes_value(true),
),
)
.subcommand(SubCommand::with_name("parse"))
.get_matches();
match matches.subcommand() {
("parse", _) => {
let tree = parse()?;
println!("{}", tree);
}
("render-test", Some(matches)) => {
let file = matches.value_of("file").unwrap();
let file = Path::new(file);
let line: usize = matches.value_of("line").unwrap().parse()?;
let line = line - 1;
let (test, tree) = render_test(file, line)?;
println!("{}\n{}", test, tree);
}
_ => unreachable!(),
}
Ok(())
}
fn parse() -> Result<String> {
let text = read_stdin()?;
let start = Instant::now();
let file = libsyntax2::parse(&text);
eprintln!("elapsed {:?}", start.elapsed());
let tree = libsyntax2::utils::dump_tree(&file);
Ok(tree)
}
fn read_stdin() -> Result<String> {
let mut buff = String::new();
::std::io::stdin().read_to_string(&mut buff)?;
Ok(buff)
}
fn render_test(file: &Path, line: usize) -> Result<(String, String)> {
let text = fs::read_to_string(file)?;
let tests = collect_tests(&text);
let test = tests.into_iter().find(|(start_line, t)| {
*start_line <= line && line <= *start_line + t.text.lines().count()
});
let test = match test {
None => bail!("No test found at line {} at {}", line, file.display()),
Some((_start_line, test)) => test,
};
let file = libsyntax2::parse(&test.text);
let tree = libsyntax2::utils::dump_tree(&file);
Ok((test.text, tree))
}
|