From 939cdf12b95d441e49b902dd8128911c1fa65608 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 29 Sep 2020 12:34:25 -0400 Subject: [PATCH] Add --stats flag for reporting parse information. --- cli/src/main.rs | 10 ++++++++++ cli/src/parse.rs | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 2f8c6dd5..4bce3d43 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -64,6 +64,7 @@ fn run() -> error::Result<()> { .arg(Arg::with_name("debug").long("debug").short("d")) .arg(Arg::with_name("debug-graph").long("debug-graph").short("D")) .arg(Arg::with_name("quiet").long("quiet").short("q")) + .arg(Arg::with_name("stat").long("stat").short("s")) .arg(Arg::with_name("time").long("time").short("t")) .arg(Arg::with_name("allow-cancellation").long("cancel")) .arg(Arg::with_name("timeout").long("timeout").takes_value(true)) @@ -234,6 +235,9 @@ fn run() -> error::Result<()> { let max_path_length = paths.iter().map(|p| p.chars().count()).max().unwrap(); let mut has_error = false; loader.find_all_languages(&config.parser_directories)?; + + let mut stats : parse::Stats = Default::default(); + for path in paths { let path = Path::new(&path); let language = @@ -249,8 +253,14 @@ fn run() -> error::Result<()> { debug, debug_graph, allow_cancellation, + &mut stats, )?; } + + if matches.is_present("stat") { + println!("{}", stats) + } + if has_error { return Error::err(String::new()); } diff --git a/cli/src/parse.rs b/cli/src/parse.rs index 13bac0f3..53ad859e 100644 --- a/cli/src/parse.rs +++ b/cli/src/parse.rs @@ -4,7 +4,7 @@ use std::io::{self, Write}; use std::path::Path; use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::Instant; -use std::{fs, thread, usize}; +use std::{fmt, fs, thread, usize}; use tree_sitter::{InputEdit, Language, LogType, Parser, Point, Tree}; #[derive(Debug)] @@ -14,6 +14,22 @@ pub struct Edit { pub inserted_text: Vec, } +#[derive(Debug, Default)] +pub struct Stats { + successful_parses : usize, + total_parses : usize, +} + +impl fmt::Display for Stats { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + return writeln!(f, "Total parses: {}; successful parses: {}; failed parses: {}; success percentage: {:.2}%", + self.total_parses, + self.successful_parses, + self.total_parses - self.successful_parses, + (self.successful_parses as f64) / (self.total_parses as f64) * 100.0); + } +} + pub fn parse_file_at_path( language: Language, path: &Path, @@ -25,6 +41,7 @@ pub fn parse_file_at_path( debug: bool, debug_graph: bool, allow_cancellation: bool, + stats: &mut Stats, ) -> Result { let mut _log_session = None; let mut parser = Parser::new(); @@ -161,6 +178,11 @@ pub fn parse_file_at_path( } } + stats.total_parses += 1; + if first_error.is_none() { + stats.successful_parses += 1; + } + if first_error.is_some() || print_time { write!( &mut stdout,