Skip to main content

Matcher

Trait Matcher 

Source
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§

Source

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 matching
  • env - Meta-variable environment to capture variables during matching
§Returns

The matched node if successful, otherwise None

Provided Methods§

Source

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 match
  • None - This matcher needs to test all node types
Source

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.

Implementations on Foreign Types§

Source§

impl Matcher for str

Source§

fn match_node_with_env<'tree, D: Doc>( &self, node: Node<'tree, D>, env: &mut Cow<'_, MetaVarEnv<'tree, D>>, ) -> Option<Node<'tree, D>>

Source§

fn get_match_len<D: Doc>(&self, node: Node<'_, D>) -> Option<usize>

Source§

impl<T> Matcher for &T
where T: Matcher + ?Sized,

Source§

fn match_node_with_env<'tree, D: Doc>( &self, node: Node<'tree, D>, env: &mut Cow<'_, MetaVarEnv<'tree, D>>, ) -> Option<Node<'tree, D>>

Source§

fn potential_kinds(&self) -> Option<BitSet>

Source§

fn get_match_len<D: Doc>(&self, node: Node<'_, D>) -> Option<usize>

Implementors§

Source§

impl Matcher for KindMatcher

Source§

impl Matcher for MatchAll

Source§

impl Matcher for MatchNone

Source§

impl Matcher for Pattern

Source§

impl Matcher for RegexMatcher

Source§

impl<M> Matcher for Op<M>
where M: Matcher,

Source§

impl<M: Matcher> Matcher for Any<M>

Source§

impl<P1, P2> Matcher for And<P1, P2>
where P1: Matcher, P2: Matcher,

Source§

impl<P1, P2> Matcher for Or<P1, P2>
where P1: Matcher, P2: Matcher,

Source§

impl<P> Matcher for Not<P>
where P: Matcher,

Source§

impl<P: Matcher> Matcher for All<P>