From ee5aa1438f0e3400217a9857b1d3341f82288a17 Mon Sep 17 00:00:00 2001 From: "diego.marcilio" Date: Fri, 18 Nov 2022 16:55:13 +0000 Subject: [PATCH] --ignore-parser-error option. - we may tolerate some rewrites that produce parse errors --- polyglot/piranha/README.md | 1 + polyglot/piranha/src/config.rs | 3 +++ polyglot/piranha/src/lib.rs | 4 +++- .../piranha/src/models/piranha_arguments.rs | 7 +++++- .../piranha/src/models/source_code_unit.rs | 22 +++++++++++-------- polyglot/piranha/src/tests/mod.rs | 2 ++ 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/polyglot/piranha/README.md b/polyglot/piranha/README.md index 4387e8b2c3..540301eccf 100644 --- a/polyglot/piranha/README.md +++ b/polyglot/piranha/README.md @@ -127,6 +127,7 @@ piranha_summary = run_piranha_cli(path_to_codebase, * `rules.toml`: *piranha rules* expresses the specific AST patterns to match and __replacement patterns__ for these matches (in-place). These rules can also specify the pre-built language specific cleanups to trigger. * `edges.toml` (_optional_): expresses the flow between the rules - `dry_run` : Disables in-place rewriting of code +- `ignore_parse_error` : Does not throw an error on syntactically incorrect source code produced by a rewrite.
Returns
diff --git a/polyglot/piranha/src/config.rs b/polyglot/piranha/src/config.rs index 0b44975581..6c99ad1d07 100644 --- a/polyglot/piranha/src/config.rs +++ b/polyglot/piranha/src/config.rs @@ -45,6 +45,9 @@ pub(crate) struct CommandLineArguments { /// Disables in-place rewriting of code #[clap(short = 'd', long, parse(try_from_str), default_value_t = false)] pub(crate) dry_run: bool, + /// Doesn't throw error on syntactically incorrect source code + #[clap(short = 'i', long, parse(try_from_str), default_value_t = false)] + pub(crate) ignore_parse_error: bool, } fn read_language_specific_rules(language_name: &str) -> Rules { diff --git a/polyglot/piranha/src/lib.rs b/polyglot/piranha/src/lib.rs index ad5d4ac4c3..0edf617b4e 100644 --- a/polyglot/piranha/src/lib.rs +++ b/polyglot/piranha/src/lib.rs @@ -57,18 +57,20 @@ use tree_sitter::Node; /// * path_to_codebase: Path to the root of the code base that Piranha will update /// * path_to_configuration: Path to the directory that contains - `piranha_arguments.toml`, `rules.toml` and optionally `edges.toml` /// * dry_run: determines if Piranha should actually update the code. +/// * ignore_parse_error: Piranha won't panic if a rewrite raises a parse error from tree-sitter. /// /// Returns Piranha Output Summary for each file touched or analyzed by Piranha. /// For each file, it reports its content after the rewrite, the list of matches and the list of rewrites. #[pyfunction] pub fn run_piranha_cli( - path_to_codebase: String, path_to_configurations: String, dry_run: bool, + path_to_codebase: String, path_to_configurations: String, dry_run: bool, ignore_parse_error: bool, ) -> Vec { let configuration = PiranhaArguments::new(CommandLineArguments { path_to_codebase, path_to_configurations, path_to_output_summary: None, dry_run, + ignore_parse_error, }); execute_piranha(&configuration) } diff --git a/polyglot/piranha/src/models/piranha_arguments.rs b/polyglot/piranha/src/models/piranha_arguments.rs index 0e03a8354b..0ea613a241 100644 --- a/polyglot/piranha/src/models/piranha_arguments.rs +++ b/polyglot/piranha/src/models/piranha_arguments.rs @@ -74,6 +74,9 @@ pub struct PiranhaArguments { /// Disables in-place rewriting of code #[getset(get = "pub")] dry_run: bool, + /// Won't panic if a rewrite raises a tree-sitter parse error + #[getset(get = "pub")] + ignore_parse_error: bool, } impl PiranhaArguments { @@ -102,7 +105,8 @@ impl PiranhaArguments { .path_to_output_summaries(args.path_to_output_summary) .language_name(piranha_args_from_config.language()) .language(piranha_args_from_config.language().get_language()) - .dry_run(args.dry_run); + .dry_run(args.dry_run) + .ignore_parse_error(args.ignore_parse_error); if let Some(v) = piranha_args_from_config.delete_file_if_empty() { args_builder.delete_file_if_empty(v); @@ -146,6 +150,7 @@ impl Default for PiranhaArguments { cleanup_comments_buffer: 2, cleanup_comments: false, dry_run: false, + ignore_parse_error: false, } } } diff --git a/polyglot/piranha/src/models/source_code_unit.rs b/polyglot/piranha/src/models/source_code_unit.rs index 6782e098ae..6af721088a 100644 --- a/polyglot/piranha/src/models/source_code_unit.rs +++ b/polyglot/piranha/src/models/source_code_unit.rs @@ -182,12 +182,16 @@ impl SourceCodeUnit { self.ast.edit(&ts_edit); self._replace_file_contents_and_re_parse(&new_source_code, parser, true); if self.ast.root_node().has_error() { - let msg = format!( - "Produced syntactically incorrect source code {}", - self.code() - ); - error!("{}", msg); - panic!("{}", msg); + if self.piranha_arguments.ignore_parse_error().clone() { + debug!("Parse error ocurred, but ignore_parse_error == true"); + } else { + let msg = format!( + "Produced syntactically incorrect source code {}", + self.code() + ); + error!("{}", msg); + panic!("{}", msg); + } } ts_edit } @@ -195,12 +199,12 @@ impl SourceCodeUnit { /// Deletes the trailing comma after the {deleted_range} /// # Arguments /// * `deleted_range` - the range of the deleted code - /// + /// /// # Returns /// code range of the closest node /// - /// Algorithm: - /// Get the node after the {deleted_range}'s end byte (heuristic 5 characters) + /// Algorithm: + /// Get the node after the {deleted_range}'s end byte (heuristic 5 characters) /// Traverse this node and get the node closest to the range {deleted_range}'s end byte /// IF this closest node is a comma, extend the {new_delete_range} to include the comma. fn delete_trailing_comma(&mut self, deleted_range: Range) -> Range { diff --git a/polyglot/piranha/src/tests/mod.rs b/polyglot/piranha/src/tests/mod.rs index 68943ff526..90163269c5 100644 --- a/polyglot/piranha/src/tests/mod.rs +++ b/polyglot/piranha/src/tests/mod.rs @@ -54,6 +54,7 @@ fn run_match_test(relative_path_to_tests: &str, number_of_matches: usize) { path_to_configurations: format!("{path_to_test_ff}/configurations/"), path_to_output_summary: None, dry_run: true, + ignore_parse_error: false, }); let output_summaries = execute_piranha(&args); @@ -77,6 +78,7 @@ fn run_rewrite_test(relative_path_to_tests: &str, n_files_changed: usize) { path_to_configurations: format!("{path_to_test_ff}/configurations/"), path_to_output_summary: None, dry_run: true, + ignore_parse_error: false, }); let output_summaries = execute_piranha(&args); // Checks if there are any rewrites performed for the file