Class: Fast::Find
- Inherits:
-
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.
Direct Known Subclasses
All, Any, Capture, FindFromArgument, FindString, FindWithCapture, InstanceMethodCall, Maybe, MethodCall, Not, Parent
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(token) ⇒ Find
Returns a new instance of Find.
372
373
374
|
# File 'lib/fast.rb', line 372
def initialize(token)
self.token = token
end
|
Instance Attribute Details
#token ⇒ Object
Returns the value of attribute token.
371
372
373
|
# File 'lib/fast.rb', line 371
def token
@token
end
|
Instance Method Details
#==(other) ⇒ Object
419
420
421
422
423
|
# File 'lib/fast.rb', line 419
def ==(other)
return false if other.nil? || !other.respond_to?(:token)
token == other.token
end
|
#compare_symbol_or_head(node, expression) ⇒ Object
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/fast.rb', line 394
def compare_symbol_or_head(node, expression)
case node
when Parser::AST::Node
node.type == expression.to_sym
when String
node == expression.to_s
else
node == expression
end
end
|
#debug(node, expression, match) ⇒ Object
411
412
413
|
# File 'lib/fast.rb', line 411
def debug(node, expression, match)
puts "#{expression} == #{node} # => #{match}"
end
|
#debug_match_recursive(node, expression) ⇒ Object
405
406
407
408
409
|
# File 'lib/fast.rb', line 405
def debug_match_recursive(node, expression)
match = original_match_recursive(node, expression)
debug(node, expression, match)
match
end
|
#match?(node) ⇒ Boolean
376
377
378
|
# File 'lib/fast.rb', line 376
def match?(node)
match_recursive(node, valuate(token))
end
|
#match_recursive(node, expression) ⇒ Object
380
381
382
383
384
385
386
387
388
389
390
391
392
|
# File 'lib/fast.rb', line 380
def match_recursive(node, expression)
case expression
when Proc then expression.call(node)
when Find then expression.match?(node)
when Symbol then compare_symbol_or_head(node, expression)
when Enumerable
expression.each_with_index.all? do |exp, i|
match_recursive(i.zero? ? node : node.children[i - 1], exp)
end
else
node == expression
end
end
|
#to_s ⇒ Object
415
416
417
|
# File 'lib/fast.rb', line 415
def to_s
"f[#{[*token].map(&:to_s).join(', ')}]"
end
|