Class: RubyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_parser_extras.rb

Overview

RubyParser is a compound parser that first attempts to parse using the 1.9 syntax parser and falls back to the 1.8 syntax parser on a parse error.

Defined Under Namespace

Classes: SyntaxError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyParser

Returns a new instance of RubyParser.



1350
1351
1352
1353
1354
1355
1356
# File 'lib/ruby_parser_extras.rb', line 1350

def initialize
  @p18 = Ruby18Parser.new
  @p19 = Ruby19Parser.new
  @p20 = Ruby20Parser.new
  @p21 = Ruby21Parser.new
  @p22 = Ruby22Parser.new
end

Class Method Details

.for_current_rubyObject



1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
# File 'lib/ruby_parser_extras.rb', line 1380

def self.for_current_ruby
  case RUBY_VERSION
  when /^1\.8/ then
    Ruby18Parser.new
  when /^1\.9/ then
    Ruby19Parser.new
  when /^2.0/ then
    Ruby20Parser.new
  when /^2.1/ then
    Ruby21Parser.new
  when /^2.2/ then
    Ruby22Parser.new
  else
    raise "unrecognized RUBY_VERSION #{RUBY_VERSION}"
  end
end

Instance Method Details

#process(s, f = "(string)", t = 10) ⇒ Object Also known as: parse



1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/ruby_parser_extras.rb', line 1358

def process s, f = "(string)", t = 10
  e = nil
  [@p22, @p21, @p20, @p19, @p18].each do |parser|
    begin
      return parser.process s, f, t
    rescue Racc::ParseError, RubyParser::SyntaxError => exc
      e = exc
    end
  end
  raise e
end

#resetObject



1372
1373
1374
1375
1376
1377
1378
# File 'lib/ruby_parser_extras.rb', line 1372

def reset
  @p18.reset
  @p19.reset
  @p20.reset
  @p21.reset
  @p22.reset
end