Sha256: 6818392f90300a659d75676fcd13b1f52dcc5fdea26d7cedfd590d91aa47a24f

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Junoser
  class Input
    def initialize(io_or_string)
      @io_or_string = io_or_string
    end

    def read
      content = if @io_or_string.respond_to?(:read)
                  @io_or_string.read
                else
                  @io_or_string.to_s
                end

      content = remove_blank_and_comment_line(content)
      content = unify_carriage_return(content)
      content = unify_square_brackets(content)
    end


    private

    # As for comment line, a trailing comment after configuration will be processed by parslet
    def remove_blank_and_comment_line(str)
      str.gsub! /^\s*#.*/, ''
      str.gsub! /^\s*\/\*((?!\*\/).)*\*\//m, ''
      str.gsub! /\n\s*/, "\n"
      str.strip
    end

    def unify_carriage_return(str)
      str.gsub! /\r\n?/, "\n"
      str
    end

    # Statements or [ ... ] may be split into multiple lines. This method unifies them into one line.
    def unify_square_brackets(str)
      lines = []

      open_brackets = 0
      str.split("\n").each do |line|
        raise "ERROR: invalid statement: #{line}" if open_brackets < 0

        if open_brackets == 0
          lines << line
        else
          lines.last << " " << line
        end

        open_brackets += line.count('[') - line.count(']')
      end

      lines.join("\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
junoser-0.7.0 lib/junoser/input.rb
junoser-0.6.0 lib/junoser/input.rb
junoser-0.5.6 lib/junoser/input.rb