Class: Fast::Find

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Find

Returns a new instance of Find.



435
436
437
# File 'lib/fast.rb', line 435

def initialize(token)
  self.token = token
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



434
435
436
# File 'lib/fast.rb', line 434

def token
  @token
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  return false if other.nil? || !other.respond_to?(:token)

  token == other.token
end

#compare_symbol_or_head(expression, node) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/fast.rb', line 457

def compare_symbol_or_head(expression, node)
  case node
  when Parser::AST::Node
    node.type == expression.to_sym
  when String
    node == expression.to_s
  when TrueClass
    expression == :true
  when FalseClass
    expression == :false
  else
    node == expression
  end
end

#debug(expression, node, match) ⇒ Object



478
479
480
# File 'lib/fast.rb', line 478

def debug(expression, node, match)
  puts "#{expression} == #{node} # => #{match}"
end

#debug_match_recursive(expression, node) ⇒ Object



472
473
474
475
476
# File 'lib/fast.rb', line 472

def debug_match_recursive(expression, node)
  match = original_match_recursive(expression, node)
  debug(expression, node, match)
  match
end

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


439
440
441
# File 'lib/fast.rb', line 439

def match?(node)
  match_recursive(valuate(token), node)
end

#match_recursive(expression, node) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/fast.rb', line 443

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_sObject



482
483
484
# File 'lib/fast.rb', line 482

def to_s
  "f[#{[*token].map(&:to_s).join(', ')}]"
end