Class: Fast::FindWithCapture

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

Overview

Allow use previous captures while searching in the AST. Use ‘\1` to point the match to the first captured element or sequential numbers considering the order of the captures.

Examples:

check comparision of integers that will always return true

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

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) ⇒ FindWithCapture

Returns a new instance of FindWithCapture.



556
557
558
559
560
561
# File 'lib/fast.rb', line 556

def initialize(token)
  token = token.token if token.respond_to?(:token)
  raise 'You must use captures!' unless token

  @capture_index = token.to_i
end

Instance Attribute Details

#previous_captures=(value) ⇒ Object (writeonly)

Sets the attribute previous_captures

Parameters:

  • value

    the value to set the attribute previous_captures to.



554
555
556
# File 'lib/fast.rb', line 554

def previous_captures=(value)
  @previous_captures = value
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


563
564
565
# File 'lib/fast.rb', line 563

def match?(node)
  node == @previous_captures[@capture_index - 1]
end

#to_sObject



567
568
569
# File 'lib/fast.rb', line 567

def to_s
  "fc[\\#{@capture_index}]"
end