Sha256: 9f069cd6cade12182c34e76781869c08e54370523d05ed05e8cb154a8e73b3a4

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'parser'
require 'parser/current'

module Leftovers
  module Parser
    class << self
      # mostly copied from https://github.com/whitequark/parser/blob/master/lib/parser/base.rb
      # but with our parser
      def parse_with_comments(string, file = '(string)', line = 1)
        PARSER.reset
        PARSER.parse_with_comments(new_source_buffer(string, file, line))
      end

      private

      def new_source_buffer(string, file, line)
        ::Parser::CurrentRuby.send(
          :setup_source_buffer, file, line, string, PARSER.default_encoding
        )
      end

      # mostly copied from https://github.com/whitequark/parser/blob/master/lib/parser/base.rb
      # but with our builder
      def parser
        p = ::Parser::CurrentRuby.new(Leftovers::AST::Builder.new)
        p.diagnostics.all_errors_are_fatal = true
        p.diagnostics.ignore_warnings = true

        p.diagnostics.consumer = lambda do |diagnostic|
          diagnostic
        end

        p
      end
    end
    PARSER = parser
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
leftovers-0.11.1 lib/leftovers/parser.rb
leftovers-0.11.0 lib/leftovers/parser.rb
leftovers-0.10.0 lib/leftovers/parser.rb
leftovers-0.9.0 lib/leftovers/parser.rb