-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for regex based CGPatterns
ghstack-source-id: aaad598e2655a3ba76e46fc0dc969c0701fdaa5c Pull Request resolved: #529
- Loading branch information
1 parent
e1c44bf
commit e4c75d0
Showing
11 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright (c) 2023 Uber Technologies, Inc. | ||
<p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
except in compliance with the License. You may obtain a copy of the License at | ||
<p>http://www.apache.org/licenses/LICENSE-2.0 | ||
<p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
express or implied. See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
use crate::models::matches::Match; | ||
use itertools::Itertools; | ||
use regex::Regex; | ||
use std::collections::HashMap; | ||
use tree_sitter::Node; | ||
|
||
/// Applies the query upon the given `node`, and gets the first match | ||
/// # Arguments | ||
/// * `node` - the root node to apply the query upon | ||
/// * `source_code` - the corresponding source code string for the node. | ||
/// * `query` - the query to be applied | ||
/// * `recursive` - if `true` it matches the query to `self` and `self`'s sub-ASTs, else it matches the `query` only to `self`. | ||
/// | ||
/// # Returns | ||
/// The range of the match in the source code and the corresponding mapping from tags to code snippets. | ||
pub(crate) fn get_all_matches_for_regex( | ||
node: &Node, source_code: String, regex: &Regex, recursive: bool, replace_node: Option<String>, | ||
) -> Vec<Match> { | ||
let code_snippet = node.utf8_text(source_code.as_bytes()).unwrap(); | ||
let all_captures = regex.captures_iter(code_snippet).collect_vec(); | ||
let names = regex.capture_names().collect_vec(); | ||
let mut all_matches = vec![]; | ||
for captures in all_captures { | ||
// Check if the range of the self (node), and the range of outermost node captured by the query are equal. | ||
let range_matches_node = node.start_byte() == captures.get(0).unwrap().start() | ||
&& node.end_byte() == captures.get(0).unwrap().end(); | ||
if recursive || range_matches_node { | ||
let group_by_tag = if let Some(ref rn) = replace_node { | ||
captures | ||
.name(rn) | ||
.unwrap_or_else(|| panic!("the tag {rn} provided in the replace node is not present")) | ||
} else { | ||
captures.get(0).unwrap() | ||
}; | ||
let matches = extract_captures(&captures, &names); | ||
all_matches.push(Match::from_regex(&group_by_tag, matches, code_snippet)); | ||
} | ||
} | ||
all_matches | ||
} | ||
|
||
fn extract_captures( | ||
captures: ®ex::Captures<'_>, names: &Vec<Option<&str>>, | ||
) -> HashMap<String, String> { | ||
names | ||
.iter() | ||
.flatten() | ||
.flat_map(|x| { | ||
captures | ||
.name(x) | ||
.map(|v| (x.to_string(), v.as_str().to_string())) | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
test-resources/java/regex_based_matcher/configurations/rules.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[[rules]] | ||
name = "replace_call" | ||
query = """rgx (?P<n>foo\\(\\))""" | ||
replace_node = "n" | ||
replace = "Foo" |
10 changes: 10 additions & 0 deletions
10
test-resources/java/regex_based_matcher/expected/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.uber.piranha; | ||
|
||
class A { | ||
|
||
void foobar() { | ||
int total = Foo; | ||
System.out.println(total); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.uber.piranha; | ||
|
||
class A { | ||
|
||
void foobar() { | ||
int total = foo(); | ||
System.out.println(total); | ||
} | ||
|
||
} |