Sha256: e7d03059e42412b16ceb05dea1a56d9f2b83708b16a4a85ccf939b2b640a4779

Contents?: true

Size: 1.72 KB

Versions: 11

Compression:

Stored size: 1.72 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)
        begin
          lines = transform
        rescue
          $stderr.puts $!
          return false
        end

        parser = Junoser::Parser.new
        parser.parse_lines(lines)
      end


      private

      def process(&block)
        stack = []

        Junoser::Input.new(@input).read.split("\n").each do |line|
          case line
          when /(?!.*})(.*){/
            stack.push $1.strip
          when /}\s*$/
            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)
        statements = []
        current_statement = ''

        current_stack.each do |stack|
          stack.gsub! 'replace: ', ''

          if stack.gsub!('inactive: ', '')
            statements << "deactivate #{current_statement}#{stack}"
          end
          current_statement << "#{stack} "
        end

        str.gsub! 'replace: ', ''

        if str.gsub!('inactive: ', '')
          statements << "deactivate #{current_statement}#{str}"
        end

        statements.unshift "set #{current_statement}#{str}"
        statements.join("\n")
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
junoser-0.6.0 lib/junoser/display/set.rb
junoser-0.5.6 lib/junoser/display/set.rb
junoser-0.5.5 lib/junoser/display/set.rb
junoser-0.5.4 lib/junoser/display/set.rb
junoser-0.5.3 lib/junoser/display/set.rb
junoser-0.5.2 lib/junoser/display/set.rb
junoser-0.5.1 lib/junoser/display/set.rb
junoser-0.5.0 lib/junoser/display/set.rb
junoser-0.4.7 lib/junoser/display/set.rb
junoser-0.4.6 lib/junoser/display/set.rb
junoser-0.4.5 lib/junoser/display/set.rb