Sha256: 5eb7762ea014a16882201e1d6767a9edb5d1abc3e7faf5baf63aa7f05b405281

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

require "ruby_parser_extras"
require "racc/parser"

##
# RubyParser is a compound parser that uses all known versions to
# attempt to parse.

class RubyParser

  VERSIONS = []

  attr_accessor :current

  def self.for_current_ruby
    name  = "V#{RUBY_VERSION[/^\d+\.\d+/].delete "."}"
    klass = if const_defined? name then
              const_get name
            else
              latest = VERSIONS.first
              warn "NOTE: RubyParser::#{name} undefined, using #{latest}."
              latest
            end

    klass.new
  end

  def self.latest
    VERSIONS.first.new
  end

  def process s, f = "(string)", t = 10
    e = nil
    VERSIONS.each do |klass|
      self.current = parser = klass.new
      begin
        return parser.process s, f, t
      rescue Racc::ParseError, RubyParser::SyntaxError => exc
        e ||= exc
      end
    end
    raise e
  end

  alias :parse :process

  def reset
    # do nothing
  end

  class Parser < Racc::Parser
    include RubyParserStuff

    def self.inherited x
      RubyParser::VERSIONS << x
    end

    def self.version= v
       @version = v
    end

    def self.version
      @version ||= Parser > self && self.name[/(?:V|Ruby)(\d+)/, 1].to_i
    end
  end

  class SyntaxError < RuntimeError; end
end

##
# Unfortunately a problem with racc is that it won't let me namespace
# properly, so instead of RubyParser::V25, I still have to generate
# the old Ruby25Parser and shove it in as V25.

require "ruby20_parser"
require "ruby21_parser"
require "ruby22_parser"
require "ruby23_parser"
require "ruby24_parser"
require "ruby25_parser"
require "ruby26_parser"

class RubyParser # HACK
  VERSIONS.clear # also a HACK caused by racc namespace issues

  class V26 < ::Ruby26Parser; end
  class V25 < ::Ruby25Parser; end
  class V24 < ::Ruby24Parser; end
  class V23 < ::Ruby23Parser; end
  class V22 < ::Ruby22Parser; end
  class V21 < ::Ruby21Parser; end
  class V20 < ::Ruby20Parser; end
end

Version data entries

16 entries across 13 versions & 3 rubygems

Version Path
brakeman-4.9.0 bundle/ruby/2.7.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb
brakeman-4.8.2 bundle/ruby/2.7.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb
brakeman-4.8.1 bundle/ruby/2.4.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb
brakeman-4.8.0 bundle/ruby/2.7.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb
ruby_parser-3.14.2 lib/ruby_parser.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.1/bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.0/bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.2/bundle/ruby/2.6.0/gems/ruby_parser-3.14.1/lib/ruby_parser.rb
brakeman-4.7.2 bundle/ruby/2.6.0/gems/ruby_parser-3.14.1/lib/ruby_parser.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/brakeman-4.7.1/bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/brakeman-4.7.0/bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
ruby_parser-3.14.1 lib/ruby_parser.rb
brakeman-4.7.1 bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/brakeman-4.7.0/bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
brakeman-4.7.0 bundle/ruby/2.6.0/gems/ruby_parser-3.14.0/lib/ruby_parser.rb
ruby_parser-3.14.0 lib/ruby_parser.rb