Class: Fast::Capture

Inherits:
Find
  • Object
show all
Defined in:
lib/fast.rb

Overview

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.

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

Examples:

capture int node

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

binding directly in the Fast.expression

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

capture the value of a local variable assignment

(${int float} _)

expression to capture only the node type

(${int float} _)

expression to capture entire node

$({int float} _)

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

({int float} $_)

expression to capture both node type and value

($_ $_)

Instance Attribute Summary collapse

Attributes inherited from Find

#token

Instance Method Summary collapse

Methods inherited from Find

#==, #compare_symbol_or_head, #debug, #debug_match_recursive, #match_recursive

Constructor Details

#initialize(token) ⇒ Capture

Returns a new instance of Capture.



631
632
633
634
# File 'lib/fast.rb', line 631

def initialize(token)
  super
  @captures = []
end

Instance Attribute Details

#capturesObject (readonly)

Stores nodes that matches with the current expression.



629
630
631
# File 'lib/fast.rb', line 629

def captures
  @captures
end

Instance Method Details

#match?(node) ⇒ Boolean

Append the matching node to #captures if it matches

Returns:

  • (Boolean)


637
638
639
# File 'lib/fast.rb', line 637

def match?(node)
  @captures << node if super
end

#to_sObject



641
642
643
# File 'lib/fast.rb', line 641

def to_s
  "c[#{token}]"
end