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.



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

def modified
  @modified
end

Instance Method Details

#+(o) ⇒ Object

:nodoc:



592
593
594
# File 'lib/flay.rb', line 592

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

#[](a) ⇒ Object

:nodoc:



582
583
584
585
586
587
588
589
590
# File 'lib/flay.rb', line 582

def [] a # :nodoc:
  s = super
  if Sexp === s then
    s.file = self.file
    s.line = self.line
    s.modified = self.modified
  end
  s
end

#all_structural_subhashesObject

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



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

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.



608
609
610
611
612
613
614
615
616
617
# File 'lib/flay.rb', line 608

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:



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

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



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/flay.rb', line 636

def pure_ruby_hash # :nodoc: see above
  hash = 0

  n = NODE_NAMES[first]

  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.



600
601
602
# File 'lib/flay.rb', line 600

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.



625
626
627
628
# File 'lib/flay.rb', line 625

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.



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

def structural_hash
  @structural_hash ||= pure_ruby_hash
end