Skip to main content

audit/
audit.rs

1//! Parse a corpus of `.tla` files and group what failed, so the gaps between
2//! this parser and the language are a list rather than an impression.
3//!
4//! ```text
5//! cargo run --release --example audit -p tla-syntax -- $(find corpus -name '*.tla')
6//! ```
7
8use std::collections::BTreeMap;
9
10struct Failure {
11    path: String,
12    line: u32,
13    source_line: String,
14}
15
16fn main() {
17    let mut parsed = 0usize;
18    let mut by_reason: BTreeMap<String, Vec<Failure>> = BTreeMap::new();
19
20    for path in std::env::args().skip(1) {
21        let Ok(src) = std::fs::read_to_string(&path) else {
22            continue;
23        };
24        match tla_syntax::parse_module(&src) {
25            Ok(_) => parsed += 1,
26            Err(e) => {
27                let source_line = src
28                    .lines()
29                    .nth(e.line as usize - 1)
30                    .unwrap_or("")
31                    .trim()
32                    .chars()
33                    .take(96)
34                    .collect();
35                by_reason.entry(reason(&e)).or_default().push(Failure {
36                    path,
37                    line: e.line,
38                    source_line,
39                });
40            }
41        }
42    }
43
44    let total = parsed + by_reason.values().map(Vec::len).sum::<usize>();
45    println!("parsed {parsed} / {total}");
46
47    let mut groups: Vec<_> = by_reason.into_iter().collect();
48    groups.sort_by_key(|(_, failures)| std::cmp::Reverse(failures.len()));
49    for (reason, failures) in groups {
50        println!("\n{:>4}  {reason}", failures.len());
51        for f in failures.iter().take(4) {
52            let file = f.path.rsplit('/').next().unwrap_or(&f.path);
53            println!("      {file}:{}  {}", f.line, f.source_line);
54        }
55    }
56}
57
58fn reason(e: &tla_syntax::Error) -> String {
59    let body = e.message.split(", found").next().unwrap_or(&e.message);
60    body.to_string()
61}