Class: Fast::FindFromArgument

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

Overview

Allow the user to interpolate expressions from external stuff. Use ‘%1` in the expression and the Matcher#prepare_arguments will interpolate the argument in the expression.

Examples:

interpolate the node value 1

Fast.match?("(int %1)", Fast.ast("1"), 1) # => true
Fast.match?("(int %1)", Fast.ast("1"), 2) # => false

interpolate multiple arguments

Fast.match?("(%1 %2)", Fast.ast("1"), :int, 1) # => true

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

Returns a new instance of FindFromArgument.



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

def initialize(token)
  token = token.token if token.respond_to?(:token)
  raise 'You must define index' unless token

  @capture_argument = token.to_i - 1
  raise 'Arguments start in one' if @capture_argument.negative?
end

Instance Attribute Details

#arguments=(value) ⇒ Object (writeonly)

Sets the attribute arguments

Parameters:

  • value

    the value to set the attribute arguments to.



552
553
554
# File 'lib/fast.rb', line 552

def arguments=(value)
  @arguments = value
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

def match?(node)
  raise 'You must define arguments to match' unless @arguments

  compare_symbol_or_head @arguments[@capture_argument], node
end

#to_sObject



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

def to_s
  "find_with_arg[\\#{@capture_argument}]"
end