Permalink
Browse files

move extractors to extractor/

  • Loading branch information...
1 parent cd08c66 commit e7de64039b4062576aea4ef09894156a125edd57 @quix committed Aug 6, 2009
@@ -29,9 +29,10 @@ def engine=(engine)
@engine = nil
else
begin
- name = "extractor_#{engine}"
- require "pure/pure_private/#{name}_check"
- @engine = PurePrivate.const_get(to_camel_case(name))
+ require "pure/pure_private/extractor/#{engine}_check"
+ @engine = PurePrivate::Extractor.const_get(
+ to_camel_case(engine.to_s)
+ )
rescue LoadError
raise PurePrivate::NotImplementedError,
"engine not available: #{engine}"
@@ -43,7 +44,9 @@ def engine=(engine)
def engine
if @engine
@engine.
- name[%r!\APure::PurePrivate::Extractor(\w+)\Z!, 1].
+ name.
+ split("::").
+ last.
gsub(%r![A-Z]!) { |capital| "_" + capital.downcase }[1..-1].
to_sym
end
@@ -0,0 +1,26 @@
+
+require 'pure/pure_private/error'
+
+module Pure
+ module PurePrivate
+ module Extractor
+ module CommonParser
+ @cache = Hash.new { |hash, key| hash[key] = Hash.new }
+
+ class << self
+ def extract(mod, method_name, file, line, processor)
+ defs = @cache[processor][file] || (
+ @cache[processor][file] = processor.new(file).run
+ )
+ spec = defs[line]
+ unless spec and spec[:name] and spec[:name] == method_name
+ raise PurePrivate::ParseError,
+ "failure parsing `#{method_name}' at #{file}:#{line}"
+ end
+ spec.merge(:file => file, :line => line)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,29 @@
+
+require 'pure/pure_private/error'
+
+module Pure
+ module PurePrivate
+ module Extractor
+ module Parameters
+ module_function
+
+ def extract(mod, method_name, file, line)
+ if method_name == :__fun
+ Hash.new
+ else
+ {
+ :name => method_name,
+ :args => mod.instance_method(method_name).parameters.map {
+ |type, name|
+ if type == :rest
+ raise SplatError.new(file, line)
+ end
+ name
+ },
+ }
+ end
+ end
+ end
+ end
+ end
+end
@@ -1,5 +1,4 @@
-# rcov fun
has_parameters = Method.instance_methods.include?(:parameters)
-require('pure/pure_private/extractor_parameters') if has_parameters
+require('pure/pure_private/extractor/parameters') if has_parameters
raise LoadError unless has_parameters
@@ -0,0 +1,106 @@
+
+require 'ripper'
+require 'pure/pure_private/extractor/common_parser'
+
+module Pure
+ module PurePrivate
+ module Extractor
+ module Ripper
+ class << self
+ def extract(mod, method_name, file, line)
+ CommonParser.extract(mod, method_name, file, line, Processor)
+ end
+ end
+
+ class Processor
+ def initialize(file)
+ @file = file
+ @defs = Hash.new
+ end
+
+ def run
+ process(::Ripper.sexp(File.read(@file)))
+ @defs
+ end
+
+ def process_def(sexp)
+ if sexp[0] == :def
+ name = sexp[1][1].to_sym
+ line = sexp[1][2][0]
+ params = (
+ case sexp[2].first
+ when :params
+ sexp[2]
+ when :paren
+ sexp[2][1]
+ else
+ raise PurePrivate::ParseError,
+ "unforeseen `def' syntax at #{@file}:#{line}"
+ end
+ )
+ if params.any? { |t| t and t[0] == :rest_param }
+ raise SplatError.new(@file, line)
+ end
+ args = (
+ if params[1].nil?
+ []
+ else
+ params[1].map { |t| t[1].to_sym }
+ end
+ )
+ @defs[line] = {
+ :name => name,
+ :args => args,
+ :sexp => sexp,
+ }
+ true
+ else
+ false
+ end
+ end
+
+ def process_fun(sexp)
+ if sexp[0] == :method_add_block and sexp[1].is_a?(Array)
+ line = (
+ if sexp[1][0] == :command and
+ sexp[1][1].is_a?(Array) and
+ sexp[1][1][1] == "fun"
+ sexp[1][1][2][0]
+ elsif sexp[1][0] == :method_add_arg and
+ sexp[1][1].is_a?(Array) and
+ sexp[1][1][0] == :fcall and
+ sexp[1][1][1].is_a?(Array) and
+ sexp[1][1][1][1] == "fun"
+ sexp[1][1][1][2][0]
+ else
+ nil
+ end
+ )
+ if line
+ @defs[line] = {
+ :name => :__fun,
+ :sexp => sexp[2],
+ }
+ true
+ else
+ false
+ end
+ else
+ false
+ end
+ end
+
+ def process(sexp)
+ if sexp.is_a? Array
+ process_def(sexp) or process_fun(sexp) or (
+ sexp.each { |sub_sexp|
+ process(sub_sexp)
+ }
+ )
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,2 @@
+
+require('ripper') and require('pure/pure_private/extractor/ripper')
@@ -0,0 +1,58 @@
+
+require 'ruby_parser'
+require 'sexp_processor'
+require 'pure/pure_private/extractor/common_parser'
+
+module Pure
+ module PurePrivate
+ module Extractor
+ module RubyParser
+ class << self
+ def extract(mod, method_name, file, line)
+ CommonParser.extract(mod, method_name, file, line, Processor)
+ end
+ end
+
+ class Processor < SexpProcessor
+ def initialize(file)
+ super()
+ @file = file
+ @defs = Hash.new
+ end
+
+ def run
+ process(::RubyParser.new.parse(File.read(@file)))
+ @defs
+ end
+
+ def process(sexp)
+ if sexp[0] == :defn
+ name = sexp[1]
+ args = sexp[2].to_a[1..-1]
+ if args.any? { |arg| arg.to_s =~ %r!\A\*! }
+ raise SplatError.new(@file, sexp.line)
+ end
+ @defs[sexp.line] = {
+ :name => name,
+ :args => args,
+ :sexp => sexp.dup,
+ }
+ sexp.clear
+ elsif sexp[0] == :iter and
+ sexp[1][0] == :call and
+ sexp[1][1] == nil and
+ sexp[1][2] == :fun
+ @defs[sexp[1].line] = {
+ :name => :__fun,
+ :sexp => [sexp[2], sexp[3]],
+ }
+ sexp.clear
+ else
+ super
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,2 @@
+
+require('ruby_parser') and require('pure/pure_private/extractor/ruby_parser')
@@ -1,25 +0,0 @@
-
-require 'pure/pure_private/error'
-require 'pure/pure_private/util'
-
-module Pure
- module PurePrivate
- module ExtractorCommonParser
- @cache = Hash.new { |hash, key| hash[key] = Hash.new }
-
- class << self
- def extract(mod, method_name, file, line, processor)
- defs = @cache[processor][file] || (
- @cache[processor][file] = processor.new(file).run
- )
- spec = defs[line]
- unless spec and spec[:name] and spec[:name] == method_name
- raise PurePrivate::ParseError,
- "failure parsing `#{method_name}' at #{file}:#{line}"
- end
- spec.merge(:file => file, :line => line)
- end
- end
- end
- end
-end
@@ -1,29 +0,0 @@
-
-unless Method.instance_methods.include? :parameters
- raise LoadError
-end
-
-module Pure
- module PurePrivate
- module ExtractorParameters
- module_function
-
- def extract(mod, method_name, file, line)
- if method_name == :__fun
- Hash.new
- else
- {
- :name => method_name,
- :args => mod.instance_method(method_name).parameters.map {
- |type, name|
- if type == :rest
- raise SplatError.new(file, line)
- end
- name
- },
- }
- end
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit e7de640

Please sign in to comment.