Sha256: cf5efd912a86c8876a013fc91ef345206b326beab007903b737b7815ec0af5e4

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'parslet'
require 'junoser/display/base'

module Junoser
  module Display
    class Set
      include Base

      def transform
        process do |current_stack, str|
          @output.puts transform_line(current_stack, str)
        end
      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
        passed
      end


      private

      def process(&block)
        stack = []

        read_io_or_string.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

1 entries across 1 versions & 1 rubygems

Version Path
junoser-0.1.1 lib/junoser/display/set.rb