From 3b6a6f6673041cf9ee315c00f9b0e24e2c067091 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 16:16:58 +0300 Subject: Add render test functionality --- cli/src/main.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cli/src/main.rs (limited to 'cli/src') 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 @@ +extern crate clap; +#[macro_use] +extern crate failure; +extern crate libsyntax2; +extern crate tools; + +use std::{fs, path::Path, io::Read}; +use clap::{App, Arg, SubCommand}; +use tools::collect_tests; + +type Result = ::std::result::Result; + +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 { + let text = read_stdin()?; + let file = libsyntax2::parse(text); + let tree = libsyntax2::utils::dump_tree(&file); + Ok(tree) +} + +fn read_stdin() -> Result { + 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(|t| { + t.start_line <= line && line <= t.start_line + t.text.lines().count() + }); + let test = match test { + None => bail!("No test found at line {} at {}", line, file.display()), + Some(test) => test, + }; + let file = libsyntax2::parse(test.text.clone()); + let tree = libsyntax2::utils::dump_tree(&file); + Ok((test.text, tree)) +} -- cgit v1.2.3