diff options
author | Ryan Fitzgerald <rwfitzge@gmail.com> | 2011-11-30 21:24:58 -0800 |
---|---|---|
committer | Ryan Fitzgerald <rwfitzge@gmail.com> | 2011-11-30 21:24:58 -0800 |
commit | b1cec2270764e597ed1eeea89fa7080dd483a1bb (patch) | |
tree | bd05bf28b6b5472f72d76dcfd59bed5c80fcf926 /lib/method_source.rb | |
parent | f3d03098bf4afa875cd7815ce7d6198e87e6754e (diff) | |
download | method_source-b1cec2270764e597ed1eeea89fa7080dd483a1bb.tar.gz |
replace ruby_parser dependency with eval
Diffstat (limited to 'lib/method_source.rb')
-rw-r--r-- | lib/method_source.rb | 47 |
1 files changed, 17 insertions, 30 deletions
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. |