Schemeのブロックコメントにマッチする正規表現(2)
開き括弧と閉じ括弧から,正規表現を生成.
def charsetq(c) case c when "[", "-", "]", "\\" "\\#{c}" else c end end def _rec_regexp(str) Array.new(str.length) {|i| [Regexp.quote(str[0, i]), charsetq(str[i, 1])] } end def rec_regexp(s1, s2) a1 = _rec_regexp(s1) a2 = _rec_regexp(s2) ary = [] a1.zip(a2) {|(h1, t1), (h2, t2)| ary << if h1 == h2 "#{h1}[^#{t1}#{t2}]" else "#{h1}[^#{t1}]|#{h2}[^#{t2}]" end } Regexp.new("(?<block>#{Regexp.quote(s1)}(?:(?:#{ary.join('|')})++|\\g<block>)*+#{Regexp.quote(s2)})") end re = rec_regexp("#|", "|#") str = <<EOS (1) (2) #| (3) |# #| # (4) | |# (5) #| (6) #| (7) |# (8) |# (9) (10) #| # | #| #| (11) | # |# |# (12) #| (13) |# |# (14) EOS puts str.scan(re)
#| (3) |# #| # (4) | |# #| (6) #| (7) |# (8) |# #| # | #| #| (11) | # |# |# (12) #| (13) |# |#
できたかな.
zipがブロックを付けるとnilを返して,ブロックを付けないとEnumeratorを返してくれなくて困った.