The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Text::Treesitter::QueryMatch - stores the result of a tree-sitter query pattern match

SYNOPSIS

Usually accessed indirectly, via Text::Treesitter::QueryCursor.

   use Text::Treesitter;
   use Text::Treesitter::QueryCursor;

   my $ts = Text::Treesitter->new(
      lang_name => "perl",
   );

   my $query = $ts->load_query_string( "path/to/query.scm" );

   my $tree = $ts->parse_string( $input );

   my $qc = Text::Treesitter::_QueryCursor->new;

   $qc->exec( $query, $tree->root_node );

   while( my $match = $qc->next_match ) {
      my @captures = $match->captures;

      next unless $query->test_predicates_for_match( $match, \@captures );

      foreach my $capture ( @captures ) {
         my $node = $capture->node;
         my $capturename = $query->capture_name_for_id( $capture->capture_id );

         printf "%s captures the text <%s>\n",
            $capturename, $node->text;
      }
   }

DESCRIPTION

Instances of this class are returned from a Text::Treesitter::QueryCursor to iterate the matches of the most recent query operation.

METHODS

pattern_index

   $index = $match->pattern_index;

Returns the index within the query indicating which pattern was responsible for this match.

capture_count

   $count = $match->capture_count;

Returns the number of captures made by this pattern.

captures

   @captures = $match->captures;

Returns a list of Capture instances, in pattern order. Each will be an instance of a class having the following accessors:

   $capture->node
   $capture->capture_id

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>