Class: Sexp

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

Overview

straight from flay-persistent

Constant Summary collapse

NODE_NAMES =
Hash.new { |h,k| h[k] = Zlib.crc32(k.to_s) }
MAX_INT32 =

:nodoc:

2 ** 32 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modifiedObject Also known as: modified?

Whether or not this sexp is a mutated/modified sexp.



543
544
545
# File 'lib/flay.rb', line 543

def modified
  @modified
end

Instance Method Details

#+(o) ⇒ Object

:nodoc:



587
588
589
# File 'lib/flay.rb', line 587

def + o # :nodoc:
  self.dup.concat o
end

#[](a) ⇒ Object

:nodoc:



576
# File 'lib/flay.rb', line 576

alias :[] :[]

#all_structural_subhashesObject

Returns a list of structural hashes for all nodes (and sub-nodes) of this sexp.



558
559
560
561
562
563
564
# File 'lib/flay.rb', line 558

def all_structural_subhashes
  hashes = []
  self.deep_each do |node|
    hashes << node.structural_hash
  end
  hashes
end

#code_indexObject Also known as: has_code?

Return the index of the last non-code element, or nil if this sexp is not a code-bearing node.



603
604
605
606
607
608
609
610
611
612
# File 'lib/flay.rb', line 603

def code_index
  {
   :block  => 0,    # s(:block,                   *code)
   :class  => 2,    # s(:class,      name, super, *code)
   :module => 1,    # s(:module,     name,        *code)
   :defn   => 2,    # s(:defn,       name, args,  *code)
   :defs   => 3,    # s(:defs, recv, name, args,  *code)
   :iter   => 2,    # s(:iter, recv,       args,  *code)
  }[self.sexp_type]
end

#initialize_copy(o) ⇒ Object

:nodoc:



566
567
568
569
570
571
572
# File 'lib/flay.rb', line 566

def initialize_copy o # :nodoc:
  s = super
  s.file = o.file
  s.line = o.line
  s.modified = o.modified
  s
end

#pure_ruby_hashObject

:nodoc: see above



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/flay.rb', line 631

def pure_ruby_hash # :nodoc: see above
  hash = 0

  n = NODE_NAMES[sexp_type]

  raise "Bad lookup: #{first} in #{sexp.inspect}" unless n

  hash += n          & MAX_INT32
  hash += hash << 10 & MAX_INT32
  hash ^= hash >>  6 & MAX_INT32

  each do |o|
    next unless Sexp === o
    hash = hash + o.pure_ruby_hash  & MAX_INT32
    hash = (hash + (hash << 10)) & MAX_INT32
    hash = (hash ^ (hash >>  6)) & MAX_INT32
  end

  hash = (hash + (hash <<  3)) & MAX_INT32
  hash = (hash ^ (hash >> 11)) & MAX_INT32
  hash = (hash + (hash << 15)) & MAX_INT32

  hash
end

#split_at(n) ⇒ Object

Useful general array method that splits the array from 0..n and the rest. Returns both sections.



595
596
597
# File 'lib/flay.rb', line 595

def split_at n
  return self[0..n], self[n+1..-1]
end

#split_codeObject

Split the sexp into front-matter and code-matter, returning both. See #code_index.



620
621
622
623
# File 'lib/flay.rb', line 620

def split_code
  index = self.code_index
  self.split_at index if index
end

#structural_hashObject

Calculate the structural hash for this sexp. Cached, so don’t modify the sexp afterwards and expect it to be correct.



550
551
552
# File 'lib/flay.rb', line 550

def structural_hash
  @structural_hash ||= pure_ruby_hash
end