class Fast::Find

Find is the top level class that respond to match?(node) interface. It matches recurively and check deeply depends of the token type.

Attributes

token[RW]

Public Class Methods

new(token) click to toggle source
# File lib/fast.rb, line 410
def initialize(token)
  self.token = token
end

Public Instance Methods

==(other) click to toggle source
# File lib/fast.rb, line 457
def ==(other)
  return false if other.nil? || !other.respond_to?(:token)

  token == other.token
end
compare_symbol_or_head(expression, node) click to toggle source
# File lib/fast.rb, line 432
def compare_symbol_or_head(expression, node)
  case node
  when Parser::AST::Node
    node.type == expression.to_sym
  when String
    node == expression.to_s
  else
    node == expression
  end
end
debug(expression, node, match) click to toggle source
# File lib/fast.rb, line 449
def debug(expression, node, match)
  puts "#{expression} == #{node} # => #{match}"
end
debug_match_recursive(expression, node) click to toggle source
# File lib/fast.rb, line 443
def debug_match_recursive(expression, node)
  match = original_match_recursive(expression, node)
  debug(expression, node, match)
  match
end
match?(node) click to toggle source
# File lib/fast.rb, line 414
def match?(node)
  match_recursive(valuate(token), node)
end
match_recursive(expression, node) click to toggle source
# File lib/fast.rb, line 418
def match_recursive(expression, node)
  case expression
  when Proc then expression.call(node)
  when Find then expression.match?(node)
  when Symbol then compare_symbol_or_head(expression, node)
  when Enumerable
    expression.each_with_index.all? do |exp, i|
      match_recursive(exp, i.zero? ? node : node.children[i - 1])
    end
  else
    node == expression
  end
end
to_s() click to toggle source
# File lib/fast.rb, line 453
def to_s
  "f[#{[*token].map(&:to_s).join(', ')}]"
end

Private Instance Methods

typecast_value(token) click to toggle source
# File lib/fast.rb, line 475
def typecast_value(token)
  case token
  when /^\d+\.\d*/ then token.to_f
  when /^\d+/ then token.to_i
  else token.to_sym
  end
end
valuate(token) click to toggle source
# File lib/fast.rb, line 465
def valuate(token)
  if token.is_a?(String)
    return valuate(LITERAL[token]) if LITERAL.key?(token)

    typecast_value(token)
  else
    token
  end
end