summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Fitzgerald <rwfitzge@gmail.com>2011-11-30 21:24:58 -0800
committerRyan Fitzgerald <rwfitzge@gmail.com>2011-11-30 21:24:58 -0800
commitb1cec2270764e597ed1eeea89fa7080dd483a1bb (patch)
treebd05bf28b6b5472f72d76dcfd59bed5c80fcf926
parentf3d03098bf4afa875cd7815ce7d6198e87e6754e (diff)
downloadmethod_source-b1cec2270764e597ed1eeea89fa7080dd483a1bb.tar.gz
replace ruby_parser dependency with eval
-rw-r--r--Rakefile1
-rw-r--r--lib/method_source.rb47
-rw-r--r--method_source.gemspec5
3 files changed, 18 insertions, 35 deletions
diff --git a/Rakefile b/Rakefile
index fbe9be2..c8b8f54 100644
--- a/Rakefile
+++ b/Rakefile
@@ -19,7 +19,6 @@ def apply_spec_defaults(s)
s.email = 'jrmair@gmail.com'
s.description = s.summary
s.require_path = 'lib'
- s.add_dependency("ruby_parser",">= 2.3.1")
s.add_development_dependency("bacon","~>1.1.0")
s.add_development_dependency("rake", "~>0.9")
diff --git a/lib/method_source.rb b/lib/method_source.rb
index daaa135..9a3c325 100644
--- a/lib/method_source.rb
+++ b/lib/method_source.rb
@@ -7,38 +7,25 @@ require "#{direc}/method_source/version"
require "#{direc}/method_source/source_location"
module MethodSource
-
- if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE == "ruby"
- require 'ripper'
-
- # Determine if a string of code is a valid Ruby expression.
- # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
- # @param [String] code The code to validate.
- # @return [Boolean] Whether or not the code is a valid Ruby expression.
- # @example
- # valid_expression?("class Hello") #=> false
- # valid_expression?("class Hello; end") #=> true
- def self.valid_expression?(code)
- !!Ripper::SexpBuilder.new(code).parse
- end
-
- else
- require 'ruby_parser'
-
- # Determine if a string of code is a valid Ruby expression.
- # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
- # @param [String] code The code to validate.
- # @return [Boolean] Whether or not the code is a valid Ruby expression.
- # @example
- # valid_expression?("class Hello") #=> false
- # valid_expression?("class Hello; end") #=> true
- def self.valid_expression?(code)
- RubyParser.new.parse(code)
- rescue Racc::ParseError, SyntaxError
- false
+ # Determine if a string of code is a valid Ruby expression.
+ # @param [String] code The code to validate.
+ # @return [Boolean] Whether or not the code is a valid Ruby expression.
+ # @example
+ # valid_expression?("class Hello") #=> false
+ # valid_expression?("class Hello; end") #=> true
+ def self.valid_expression?(str)
+ if defined?(Rubinius::Melbourne19) && RUBY_VERSION =~ /^1\.9/
+ Rubinius::Melbourne19.parse_string(str)
+ elsif defined?(Rubinius::Melbourne)
+ Rubinius::Melbourne.parse_string(str)
else
- true
+ catch(:valid) {
+ eval("BEGIN{throw :valid}\n#{str}")
+ }
end
+ true
+ rescue SyntaxError
+ false
end
# Helper method responsible for extracting method body.
diff --git a/method_source.gemspec b/method_source.gemspec
index e9f337b..3b267ee 100644
--- a/method_source.gemspec
+++ b/method_source.gemspec
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Mair (banisterfiend)"]
- s.date = "2011-10-26"
+ s.date = "2011-11-30"
s.description = "retrieve the sourcecode for a method"
s.email = "jrmair@gmail.com"
s.files = [".gemtest", ".travis.yml", ".yardopts", "Gemfile", "LICENSE", "README.markdown", "Rakefile", "lib/method_source.rb", "lib/method_source/source_location.rb", "lib/method_source/version.rb", "method_source.gemspec", "test/test.rb", "test/test_helper.rb"]
@@ -20,16 +20,13 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<ruby_parser>, [">= 2.3.1"])
s.add_development_dependency(%q<bacon>, ["~> 1.1.0"])
s.add_development_dependency(%q<rake>, ["~> 0.9"])
else
- s.add_dependency(%q<ruby_parser>, [">= 2.3.1"])
s.add_dependency(%q<bacon>, ["~> 1.1.0"])
s.add_dependency(%q<rake>, ["~> 0.9"])
end
else
- s.add_dependency(%q<ruby_parser>, [">= 2.3.1"])
s.add_dependency(%q<bacon>, ["~> 1.1.0"])
s.add_dependency(%q<rake>, ["~> 0.9"])
end