Sha256: e6a376437aefc5de29b592caaf9415b8d397dd1387125974e33034a53266d38b

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'parslet'
require 'junoser/input'

module Junoser
  module Display
    class Set
      def initialize(io_or_string)
        @input = io_or_string
      end

      def transform
        result = ''

        process do |current_stack, str|
          result << transform_line(current_stack, str) << "\n"
        end

        result
      end

      def commit_check(&block)
        parser = Junoser::Parser.new
        passed = true

        process do |current_stack, str|
          config = transform_line(current_stack, str)

          begin
            parser.parse config
          rescue Parslet::ParseFailed
            $stderr.puts "Invalid syntax:  #{config}"
            passed = false
          end
        end

      rescue
        $stderr.puts $!
        passed = false
      ensure
        return passed
      end


      private

      def process(&block)
        stack = []

        Junoser::Input.new(@input).read.split("\n").each do |line|
          case line
          when /(.*){/
            stack.push $1.strip
          when '}'
            stack.pop
          when /((?!\[).*)\[(.*)\];/
            $2.split("\s").each do |i|
              yield stack, "#{$1.strip} #{i}"
            end
          when /(.*);/
            yield stack, $1
          else
            raise "ERROR: unknown statement:  #{line}"
          end
        end
      end

      def transform_line(current_stack, str)
        statement = if current_stack.empty?
                      str
                    else
                      statement = "#{current_stack.join(' ')} #{str}"
                    end

        if statement.gsub!('inactive: ', '')
          "deactivate #{statement}"
        else
          "set #{statement}"
        end
      end

      def struct(stack, statement, offset=2)
        width = 2
        if label = stack.shift
          %[#{" "*offset}#{label} {
#{struct(stack, statement, width+offset)}
#{" "*offset}}]
        else
          %[#{" "*offset}#{statement};]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
junoser-0.1.4 lib/junoser/display/set.rb
junoser-0.1.3 lib/junoser/display/set.rb