Sha256: 1e4a95459e584ffc635c82449a7b0c52b207c0352150925ec9595e4b54bab2ea

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require "psych"

module AnnotateRb
  module ModelAnnotator
    module FileParser
      class YmlParser
        class << self
          def parse(string)
            _parser = new(string).tap(&:parse)
          end
        end

        attr_reader :comments, :starts, :ends

        def initialize(input)
          @input = input
          @comments = []
          @starts = []
          @ends = []
        end

        def parse
          parse_comments
          parse_yml
        end

        private

        def parse_comments
          # Adds 0-indexed line numbers
          @input.split($/).each_with_index do |line, line_no|
            if line.strip.starts_with?("#")
              @comments << [line, line_no]
            end
          end
        end

        def parse_yml
          # https://docs.ruby-lang.org/en/master/Psych.html#module-Psych-label-Reading+to+Psych-3A-3ANodes-3A-3AStream+structure
          parser = Psych.parser
          parser.parse(@input)

          stream = parser.handler.root

          if stream.children.any?
            doc = stream.children.first
            @starts << [nil, doc.start_line]
            @ends << [nil, doc.end_line]
          else
            # When parsing a yml file, streamer returns an instance of `Psych::Nodes::Stream` which is a subclass of
            #   `Psych::Nodes::Node`. It along with children nodes, implement #start_line and #end_line.
            #
            # When parsing input that is only comments, the parser counts #start_line as the start of the file being
            #   line 0.
            #
            # What we really want is where the "start" of the yml file would happen, which would be after comments.
            # This is stream#end_line.
            @starts << [nil, stream.end_line]
            @ends << [nil, stream.end_line]
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
annotaterb-4.12.0 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb
annotaterb-4.11.0 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb
annotaterb-4.10.2 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb
annotaterb-4.10.1 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb
annotaterb-4.10.0 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb
annotaterb-4.9.0 lib/annotate_rb/model_annotator/file_parser/yml_parser.rb