Add --stats flag for reporting parse information.

This commit is contained in:
Patrick Thomson 2020-09-29 12:34:25 -04:00
parent 5080de496a
commit 939cdf12b9
2 changed files with 33 additions and 1 deletions

View file

@ -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());
}

View file

@ -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<u8>,
}
#[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<bool> {
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,