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?(Fast.ast("1"), "(int %1)", 1) # => true
Fast.match?(Fast.ast("1"), "(int %1)", 2) # => false

interpolate multiple arguments

Fast.match?(Fast.ast("1"), "(%1 %2)", :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.



516
517
518
519
520
521
522
# File 'lib/fast.rb', line 516

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.



514
515
516
# File 'lib/fast.rb', line 514

def arguments=(value)
  @arguments = value
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


524
525
526
527
528
# File 'lib/fast.rb', line 524

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

  compare_symbol_or_head node, @arguments[@capture_argument]
end

#to_sObject



530
531
532
# File 'lib/fast.rb', line 530

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