Sha256: 1fe93b99bd6c7c6c115910fc5f96cd424019c1bc5d42dcb49a87dc55c9e09308

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "ripper_ruby_parser/commenting_ripper_parser"
require "ripper_ruby_parser/sexp_processor"

module RipperRubyParser
  # Main parser class. Brings together Ripper and our
  # RipperRubyParser::SexpProcessor.
  class Parser
    attr_accessor :extra_compatible

    def initialize
      @extra_compatible = false
    end

    def parse(source, filename = "(string)", lineno = 1)
      parser = CommentingRipperParser.new(source, filename, lineno)
      exp = parser.parse

      processor = SexpProcessor.new(filename: filename, extra_compatible: extra_compatible)
      result = processor.process exp

      result = result[1] if result.sexp_type == :begin

      if result.sexp_type == :void_stmt
        nil
      else
        trickle_up_line_numbers result
        trickle_down_line_numbers result
        result
      end
    end

    private

    def trickle_up_line_numbers(exp)
      exp.each do |sub_exp|
        if sub_exp.is_a? Sexp
          trickle_up_line_numbers sub_exp
          exp.line ||= sub_exp.line
        end
      end
    end

    def trickle_down_line_numbers(exp)
      exp.each do |sub_exp|
        if sub_exp.is_a? Sexp
          sub_exp.line ||= exp.line
          trickle_down_line_numbers sub_exp
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ripper_ruby_parser-1.11.0 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.10.0 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.9.0 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.8.0 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.7.2 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.7.1 lib/ripper_ruby_parser/parser.rb
ripper_ruby_parser-1.7.0 lib/ripper_ruby_parser/parser.rb