pub trait Matcher {
// Required method
fn match_node_with_env<'tree, D: Doc>(
&self,
_node: Node<'tree, D>,
_env: &mut Cow<'_, MetaVarEnv<'tree, D>>,
) -> Option<Node<'tree, D>>;
// Provided methods
fn potential_kinds(&self) -> Option<BitSet> { ... }
fn get_match_len<D: Doc>(&self, _node: Node<'_, D>) -> Option<usize> { ... }
}Expand description
Core trait for matching AST nodes against patterns.
Implementors define how to match nodes, whether by structure, content, kind, or other criteria. The matcher can also capture meta-variables during the matching process.
§Type Parameters
The trait is generic over document types to support different source encodings and language implementations.
§Example Implementation
use thread_ast_engine::Matcher;
struct SimpleKindMatcher {
target_kind: String,
}
impl Matcher for SimpleKindMatcher {
fn match_node_with_env<'tree, D: Doc>(
&self,
node: Node<'tree, D>,
_env: &mut Cow<MetaVarEnv<'tree, D>>,
) -> Option<Node<'tree, D>> {
if node.kind() == self.target_kind {
Some(node)
} else {
None
}
}
}Required Methods§
Sourcefn match_node_with_env<'tree, D: Doc>(
&self,
_node: Node<'tree, D>,
_env: &mut Cow<'_, MetaVarEnv<'tree, D>>,
) -> Option<Node<'tree, D>>
fn match_node_with_env<'tree, D: Doc>( &self, _node: Node<'tree, D>, _env: &mut Cow<'_, MetaVarEnv<'tree, D>>, ) -> Option<Node<'tree, D>>
Attempt to match a node, updating the meta-variable environment.
Returns the matched node if successful, or None if the node doesn’t match.
The returned node is usually the input node, but can be different for
matchers like Has that match based on descendants.
§Parameters
node- The AST node to test for matchingenv- Meta-variable environment to capture variables during matching
§Returns
The matched node if successful, otherwise None
Provided Methods§
Sourcefn potential_kinds(&self) -> Option<BitSet>
fn potential_kinds(&self) -> Option<BitSet>
Provide a hint about which node types this matcher can match.
Returns a bitset of node kind IDs that this matcher might match,
or None if it needs to test all node types. Used for optimization
to avoid testing matchers against incompatible nodes.
§Returns
Some(BitSet)- Specific node kinds this matcher can matchNone- This matcher needs to test all node types
Sourcefn get_match_len<D: Doc>(&self, _node: Node<'_, D>) -> Option<usize>
fn get_match_len<D: Doc>(&self, _node: Node<'_, D>) -> Option<usize>
Determine how much of a matched node should be replaced.
Used during replacement to determine the exact span of text to replace. Typically skips trailing punctuation or anonymous nodes.
§Parameters
node- The matched node
§Returns
Number of bytes from the node’s start position to replace,
or None to replace the entire node.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.