From c6c9170576f46ee7b7b0ff9532153c946ba9d02f Mon Sep 17 00:00:00 2001 From: Andrew Helwer Date: Sat, 11 Nov 2023 14:56:43 -0500 Subject: [PATCH] Output multiple errors & change diamond operator (#8) Signed-off-by: Andrew Helwer <2n8rn1w1f@mozmail.com> --- Cargo.toml | 4 ++-- resources/tla-unicode.csv | 2 +- src/lib.rs | 34 ++++++++++++++++------------------ src/main.rs | 9 +++++---- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2640fb8..feda42c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tlauc" description = "Rewrites TLA⁺ specs to use Unicode symbols instead of ASCII, and vice-versa" -version = "0.1.5" +version = "0.2.0" authors = ["Andrew Helwer <2n8rn1w1f@mozmail.com>"] repository = "https://github.com/tlaplus-community/tlauc" license = "MIT" @@ -17,7 +17,7 @@ clap = { version = "4.0.32", features = ["derive"] } csv = "1.3.0" serde = { version = "1.0.192", features = ["derive"] } tree-sitter = "0.20.10" -tree-sitter-tlaplus = "1.0.4" +tree-sitter-tlaplus = "1.0.6" [dev-dependencies] glob = "0.3.0" diff --git a/resources/tla-unicode.csv b/resources/tla-unicode.csv index 6c59523..b50c7fe 100644 --- a/resources/tla-unicode.csv +++ b/resources/tla-unicode.csv @@ -14,7 +14,7 @@ case_arrow,->,→,U+2192 label_as,::,∷,U+2237 lnot,~;\lnot;\neg,¬,U+00AC always,[],□,U+25A1 -eventually,<>,⋄,U+22C4 +eventually,<>,◇,U+25C7 implies,=>,⇒,U+21D2 plus_arrow,-+->,⇸,U+21F8 equiv,\equiv,≡,U+2261 diff --git a/src/lib.rs b/src/lib.rs index f05a724..0744019 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ pub enum Mode { pub enum TlaError { InputFileParseError { parse_tree: Tree, - first_error_line: Option, + error_lines: Vec, }, OutputFileParseError { output_tree: Tree, @@ -38,11 +38,10 @@ pub fn rewrite(input: &str, mode: &Mode, force: bool) -> Result Result Result<(), usize> { +fn find_error_lines(tree: &Tree) -> Vec { + let mut error_lines: Vec = vec![]; traverse_parse_tree(tree, |n| { if n.is_error() || n.is_missing() { - Err(n.start_position().row + 1) - } else { - Ok(()) + error_lines.push(n.start_position().row + 1); } - }) + }); + error_lines } -fn traverse_parse_tree(tree: &Tree, m: fn(Node) -> Result<(), T>) -> Result<(), T> { +fn traverse_parse_tree(tree: &Tree, mut visit: F) +where + F: FnMut(Node), +{ let mut cursor: TreeCursor = tree.walk(); loop { // Every time a new node is found the control flow passes here - m(cursor.node())?; + visit(cursor.node()); // Descend as far as possible if !cursor.goto_first_child() { loop { @@ -108,7 +110,7 @@ fn traverse_parse_tree(tree: &Tree, m: fn(Node) -> Result<(), T>) -> Result<( // parent's sibling in next loop iteration if !cursor.goto_parent() { // If parent does not exist, we are done - return Ok(()); + return; } } } @@ -555,13 +557,9 @@ mod tests { Ok(converted) => converted, Err(TlaError::InputFileParseError { parse_tree, - first_error_line, + error_lines, }) => { - panic!( - "{}\n{}", - first_error_line.unwrap_or(0), - parse_tree.root_node().to_sexp() - ) + panic!("{:?}\n{}", error_lines, parse_tree.root_node().to_sexp()) } Err(TlaError::OutputFileParseError { output_tree, diff --git a/src/main.rs b/src/main.rs index 04dc0fc..6905faf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,10 +77,11 @@ fn convert(config: &Config, mode: Mode) -> Result<()> { output_file.write_all(output.as_bytes()).context(format!("Failed to write to output file [{}]", &config.output))?; Ok(()) }, - Err(TlaError::InputFileParseError { first_error_line, .. }) => { - let line_msg = first_error_line.map_or( - "Could not identify line of first syntax error.".to_string(), - |line| format!("First syntax error might occur on line {}.", line)); + Err(TlaError::InputFileParseError { error_lines, .. }) => { + let line_msg = match error_lines.as_slice() { + [] => "Could not identify line of first syntax error.".to_string(), + [..] => format!("Syntax errors might occur on or near the following lines: {:?}.", error_lines) + }; Err(anyhow!("Failed to correctly parse input TLA⁺ file; use --force flag to bypass this check.\n".to_string() + &line_msg)) } Err(TlaError::OutputFileParseError{..}) => Err(anyhow!("Failed to correctly parse converted TLA⁺ output; this is a bug, please report it to the maintainer! Use --force to bypass this check (not recommended).")),