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.
341
342
343
|
# File 'lib/fast.rb', line 341
def initialize(token)
self.token = token
end
|
Instance Attribute Details
#token ⇒ Object
Returns the value of attribute token.
340
341
342
|
# File 'lib/fast.rb', line 340
def token
@token
end
|
Instance Method Details
#==(other) ⇒ Object
388
389
390
391
392
|
# File 'lib/fast.rb', line 388
def ==(other)
return false if other.nil? || !other.respond_to?(:token)
token == other.token
end
|
#compare_symbol_or_head(expression, node) ⇒ Object
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/fast.rb', line 363
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) ⇒ Object
380
381
382
|
# File 'lib/fast.rb', line 380
def debug(expression, node, match)
puts "#{expression} == #{node} # => #{match}"
end
|
#debug_match_recursive(expression, node) ⇒ Object
374
375
376
377
378
|
# File 'lib/fast.rb', line 374
def debug_match_recursive(expression, node)
match = original_match_recursive(expression, node)
debug(expression, node, match)
match
end
|
#match?(node) ⇒ Boolean
345
346
347
|
# File 'lib/fast.rb', line 345
def match?(node)
match_recursive(valuate(token), node)
end
|
#match_recursive(expression, node) ⇒ Object
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# File 'lib/fast.rb', line 349
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 ⇒ Object
384
385
386
|
# File 'lib/fast.rb', line 384
def to_s
"f[#{[*token].map(&:to_s).join(', ')}]"
end
|