Sha256: f6a5bfe8638a95ec90c2d29457126ed5c70400e2f59c52848d7d9d1eb93c72d8

Contents?: true

Size: 1.9 KB

Versions: 11

Compression:

Stored size: 1.9 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|
          if stack.gsub!('inactive: ', '')
            statements << "deactivate #{current_statement}#{stack}"
          end
          current_statement << "#{stack} "
        end

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

        statements.unshift "set #{current_statement}#{str}"
        statements.join("\n")
      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

11 entries across 11 versions & 1 rubygems

Version Path
junoser-0.3.1 lib/junoser/display/set.rb
junoser-0.3.0 lib/junoser/display/set.rb
junoser-0.2.13 lib/junoser/display/set.rb
junoser-0.2.12 lib/junoser/display/set.rb
junoser-0.2.11 lib/junoser/display/set.rb
junoser-0.2.10 lib/junoser/display/set.rb
junoser-0.2.9 lib/junoser/display/set.rb
junoser-0.2.8 lib/junoser/display/set.rb
junoser-0.2.7 lib/junoser/display/set.rb
junoser-0.2.6 lib/junoser/display/set.rb
junoser-0.2.5 lib/junoser/display/set.rb