Skip to content

Commit

Permalink
perf: hoist query initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 21, 2024
1 parent c8a3640 commit 459d32a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 12 additions & 9 deletions parsing/src/identifier_extractor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use regex::Regex;
use tree_sitter::{Node, Query, QueryCursor};
use tree_sitter::{Language, Node, Query, QueryCursor};

pub trait IdentifierExtractor {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>>;
Expand All @@ -9,9 +9,10 @@ pub struct RegularExpression(Regex);

impl RegularExpression {
pub fn new(regex: &'static str) -> Self {
let regex_query = regex::Regex::new(regex)
.expect("Invalid regex provided for building RegularExpression");
Self(regex_query)
Self(
regex::Regex::new(regex)
.expect("Invalid regex provided for building RegularExpression"),
)
}
}

Expand All @@ -23,20 +24,22 @@ impl IdentifierExtractor for RegularExpression {
}
}

pub struct TreeSitterQuery(&'static str);
pub struct TreeSitterQuery(Query);

impl TreeSitterQuery {
pub fn new(query: &'static str) -> Self {
Self(query)
pub fn new(query: &'static str, language: Language) -> Self {
Self(
Query::new(language, query)
.expect("Invalid Query provided for building TreeSitterQuery"),
)
}
}

impl IdentifierExtractor for TreeSitterQuery {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>> {
let query = Query::new(node.language(), self.0).ok()?;
let mut cursor = QueryCursor::new();
let identifier = cursor
.matches(&query, node, src.as_bytes())
.matches(&self.0, node, src.as_bytes())
.flat_map(|a_match| {
a_match
.captures
Expand Down
6 changes: 4 additions & 2 deletions parsing/src/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ impl From<Language> for ParserConfiguration {
identifier_extractors: {
let mut map: HashMap<&'static str, Box<dyn IdentifierExtractor>> =
HashMap::new();
map.insert("constructor_declaration", Box::new(TreeSitterQuery::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert("method_declaration", Box::new(TreeSitterQuery::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert("constructor_declaration", Box::new(TreeSitterQuery::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#, tree_sitter_java::language())));
map.insert("method_declaration", Box::new(TreeSitterQuery::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#, tree_sitter_java::language())));
map.insert(
"field_declaration",
Box::new(TreeSitterQuery::new(
r#"(variable_declarator name: _ @name)"#,
tree_sitter_java::language(),
)),
);
map.insert(
"import_declaration",
Box::new(TreeSitterQuery::new(
r#"(import_declaration "import" _ @resource)"#,
tree_sitter_java::language(),
)),
);

Expand Down

0 comments on commit 459d32a

Please sign in to comment.