lib/junoser/squash.rb in junoser-0.3.9 vs lib/junoser/squash.rb in junoser-0.3.10

- old
+ new

@@ -1,88 +1,41 @@ require 'junoser' require 'parslet' module Junoser - class DeleteTransformer < 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 - end - - rule(label: simple(:label)) do - ")#{Regexp.escape(label.to_s)}" - end - - rule(label: simple(:label), child: simple(:child)) do - "#{Regexp.escape(label.to_s)} #{child}" - end - - rule(label: simple(:label), child: sequence(:children)) do - %[#{Regexp.escape(label.to_s)} #{children.join(' ')}] - end - - rule(statement: simple(:statement), argument: simple(:argument)) do - "#{statement} #{argument}" - end - - rule(statement: simple(:statement), argument: sequence(:arguments)) do - %[#{statement} #{arguments.join(' ')}] - end - - rule(oneline: simple(:str)) do - str - end - - rule(oneline: sequence(:strs)) do - strs.join(' ') - end - - rule(enum: simple(:str)) do - str - end - - rule(enum: sequence(:strs)) do - strs.join(' ') - end - end - class Squash def initialize(io_or_string) @input = io_or_string @lines = [] @parser = Junoser::Parser.new - @transformer = DeleteTransformer.new + @transformer = Junoser::Transformer.new end def transform config = Junoser::Input.new(@input).read.split("\n") config.each do |l| l.strip! case l - when /^set / - @lines << l - when /^delete / - to_delete = @parser.parse(l.gsub(/^delete /, 'set ')) - delete_lines @transformer.apply(to_delete) + when /^(set|deactivate) / + @lines << l + when /^delete / + delete_lines delete_pattern(l.gsub(/^delete /, 'set ')) + when /^insert (.*) before (.*)/ + insert_before insert_pattern("set #{$1}"), $2 + when /^insert (.*) after (.*)/ + insert_after insert_pattern("set #{$1}"), $2 end end @lines.uniq! remove_subcommand(@lines).map(&:strip).join("\n") end private def remove_subcommand(lines) - lines.each_with_index do |l,i| + lines.each_with_index do |l, i| lines[i..-1].each do |l2| if l.include?(l2) and l != l2 lines.delete(l2) end end @@ -91,8 +44,51 @@ def delete_lines(pattern) @lines.each do |l| l.sub!(/#{pattern}/) { $1 } end + end + + def split_last_token(line) + tokens = join_arg(@transformer.apply(@parser.parse(line))).split("\n") + tokens.map! { |t| + t.gsub!(/arg\((.*)\)/) { "#$1" } # Strip arg + Regexp.escape(t.strip) + } + + [tokens[0..-2].join(' '), tokens.last] + end + + # Ported from lib/junoser/display/config_store.rb + def join_arg(str) + str.gsub!(/\narg\((.*)\)$/) { " #$1" } + str.gsub!(/arg\((.*)\)/) { "#$1" } + str + end + + def delete_pattern(line) + line, last_token = split_last_token(line) + "(#{line}\s+)#{last_token}.*" + end + + def insert_pattern(line) + line, last_token = split_last_token(line) + "(#{line})\s+#{last_token}" + end + + def insert_before(pattern_to_insert, key_token) + key_pattern = pattern_to_insert.sub(/\).*/) { ") #{key_token}" } + + lines_to_insert = @lines.select { |l| l =~ /#{pattern_to_insert}/ } + @lines.reject! { |l| l =~ /#{pattern_to_insert}/ } + + key_index = @lines.index { |l| l =~ /#{key_pattern}/ } + @lines.insert(key_index, lines_to_insert).flatten! + end + + def insert_after(pattern_to_insert, key_token) + @lines.reverse! + insert_before pattern_to_insert, key_token + @lines.reverse! end end end