class Fast::Capture

Capture some expression while searching for it.

The captures behaves exactly like Fast::Find and the only difference is that when it {#match?} stores captures for future usage.

@example capture int node

capture = Fast::Capture.new("int") => #<Fast::Capture:0x00...e0 @captures=[], @token="int">
capture.match?(Fast.ast("1")) # => [s(:int, 1)]

@example binding directly in the Fast.expression

Fast.match?(Fast.ast("1"), "(int $_)") # => [1]

@example capture the value of a local variable assignment

(${int float} _)

@example expression to capture only the node type

(${int float} _)

@example expression to capture entire node

$({int float} _)

@example expression to capture only the node value of int or float nodes

({int float} $_)

@example expression to capture both node type and value

($_ $_)

You can capture stuff in multiple levels and build expressions that reference captures with Fast::FindWithCapture.

Attributes

captures[R]

Stores nodes that matches with the current expression.

Public Class Methods

new(token) click to toggle source
Calls superclass method Fast::Find::new
# File lib/fast.rb, line 602
def initialize(token)
  super
  @captures = []
end

Public Instance Methods

match?(node) click to toggle source

Append the matching node to {#captures} if it matches

Calls superclass method Fast::Find#match?
# File lib/fast.rb, line 608
def match?(node)
  @captures << node if super
end
to_s() click to toggle source
# File lib/fast.rb, line 612
def to_s
  "c[#{token}]"
end