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.


486
487
488
489
490
491
492
# File 'lib/fast.rb', line 486

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.


484
485
486
# File 'lib/fast.rb', line 484

def arguments=(value)
  @arguments = value
end

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)

494
495
496
497
498
# File 'lib/fast.rb', line 494

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

  compare_symbol_or_head @arguments[@capture_argument], node
end

#to_sObject


500
501
502
# File 'lib/fast.rb', line 500

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