Permalink
Browse files

Refactoring in flay. Almost ready to pull these two into a common par…

…ser + ast visitor
  • Loading branch information...
1 parent 2da4b15 commit ef78ad105808d11ea477cea075c7d04849a905f6 Burke Libbey committed Nov 24, 2011
Showing with 29 additions and 42 deletions.
  1. +27 −17 lib/flay.rb
  2. +0 −23 lib/flay/sexp_extensions.rb
  3. +2 −2 spec/flay/flay_spec.rb
View
@@ -2,12 +2,17 @@
require 'ruby_parser'
require 'flay/reporter'
-require 'flay/sexp_extensions'
class String
attr_accessor :group
end
+class Sexp
+ def structural_hash
+ @structural_hash ||= self.structure.hash
+ end
+end
+
class Flay
include Flay::Reporter
@@ -49,30 +54,20 @@ def initialize(option = nil)
def process(*files)
files.each do |file|
- process_file(file)
+ ast = Ruby19Parser.new.process(File.read(file), file)
+ process_ast(ast)
end
analyze
end
- def process_file(file)
- warn "Processing #{file}" if option[:verbose]
-
+ def process_ast(ast)
begin
- sexp = parse_file(file)
- process_sexp(sexp) if sexp
+ process_sexp(ast) if ast
rescue SyntaxError => e
warn " skipping #{file}: #{e.message}"
end
end
- def parse_file(file)
- Ruby19Parser.new.process(File.read(file), file)
- rescue => e
- warn " #{e.message.strip}"
- warn " skipping #{file}"
- nil
- end
-
def analyze
prune
@@ -84,8 +79,23 @@ def analyze
end
end
+ def all_structural_subhashes(node)
+ hashes = []
+ sexp_deep_each(node) do |n|
+ hashes << n.structural_hash
+ end
+ hashes
+ end
+
+ def sexp_deep_each(node, &block)
+ node.select { |s| s.kind_of?(Sexp) }.each do |sexp|
+ block[sexp]
+ sexp_deep_each(sexp, &block)
+ end
+ end
+
def process_sexp(pt)
- pt.deep_each do |node|
+ sexp_deep_each(pt) do |node|
next unless node.any? { |sub| Sexp === sub }
next if node.mass < self.mass_threshold
@@ -101,7 +111,7 @@ def prune
all_hashes = {}
hashes.values.each do |nodes|
nodes.each do |node|
- node.all_structural_subhashes.each do |h|
+ all_structural_subhashes(node).each do |h|
all_hashes[h] = true
end
end
@@ -1,23 +0,0 @@
-class Sexp
-
- def structural_hash
- @structural_hash ||= self.structure.hash
- end
-
- def all_structural_subhashes
- hashes = []
- self.deep_each do |node|
- hashes << node.structural_hash
- end
- hashes
- end
-
- def deep_each(&block)
- select { |s| s.kind_of?(Sexp) }.each do |sexp|
- block[sexp]
- sexp.deep_each(&block)
- end
- end
-
-end
-
@@ -35,11 +35,11 @@
s[3][1] .hash,
].sort
- @s.all_structural_subhashes.sort.uniq.should == expected
+ Flay.new.all_structural_subhashes(@s).sort.uniq.should == expected
x = []
- @s.deep_each do |o|
+ Flay.new.sexp_deep_each(@s) do |o|
x << o.structural_hash
end

0 comments on commit ef78ad1

Please sign in to comment.