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.



552
553
554
# File 'lib/flay.rb', line 552

def modified
  @modified
end

Instance Method Details

#+(o) ⇒ Object

:nodoc:



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

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

#[](a) ⇒ Object

:nodoc:



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

alias :[] :[]

#all_structural_subhashesObject

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



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

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.



612
613
614
615
616
617
618
619
620
621
# File 'lib/flay.rb', line 612

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:



575
576
577
578
579
580
581
# File 'lib/flay.rb', line 575

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



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/flay.rb', line 640

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.



604
605
606
# File 'lib/flay.rb', line 604

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.



629
630
631
632
# File 'lib/flay.rb', line 629

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.



559
560
561
# File 'lib/flay.rb', line 559

def structural_hash
  @structural_hash ||= pure_ruby_hash
end