Skip to content

Instantly share code, notes, and snippets.

@pocke pocke/ski.rb

Created Jan 29, 2020
Embed
What would you like to do?
expr_gen = -> (n) { "(?<expr#{n}>[^()]|\\(\\g<expr#{n}>+\\))" }
simple = '(?<simple>(?:[()]|[^ski])*)'
S = /^#{simple}s#{expr_gen.(1)}#{expr_gen.(2)}#{expr_gen.(3)}(?<rest>.*)/
K = /^#{simple}k#{expr_gen.(1)}#{expr_gen.(2)}(?<rest>.*)$/
I = /^#{simple}i(?<rest>.+)/
P = /^#{simple}\((?<content>#{expr_gen.(1)}*)\)(?<rest>.*)/
$s = '\k<simple>(\k<expr1>\k<expr3>)(\k<expr2>\k<expr3>)\k<rest>'
$k = '\k<simple>\k<expr1>\k<rest>'
$i = '\k<simple>\k<rest>'
$p = '\k<simple>\k<content>\k<rest>'
def ski(str)
() while str.gsub!(S, $s) ||
str.gsub!(K, $k) ||
str.gsub!(I, $i) ||
str.gsub!(P, $p)
str
end
p ski('sxyza') == 'xzyza'
p ski('kxy') == 'x'
p ski('ixx') == 'xx'
p ski('(x)y') == 'xy'
p ski('kixy') == 'y'
p ski('(ix)') == 'x'
p ski('s(k(si))kAB') == 'BA'
X = /#{simple}$/
$x = 'echo \k<simple>'
Y = /(?<content>.*[ski].*)$/
$y = 'echo ruboty ski \k<content>'
[S, $s, K, $k, P, $p, I, $i].each_slice(2) do |a, b|
puts "ruboty replace ^ruboty ski #{a.inspect[2..-2]} with ruboty ski #{b}"
end
[X, $x, Y, $y].each_slice(2) do |a, b|
puts "ruboty replace ^ruboty ski #{a.inspect[1..-2]} with ruboty #{b}"
end
@pocke

This comment has been minimized.

Copy link
Owner Author

pocke commented Jan 29, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.