Sha256: 6c7bae411c207b29e2171d5c32a280948cb31ced147e066b51f3511284dc97a8

Contents?: true

Size: 1.27 KB

Versions: 16

Compression:

Stored size: 1.27 KB

Contents

require 'parslet'

module Junoser
  class Transformer < Parslet::Transform
    rule(config: simple(:config)) do
      config.to_s
    end

    rule(config: sequence(:configs)) do
      configs.join("\n")
    end

    rule(arg: simple(:arg)) do
      "arg(#{arg})"
    end

    rule(label: simple(:label)) do
      label.to_s
    end

    rule(label: simple(:label), child: simple(:child)) do
      "#{label}\n#{child}"
    end

    rule(label: simple(:label), child: sequence(:children)) do
      Junoser::Transformer.remove_slash_asterisk children
      %[#{label}\n#{children.join("\n")}]
    end

    rule(statement: simple(:statement), argument: simple(:argument)) do
      "#{statement} #{argument}"
    end

    rule(statement: simple(:statement), argument: sequence(:arguments)) do
      Junoser::Transformer.remove_slash_asterisk arguments
      %[#{statement}\n#{arguments.join("\n")}]
    end

    rule(oneline: simple(:str)) do
      str.to_s.gsub("\n", ' ')
    end

    rule(oneline: sequence(:strs)) do
      strs.join(' ')
    end


    def self.remove_slash_asterisk(array)
      open = array.index("arg(/*)\n")
      close = array.index("arg(*/)")

      if open && close
        (open..close).reverse_each do |i|
          array.delete_at i
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
junoser-0.3.3 lib/junoser/transformer.rb
junoser-0.3.2 lib/junoser/transformer.rb
junoser-0.3.1 lib/junoser/transformer.rb
junoser-0.3.0 lib/junoser/transformer.rb
junoser-0.2.13 lib/junoser/transformer.rb
junoser-0.2.12 lib/junoser/transformer.rb
junoser-0.2.11 lib/junoser/transformer.rb
junoser-0.2.10 lib/junoser/transformer.rb
junoser-0.2.9 lib/junoser/transformer.rb
junoser-0.2.8 lib/junoser/transformer.rb
junoser-0.2.7 lib/junoser/transformer.rb
junoser-0.2.6 lib/junoser/transformer.rb
junoser-0.2.5 lib/junoser/transformer.rb
junoser-0.2.4 lib/junoser/transformer.rb
junoser-0.2.3 lib/junoser/transformer.rb
junoser-0.2.2 lib/junoser/transformer.rb