Class: Rewrite::ByExample::SexpEntity

Inherits:
EntityMatcher show all
Defined in:
lib/rewrite/by_example/sexp_entity.rb

Overview

A SexpEntity is a matcher that is initialized with example sexps, so it matches a literal tree. I think SexpEntity is right, however not all things you build with ObjectToSequence are Sexps?

Direct Known Subclasses

LiteralEntity

Instance Attribute Summary collapse

Attributes inherited from EntityMatcher

#predicate

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityMatcher

#after_unfold, #such_that!

Constructor Details

#initialize(*foo) ⇒ SexpEntity

Returns a new instance of SexpEntity.



14
15
16
17
# File 'lib/rewrite/by_example/sexp_entity.rb', line 14

def initialize(*foo)
  raise "refactor to ObjectToMatcher" unless foo.empty?
  raise "refactor to ObjectToMatcher" if block_given?
end

Instance Attribute Details

#sequenceObject

Returns the value of attribute sequence.



12
13
14
# File 'lib/rewrite/by_example/sexp_entity.rb', line 12

def sequence
  @sequence
end

Class Method Details

.for_sequence(sequence) ⇒ Object



19
20
21
22
23
# File 'lib/rewrite/by_example/sexp_entity.rb', line 19

def self.for_sequence(sequence)
  s = SexpEntity.new
  s.sequence = sequence
  return s
end

.proc_capturerObject



43
44
45
# File 'lib/rewrite/by_example/sexp_entity.rb', line 43

def self.proc_capturer
  SexpEntity.new(:proc, nil,  Bind.new(:sexp, AnyEntity.new))
end

Instance Method Details

#fold(enum_of_bindings) ⇒ Object



35
36
37
# File 'lib/rewrite/by_example/sexp_entity.rb', line 35

def fold (enum_of_bindings)
  self.sequence.fold(enum_of_bindings)
end

#to_sObject



39
40
41
# File 'lib/rewrite/by_example/sexp_entity.rb', line 39

def to_s
  "s( #{sequence.to_s} )"
end

#unfold(sexp) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rewrite/by_example/sexp_entity.rb', line 25

def unfold (sexp)
  if sexp.kind_of? Array
    self.sequence.unfolders_by_length(sexp.length).each { |unfolder|
      unfolded = unfolder.call(sexp)
      return unfolded if unfolded && (predicate.nil? || predicate.call(unfolded))
    }
    nil
  end
end