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.



1395
1396
1397
1398
1399
1400
1401
1402
# File 'lib/ruby_parser_extras.rb', line 1395

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

Class Method Details

.for_current_rubyObject



1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# File 'lib/ruby_parser_extras.rb', line 1427

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
  when /^2.3/ then
    Ruby23Parser.new
  else
    raise "unrecognized RUBY_VERSION #{RUBY_VERSION}"
  end
end

Instance Method Details

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



1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
# File 'lib/ruby_parser_extras.rb', line 1404

def process s, f = "(string)", t = 10
  e = nil
  [@p23, @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



1418
1419
1420
1421
1422
1423
1424
1425
# File 'lib/ruby_parser_extras.rb', line 1418

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