Skip to main content

PatternMatcher

Struct PatternMatcher 

Source
pub struct PatternMatcher { /* private fields */ }
Expand description

Pattern matcher using ast-grep’s metavariable syntax.

§Metavariable Syntax

  • $NAME - Matches a single node and captures it
  • $$$NAME - Matches zero or more nodes (variadic)
  • $_ - Matches any single node (anonymous)

§Example Patterns

fn $NAME($$$PARAMS) { $$$BODY }     // Match function definition
struct $NAME { $$$FIELDS }           // Match struct definition
$EXPR.clone()                        // Match .clone() calls
OtelExporter::$VARIANT               // Match enum variants

Implementations§

Source§

impl PatternMatcher

Source

pub fn new(source: &str) -> Self

Create a new pattern matcher for the given source code.

Source

pub fn find_all(&self, pattern: &str) -> Result<Vec<PatternMatch>, AstGrepError>

Find all matches for a pattern.

Source

pub fn find_unique(&self, pattern: &str) -> Result<PatternMatch, AstGrepError>

Find exactly one match for a pattern.

Source

pub fn has_match(&self, pattern: &str) -> bool

Check if a pattern has any matches.

Source

pub fn find_in_range( &self, pattern: &str, start: usize, end: usize, ) -> Result<Vec<PatternMatch>, AstGrepError>

Find matches within a specific byte range (for context constraints).

Source

pub fn find_in_function( &self, pattern: &str, function_name: &str, ) -> Result<Vec<PatternMatch>, AstGrepError>

Find matches that are inside a function with the given name.

Source

pub fn source(&self) -> &str

Get the source code.

Source

pub fn find_by_kind_with_field( &self, kind: &str, field_filter: Option<(&str, &str)>, ) -> Result<Vec<PatternMatch>, AstGrepError>

Find all nodes of a specific kind, optionally filtering by a pattern on a field.

This is useful for constructs that aren’t valid standalone Rust syntax, like match arms (PAT => BODY). Since match arms can’t be parsed in isolation, we find them by kind and optionally filter by matching a pattern against a specific field.

§Example
// Find all match arms where the pattern is OtelExporter::Statsig
let arms = matcher.find_by_kind_with_field(
    "match_arm",
    Some(("pattern", "OtelExporter::Statsig")),
)?;
Source

pub fn find_match_arms( &self, pattern: &str, ) -> Result<Vec<PatternMatch>, AstGrepError>

Find match arms by their pattern.

Convenience method for finding match arms since they can’t be matched directly with patterns (not valid standalone Rust syntax).

§Example
let arms = matcher.find_match_arms("OtelExporter::Statsig")?;
for arm in arms {
    println!("Found arm: {}", arm.text);
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.